uva 1608 - Non-boring sequences

We were afraid of making this problem statement too boring, so we decided to keep it short. A sequenceis called non-boring if its every connected subsequence contains a unique element, i.e. an element suchthat no other element of that subsequence has the same value.

Given a sequence of integers, decide whether it is non-boring.

Input

The first line of the input contains the number of test cases T . The descriptions of the test cases follow:Each test case starts with an integer n (1 n 200000) denoting the length of the sequence. Inthe next line the n elements of the sequence follow, separated with single spaces. The elements are

non-negative integers less than 109.

Output

Print the answers to the test cases in the order in which they appear in the input. For each test caseprint a single line containing the word ‘non-boring’ or ‘boring’.

Sample Input

4
5

1 2 3 4 5 

5

1 1 1 1 1 

5

1 2 3 2 1 

5

1 1 2 1 1

Sample Output

non-boring
boring             
non-boring
boring
此题暴露了复杂度分析的缺点。
//
//  main.cpp
//  uva 1608 - Non-boring sequences
//
//  Created by XD on 15/8/15.
//  Copyright (c) 2015年 XD. All rights reserved.
//

/*
 此题是分治法的应用,将一个大问题可以分解为一个两个小问题,然后逐个解决。
 另外在分析递归程序的复杂度时候尽量将式子写出来,然后分析一下。另外在查找某个值的时候从两边同时查找的期望小一些,这也是分治法好处。
 另外要额外注意预处理,预处理是挖掘一下数据的性质,并按照算法的需求来加以存储,使得在算法执行过程中可以简化复杂度。
 
 
 内联函数。。是指在编译的时候直接将函数的调用替换成函数体的形式,而不是调用,这样可以一定程度的提高执行效率。
 同时注意内联函数是需要函数体和声明连在一起的。
 
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std ;
const int maxn = 200000+5 ;
int seq[maxn] ;
int pre[maxn] ,nextv[maxn] ;
map cur ;

inline bool unique(int p ,  int L , int R)
{
    return pre[p] < L && nextv[p] > R ;
}
int check(int L , int R)
{
    if (L >= R) {
        return 1 ;
    }
    for (int d = 0 ; L+d <= R-d ; d++) {
        if(unique(L + d , L  ,R))
        {
            return check(L ,  L+d-1) && check(L + d +1 , R) ;
        }
        if (L+d == R-d) {
            return 0  ;
        }
        if (unique(R-d , L ,R)) {
            return check(L, R-d-1)&&check(R-d +1, R) ;
        }
    }
    return 0 ;
}


int main(int argc, const char * argv[]) {
    int T,n;scanf("%d" ,&T) ;
    while (T--) {
        int ok = 0 ;
        scanf("%d",&n) ;
        cur.clear() ;
        for (int i = 0; i < n ; i++) {
            scanf("%d" ,&seq[i]) ;
            if (!cur.count(seq[i])) {
                pre[i] = -1 ;
            }
            else pre[i] = cur[seq[i]] ;
            cur[seq[i]] = i ;
            
        }
        cur.clear();
        for(int i = n-1; i >= 0; i--) {
            if(!cur.count(seq[i])) nextv[i] = n;
            else nextv[i] = cur[seq[i]];
            cur[seq[i]] = i;
        }

        for(int i = 1 ; i < n ;i++  ){
            if (seq[i]==seq[i-1]) {
                ok=1;break ;
            }
        }
        if (ok) {
            printf("boring\n") ; continue ;
        }
        ok= 0 ;
        if (check(0, n-1)) {
            printf("non-boring\n") ;
            continue ;
        }
        printf("boring\n") ;
        
        
    }
    return 0;
}


你可能感兴趣的:(uva 1608 - Non-boring sequences)