Uva1608
如果一个序列的所有子序列中均存在至少一个元素,这个元素在该子序列中只出现一次,则这个序列non-boring。
当一个序列[x,y]中没有元素只出现一次,那么该序列不符合要求,如果有的话,设为第i个元素,则 只要[x,i-1]和 [i+1,n] 符合要求,则该序列符合要求。
在O(N)的时间内预处理一个元素左右离它最近的相同元素的位置,即可在O(1)时间内查询是否这个元素在给定序列中只出现一次。
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
int t;
int a[maxn+];
int lef[maxn+];
int rgt[maxn+];
map<int, int> ml, mr;
bool judge(int i, int x, int y){
if(lef[i] < x && rgt[i] > y){
//printf("%d %d %d\n",i,lef[i],rgt[i]);
return ;
} else {
return ;
}
}
bool dfs(int x, int y){
if(x >= y){
return ;
}
if(x + == y){
return a[x] != a[y];
}
int res = ;
for(int i = x, j = y; i <= j ; ++i, --j){
if(judge(i, x, y)){
// printf("dsadsa:%d %d %d\n",i,x,y);
res = dfs(x, i-) && dfs(i+, y);
} else if(judge(j, x, y)){
// printf("AAAAAA:%d %d %d\n",j,x,y);
res = dfs(x,j-) && dfs(j+, y);
}
if(res == ){
return ;
}
}
return ;
}
void init(){
ml.clear();
mr.clear();
}
int main(){
scanf("%d",&t);
while(t--){
init();
int n;
scanf("%d",&n);
for(int i = ; i <= n; ++i){
scanf("%d",&a[i]);
lef[i] = ;
rgt[i] = inf;
}
for(int i = , j = n; i <= n && j >= ; ++i,--j){
int tmp = ml[a[i]];
int tmp1 = mr[a[j]];
if(tmp){
lef[i] = tmp;
}
ml[a[i]] = i;
if(tmp1){
rgt[j] = tmp1;
}
mr[a[j]] = j;
}
int ans = dfs(,n);
if(ans == ){
printf("boring\n");
} else {
printf("non-boring\n");
}
}
}
Uva1608的更多相关文章
- UVa1608 UVaLive6258 Non-boring sequences
填坑系列(p.248) 比较神 从两端枚举 最坏复杂度就成O(nlogn)了 #include<cstdio> #include<cstdlib> #include<al ...
- 8-16 不无聊序列 non-boring sequences uva1608
题意: 如果一个序列的任意连续子序列中至少有一个只出现一次的元素 则称这个序列是 不无聊序列 输入一个n个元素的序列a 判断是不是不无聊序列 一开始想当然 以为只要 2位的子序列都满足即可 ...
- uva1608 Non-boring sequences
某个序列找到唯一元素后,判断被分成的两边的序列即可问题在于找到唯一元素连续序列,重复元素的问题:感觉很有一般性 查找相同元素用map,last,next存上一个相同元素的位置复杂度计算有点思考:记录l ...
- UVA-1608
We were afraid of making this problem statement too boring, so we decided to keep it short. A sequen ...
- 题解 UVA1608 【不无聊的序列 Non-boring sequences】
思路: 算法很显然: 一.在区间\([l,r]\)找到一个只出现一次的元素P(如果不存在,那么序列\(boring\)) 二.递归处理区间\([l,p-1]\)和区间\([p+1,r]\). 其关键在 ...
- ●UVA 1608 Non-boring sequences
题链: https://vjudge.net/problem/UVA-1608#author=chenchonghan题解: 分治 如果一个区间[l,r]里面在p位置出现了一个只出现一次的元素,(如果 ...
随机推荐
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
- ES6学习之数组扩展
扩展运算符(...将数组分割为用逗号分割的参数序列) console.log(...[1,2,3]) //1 2 3 可替换数组的apply写法: function test(x,y,z){ cons ...
- HTML表格中<td scope="col">与<td scope="row">的含义
HTML表格中<td scope="col">与<td scope="row">的含义 表格中 <td scope="c ...
- 基于Docker部署私有npm
NPM作为前端最cool及最烂的包管理器,它解决困扰前端工程化发展中代码模块管理的大问题.但是随着业务需求的发展,我们的代码从以前的单项目复用,延伸出了多项目复用的需求.本来项目之间代码复用管理的情景 ...
- 服务器修改密码cmd
net user 账号 要修改的密码
- js链表操作
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 6、RNA-Seq Analysis Pipeline
Created by Dhivya Arasappan, last modified by Dennis C Wylie on Nov 08, 2015 This pipeline uses an a ...
- 6.5 系统打开缓慢,怎么办?---更新Ubuntu系统
早早的来公司打开电脑,希望看到Ubuntu能启动成功.可是,当我重启后,使用Ubuntu系统,打开界面速度非常慢,当时,又怀疑自己安装出错了.而且,6.2日Ubuntu系统的工作日志又没了.无奈,我把 ...
- python drift
install dependency(optional):(本文来自 不才b_d 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/sinat_36184075/arti ...
- CEPH安装教程(上)
环境拓扑 主机 配置 地址 运行服务 node CPU:1 内存:2GB 磁盘:vda(20GB) br-mgmt:92.0.0.250 br-ex:192.168.203.250/19 ntp an ...