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

本文介绍了一种算法,用于判断一组学生是否能按照顺时针或逆时针方向形成一个从最小到最大或从最大到最小的连续数字序列,以此决定他们是否可以开始跳圆圈舞。算法通过构建一个双倍长度的序列并检查其是否满足特定条件来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题目:

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值