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).
 
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = + ;
  6.  
  7. map<int, int> mmap;
  8.  
  9. int n, num[maxn];
  10. int Next[maxn], Pre[maxn];
  11.  
  12. bool dfs(int le, int ri) {
  13. if (le >= ri) return true;
  14.  
  15. int i = le, j = ri;
  16. int mid;
  17. while (i <= j) {
  18. if (Pre[i] < le && Next[i] > ri) {
  19. mid = i;
  20. break;
  21. }
  22. if (Pre[j] < le && Next[j] > ri) {
  23. mid = j;
  24. break;
  25. }
  26. i++, j--;
  27. }
  28. if (i > j) return false;
  29. return (dfs(le, mid - ) && dfs(mid + , ri));
  30. }
  31.  
  32. int main()
  33. {
  34. //freopen("input.txt", "r", stdin);
  35. int iCase;
  36. scanf("%d", &iCase);
  37. while (iCase--) {
  38. scanf("%d", &n);
  39. for (int i = ; i <= n; i++) {
  40. scanf("%d", &num[i]);
  41. }
  42.  
  43. mmap.clear();
  44. for (int i = ; i <= n; i++) {
  45. if (!mmap.count(num[i])) {
  46. Pre[i] = ;
  47. mmap[num[i]] = i;
  48. }
  49. else {
  50. int pos = mmap[num[i]];
  51. Pre[i] = pos;
  52. mmap[num[i]] = i;
  53. }
  54. }
  55.  
  56. mmap.clear();
  57. for (int i = n; i >= ; i--) {
  58. if (!mmap.count(num[i])) {
  59. Next[i] = n + ;
  60. mmap[num[i]] = i;
  61. }
  62. else {
  63. int pos = mmap[num[i]];
  64. Next[i] = pos;
  65. mmap[num[i]] = i;
  66. }
  67. }
  68.  
  69. bool ok = false;
  70. if (dfs(, n)) ok = true;
  71.  
  72. if (ok) printf("non-boring\n");
  73. else printf("boring\n");
  74. }
  75. return ;
  76. }

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. Java_Comparable,Comparator两接口区别

    Comparable和Comparator的区别 根本区别 1.Comparable是一个内比较器,Comparator是一个外比较器 封装的包不同 java.util.Comparator java ...

  2. Spring核心——设计模式与IoC

    “Spring”——每一个Javaer开发者都绕不开的字眼,从21世纪第一个十年国内异常活跃的SSH框架,到现在以Spring Boot作为入口粘合了各种应用.Spring现在已经完成了从web入口到 ...

  3. 4. explain简介

    一.是什么 使用 explain 关键字可以模拟优化器执行SQl查询语句,从而知道 mysql 是如何处理你的sql语句的.分析你的查询语句或是表的结构的性能瓶颈. 二.能干嘛 表的读取顺序 数据读取 ...

  4. 自己用HashMap来模拟一个Session缓存(简易版)

    本文记录:Hibernate中一级缓存的特点. 一级缓存的细节什么操作会向一 1.级缓存放入数据 save,update,saveOrUpdate,load,get,list,iterate,lock ...

  5. Java 原生网络编程.

    一.概念 Java 语言从其诞生开始,就和网络紧密联系在一起.在 1995 年的 Sun World 大会上,当时占浏览器市场份额绝对领先的网景公司宣布在浏览器中支持Java,从而引起一系列的公司产品 ...

  6. 为什么越来越少的人用jQuery

    摘要:JQuery该退役了. 原文:为什么越来越少的人用jQuery 作者:Lemonade Fundebug经授权转载,版权归原作者所有. 最早期的开发,大多都使用jQuery,它给我们带来了很多的 ...

  7. 快速掌握JavaScript面试基础知识(三)

    译者按: 总结了大量JavaScript基本知识点,很有用! 原文: The Definitive JavaScript Handbook for your next developer interv ...

  8. 洛谷P4590 [TJOI2018]游园会(状压dp LCS)

    题意 题目链接 Sol 这个题可能是TJOI2018唯一的非模板题了吧.. 考虑LCS的转移方程, \[f[i][j] = max(f[i - 1][j], f[i][j - 1], f[i - 1] ...

  9. 在 Apex 中使用合并统计查询

    SOQL 中的合并统计查询 在 SOQL 中,我们可以使用一系列函数来进行合并统计查询.它们的功能和标准 SQL 中的 SUM(),COUNT() 等函数类似. 官方文档 Apex 中使用合并统计查询 ...

  10. loadrunner脚本优化-ParameterList参数类型介绍

    脚本优化-Parameter List参数类型介绍 by:授客 QQ:1033553122 篇幅问题,这里采用网盘下载的方式和大家分享: 百度网盘分享: 链接: http://pan.baidu.co ...