UVA - 1608 Non-boring sequences

Non-boring sequences

We were afraid of making this problem statement too boring, so we decided to keep it short. A sequence is called non-boring if its every connected subsequence contains a unique element, i.e. an element such that 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. In the 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 case print 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

题目略难,看了大神代码才勉强理解

#include <iostream>
#include <map>

using namespace std;

const int maxn = 2e5 + 10;

int a[maxn], l[maxn], r[maxn];

int judge(int left, int right)
{
    if(left >= right)
        return 1;
    for(int i = 0; i <= (right-left)/2; i++)//二分
    {
        if(l[left+i]<left && r[left+i]>right)//从左往右查是否有无聊的序列
            return judge(left, left+i-1) && judge(left+i+1, right);
        if(l[right-i]<left && r[right-i]>right)//从右往左查是否有无聊的序列
            return judge(left, right-i-1) && judge(right-i+1, right);
    }
    return 0;
}

int main()
{
    //std::ios::sync_with_stdio(false);
    int t, n;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        map<int, int> m;
        for(int i = 0; i < n; i++)//记录左边最近相同数字的坐标
        {
            if(!m.count(a[i]))
                l[i] = -1;
            else
                l[i] = m[a[i]];
            m[a[i]] = i;
        }
        m.clear();
        for(int i = n-1; i >=0; i--)//记录右边最近相同数字的坐标
        {
            if(!m.count(a[i]))
                r[i] = n;
            else
                r[i] = m[a[i]];
            m[a[i]] = i;
        }
        if(judge(0, n-1))
            cout << "non-boring" << endl;
        else
            cout << "boring" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值