[模拟]Codeforces Circle of Students

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 n1n−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 i1i−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 (1q2001≤q≤200) — the number of queries. Then qq queries follow.

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

The second line of the query contains a permutation of indices p1,p2,,pnp1,p2,…,pn (1pin1≤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且保持单调性,可以允许一次单调性的改变

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=1e3+5;
 4 int a[amn];
 5 int main(){
 6     int q,n;
 7     cin>>q;
 8     while(q--){
 9         cin>>n;
10         for(int i=1;i<=n;i++)cin>>a[i];
11         int valid=1;
12         if(n>1){
13             int ed=n,f=a[2]-a[1],fr=1;
14             for(int i=1;i!=ed;){
15                 int nex=i+1<=n?i+1:1;
16                 if(abs(a[i]-a[nex])!=1){
17                     if(fr){ed=i,fr=0;f=(i==1)?-f:f;i=(i+1<=n)?i+1:1;continue;}
18                     else {valid=0;break;}
19                 }
20                 if(i==ed)break;
21                 if(f>0){
22                     if(abs(a[i]-a[nex])!=1||a[nex]-a[i]<0){valid=0;break;}
23                 }
24                 else if(f<0){
25                     if(abs(a[i]-a[nex])!=1||a[nex]-a[i]>0){valid=0;break;}
26                 }
27                 else {valid=0;break;}
28                 i=(i+1<=n)?i+1:1;
29             }
30         }
31         if(valid)cout<<"YES\n";
32         else cout<<"NO\n";
33     }
34 }
35 /***
36 模拟题,长度只有200,看能不能完整绕一圈就好了,中间要两两之差绝对值为1且保持单调性,可以允许一次单调性的改变
37 51234
38 15432
39 ***/

 

posted @ 2019-08-14 09:23  Railgun000  阅读(317)  评论(0编辑  收藏  举报