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;

}

UVA-1608的更多相关文章

  1. uva 1608 不无聊的序列

    uva 1608 不无聊的序列 紫书上有这样一道题: 如果一个序列的任意连续子序列中都至少有一个只出现一次的元素,则称这个序列时不无聊的.输入一个n个元素的序列,判断它是不是无聊的序列.n<=2 ...

  2. UVa 1608,Non-boring sequences

    好诡异的一个题啊 紫书上关于从左边找还是从两边往中间找的讨论没有看懂,怎么一下就找到唯一的元素了(⊙_⊙?) 方法就是用的书上讲的方法,类似于uva 11572,不过这个题需要预处理存下两边的最近的相 ...

  3. UVa 1608 - Non-boring sequences

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVa 1608 (分治 中途相遇) Non-boring sequences

    预处理一下每个元素左边和右边最近的相邻元素. 对于一个区间[l, r]和区间内某一个元素,这个元素在这个区间唯一当且仅当左右两边最近的相邻元素不在这个区间内.这样就可以O(1)完成查询. 首先查找整个 ...

  5. ●UVA 1608 Non-boring sequences

    题链: https://vjudge.net/problem/UVA-1608#author=chenchonghan题解: 分治 如果一个区间[l,r]里面在p位置出现了一个只出现一次的元素,(如果 ...

  6. UVa 1608 Non-boring sequences (分治)

    题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 析:首先如果整个序列中有一个只出过一次的元素,假设是第 p 个,那 ...

  7. UVA - 1608 Non-boring sequences (分治,中途相遇法)

    如果一个序列中是否存在一段连续子序列中的每个元素在该子序列中都出现了至少两次,那么这个序列是无聊的,反正则不无聊.给你一个长度为n(n<=200000)的序列,判断这个序列是否无聊. 稀里糊涂A ...

  8. UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)

    题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 先预处理出每个元素之前和之后相同元素出现的位置,就可以在O(1)的 ...

  9. UVA - 1608 Non-boring sequences(分治法)

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

  10. 紫书 例题8-16 UVa 1608 (递归)

    题意: 判断所给序列是否满足任意连续子序列中至少有一个出现一次的元素. 思路:在整体中找到一个只出现一次的元素, 然后在递归两边.因为两边的序列中有这个数那就满足要求, 所以就看剩下的序列漫步满足要求 ...

随机推荐

  1. docker项目——搭建飞机大战小游戏

    项目2:搭建打飞机小游戏,验证数据持久化(最底下有链接) 第一步:拉取镜像 [root@localhost docker-image]# docker load < httpd_img.tar. ...

  2. JavaScript 运行原理

    i{margin-right:4px;margin-top:-0.2em}.like_comment_tips .weui-icon-success{background:transparent ur ...

  3. nyoj 199-无线网络覆盖 (ceil())

    199-无线网络覆盖 内存限制:64MB 时间限制:3000ms 特判: No 通过数:4 提交数:13 难度:3 题目描述: 我们的乐乐同学对于网络可算得上是情有独钟,他有一个计划,那就是用无线网覆 ...

  4. python:调用bash

    利用os模块 python调用Shell脚本,有三种方法: os.system(cmd)返回值是脚本的退出状态码 os.popen(cmd)返回值是脚本执行过程中的输出内容 commands.gets ...

  5. 附010.Kubernetes永久存储之GlusterFS超融合部署

    一 前期准备 1.1 基础知识 在Kubernetes中,使用GlusterFS文件系统,操作步骤通常是: 创建brick-->创建volume-->创建PV-->创建PVC--&g ...

  6. Nginx 匹配流程一览

    在 nginx server 模块中,location 的定义长被用来匹配一个标准的 URI, 并根据 URI 的不同做出相应的服务方案. nginx location 匹配的优先级 在 locati ...

  7. python_day04

    今日内容: 一.爬虫三部曲: 1.发送请求 2.解析数据 3.保存数据 4.解析详情页,获取视频地址 mport requests import re #正则模块 import uuid #uuid. ...

  8. 20191010-9 alpha week 1/2 Scrum立会报告+燃尽图 07

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/8752 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 韩昊 ...

  9. node mysql+node+express 表查询及接口建立(6)

    一.一张表查询 查询一张表在上一章节说过了,查询全部使用*,具体的就写字段名 'SELECT * FROM company' //查询所有使用* 'SELECT * FROM company WHER ...

  10. python3 之 内置函数enumerate

    python3 内置函数enumerate一.简介: 该函数在字面上是枚举.列举的意思,用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列, 同时列出数据和数据下标,一般用在 for ...