题意:如果一个序列的任意连续子序列中至少有一个只出现一次的元素,则称这个序列式为non-boring。输入一个n(n≤200000)个元素的序列A(各个元素均为109以内的非负整数),判断它是否无聊。

分析:

1、记录下每个元素左边和右边最近的与它值相同的元素的位置。

2、如果某个元素在某一序列中只出现过一次,那这个序列中所有包含该元素的子序列都是non-boring,因此只需要研究在这个序列中以该元素为分界线,其左边和右边的子序列是否无聊即可。

3、因为如果单独从左扫或从右扫,最坏情况可能是在右端或左端,所以从两边同时向中间扫。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 200000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN], l[MAXN], r[MAXN];
map<int, int> mp;
bool solve(int L, int R){
if(L >= R) return true;
for(int i = 0; i <= (R - L) / 2; ++i){
if(l[L + i] < L && r[L + i] > R){
return solve(L, L + i - 1) && solve(L + i + 1, R);
}
if(l[R - i] < L && r[R - i] > R){
return solve(L, R - i - 1) && solve(R - i + 1, R);
}
}
return false;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
mp.clear();
for(int i = 0; i < n; ++i){
if(!mp.count(a[i])) l[i] = -1;
else l[i] = mp[a[i]];
mp[a[i]] = i;
}
mp.clear();
for(int i = n - 1; i >= 0; --i){
if(!mp.count(a[i])) r[i] = n;
else r[i] = mp[a[i]];
mp[a[i]] = i;
}
bool ok = solve(0, n - 1);
if(ok){
printf("non-boring\n");
}
else{
printf("boring\n");
}
}
return 0;
}

  

最新文章

  1. 常用JavaScript触发事件
  2. iOS的一些面试题分析总结(0)
  3. 面试题目——《CC150》线程与锁
  4. bootstrop验证手机号
  5. How do annotations work internally--转
  6. 使用JDBC处理MySQL大数据
  7. Swift:函数和闭包
  8. [C++]默认构造函数
  9. sql 常用操作脚本代码
  10. VB.NET入门基础
  11. jQuery随记
  12. 浅谈web前端开发阅历
  13. docker的一些常用命令整理
  14. Silverlight程序之修改命名空间
  15. Maven 如何发布 jar 包到 Nexus 私库
  16. Python 常用 代码片段
  17. navicat连接oracle失败
  18. A* 寻路学习
  19. IO练习文件读取
  20. Swift3 文件操作常用方法汇总

热门文章

  1. centos7一步一步搭建docker nginx 及重点讲解
  2. Python 基础之递归 递归函数 尾递归 斐波那契
  3. 在Windows中实现Java调用DLL(转载)
  4. 自定义Model类
  5. list的三个子类的特点
  6. Python输出三位数以内的水仙花数
  7. JavaScript高级程序设计(第3版)每章小结(1-5)
  8. Python字符串(一)
  9. android图片保存到SQLlite如何实现?
  10. MariaDB——相关概念与sql语句