UVA 1608 Non-boring sequences

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

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

int n,a[maxn];
int L[maxn],R[maxn];
map<int,int> pos;

bool judge(int l,int r)
{
    if(l>=r) return 1;
    //cout<<l<<" "<<r<<endl;
    int li=l,ri=r;
    int nex=0;
    int tag=1;
    while(li<=ri){
        if(tag){
            if(L[li]<l&&R[li]>r){
                nex=li;break;
            }
            li++;
        }
        else{
            if(L[ri]<l&&R[ri]>r){
                nex=ri;break;
            }
            ri--;
        }
        tag^=1;
    }
    if(nex==0) return 0;
    return judge(l,nex-1)&&judge(nex+1,r);
}

int main()
{
    freopen("in.txt","r",stdin);
    int T;cin>>T;
    while(T--){
        scanf("%d",&n);
        REP(i,1,n) scanf("%d",&a[i]);
        pos.clear();
        REP(i,1,n){
            if(pos[a[i]]) L[i]=pos[a[i]];
            else L[i]=0;
            pos[a[i]]=i;
        }
        pos.clear();
        for(int i=n;i>=1;i--){
            if(pos[a[i]]) R[i]=pos[a[i]];
            else R[i]=n+1;
            pos[a[i]]=i;
        }
        puts(judge(1,n)?"non-boring":"boring");
    }
    return 0;
}
/**
多加个离散化就T了,fuck。。。
话说回来,这题居然能这样优化。。。但这应该是在分治中一个很常见的思想:中途相遇法。
*/
View Code
没有AC不了的题,只有不努力的ACMER!
原文地址:https://www.cnblogs.com/--560/p/5042954.html