POJ 1442 Black Box

  优先队列水过~~~

  

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <cstdlib>
 7 #include <queue>
 8 
 9 using namespace std;
10 
11 int seq[30010];
12 
13 int main()
14 {
15     int n,m,i,j,order,top,lastorder;
16 
17     while(scanf("%d %d",&m,&n) != EOF)
18     {
19 
20         priority_queue<int> q1;
21         priority_queue<int ,vector<int>,greater<int> > q2;
22 
23         for(i = 0; i < m; ++i)
24             scanf("%d",&seq[i]);
25 
26         lastorder = 0;
27 
28         top = 0;
29 
30         for(i = 0; i < n; ++i)
31         {
32             scanf("%d",&order);
33 
34             for(j = lastorder; j < order; ++j)
35             {
36                 if(top < i+1)
37                 {
38                     q1.push(seq[j]);
39                     ++top;
40                 }
41                 else
42                 {
43                     q2.push(seq[j]);
44                 }
45             }
46 
47             lastorder = order;
48 
49             while(top < i+1)
50             {
51                 ++top;
52                 q1.push(q2.top());
53                 q2.pop();
54             }
55 
56 
57             while(!q1.empty() && !q2.empty() && q1.top() > q2.top())
58             {
59                 q1.push(q2.top());
60                 q2.push(q1.top());
61                 q1.pop();
62                 q2.pop();
63             }
64             printf("%d\n",q1.top());
65         }
66     }
67     return 0;
68 }
View Code

 

posted @ 2013-08-19 10:44  好小孩  阅读(160)  评论(0编辑  收藏  举报