Circle of Students
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn students standing in a circle in some order. The index of the ii-th student is pipi. It is guaranteed that all indices of students are distinct integers from 11 to nn (i. e. they form a permutation).

Students want to start a round dance. A clockwise round dance can be started if the student 22 comes right after the student 11 in clockwise order (there are no students between them), the student 33 comes right after the student 22 in clockwise order, and so on, and the student nncomes right after the student n−1n−1 in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student ii should be right after the student i−1i−1 in counterclockwise order (this condition should be met for every ii from 22 to nn).

For example, if the indices of students listed in clockwise order are [2,3,4,5,1][2,3,4,5,1], then they can start a clockwise round dance. If the students have indices [3,2,1,4][3,2,1,4] in clockwise order, then they can start a counterclockwise round dance.

Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤2001≤q≤200) — the number of queries. Then qq queries follow.

The first line of the query contains one integer nn (1≤n≤2001≤n≤200) — the number of students.

The second line of the query contains a permutation of indices p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n), where pipi is the index of the ii-th student (in clockwise order). It is guaranteed that all pipi are distinct integers from 11 to nn (i. e. they form a permutation).

Output

For each query, print the answer on it. If a round dance can be started with the given order of students, print "YES". Otherwise print "NO".

Example
input

Copy
5
4
1 2 3 4
3
1 3 2
5
1 2 3 5 4
1
1
5
3 2 1 5 4
output

Copy
YES
YES
NO
YES
YES

模拟题,长度只有200,看能不能完整绕一圈就好了,中间要两两之差绝对值为1且保持单调性,可以允许一次单调性的改变

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e3+;
int a[amn];
int main(){
int q,n;
cin>>q;
while(q--){
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
int valid=;
if(n>){
int ed=n,f=a[]-a[],fr=;
for(int i=;i!=ed;){
int nex=i+<=n?i+:;
if(abs(a[i]-a[nex])!=){
if(fr){ed=i,fr=;f=(i==)?-f:f;i=(i+<=n)?i+:;continue;}
else {valid=;break;}
}
if(i==ed)break;
if(f>){
if(abs(a[i]-a[nex])!=||a[nex]-a[i]<){valid=;break;}
}
else if(f<){
if(abs(a[i]-a[nex])!=||a[nex]-a[i]>){valid=;break;}
}
else {valid=;break;}
i=(i+<=n)?i+:;
}
}
if(valid)cout<<"YES\n";
else cout<<"NO\n";
}
}
/***
模拟题,长度只有200,看能不能完整绕一圈就好了,中间要两两之差绝对值为1且保持单调性,可以允许一次单调性的改变
51234
15432
***/

最新文章

  1. logback
  2. 分享google的技能的11个级别,大家看看自己到哪个级别了?
  3. java net编程
  4. hdu 1039 (string process, fgets, scanf, neat utilization of switch clause) 分类: hdoj 2015-06-16 22:15 38人阅读 评论(0) 收藏
  5. 读取 RSSI
  6. dedecms代码研究三
  7. Run python as a daemon process
  8. 高质量JavaScript代码书写基本要点
  9. ubuntu ipv6网络电视(avplay)
  10. MongoDB的SSL实现分析
  11. freemarker对数字的处理
  12. Redis 安装教程 (Windows 2.6.13 稳定版)
  13. Oracle基本代码学习
  14. 分析器错误(在浏览器中查看.aspx)
  15. 教你成为全栈工程师(Full Stack Developer) 〇-什么是全栈工程师
  16. object model 概述
  17. ReactiveCocoa源码解读(二)
  18. 解析 .Net Core 注入 (3) 创建对象
  19. ng-if ng-show ng-hide区别(面试题)
  20. Postgre: How to import UUID function into Postgre 9.3

热门文章

  1. 从VR泛滥到倒闭看热门投机的山寨创业心态
  2. Python计算给定日期的周内的某一天
  3. MVC07
  4. Kafka体系架构详细分解
  5. Typora[MarkDown编辑器]+(PicGo+Github+JsDelivr)[个人图床] ,开启你的高效创作
  6. vue_相同组件,不同url跳转不重新渲染的解决方法
  7. Oracle批量插入有日期类型数据
  8. Gnome 究极无死角美化!!!不要再说gnome丑啦!!!
  9. Prometheus 监控平台的搭建
  10. 利用Python爬取OPGG上英雄联盟英雄胜率及选取率信息