Problem UVA1608-Non-boring sequences

Accept: 227  Submit: 2541
Time Limit: 3000 mSec

Problem Description

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

 
题解:典型的分治,如果找到一个数只出现一次,那么所有跨过这个数的区间都已经成立了,因此只需判断这个数左边和右边的区间的所有子区间是否成立,这样分治的感觉就很明显了,每次在所要判断的区间内找只出现一次的数字,递归判断左右,找数字的时候从左右两边开始一起找,避免最差的情况下变成O(n^2).
 
 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 map<int, int> mmap;

 int n, num[maxn];
int Next[maxn], Pre[maxn]; bool dfs(int le, int ri) {
if (le >= ri) return true; int i = le, j = ri;
int mid;
while (i <= j) {
if (Pre[i] < le && Next[i] > ri) {
mid = i;
break;
}
if (Pre[j] < le && Next[j] > ri) {
mid = j;
break;
}
i++, j--;
}
if (i > j) return false;
return (dfs(le, mid - ) && dfs(mid + , ri));
} int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%d", &num[i]);
} mmap.clear();
for (int i = ; i <= n; i++) {
if (!mmap.count(num[i])) {
Pre[i] = ;
mmap[num[i]] = i;
}
else {
int pos = mmap[num[i]];
Pre[i] = pos;
mmap[num[i]] = i;
}
} mmap.clear();
for (int i = n; i >= ; i--) {
if (!mmap.count(num[i])) {
Next[i] = n + ;
mmap[num[i]] = i;
}
else {
int pos = mmap[num[i]];
Next[i] = pos;
mmap[num[i]] = i;
}
} bool ok = false;
if (dfs(, n)) ok = true; if (ok) printf("non-boring\n");
else printf("boring\n");
}
return ;
}

UVA1608-Non-boring sequences(分治)的更多相关文章

  1. 【bzoj4059】[Cerc2012]Non-boring sequences 分治

    题目描述 我们害怕把这道题题面搞得太无聊了,所以我们决定让这题超短.一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的数字,即每个子序列里至少存在一个数字只出现一次.给定一个整数序列, ...

  2. UVa 1608 Non-boring sequences (分治)

    题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 析:首先如果整个序列中有一个只出过一次的元素,假设是第 p 个,那 ...

  3. bzoj 4059:Non-boring sequences 分治

    题目: 我们害怕把这道题题面搞得太无聊了,所以我们决定让这题超短.一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的数字,即每个子序列里至少存在一个数字只出现一次.给定一个整数序列,请 ...

  4. UVa1608 UVaLive6258 Non-boring sequences

    填坑系列(p.248) 比较神 从两端枚举 最坏复杂度就成O(nlogn)了 #include<cstdio> #include<cstdlib> #include<al ...

  5. UVA - 1608 Non-boring sequences (分治,中途相遇法)

    如果一个序列中是否存在一段连续子序列中的每个元素在该子序列中都出现了至少两次,那么这个序列是无聊的,反正则不无聊.给你一个长度为n(n<=200000)的序列,判断这个序列是否无聊. 稀里糊涂A ...

  6. Boring Class HDU - 5324 (CDQ分治)

    Mr. Zstu and Mr. Hdu are taking a boring class , Mr. Zstu comes up with a problem to kill time, Mr. ...

  7. BZOJ 4059 [Cerc2012]Non-boring sequences(启发式分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4059 [题目大意] 一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的 ...

  8. Non-boring sequences(启发式分治)

    题意:一个序列被称作是不无聊的,当且仅当,任意一个连续子区间,存在一个数字只出现了一次,问给定序列是否是不无聊的. 思路:每次找到一个只出现了一次的点,其位置的pos,那么继续分治[L,pos-1], ...

  9. BZOJ 4059: [Cerc2012]Non-boring sequences(启发式分治)

    传送门 解题思路 首先可以想到要预处理一个\(nxt_i\)和\(pre_i\),表示前后与当前位置权值相同的节点,那么这样可以迅速算出某个点在某段区间是否出现多次.然后这样的话就考虑分治,对于\([ ...

随机推荐

  1. mybatis_01简介

    1.1 MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...

  2. 12. ReadWriteLock 读写锁

    package com.gf.demo11; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent. ...

  3. Java多线程之内存可见性

    阅读本文约“3分钟” 共享变量在线程间的可见性 synchronized实现可见性 volatile实现可见性 —指令重排序 —as-if-serial语义 —volatile使用注意事项 synch ...

  4. python文件

    目录 1. 文件的概念 1.1 文件的概念和作用 1.2 文件的存储方式 2. 文件的基本操作 2.1 操作文件的套路 2.2 操作文件的函数/方法 2.3 read 方法 -- 读取文件 2.4 打 ...

  5. React.cloneElement

    作用: 克隆react element, 并传递props, 和children React.cloneElement( element, [props], [...children] ) // ch ...

  6. 微信小程序 JS 获取View 和 屏幕相关属性(高度、宽度等等)

    wx.getSystemInfo({success: function (res) {thisWidth = res.windowWidth;}}); that.setData({view_Width ...

  7. ChartControl ViewType.Pie3D 用法测试

    效果图一. public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void For ...

  8. angularjs-select2的使用

    1.引入文件 '/select2.css', '/select2-bootstrap.css', '/select2.min.js', ‘/angular-select2.min.js’ 2.页面 3 ...

  9. meta、link、script的常用写法

    meta 设置编码格式 <meta charset='utf-8'> 设置关键字 <meta name="keywords" content="音乐 播 ...

  10. 自定义控件详解(六):Paint 画笔MaskFilter过滤

    首先看一个API:setMaskFilter(MaskFilter maskfilter): 设置MaskFilter,可以用不同的MaskFilter实现滤镜的效果,如滤化,立体等. 以下有两个Ma ...