志愿者选拔(单调队列)

Problem 1894 志愿者选拔

Accept: 1783    Submit: 5564 Time Limit: 1500 mSec    Memory Limit : 32768 KB

 Problem Description

世博会马上就要开幕了,福州大学组织了一次志愿者选拔活动。参加志愿者选拔的同学们排队接受面试官们的面试。参加面试的同学们按照先来先面试并且先结束的原则接受面试官们的考查。面试中每个人的人品是主要考查对象之一。(提高人品的方法有扶老奶奶过街,不闯红灯等)作为主面试官的John想知道当前正在接受面试的同学队伍中人品值最高的是多少。于是他请你帮忙编写一个程序来计算。

 Input

输入数据第一行为一整数T,表示有T组输入数据。每组数据第一行为”START”,表示面试开始接下来的数据中有三种情况:
  输入 含义
1 C NAME RP_VALUE 名字为NAME的人品值为RP_VALUE的同学加入面试队伍。(名字长度不大于5,0 <= RP_VALUE <= 1,000,000,000)
2 G 排在面试队伍最前面的同学面试结束离开考场。
3 Q 主面试官John想知道当前正在接受面试的队伍中人品最高的值是多少。
最后一行为”END”,表示所有的面试结束,面试的同学们可以依次离开了。所有参加面试的同学总人数不超过1,000,000

 Output

对于每个询问Q,输出当前正在接受面试的队伍中人品最高的值,如果当前没有人正在接受面试则输出-1。

 Sample Input

2 START C Tiny 1000000000 C Lina 0 Q G Q END START Q C ccQ 200 C cxw 100 Q G Q C wzc 500 Q END

 Sample Output

1000000000 0 -1 200 100 500

 Hint

数据较大建议使用scanf,printf 不推荐使用STL
题解:set+queue超时, 其实是个单调队列。。。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#include<set>
#include<queue>
using namespace std;
/*
struct Node{
    char name[6];
    int rp_value;
    friend bool operator < (Node a, Node b){
            return a.rp_value > b.rp_value;
    }
};
set<Node>st;
set<Node>::iterator iter;
queue<Node>Q;
int main(){
    char s[6];
    int tp_value, T;
    
    scanf("%d", &T);
    while(T--){
        while(!Q.empty())Q.pop();
        st.clear();
        scanf("%s", s);
        int tp = 0, cur = 0;
        while(scanf("%s", s), strcmp(s, "END")){
            if(s[0] == 'C'){
                Node a;
                scanf("%s%d", a.name, &a.rp_value);
                st.insert(a);
                Q.push(a);
            }
            else if(s[0] == 'Q'){
                if(st.empty())
                    puts("-1");
                else
                    printf("%d
", st.begin()->rp_value);
            }
            else if(s[0] == 'G' && !Q.empty()){
                iter = st.find(Q.front());
                if(iter != st.end())st.erase(iter);
                Q.pop();
            }
        }
    }
    return 0;
}
*/
struct Node{
    int cur;
    char name[6];
    int rp;
};
Node Q[1000100];
int main(){
    char s[6];
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%s", s);
        int head, tail;
        head = 0;tail = -1;
        int cur = 0, Gcur= 0;
        while(scanf("%s", s), strcmp(s, "END")){
            if(s[0] == 'C'){
                Node a;
                scanf("%s%d", a.name, &a.rp);
                a.cur = ++cur;
                while(head <= tail &&(tail == -1 || Q[tail].rp <= a.rp))
                    tail--;
                Q[++tail] = a;
            }
            else if(s[0] == 'Q'){
                if(head > tail)
                    puts("-1");
                else
                    printf("%d
", Q[head].rp);
            }
            else if(s[0] == 'G'){
                Gcur++;
                if(head <= tail && Gcur == Q[head].cur)
                    head++;
            }
        }
    }        
    return 0;
}
原文地址:https://www.cnblogs.com/handsomecui/p/5491952.html