单调队列的几个性质:
如果我要知道区间最大值,需要维护单调递减的队列,那么队头元素就对应最大值。
如果我要知道区间最小值,需要维护单调递增的队列,那么队头元素就对应最小值。
队列里存放的可以是元素下标,也可以是元素值。
如果维护单增队列(就是要知道区间最小值),如果当前入队元素比队尾小,那么去掉队尾,直至比队尾元素大,因为可以保证查询区间的最小值一定是队头元素。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1000000;
char s[21];
int a[maxn+10],q[maxn+10];
int n;
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%s",s);
int x,rear=1,front=1;
n=0;
int le=1;
while(~scanf("%s",s)&&strcmp(s,"END"))
{
if(s[0]=='C')
{
scanf("%s%d",&s,&x);
a[++n]=x;
while(rear-front>0&&a[q[rear-1]]<=a[n])
{
rear--;
}
q[rear++]=n;
}
else if(s[0]=='G')
{
if(rear-front>0&&q[front]==le)
{
front++;
}
le++;
}
else
{
if(rear-front<=0)
{
puts("-1");
}
else
{
printf("%d\n",a[q[front] ]);
}
}
}
}
return 0;
}