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. Job Shop

    flow shop: 如果每个作业需要在每个处理机上加工,而且每个作业的工序也相同,即在处理机上加工的顺序相同,则这种多类机的环境称为同顺序作业或流水作业. job shop: 如果每个作业需要在每个 ...

  2. 确认下眼神!有没有遇上对的工资(测试leader)

    屏蔽敏感信息,直接上图: ▼

  3. IdentityServer4.FreeSql 持久化实现

    前言 故事是这样开始的: 然后突然又来了句... 扪心自问自从不知道怎么当了 FreeSql 开发群 () 的管理以来, 几乎没有给 FreeSql 做过什么贡献...惭愧惭愧. 借此机会, 似乎可以 ...

  4. C++走向远洋——52(十三周阅读程序)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  5. file_put_contents生成ansi文件

    $line_body = array('张三','李四','王五'); $line_body = array_map(function ($element){return iconv('UTF-8', ...

  6. 小白自学机器学习----3.令人头秃的pytorch安装 (No module named 'tools.nnwrap' 错误)

    tensorflow 刚刚会写基础的模块了,今天找到研究方向的代码是pytorch实现的 总是看到这句话,人生苦短,我用pytorch 看来pytorch应该比tensorflow好学,但是!! py ...

  7. 分享到微信,QQ等各大网络媒体网站代码

    http://www.jiathis.com/ 打开此网站,如果没有账号,请注册一下,然后登陆账号,进入网页以后直接可以复制代码到页面的标签,进行css样式布局,直接可以在页面测试,如果方便的话直接百 ...

  8. js 打开新窗口方式

    之前的项目,有个功能是下载文件,这里只要在浏览器输入 url 就会下载那个文件了.当时我只是简单得使用 window.open ,但是却会被浏览器进行拦截,要手动开启才行,然后就搜索研究其他方法,就看 ...

  9. 面向web前端及node开发人员的vim配置

    鉴于 window 下基本用不到 vim,所以下面内容不再提及 window,具体可以在相应 github 中查看手册操作基础:已装有上有 nodejs(npm).没装的可以移步官网:https:// ...

  10. 读《Java并发编程的艺术》学习笔记(一)

    接下来一个系列,是关于<Java并发编程的艺术>这本书的读书笔记以及相关知识点,主要是为了方便日后多次复习和防止忘记.废话不多说,直接步入主题: 第1章  并发编程的挑战 并发编程的目的是 ...