A. Circle of Students (traversal matching) Codeforces Round #579 (Div. 3)

Link to the original question: https://codeforces.com/contest/1203/problem/A

Insert picture description here
Test sample

input
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
YES
YES
NO
YES
YES

Meaning of the question: Give you a circle position arrangement and judge whether they can start the round dance. (The condition is that clockwise or counterclockwise is 1 11 tonnThe arrangement of n . )

The idea of ​​solving the problem: open a double array and connect the end to end to judge whether there is such an arrangement. Simple traversal.

AC code

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
    
    
		while(t--){
    
    
			cin>>n;
			vector<int> nums(n+1);
			rep(i,1,n){
    
    
				cin>>nums[i];
			}
			rep(i,1,n){
    
    
				nums.pb(nums[i]);
			}
			bool flag=true;
			rep(i,1,2*n){
    
    
				if(i+n-1>2*n){
    
    
					break;
				}
				flag=true;
				rep(j,1,n){
    
    
					if(nums[i+j-1]!=j){
    
    
						flag=false;
						break;
					}
				}
				if(flag)break;
			}
			if(flag){
    
    
				cout<<"YES"<<endl;
				continue;
			}
			rep(i,1,2*n){
    
    
				if(i+n-1>2*n){
    
    
					break;
				}
				flag=true;
				rep(j,1,n){
    
    
					if(nums[i+j-1]!=n-j+1){
    
    
						flag=false;
						break;
					}
				}
				if(flag)break;
			}
			if(flag){
    
    
				cout<<"YES"<<endl;
			}
			else{
    
    
				cout<<"NO"<<endl;
			}
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/hzf0701/article/details/109260853