博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA-1608
阅读量:5090 次
发布时间:2019-06-13

本文共 2098 字,大约阅读时间需要 6 分钟。

We were afraid of making this problem statement too boring, so we decided to keep it short. A sequenceis called non-boring if its every connected subsequence contains a unique element, i.e. an element suchthat no other element of that subsequence has the same value.Given a sequence of integers, decide whether it is non-boring.InputThe 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. Inthe next line the n elements of the sequence follow, separated with single spaces. The elements arenon-negative integers less than 109.OutputPrint the answers to the test cases in the order in which they appear in the input. For each test caseprint a single line containing the word ‘non-boring’ or ‘boring’.Sample Input451 2 3 4 551 1 1 1 151 2 3 2 151 1 2 1 1Sample Outputnon-boringboringnon-boringboring

题意:就是判断一串数字的任意子序列是否满足至少有一个元素在该序列中只出现一次;题好理解,就是写有点难。

题解:对于一组数在区间(l,r)中有一个数M只出现一次,则只需要判断(l,M-1)和(M+1,r)区间是否有这样的数就可以啦,如果找不到返回false。可以利用递归,首先将该数组中的数从其位置开始向左右遍历,找到其左右第一个与其位置相同的下标(可以利用map来查找)。

AC代码为:

#include <iostream>

#include <algorithm>
#include <map>
#include <cstdio>
#include<cstring>
using namespace std;
int a[200010];
int l[200010], r[200010];
int n;
bool f(int left, int right)
{
if (left >= right)
return true;
for (int i = 0; i <= (right - left) / 2; i++)
{
if (l[left + i]<left && r[left + i]>right)
return f(left, left + i - 1) && f(left + i + 1, right);
if (l[right - i]<left && r[right - i]>right)
return f(left, right - i - 1) && f(right - i + 1, right);
}
return false;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
map<int, int> m;
scanf("%d",&n);
for (int i = 0; i<n; i++)
scanf("%d", a+i);
m.clear();
for (int i=0; i<n; i++)
{
if (m.count(a[i]))
l[i] = m[a[i]];
else
l[i] = -2;
m[a[i]] = i;
}
m.clear();
for (int i = n - 1; i >= 0; i--)
{
if (m.count(a[i]))
r[i] = m[a[i]];
else
r[i] = n;
m[a[i]] = i;s
}
if (f(0,n-1))
printf("non-boring\n");
else
printf("boring\n");
}
return 0;
}

转载于:https://www.cnblogs.com/songorz/p/9386622.html

你可能感兴趣的文章
【2.2】创建博客文章模型
查看>>
【3.1】Cookiecutter安装和使用
查看>>
【2.3】初始Django Shell
查看>>
Linux(Centos)之安装Redis及注意事项
查看>>
bzoj 1010: [HNOI2008]玩具装箱toy
查看>>
Kotlin动态图
查看>>
seven day--面向对象及模块
查看>>
各种求逆元
查看>>
基元线程同步构造
查看>>
ElasticSearch 获取es信息以及索引操作
查看>>
Apollo快速安装视频教程
查看>>
mysql 用户管理和权限设置(转)
查看>>
PHP进程通信基础——信号
查看>>
32复用
查看>>
COGS 1578. 次小生成树初级练习题
查看>>
jquery $.fn $.fx是什么意思有什么用
查看>>
javaWeb中的文件上传下载
查看>>
开始学习MFC
查看>>
第三周作业(三)
查看>>
手把手教你如何使用webpack+react
查看>>