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 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...
随机推荐
- python面试题之如何读取大文件
① 利用生成器generator ②迭代器进行迭代遍历:for line in file 文章转载自Python黑洞网
- Leetcode Lect1 String相关题目
Java 的 String 类基本用法介绍:http://www.runoob.com/java/java-string.html Java 的 String.substring 函数:https:/ ...
- wangEditor 文本编辑器
参考:https://www.cnblogs.com/Scholars/p/8968838.html 下载:http://www.wangeditor.com/ 前端代码: <script ty ...
- JVM中类加载器的父委托机制
类加载器 类加载器用来把类加载到Java虚拟机中. 类加载器的类型 有两种类型的类加载器: 1.JVM自带的加载器: 根类加载器(Bootstrap) 扩展类加载器(Extension) 系统类加载器 ...
- Error- Overloaded method value createDirectStream in error Spark Streaming打包报错
直接上代码 StreamingExamples.setStreamingLogLevels() val Array(brokers, topics) = args // Create context ...
- 初学Java 从控制台读取输入
代码 import java.util.Scanner; public class ComputeArea { public static void main(String[] args) { Sca ...
- Center OS 7
1:关闭防火墙 systemctl stop iptables.service 2:禁止开启启动 systemctl disable firewalld.service 3:查看防火墙 firewal ...
- vsftpd配置详解
匿名用户权限控制: anonymous_enable=YES #是否启用匿名用户 no_anon_password=YES #匿名用户login时不询问口令 anon_upload_enable=(y ...
- day17 python re模块 简易爬虫
day17 python 一.re模块 1.re模块的基础方法 查找findall() import re #re.findall(pattern,string,flags ...
- @PostMapping
@PostMapping映射一个POST请求 Spring MVC新特性 提供了对Restful风格的支持 @GetMapping,处理get请求 @PostMapping,处理post请求 @Put ...