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 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...
随机推荐
- GeneXus笔记本—常用函数(上)
国庆放假没事怎么办?写点笔记充会儿电! ≖‿≖✧ 哈哈哈 !!最近在参与公司的其中一个项目中,发现了一些函数自己没见过 也没使用过,但是这些函数都是GeneXus中自带的一些 这此记录的目的就是为 ...
- 2017ICPC沈阳赛现场赛 L-Tree (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6228 题目大意:给一棵树,需要用k种颜色给树上的节点染色,问你在最优的染色方案下,相同颜色的节点连接的 ...
- spring的统一进行异常处理
public class ExceptionHandler extends SimpleMappingExceptionResolver { private static final Logger l ...
- UVA11540 Sultan's Chandelier Burnside 引理 + DP
题目传送门 https://vjudge.net/problem/UVA-11540 https://uva.onlinejudge.org/index.php?option=com_onlineju ...
- 51nod 1253:Kundu and Tree(组合数学)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 所有的三元组的可能情况数有ans0=C(n,3).然后 ...
- shell脚本条件测试与比较
1.条件测试常用语法 test 测试表达式 利用test命令进行条件测试表达式,test命令与测试表达式之间至少有一个空格 [ 测试表达式 ] 通过[ ]中括号进行条件测试表达式,[]中括号边界与测试 ...
- php str_repeat()函数 语法
php str_repeat()函数 语法 str_repeat()函数怎么用? php str_repeat()函数用于重复使用指定字符串,语法是str_repeat(string,repeat), ...
- 4412 linux延时和时间
基本知识 • linux中延时函数很简单,却经常用到• 在操作系统中和单片机处理延时方式就完全不一样了,不可能是使用for循环浪费系统资源.而是有专门的接口函数• linux系统编程中常用的延时函数: ...
- iptables防火墙相关命令详解
前提基础: 当主机收到一个数据包后,数据包先在内核空间中处理,若发现目的地址是自身,则传到用户空间中交给对应的应用程序处理,若发现目的不是自身,则会将包丢弃或进行转发. iptables实现防火墙功能 ...
- delphi按字节长度分割字符串函数(转)
此字符串分割函数用delphi编写,可以适应字符串中存在双字节字符和单字节字符. function TricheditEfm.SplitString(source:string;Sleng:Integ ...