1 /******************************************************************
 2 题目:           志愿者选拔(FZU 1894)
 3 算法:          单调队列
 4 算法思想:      在每个元素入队的时候入队的时候,使队列单调,查找
 5                  的时候就能很快找到最值。
 6 *******************************************************************/
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<algorithm>
10 #include<iostream>
11 using namespace std;
12 
13 const int mx=1000006;
14 struct Q
15 {
16     int x,di;
17 };
18 Q q[mx];
19 char s[10];
20 
21 int main()
22 {
23     int t;
24     scanf("%d",&t);
25     while (t--)
26     {
27         int h=0,r=-1;                       ///h为对头,r为队尾
28         int cut=0,id=0;                     ///cut为删除数的个数,id为插入数的下标。
29         scanf("%s",s);
30         while (~scanf("%s",s))
31         {
32             if (s[0]=='E') break;
33             if (s[0]=='G') cut++;
34             if (s[0]=='Q')
35             {
36                 if (cut>=id) printf("-1\n");     ///删除数个数大于等于最大数id,队列为空
37                 else
38                 {
39                     while (q[h].di<=cut) h++;    ///由于队列是单调的,所以只要找到第一个没
40                                                  ///有被删除数就是最大数,只要一个数的id大于
41                                                  ///删除数的个数,那么这个数就没有被删除
42                     printf("%d\n",q[h].x);
43                 }
44             }
45             if (s[0]=='C')
46             {
47                 int x;
48                 scanf("%s%d",s,&x);
49                 while (r>=h&&q[r].x<x)          ///找到这个数要插入的为,使队列从头到尾是单调
50                                                 ///的,这样做虽然会覆盖一些数,但是覆盖的数已
51                                                 ///经不会再输出了。
52                 {
53                     r--;
54                 }
55                 q[++r].x=x;
56                 q[r].di=++id;
57             }
58         }
59     }
60 }

 

posted on 2016-08-04 10:59  pb2016  阅读(174)  评论(0编辑  收藏  举报