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的更多相关文章

  1. 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)

    4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 16 ...

  2. BZOJ 4059: [Cerc2012]Non-boring sequences ( )

    要快速在一段子序列中判断一个元素是否只出现一次 , 我们可以预处理出每个元素左边和右边最近的相同元素的位置 , 这样就可以 O( 1 ) 判断. 考虑一段序列 [ l , r ] , 假如我们找到了序 ...

  3. UVA1608-Non-boring sequences(分治)

    Problem UVA1608-Non-boring sequences Accept: 227  Submit: 2541Time Limit: 3000 mSec Problem Descript ...

  4. poj 1776 Task Sequences

    http://poj.org/problem?id=1776 题意: 有一个机器要完成N个作业, 给你一个N*N的矩阵, M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业 M ...

  5. 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 ...

  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. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  8. [Leetcode] Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  9. 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)

    Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...

随机推荐

  1. 14、numpy——统计函数

    NumPy 统计函数 NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等. 函数说明如下:(沿哪条轴执行,就是是最后结果的形式) 1.numpy.amin() 和 ...

  2. installing-sql-server-2012-error-prior-visual-studio-2010-instances-requiring 转摘

    there are two way: First : Inside your CD of SQL Server 2012 you can go to this path \redist\VisualS ...

  3. spring(五):spring中Aware接口的使用

    spring中自定义组件需要使用spring的底层组件时,可以通过自定义组件实现相关XxxAware接口,重写其中的方法进而实现 例如:自定义一个组件,该组件中需要使用ApplicationConte ...

  4. 导入excle到服务器时候删除服务器历史数据

    //删除历史数据EXCLE 当天前一天的数据都删除 var folder = Path.GetDirectoryName(absFilePath); var files = Directory.Get ...

  5. 2019-9-2-win10-uwp-打电话

    title author date CreateTime categories win10 uwp 打电话 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 17 ...

  6. (转)yum的$releasever真是太反动了

    Posted on 2009年 10月9日 by JulyClyde 来看这篇文章的人,大都应该同意<Unix编程艺术>中提到的那些观点吧.今天就给大家看一个反例:yum 的 $relea ...

  7. 基于TI 多核DSP 的大数据智能计算处理解决方案

    北京太速科技有限公司 大数据智能计算,是未来的一个发展趋势,大数据计算系统主要完成数据的存储和管理:数据的检索与智能计算. 特别是在智能城市领域,由于人口聚集给城市带来了交通.医疗.建筑等各方面的压力 ...

  8. 笔记--NS_SWIFT_NAME与@objc区别与用途

    swift中使用Selector经常要在方法前面添加@objc,除了默认的@objc,其实我们还可以添加自己制定的swift中调用的函数名 @objc(pushToControllerName:par ...

  9. vuex的配置使用

    为什么使用vuex? vuex主要是是做数据交互,父子组件传值可以很容易办到,但是兄弟组件间传值(兄弟组件下又有父子组件),或者大型spa单页面框架项目,页面多并且一层嵌套一层的传值,异常麻烦,用vu ...

  10. 清理maven缓存

    原文:https://blog.csdn.net/viplisong/article/details/82963989maven下载失败后会缓存文件,可能导致下次下载失败.通过以下两步清理 1.cd ...