您的位置:首页 > 其它

Ural 1126. Magnetic Storms

2013-08-01 21:46 288 查看
题好难读,输出长度为k的区间内的峰值。

单调队列的应用。

单调队列的插入就是把一个元素加入,从队尾开始到比第一个他的大的元素位置之后,在之后的元素全部抛弃。

删除就是从对头找到第一个满足可在队列中的元素作为对头。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <stack>
#include <queue>

using  namespace std;
const int maxn=25010;
int RMax[maxn],pos[maxn];
int main()
{
//freopen("data","r",stdin);
int n,m;
scanf("%d",&m);
int l=0,r=0,t=0;//head and tail
while(scanf("%d",&n)&&n!=-1)
{
//cout<<"t="<<t<<endl;
t++;
while(l>=r)// into queue
{
if(l>=0&&RMax[l]<=n)
{
l--;
}
else
break;
}
RMax[++l]=n;
pos[l]=t;
while(pos[r]+m<=t)
{
r++;
}
//cout<<t<<" "<<m<<endl;
if(t>=m)
{
printf("%d\n",RMax[r]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: