Ural 1126. Magnetic Storms

来源:互联网 发布:桌面数据恢复软件 编辑:程序博客网 时间:2024/05/17 04:06

题好难读,输出长度为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;}


原创粉丝点击