【HDOJ】1908 Double Queue

双端队列+二分。

 1 #include <cstdio>
 2 
 3 #define MAXN 1000005
 4 
 5 typedef struct {
 6     int id;
 7     int p;
 8 } node_st;
 9 
10 node_st que[MAXN];
11 int rear = 0,front = 0;
12 
13 void Insert(int id, int p) {
14     int l = front, r = rear, mid;
15 
16     while ( l <= r) {
17         mid = (l+r)>>1;
18         if (que[mid].p == p)
19             break;
20         if (que[mid].p < p)
21             l = mid+1;
22         else
23             r = mid - 1;
24     }
25     while (mid<rear && que[mid].p <= p)
26         ++mid;
27     for (int i=rear-1; i>=mid; --i)
28         que[i+1] = que[i];
29     que[mid].id = id;
30     que[mid].p = p;
31     rear++;
32 }
33 
34 int main() {
35     int c, id, p;
36 
37     while (scanf("%d", &c)!=EOF && c) {
38         if (c == 1) {
39             scanf("%d %d", &id, &p);
40             Insert(id, p);
41         } else {
42             if (rear == front)
43                 printf("0\n");
44             else {
45                 if (c == 2)
46                     printf("%d\n", que[--rear].id);
47                 else
48                     printf("%d\n", que[front++].id);
49             }
50         }
51     }
52 
53     return 0;
54 }

 

 

posted on 2014-06-14 16:40  Bombe  阅读(182)  评论(0编辑  收藏  举报

导航