UVa 1608 Non-boring sequences (分治)
题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的。
析:首先如果整个序列中有一个只出过一次的元素,假设是第 p 个,那么我就可以看他左边和右边的序列是不是不无聊,也就是判断 1~p-1 和 p+1 ~ n,这可以用分治来进行处理。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200000 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
} int a[maxn];
int l[maxn], r[maxn];
map<int, int> mp; bool dfs(int L, int R){
if(L >= R) return true;
int t = R - L >> 1;
for(int i = 0; i <= t; ++i){
if(l[i+L] < L && r[i+L] > R) return dfs(L, i+L-1) && dfs(i+1+L, R);
if(l[R-i] < L && r[R-i] > R) return dfs(L, R-i-1) && dfs(R-i+1, R);
}
return false;
} int main(){
int T;
cin >> T;
while(T--){
scanf("%d", &n);
mp.cl;
for(int i = 1; i <= n; ++i){
scanf("%d", a+i);
if(mp.count(a[i])) l[i] = mp[a[i]];
else l[i] = 0;
mp[a[i]] = i;
}
mp.cl;
for(int i = n; i; --i){
if(mp.count(a[i])) r[i] = mp[a[i]];
else r[i] = n+1;
mp[a[i]] = i;
}
puts(dfs(1, n) ? "non-boring" : "boring");
}
return 0;
}
UVa 1608 Non-boring sequences (分治)的更多相关文章
- UVa 1608,Non-boring sequences
好诡异的一个题啊 紫书上关于从左边找还是从两边往中间找的讨论没有看懂,怎么一下就找到唯一的元素了(⊙_⊙?) 方法就是用的书上讲的方法,类似于uva 11572,不过这个题需要预处理存下两边的最近的相 ...
- uva 1608 不无聊的序列
uva 1608 不无聊的序列 紫书上有这样一道题: 如果一个序列的任意连续子序列中都至少有一个只出现一次的元素,则称这个序列时不无聊的.输入一个n个元素的序列,判断它是不是无聊的序列.n<=2 ...
- 【bzoj4059】[Cerc2012]Non-boring sequences 分治
题目描述 我们害怕把这道题题面搞得太无聊了,所以我们决定让这题超短.一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的数字,即每个子序列里至少存在一个数字只出现一次.给定一个整数序列, ...
- UVa 1608 (分治 中途相遇) Non-boring sequences
预处理一下每个元素左边和右边最近的相邻元素. 对于一个区间[l, r]和区间内某一个元素,这个元素在这个区间唯一当且仅当左右两边最近的相邻元素不在这个区间内.这样就可以O(1)完成查询. 首先查找整个 ...
- UVA - 1608 Non-boring sequences (分治,中途相遇法)
如果一个序列中是否存在一段连续子序列中的每个元素在该子序列中都出现了至少两次,那么这个序列是无聊的,反正则不无聊.给你一个长度为n(n<=200000)的序列,判断这个序列是否无聊. 稀里糊涂A ...
- UVA - 1608 Non-boring sequences (分治)
题意:如果一个序列的任意连续子序列中至少有一个只出现一次的元素,则称这个序列式为non-boring.输入一个n(n≤200000)个元素的序列A(各个元素均为109以内的非负整数),判断它是否无聊. ...
- UVa 1608 - Non-boring sequences
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- ●UVA 1608 Non-boring sequences
题链: https://vjudge.net/problem/UVA-1608#author=chenchonghan题解: 分治 如果一个区间[l,r]里面在p位置出现了一个只出现一次的元素,(如果 ...
- UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)
题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 先预处理出每个元素之前和之后相同元素出现的位置,就可以在O(1)的 ...
随机推荐
- jquery发送数组
假设keywordid_list是一个javascript数组,将它发送到keywordbatchsetting.php. $.ajax({ url:"keywordbatchsetting ...
- 详解AJAX核心中的XMLHttpRequest对象
转自:http://developer.51cto.com/art/200904/119577.htm XMLHttpRequest 对象是AJAX功能的核心,要开发AJAX程序必须从了解XMLHtt ...
- JS的事件流的概念
事件的概念: HTML中与javascript交互是通过事件驱动来实现的,例如鼠标点击事件.页面的滚动事件onscroll等等,可以向文档或者文档中的元素添加事件侦听器来预订事件.想要知道这些事件是在 ...
- LinkedHashMap学习
一.概述 LinkedHashMap继承自HashMap,是Map接口的一个具体实现,它是有序的,可以按照插入顺序先后和访问时间先后进行排序,选择哪种排序方式取决于在新建LinkedHashMap的时 ...
- ansible自动化运维工具使用详解
一. ansible 简介 1. ansible ansible是新出现的 自动化 运维工具 , 基于Python研发 . 糅合了众多老牌运维工具的优点实现了批量操作系统配置.批量程序的部署.批量运行 ...
- MySQL查看用户权限的两种方法
http://yanue.net/post-96.html MySQL查看用户权限命令的两方法: 一. 使用MySQL grants MySQL grant详细用法见:http://yanue.net ...
- SQL 语句中的in、find_in_set、like的区别
1.in查询相当于多个or条件的叠加,例如: select * from user where user_id in (1,2,3);等效于select * from user where user_ ...
- git 转移
git push --mirror https://github.com/cloud-pi/drbd-docker-plugin.git
- debian下使用dig/nslookup
debian默认没有安装dig/nslookup命令,使用下面命令安装: apt-get install dnsutils red-hat系列使用: yum install bind-utils ho ...
- call和callvirt
call以非虚方式调用虚函数. callvirt以虚方式调用虚函数,调用的时候会判断真正引用的对象,调用该类型的