原题目:
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 nn comes 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".
样例:
5
4
1 2 3 4
3
1 3 2
5
1 2 3 5 4
1
1
5
3 2 1 5 4
YES
YES
NO
YES
YES
中文概要:
顺时针或者逆时针都能从小到大或者从大到小
思路:构成一个环来判断
刚开始没往环哪想,想着分成四种情况分类与讨论,不过想想分类讨论应该没有啥错的,就是代码量有点大
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
int T,n;
int a[1000];
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
a[i+n]=a[i];
}
if(n==1){
printf("YES\n");
continue;
}
int j,flag=0;
for(int i=1;i<=n;i++){
for(j=i+1;j<=i+n-1;j++){
if(a[i+1]-a[i]>0){
if(a[j]-a[j-1]<0){
break;
}
}
else if(a[i+1]-a[i]<0){
if(a[j]-a[j-1]>0){
break;
}
}
}
if(j==i+n){
flag=1;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
下面贴榜首的代码,真的想不粗来
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<class T>void chkmin(T &x,const T &y){
if(y<x){
x=y;
}
}
template<class T>void chkmax(T &x,const T &y){
if(y>x){
x=y;
}
}
template<class T>void sort(vector<T> &a){
sort(a.begin(),a.end());
}
template<class T>void reverse(vector<T> &a){
reverse(a.begin(),a.end());
}
void reverse(string &a){
reverse(a.begin(),a.end());
}
template<class T,class Cmp>void sort(vector<T> &a,Cmp cmp){
sort(a.begin(),a.end(),cmp);
}
template<class T>void unique(vector<T> &a){
a.resize(unique(a.begin(),a.end())-a.begin());
}
int readInt(){
int x=0;
bool sign=false;
char c=getchar();
while(!isdigit(c)){
sign=c=='-';
c=getchar();
}
while(isdigit(c)){
x=x*10+c-'0';
c=getchar();
}
return sign?-x:x;
}
ll readLong(){
ll x=0;
bool sign=false;
char c=getchar();
while(!isdigit(c)){
sign=c=='-';
c=getchar();
}
while(isdigit(c)){
x=x*10+c-'0';
c=getchar();
}
return sign?-x:x;
}
string readString(){
string s;
char c=getchar();
while(isspace(c)){
c=getchar();
}
while(!isspace(c)){
s+=c;
c=getchar();
}
return s;
}
void solve_single_test(int test_id=1){
int n=readInt();
vector<int> a(n);
for(int i=0;i<n;++i){
a[i]=readInt()-1;
}
bool ok1=true,ok2=true;
for(int i=0;i+1<n;++i){
ok1&=((a[i]+1)%n==a[i+1]);
ok2&=((a[i+1]+1)%n==a[i]);
}
if(ok1||ok2){
puts("YES");
}else{
puts("NO");
}
}
void solve_multiple_tests(){
int n_tests=readInt();
for(int i=1;i<=n_tests;++i){
solve_single_test(i);
}
}
int main(){
solve_multiple_tests();
return 0;
}