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. js节点的类型

    1. dom>documentElement>body>tagname 2.我们常用的节点标签. 元素节点(标签) 文本节点 属性节点(标签里的属性) 3.document有个属性n ...

  2. 汇编语言--微机CPU的指令系统(五)(算术运算指令)

    (3)算术运算指令 算术运算指令是反映CPU计算能力的一组指令,也是编程时经常使用的一组指令.它包括:加.减.乘.除及其相关的辅助指令. 该组指令的操作数可以是8位.16位和32位(80386+).当 ...

  3. JS中将变量转为字符串

    译者按: 语言的细枝末节了解一下就可以了,不需要太较真,不过如果一点也不知道的话,那就不太妙了. 原文: Converting a value to string in JavaScript 译者:  ...

  4. Atom插件安装及推荐

    简介(了解更多去google或baidu) Atom 代码编辑器支持 Windows.Mac.Linux 三大桌面平台,完全免费,并且已经在 GitHub 上开放了全部的源代码.在经过一段长时间的迭代 ...

  5. 04-HTML-图片标签

    <html> <head>  <title>图片标签学习</title>  <meta charset="utf-8"/> ...

  6. jQuery的一生

    jQuery 1.什么是jQuery? 是轻量级的,兼容多浏览器的JavaScript库,使用户能够方便的处理HTML Document,Events,实现动画效果,方便进行Ajax交互,能够极大地简 ...

  7. 获取邮箱的DNS和MX 工具类

    1.导入Maven  DNS  包: <dependency> <groupId>dnsjava</groupId> <artifactId>dnsja ...

  8. BZOJ2746: [HEOI2012]旅行问题(AC自动机 LCA)

    Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1188  Solved: 383[Submit][Status][Discuss] Descripti ...

  9. loj#6031. 「雅礼集训 2017 Day1」字符串(SAM 广义SAM 数据分治)

    题意 链接 Sol \(10^5\)次询问每次询问\(10^5\)个区间..这种题第一感觉就是根号/数据分治的模型. \(K\)是个定值这个很关键. 考虑\(K\)比较小的情况,可以直接暴力建SAM, ...

  10. 使用adb命令通过IP地址连接手机

    前提:已经通过USB设备线连接过电脑,并成功安装驱动. adb连接手机进行调试有两种方式,一种是使用USB线,另一种是使用无线WiFi. 第一种  使用USB线连接 1. 在手机上启用USB调试 2. ...