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
Sample Input
Sample Output
non-boring
boring
non-boring
boring
#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(分治)的更多相关文章
- 【bzoj4059】[Cerc2012]Non-boring sequences 分治
题目描述 我们害怕把这道题题面搞得太无聊了,所以我们决定让这题超短.一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的数字,即每个子序列里至少存在一个数字只出现一次.给定一个整数序列, ...
- UVa 1608 Non-boring sequences (分治)
题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 析:首先如果整个序列中有一个只出过一次的元素,假设是第 p 个,那 ...
- bzoj 4059:Non-boring sequences 分治
题目: 我们害怕把这道题题面搞得太无聊了,所以我们决定让这题超短.一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的数字,即每个子序列里至少存在一个数字只出现一次.给定一个整数序列,请 ...
- UVa1608 UVaLive6258 Non-boring sequences
填坑系列(p.248) 比较神 从两端枚举 最坏复杂度就成O(nlogn)了 #include<cstdio> #include<cstdlib> #include<al ...
- UVA - 1608 Non-boring sequences (分治,中途相遇法)
如果一个序列中是否存在一段连续子序列中的每个元素在该子序列中都出现了至少两次,那么这个序列是无聊的,反正则不无聊.给你一个长度为n(n<=200000)的序列,判断这个序列是否无聊. 稀里糊涂A ...
- 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. ...
- BZOJ 4059 [Cerc2012]Non-boring sequences(启发式分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4059 [题目大意] 一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的 ...
- Non-boring sequences(启发式分治)
题意:一个序列被称作是不无聊的,当且仅当,任意一个连续子区间,存在一个数字只出现了一次,问给定序列是否是不无聊的. 思路:每次找到一个只出现了一次的点,其位置的pos,那么继续分治[L,pos-1], ...
- BZOJ 4059: [Cerc2012]Non-boring sequences(启发式分治)
传送门 解题思路 首先可以想到要预处理一个\(nxt_i\)和\(pre_i\),表示前后与当前位置权值相同的节点,那么这样可以迅速算出某个点在某段区间是否出现多次.然后这样的话就考虑分治,对于\([ ...
随机推荐
- 【Java每日一题】20170324
20170323问题解析请点击今日问题下方的“[Java每日一题]20170324”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...
- Java基础IO流(四)序列化与反序列化
对象的序列化与反序列化: 对象的序列化,就是将Object转换成byte序列,反之叫对象的反序列化. 序列化流(ObjectOutInputStream),是过滤流 -------writeObjec ...
- learnVUE-note
title: learnVUE-note date: 2018-02-27 15:57:37 tags: categories: 前端技术 --- 本文是自己在学习Vue中的 VUE事件处理 在事件处 ...
- Oracle+mybatis实现对数据的简单增删改查
第一步:--创建一个表空间:名字叫 mybatis,建在D盘下的date文件夹下: 第二步:创建用户,名字叫 lisi ,密码为 :123456 第三步:给用户授权: 第四步:我们在 li ...
- 【软工神话】第四篇(Alpha冲刺)
前言:咳咳,一个多月了,最近忙这忙那,都把这个故事给落下了,昨晚刚回到家,白天得干活,晚上赶紧把这个故事写完,以此纪念自己的软工. 说明:故事中的人物均是化名,故事情节经过些许加工,故事情节并没有针对 ...
- JS怎样实现图片的懒加载以及jquery.lazyload.js的使用
在项目中有时候会用到图片的延迟加载,那么延迟加载的好处是啥呢? 我觉得主要包括两点吧,第一是在包含很多大图片长页面中延迟加载图片可以加快页面加载速度:第二是帮助降低服务器负担. 下面介绍一下常用的延迟 ...
- Android内存优化(四)LeakCanary使用详解
LeakCanary是检测App内存泄露的工具, 内存泄露是Android开发中常见的问题, 使用程序的稳定性下降. LeakCanary 的机制如下: RefWatcher.watch() 会以监控 ...
- C#:关于C#4中IEnumerable<out T>的理解
IEnumerable<out T>这个接口非常常见,它是最基础的泛型集合接口,表示可迭代的项的序列. 但是奇怪的是为什么泛型参数要带一个“out”? 经过一番资料查阅后,发现此“out” ...
- (网页)table加上分页,优点可随便加样式
1.先有静态的页面: <div class="col-xs-12"> <table id="tbtablesaleinfo" class=&q ...
- css 布局之定位 相对/绝对/成比例缩放
给body添加 overflow: hidden; 可以将页面所有的 滚动条隐藏,但必须要给body 设置一个高度 overflow: hidden; height:864px; 父元素必须要设置 p ...