A. Circle of Students

题目链接: https://codeforces.com/contest/1203/problem/A

A. 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
题意:给你一个首尾相连的序列,问你是否构成一个连续上升或下降的序列,连续是指他们中间的差值只能为1
思路:Div3的A题,但是当时做的时候卡住了,结果就放弃了那场比赛,我真的太菜了
观察题目后我们发现,只要序列相邻的两个元素差值为1的个数为1,则说明可以构成一个圆环
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,n;
int arr[205];
int main(){
    cin>>t;
    int cnt;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>arr[i];
        cnt=0;
        if (abs(arr[1]-arr[n])>1)cnt++;
        for(int i=1;i<=n-1;i++){
            if(abs(arr[i]-arr[i+1])>1)
                cnt++;
        }
        if(cnt>=2)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
    }
    return 0;
}

两个代码的思路其实是一致的,不过实现方法不同

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#define ll long long int
using namespace std;
int a[1010];
int main(){    
    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        for(int i=0;i<n;i++) cin>>a[i];
        int cnt1 = 0;
        int cnt2 = 0;
        for(int i=0;i<n;i++){
            if(a[i] == a[(i - 1 + n) % n] + 1){
                cnt1++;
            }
            if(a[i] == a[(i + 1 ) % n] + 1){
                cnt2++;
            }
        }
        if(cnt1 == n - 1 || cnt2 == n - 1){
            cout << "YES" << endl;
        }
        else {
            cout << "NO" << endl;
        }
    }
    
    return 0;
}

 

 

转载于:https://www.cnblogs.com/1911087165zzx/p/11350623.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值