You are given an array a[0…n−1] of n integers. This array is called a “valley” if there exists exactly one subarray a[l…r] such that:

0≤l≤r≤n−1,
al=al+1=al+2=⋯=ar,
l=0 or al−1>al,
r=n−1 or ar<ar+1.
Here are three examples:

The first image shows the array [3,2,2,1,2,2,3], it is a valley because only subarray with indices l=r=3 satisfies the condition.

The second image shows the array [1,1,1,2,3,3,4,5,6,6,6], it is a valley because only subarray with indices l=0,r=2 satisfies the codition.

The third image shows the array [1,2,3,4,3,2,1], it is not a valley because two subarrays l=r=0 and l=r=6 that satisfy the condition.

You are asked whether the given array is a valley or not.

Note that we consider the array to be indexed from 0.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the array.

The second line of each test case contains n integers ai (1≤ai≤109) — the elements of the array.

It is guaranteed that the sum of n over all test cases is smaller than 2⋅105.

Output
For each test case, output “YES” (without quotes) if the array is a valley, and “NO” (without quotes) otherwise.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

Example
inputCopy
6
7
3 2 2 1 2 2 3
11
1 1 1 2 3 3 4 5 6 6 6
7
1 2 3 4 3 2 1
7
9 7 4 6 9 9 10
1
1000000000
8
9 4 4 5 9 4 9 10
outputCopy
YES
YES
NO
YES
YES
NO
Note
The first three test cases are explained in the statement.

分析

  1. 题意:看看一个序列是否存在,当前某段序列(也可以是一个数),是否小于紧挨着它的左边那个数,是否小于相邻的右边那个数;也就是让这个序列或者这个单独的数,小于两边的数;此题要求一个序列只能存在一种情况,也就是有多个l,r都能满足条件,就不符合题意;
  2. l,r可以指向一段连续相同的数的序列,也可以共同指向一个单独的数,所以我们可以把重复元素去掉,直接比较单个数;一开始想着全部去重,这是不对的,因为同一个数字可能不是连续出现的,全去重了就不对了;然后考虑到局部部分去重,把连续的相同的数去掉,然后存放在一个数组里;
  3. 然后对第一个数、最后一个数特判,其他的数分别和两边的数相比较记录有几种满足的情况即可;
#include<bits/stdc++.h>using namespace std;int t, n, cnt;
int a[200005];
int b[200005];//把连续的数删掉的得到的数组int main() {cin >> t;while (t--) {cin >> n;for (int i = 0; i < n; ++i) {cin >> a[i];}cnt = 0;//当前数组的元素个数b[cnt++] = a[0];//把连续的数去重for (int i = 1; i < n; ++i) {if (a[i] != a[i - 1])b[cnt++] = a[i];}int sum = 0;//满足山谷的情况数if (cnt == 1 || cnt == 2) {cout << "YES" << endl;} else {//特判第一个数if (b[0] < b[1])sum++;//中间的数和前后比for (int i = 1; i < cnt - 1; ++i) {if (b[i] < b[i - 1] && b[i] < b[i + 1])sum++;}//特判最后一个数if (b[cnt - 1] < b[cnt - 2])sum++;if (sum > 1)cout << "NO" << endl;elsecout << "YES" << endl;}}return 0;
}

