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 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...
随机推荐
- vue 中使用 watch 出现了如下的报错
vue 中使用 watch 出现了如下的报错 报错: Method "watch" has type "object" in the component def ...
- CSS-02 BFC的理解
两个概念 感觉BFC挺重要的,于是最近查阅网上资料后小结一下,如果有不对的地方还望指正. 先理解两个概念: BOX :盒子模型 Block-Leave Box :块级元素 display属性为bloc ...
- linux的vsftp使用介绍
vsftp 常用操作 安装 yum install vsftpd 启动服务 service vsftpd start 开启自动启动 chkconfig --level 35 vsftpd on 关闭匿 ...
- 268-基于FMC接口的DSP TMS320C6657子卡模块
基于FMC接口的DSP TMS320C6657子卡模块 一. 概述 FMC连接器是一种高速多pin的互连器件,广泛应用于板卡对接的设备中,特别是在xilinx公司的所有开发板中都使用. ...
- vue,一路走来(6)--微信支付
微信支付 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 分享一下vue实现微信支付.在微信浏览器里面 ...
- openssl版本升级
1.查看openssl版本 [root@node2 openssh-8.0p1]# openssl version OpenSSL 1.0.1e-fips 11 Feb 2013 2.下载指定版本的o ...
- .net core 操作oracle
依赖项——右键——管理NuGet程序包——浏览——输入以下内容 oracle.ManagedDataAccess.core(记得勾选包括预发行版) 在页面中加入操作数据库的方法 public IAct ...
- vim/vi编辑工具实现多行注释和取消注释
转载声明:本文为转载文章 原文地址:https://blog.51cto.com/3332935/2106143 多行注释: 进入命令行模式--> 将光标移动到要注释的第一行位置--> 按 ...
- ubuntu 安装phpunit
一.下载安装 wget https://phar.phpunit.de/phpunit-7.2.phar chmod +x phpunit-7.2.phar sudo mv phpunit-7.2.p ...
- grep正则表达式(一)
新建一批 txt 文件: [me@linuxbox ~]$ ls /bin > dirlist-bin.txt [me@linuxbox ~]$ ls /usr/bin > dirlist ...