The meaning of problems: inputting a set of numbers from 1 to n, it is determined whether it can constitute a valid ring
ps: legal cycloalkyl refers 1,2,3,4 ,,,, n or n, n - 1 ,,,, end to end ring 2 consisting of - 1, n
Ideas: The number of input connected end to end of the original array, violent searches whether a length of n, and continuously rising or continuously falling substring
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 201*5; 4 int a[maxn]; 5 int main() { 6 int q; 7 cin>>q; 8 while(q --){ 9 int n; 10 cin>>n; 11 for(int i = 0; i < n; i ++){ 12 int inp; 13 cin>>inp; 14 a[i] = a[i + n] = a[i + 2*n] = inp; 15 } 16 bool ans = false; 17 for(int i = 0; i < 3*n; i ++){ 18 int cnt = 1; 19 int ptr = i + 1; 20 while(a[ptr] > a[ptr - 1] && ptr < 3*n){ 21 cnt ++; 22 ptr ++; 23 } 24 if(cnt >= n){ 25 ans = true; 26 break; 27 } 28 cnt = 1; 29 ptr = i + 1; 30 while(a[ptr] < a[ptr - 1] && ptr < 3*n){ 31 cnt ++; 32 ptr ++; 33 } 34 if(cnt >= n){ 35 ans = true; 36 break; 37 } 38 } 39 if(ans){ 40 cout<<"YES"<<endl; 41 }else { 42 cout<<"NO"<<endl; 43 } 44 } 45 return 0; 46 }