D. Challenging Valleys相关推荐

  1. Codeforces Round #835 (Div. 4) - D. Challenging Valleys

    D. Challenging Valleys 枚举所有相等的连续段,判断符合条件的是否只有一段即可. #include<bits/stdc++.h> using namespace std ...

  2. 【原创】TLV5618芯片驱动程序

    /** ****************************************************************************** * @file    T_TLV5 ...

  3. Hills And Valleys CodeForces - 1467B

    Hills And Valleys CodeForces - 1467B 题意: 修改数列中的 一个 数字 使得峰(波峰.波谷)的数量最少 题解: 修改一个数,最多只能影响左右两个数,所能减少的峰的数 ...

  4. TLV5618 双路12位DAC 模拟SPI驱动

    一.TLV65618芯片 TLV5618A 带掉电功能 2.7V-5.5V,低功耗双路 12 位数模转换器 特点 ●双路 12 位电压输出 DAC ●可编程调节转换时间 - 快速模式 3μs - 低速 ...

  5. 【POI 2007】Ridges and Valleys山峰和山谷(GRZ)

    http://www.zybbs.org/JudgeOnline/problem.php?id=1102 八中题目描述太DT了--还是看这里: http://main.edu.pl/en/archiv ...

  6. P3456 [POI2007]GRZ-Ridges and Valleys

    题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是相邻的格子.(所以与(i,j)相邻的格子有(i ...

  7. 山峰和山谷 Ridges and Valleys(bfs)

    山峰和山谷 Ridges and Valleys 题意 水平竖直和斜方向是一个点数值的比较对象,一共八个.相同的的高度可以连接在一起,形成山谷.山峰或者啥都不是.如果对于一个连接在一起的一块地方,其任 ...

  8. 【ybt高效进阶1-5-2】【luogu P3456】山峰和山谷 / GRZ-Ridges and Valleys

    山峰和山谷 / GRZ-Ridges and Valleys 题目链接:ybt高效进阶1-5-2 / luogu P3456 题目大意 对于山谷和山峰,我们这样定义: 它们是一个连通块(其中的高度都相 ...

  9. 流利阅读12.20 Angry young women: A new generation of activists is challenging misogyny

    下载pdf资料: GitHub - zhbink/LiuLiYueDu: 流利阅读pdf汇总 流利阅读对每期内容均有很好的文章讲解,向您推荐. 您可以关注微信公众号:流利阅读 了解详情. Angry ...

  10. Day1:Angry young women: A new generation of activists is challenging misogyny

    Day1:Angry young women: A new generation of activists is challenging misogyny 愤怒的年轻女性:新时代的活跃分子正在挑战厌女 ...

最新文章

  1. XML与DataSet的相互转换
  2. Android app内语言环境切换
  3. 数据中心机房的监控系统是否重要?
  4. Python中使用pip install ncmbot时各种错误解决办法
  5. php判断是否大写字母,PHP判断字符串由数字和大小写字母组成、长度至少6位
  6. 【Java代码】反射机制处理传递给mapper文件的非Map类型参数对象(指定属性为空则设置默认值)
  7. 【教女朋友学网络系列5】之VLAN与Trunk、单臂路由
  8. java.lang.OutOfMemoryError: Java heap space 错误及解决办法
  9. broken pipe怎么解决 数据太大_振动筛噪音太大、扬尘问题严重,不够环保怎么办?看看这篇文章,帮您解决困扰...
  10. pointer-events:none解决重叠元素不能感应鼠标事件的问题
  11. Linux 软件包管理
  12. 未找到与约束ContractName,无法打开项目的解决方案
  13. stm32 火灾自动报警及联动控制源码_1个视频了解火灾自动报警系统联动全过程!...
  14. 笔记本电脑总是锁定计算机呢,笔记本电脑键盘锁定了怎么办有什么方法解锁
  15. Android相机预览页面被压缩和拉伸问题
  16. DDOS渗透与攻防(二)之SYN-Flood攻击
  17. UPnP的功能和使用
  18. 【如何面试求职】软件方面毕业生 求职帮助
  19. 编码的奥秘:发报机与断电器
  20. visio图形线性阵列

热门文章

  1. 用尽量简单地话,一次讲明白傅里叶级数(FS)、傅里叶变换(FT)、离散时间傅里叶变换(DTFT)、离散傅里叶级数(DFS)、离散傅里叶变换(DFT)以及它们之间的联系和区别。
  2. 理解Linux的平均负载和性能监控
  3. js截取url所带参数方法与url截取字段中包含中文会乱码的解决方案
  4. VMware安装_CentOS 7.x系统
  5. Python 豆瓣TOP250 电影爬取
  6. Idea字体美化终极解决方案
  7. 考研英语 单词常见熟词生义
  8. uq mysql_MySQL workbench中的PK,NN,UQ,BIN,UN,ZF,AI说明
  9. JAVA毕业设计共享充电宝管理系统演示录像2021计算机源码+lw文档+系统+调试部署+数据库
  10. mmo mysql_一次MMO游戏服务器性能压测记录