福建农大 1084 检查站

单调队列 见 http://www.cnblogs.com/lxf90/articles/2012016.html

第一,原序列的坐标单调

第二,原序列的值单调

  
    
/* 单调队列 */
#include
< stdio.h >
using namespace std;
const int MAXN = 100001 ;
struct dqueue{
int val;
int idx;
}que[MAXN];
int a[MAXN], cnt;
int head, tail;
int lefts;
int main(){
int n;
// cin >> n;
scanf( " %d " , & n);
head
= tail = 0 ;
cnt
= 0 ;
lefts
= 0 ;
for ( int i = 0 ; i < n; ++ i){
// string s;
// cin >> s;
char s[ 20 ];
scanf(
" %s " ,s);
if (s[ 0 ] == ' Q ' ){
if ( head > tail ){
// cout << "0" << endl;
printf( " 0\n " );
}
else {
// cout <<que[head].val << endl;
printf( " %d\n " ,que[head].val);
}
}
else if (s[ 0 ] == ' I ' ){
// cin >> a[cnt++];
scanf( " %d " , & a[cnt ++ ]);
if (head > tail){
head
= tail = 0 ;
que[head].idx
= cnt - 1 ;
que[head].val
= a[cnt - 1 ];
continue ;
}
while (head <= tail && que[tail].val <= a[cnt - 1 ])
tail
-- ;
++ tail;
que[tail].val
= a[cnt - 1 ];
que[tail].idx
= cnt - 1 ;
}
else {
lefts
++ ;
if (que[head].idx < lefts)
head
++ ;
}
}
return 0 ;
}

你可能感兴趣的:(福建农大 1084 检查站)