UVA1608 not boring sequence Non-boring sequences

Minute Osamu

Thank lrj thorough explanation.

We started with a map can be obtained position and the next occurrence of the first occurrence of a number, and then be able to determine a number in the \ (l \) to \ (r \) in this number only appears once whether .

Since any continuous sequences have at least one element of the sole, then we can find the number in this sequence a unique presence, we just assume that the subscript ta is k, then we find anything that contains this interval numbers are not boring range.

Therefore, we can then Analyzing \ (L \) to \ (k-1 \) sequence and \ (k + 1 \) to \ (R & lt \) sequence if boring sequence, if not, then \ (L \) to \ (r \) this sequence so it is not boring sequence.

Which is the boundary

if(l==r||l>r)return 1;

We will find a problem, if we traverse from left to right, it is clear that if we request a \ (k \) on the far right, each partition is so time complexity directly explosions O ( \ ( ^ 2 the n-\) ), right to left traversal will encounter this situation. So we can consider traverse middle from both sides, in the worst case \ (K \) will be in the middle, then the time complexity is the classic recursive \ (T (n) = 2T (\ frac {n} {2 }) + O (the n-) = O (nlog the n-^) \) (do not understand, then suggest to learn aboutMaster theorem), is acceptable.

Finally, I offeruglyCode

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
int T,n;
const int N=200010;
int a[N],pre[N],nt[N];
map<int,int>mp;
int read()
{
    char ch;int x=0,f=1;
    while(!isdigit(ch=getchar()))
    {(ch=='-')&&(f=-f);}
    while(isdigit(ch))
    {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
bool work(int l,int r)
{
    if(l==r||l>r)return 1;
    int linl=l,linr=r;
    while(linr>linl)
    {
        linr--;
        if(pre[linr]<l&&nt[linr]>r)
            if(work(l,linr-1)&&work(linr+1,r))return 1;
        linl++;
        if(pre[linl]<l&&nt[linl]>r)
            if(work(l,linl-1)&&work(linl+1,r))return 1;
    }
    return 0;
}
int main()
{
    cin>>T;
    while(T--)
    {
        mp.clear();
        cin>>n;
        for(int i=1;i<=n;++i)
        {
            a[i]=read();
            pre[i]=0;nt[i]=n+1;     
        }
        for(int i=1;i<=n;++i)
        {
            if(mp.find(a[i])!=mp.end())pre[i]=mp[a[i]],nt[mp[a[i]]]=i;
            mp[a[i]]=i;
        }
        if(work(1,n))printf("non-boring\n");
        else printf("boring\n");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/wljss/p/11498954.html