裸的单调队列优化DP
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
int q[1100000], pos[1100000], head, tail;
int nownum, leavenow;
int main(){
int T;
char s[10];
cin >> T;
while( T-- ){
cin >> s;
head = 0;
tail = -1;
nownum = 0;
leavenow = 0;
while( scanf( "%s", s ) && strcmp( s, "END" ) != 0 ){
int temp;
if( s[0] == 'C' ){
scanf( "%s%d", s, &temp );
nownum++;
while( head <= tail && temp >= q[tail] ) tail--;
tail++;
q[tail] = temp;
pos[tail] = nownum;
}else if( s[0] == 'G' ){
leavenow++;
while( head <= tail && pos[head] <= leavenow ) head++;
}else{
if( head <= tail ){
printf( "%d\n", q[head] );
}else{
printf( "-1\n" );
}
}
}
}
return 0;
}