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
***/

[模拟]Codeforces Circle of Students的更多相关文章

  1. CF #579 (Div. 3) A.Circle of Students

    A. Circle of Students time limit per test2 seconds memory limit per test256 megabytes inputstandard ...

  2. codeforces 1287A -Angry Students(模拟)

    It's a walking tour day in SIS.Winter, so t groups of students are visiting Torzhok. Streets of Torz ...

  3. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  4. 模拟 Codeforces Round #203 (Div. 2) C. Bombs

    题目地址:http://codeforces.com/problemset/problem/350/C /* 题意:机器人上下左右走路,把其他的机器人都干掉要几步,好吧我其实没读懂题目, 看着样例猜出 ...

  5. 模拟 Codeforces Round #249 (Div. 2) C. Cardiogram

    题目地址:http://codeforces.com/contest/435/problem/C /* 题意:给一组公式,一组数据,计算得到一系列的坐标点,画出折线图:) 模拟题:蛮恶心的,不过也简单 ...

  6. 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

    题目传送门 /* 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 题目倒是很长:) */ #include <cstdio> #include <algorithm> ...

  7. queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

    题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...

  8. 模拟 Codeforces Round #288 (Div. 2) A. Pasha and Pixels

    题目传送门 /* 模拟水题:给定n*m的空白方格,k次涂色,将(x,y)处的涂成黑色,判断第几次能形成2*2的黑色方格,若不能,输出0 很挫的判断四个方向是否OK */ #include <cs ...

  9. 【Codeforces 332C】Students' Revenge

    Codeforces 332 C 我爱对拍,对拍使我快乐... 题意:有\(n\)个议题,学生们会让议会同意\(p\)个,其中主席会执行\(k\)个, 每一个议题执行后主席会掉\(a_i\)的头发,不 ...

随机推荐

  1. AJAX学习小结

    12345678910 $.ajax({ "url":"", //访问路径 "data":"", // 需要传输的数据 ...

  2. Leetcode 142题 环形链表 II(Linked List Cycle II) Java语言求解

    题目描述: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 p ...

  3. Apple App签名机制

    概览 数字签名 签名机制与验证过程 操作流程 数字签名 摘要算法 将任意长度文本通过一个算法得到一个固定长度的文本. 源文本不同,计算结果必然不同 无法从结果反推源 例如,MD5和SHA算法 非对称加 ...

  4. yii2设置默认控制器

    以Yii2高级模板配置为例

  5. OCR场景文本识别:文字检测+文字识别

    一. 应用背景 OCR(Optical Character Recognition)文字识别技术的应用领域主要包括:证件识别.车牌识别.智慧医疗.pdf文档转换为Word.拍照识别.截图识别.网络图片 ...

  6. JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)

    5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...

  7. 最新版jdk 13环境变量配置

    1.配置环境变量 右击“我的电脑”-->"属性"-->"高级系统设置"-->"高级"-->"环境变量&qu ...

  8. SQL之开窗函数详解--可代替聚合函数使用

    在没学习开窗函数之前,我们都知道,用了分组之后,查询字段就只能是分组字段和聚合的字段,这带来了极大的不方便,有时我们查询时需要分组,又需要查询不分组的字段,每次都要又到子查询,这样显得sql语句复杂难 ...

  9. C# .Net Core 3.1 中关于Process.Start 启动Url链接的问题

    WPF 项目迁移到.Net Core中时居然出了一堆问题...(很无语) 今天在使用的时候居然发现Process.Start居然打不开Url链接了? 报 找不到指定文件 的异常?! 一.bug重现 首 ...

  10. GitHub 热点速览 Vol.11:回暖的 GitHub 迎来上千星的图片流项目

    作者:HelloGitHub-小鱼干 摘要:连着两周成绩平平的 GitHub Trending 榜,终于和三月的天气一样进入全面变暖的模式,无论是本周刚开源搭乘 ocr 热点并获得 1,500+ st ...