UVA 11491 Erasing and Winning

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=1<<29;

int n,d;
int a[maxn];
int ans[maxn],cnt;
deque<int> q;

int main()
{
    freopen("in.txt","r",stdin);
    while(cin>>n>>d,n||d){
        REP(i,1,n) scanf("%1d",&a[i]);
        while(!q.empty()) q.pop_back();
        REP(i,1,d){
            while(!q.empty()&&q.back()<a[i]) q.pop_back();
            q.push_back(a[i]);
        }
        cnt=0;
        REP(i,d+1,n){
            while(!q.empty()&&q.back()<a[i]) q.pop_back();
            q.push_back(a[i]);
            ans[++cnt]=q.front();q.pop_front();
        }
        REP(i,1,cnt) printf("%d",ans[i]);
        puts("");
    }
    return 0;
}
View Code

单调队列维护划窗最值的题,一开始愣是写完了线段树才发现是划窗最值。。

你可能感兴趣的:(UVA 11491 Erasing and Winning)