UVA
来源:互联网 发布:java删除文件失败 编辑:程序博客网 时间:2024/05/16 08:18
We were afraid of making this problem statement too boring, so we decided to keep it short. A sequence
is called non-boring if its every connected subsequence contains a unique element, i.e. an element such
that no other element of that subsequence has the same value.
Given a sequence of integers, decide whether it is non-boring.
Input
The first line of the input contains the number of test cases T. The descriptions of the test cases follow:
Each test case starts with an integer n (1 ≤ n ≤ 200000) denoting the length of the sequence. In
the next line the n elements of the sequence follow, separated with single spaces. The elements are
non-negative integers less than 109.
Output
Print the answers to the test cases in the order in which they appear in the input. For each test case
print a single line containing the word ‘non-boring’ or ‘boring’.
Sample Input
4
5
1 2 3 4 5
5
1 1 1 1 1
5
1 2 3 2 1
5
1 1 2 1 1
Sample Output
non-boring
boring
non-boring
boring
一组数列,如果其中有一个数字只出现过一次,则可以判定它是non-boring的。
先找出母序列中唯一的数,它在母序列中是唯一的,那么在子序列中也是唯一的,则所有跨过它的子序列也都是non-boring的,不用判断,所以可以递归查找数字两边的子序列,直到查到长度为1则可以返回true。
那么如何判断这个数在序列中是唯一的?可以在读入的时候记录这个数字上一次出现和下一次出现的位置,判断是否在数列的范围内即可。
#include <stdio.h>#include <map>using namespace std;int l[200015],r[200015],a[200015];bool sss(int x,int y){ if(x>=y){ return true; } for(int i=0;i<=(y-x)/2;i++){ if(l[x+i]<x && r[x+i]>y){ return sss(x,x+i-1) && sss(x+i+1,y); } if(l[y-i]<x&&r[y-i]>y){ return sss(x,y-i-1) && sss(y-i+1,y); } } return false;}int main(){ int T,n,i,j,k; scanf("%d",&T); while(T--){ map<int,int> m; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d",&a[i]); } for(i=0;i<n;i++){ if(!m.count(a[i])){ l[i]=-1; }else{ l[i]=m[a[i]]; } m[a[i]]=i; } m.clear(); for(i=n-1;i>=0;i--){ if(!m.count(a[i])){ r[i]=n; }else{ r[i]=m[a[i]]; } m[a[i]]=i; } m.clear(); if(sss(0,n-1)){ printf("non-boring\n"); }else{ printf("boring\n"); } } return 0;}
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- linux 安装MySQL
- C#调用sap rfc 接口
- 依存句法分析和语义依存分词的区别
- pagehelper
- Informatica基础系列(三)——表达式、筛选、查找组件
- UVA
- 使用SHGetFileInfo函数获取任何文件大图标
- MySQL数据库安装与配置详解
- java调用Python遇到的一系列问题
- 大学生职业生涯规划
- sqlserver中如何模糊查找表名或存储名等等?利用sys.objects
- 《深入分析Java Web技术内幕》学习(二)
- Rectified Linear Units, 线性修正单元激活函数
- Test