
Accept: 1381 Submit: 4319
Time Limit: 1500 mSec Memory Limit : 32768 KB
Problem Description
Input
输入 | 含义 | |
1 | C NAME RP_VALUE | 名字为NAME的人品值为RP_VALUE的同学加入面试队伍。(名字长度不大于5,0 <= RP_VALUE <= 1,000,000,000) |
2 | G | 排在面试队伍最前面的同学面试结束离开考场。 |
3 | Q | 主面试官John想知道当前正在接受面试的队伍中人品最高的值是多少。 |
Output
Sample Input
2STARTC Tiny 1000000000C Lina 0QGQENDSTARTQC ccQ 200C cxw 100QGQC wzc 500QEND
Sample Output
10000000000-1200100500
利用单调队列维护单调性,用leave记录排在面试队伍最前面的坐标
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000010
struct node{
int v,x;
}q[N];
int main(){
// freopen("C:\\Users\\F\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\F\\Desktop\\out.txt", "w", stdout);
int t;
char str[10];
scanf("%d",&t);
int rp;
int front,now,leave,rear;
while(t--){
front=0;
now=1;
leave=0;
rear=-1;
scanf("%s",str);
while(scanf("%s",str)&&strcmp("END",str)!=0){
if(str[0]=='C'){
scanf("%s%d",str,&rp);
while(front<=rear&&q[rear].v<=rp)rear--;//若队尾小于当前入队值,则队尾出队列,维护队列单调性
node a;
a.v=rp;
a.x=now++;
q[++rear]=a;
}
else if(str[0]=='Q'){
while(front<=rear&&q[front].x<=leave)front++;
if(rear<front)
printf("-1\n");
else
printf("%d\n",q[front].v);
}
else{
leave++;
}
}
}
return 0;
}