UVA1608_Non-boring sequences
Non-boring sequences
大致题意:
给你一个字符串,问你他的任一子串是否都包含一个唯一的字符
思路:
看似简单,实际一丁点思路都没有
后面看汝佳的讲解都看了好长时间
大概思路就是,先找一个唯一字符,然后递归这个字符左右两边的字符串,看他们是否满足,知道只含一个元素.但是万一那个唯一元素是最后一个元素呢???这样复杂度就是n方了,这个时候神奇的优化方法出现了!!!!从两边往中间找,最坏情况就是在中间!!!!!
因为Tn = 2*T(n/2) + O(n),最坏也就nlogn
这样的优化方案简直是反人类!!!
不过却真的要好好总结
虽然说我也说不好为什么要这样优化
YY:
为什么要这样呢,因为不管怎么找都会存在一个最坏的情况,所以我们要让最坏的情况"最好",首先最坏情况肯定是查找到最后一个才是,那么先加上O(n)!!!!那如何优化最好呢???一个想法就是让O(n)最坏的情况出现次数最少,那么二分思想就容易想到了,因为使用二分能最快的到达只含有一个元素情况!!!!所以最坏的情况却配上了"最好的情况",这样相互抵消一点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector> using namespace std;
const int maxn = 2e5+100;
#define pr(x) cout << #x << " = " << x << " ";
#define prln(x) cout << #x << " = " << x <<endl;
//#define ll long long int a[maxn], l[maxn], r[maxn];
map<int,int> mp;
bool dfs(int ll, int rr) {
if(ll >= rr) return true;
int maxd = (rr - ll + 1) /2;
for(int d = 0; d <= maxd; ++d) {
if(l[ll+d] < ll && r[ll+d] > rr)
return dfs(ll, ll + d -1) && dfs(ll + d + 1, rr);
if(l[rr-d] < ll && r[rr-d] > rr)
return dfs(ll, rr - d -1) && dfs(rr - d + 1, rr);
}
return false;
}
int main(){
#ifdef LOCAL
freopen("C:\\Users\\User Soft\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\User Soft\\Desktop\\out.txt","w",stdout);
#endif
int t,n; cin >> t;
while( t-- ) {
cin >> n;
mp.clear();
for(int i = 1; i <= n; ++i){
scanf("%d", a + i);
l[i] = -1;
r[i] = n + 1;
}
for(int i = 1; i <= n; ++i) {
if(mp.count(a[i])) {
l[i] = mp[a[i]];
r[l[i]] = i;
}
mp[a[i]] = i;
}
if(dfs(1,n)) cout << "non-boring" << endl;
else cout << "boring" << endl;
}
return 0;
}
UVA1608_Non-boring sequences的更多相关文章
- 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)
4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 440 Solved: 16 ...
- BZOJ 4059: [Cerc2012]Non-boring sequences ( )
要快速在一段子序列中判断一个元素是否只出现一次 , 我们可以预处理出每个元素左边和右边最近的相同元素的位置 , 这样就可以 O( 1 ) 判断. 考虑一段序列 [ l , r ] , 假如我们找到了序 ...
- UVA1608-Non-boring sequences(分治)
Problem UVA1608-Non-boring sequences Accept: 227 Submit: 2541Time Limit: 3000 mSec Problem Descript ...
- poj 1776 Task Sequences
http://poj.org/problem?id=1776 题意: 有一个机器要完成N个作业, 给你一个N*N的矩阵, M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业 M ...
- 2015 Multi-University Training Contest 3 hdu 5324 Boring Class
Boring Class Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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. ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [Leetcode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)
Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...
随机推荐
- [BZOJ3307] 雨天的尾巴(树上差分+线段树合并)
[BZOJ3307] 雨天的尾巴(树上差分+线段树合并) 题面 给出一棵N个点的树,M次操作在链上加上某一种类别的物品,完成所有操作后,要求询问每个点上最多物品的类型. N, M≤100000 分析 ...
- [暑假集训Day1T2]北极通讯网络
这题主要考察对“卫星电话”的理解,k个卫星电话相当于可以让k个联通块保持联通,因此我们只需要让原图连成k个联通块,然后给每个联通块的任意一个节点发一部卫星电话即可.因此我们需要连n-k条边,特别地,当 ...
- 18、NumPy——矩阵库(Matrix)
NumPy 矩阵库(Matrix) NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象. 一个 的矩阵是一个由行(row)列(col ...
- BUUCTF--reverse3
测试文件:https://buuoj.cn/files/aa4f6c7e8d5171d520b95420ee570e79/a9d22a0e-928d-4bb4-8525-e38c9481469e.ra ...
- LeetCode 852. Peak Index in a Mountain Array(C++)
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
- Python之获取文件夹中文件列表以及glob与fnmatch模块的使用
获取文件夹中的文件列表 print(os.listdir("../secondPackage")) # ['__init__.py', 'secondCookBook.py', ' ...
- supermap idesktop连接oraclesptial数据源
1.要使用相同的版本,如iServer 9D, iDesktop9D ,32位的 plsql,32位的 oracleinstance_client 11g 2.当时遇到的问题是使用oracleinst ...
- JavaScript深入之词法作用域和动态作用域(转载)
作用域 作用域是指程序源代码中定义变量的区域. 作用域规定了如何查找变量,也就是确定当前执行代码对变量的访问权限. JavaScript 采用词法作用域(lexical scoping),也就是静态作 ...
- 使用jstack排查线程问题
以一个例子来演示排查服务器cpu占用率过高的问题. 准备 将下面的代码文件上传到服务器上,然后使用javac编译,并使用java命令将程序跑起来. public class JStackCase { ...
- Tab选项卡 延迟切换效果js实现
try.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...