codeforce 579 A,B

There are n students standing in a circle in some order. The index of the i-th student is p_i. It is guaranteed that all indices of students are distinct integers from 1 to n (i.?e. they form a permutation).

Students want to start a round dance. A clockwise round dance can be started if the student 2 comes right after the student 1 in clockwise order (there are no students between them), the student 3 comes right after the student 2 in clockwise order, and so on, and the student n comes right after the student n - 1 in clockwise order. A counterclockwise round dance is almost the same thing ¡ª the only difference is that the student i should be right after the student i - 1 in counterclockwise order (this condition should be met for every i from 2 to n).

For example, if the indices of students listed in clockwise order are [2, 3, 4, 5, 1], then they can start a clockwise round dance. If the students have indices [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 q independent queries.

Input
The first line of the input contains one integer q (1 ≤ q ≤ 200) — the number of queries. Then q queries follow.

The first line of the query contains one integer n (1 ≤ n ≤ 200) — the number of students.

The second line of the query contains a permutation of indices p_1, p_2, …, p_n (1 ≤ p_i ≤ n), where p_i is the index of the i-th student (in clockwise order). It is guaranteed that all p_i are distinct integers from 1 to n (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”.

SampleInput
5
4
1 2 3 4
3
1 3 2
5
1 2 3 5 4
1
1
5
3 2 1 5 4
SampleOutput
YES
YES
NO
YES
YES
Submit
题意:问数组中从一个点开始向左或向右是否满足递增且差一(呈圈型)
思路:模拟, 发现我对模拟有种抗拒 其实是写的出来的 , 感觉写题还是太少

#include <iostream>

using namespace std;

int a[1005];

int main()
{
    int n,t;

    cin >> t;

    while(t--)
    {
        cin >> n;

        for(int i=0; i<n; i++)
            cin >> a[i],a[i+n]=a[i],a[i+n*2]=a[i];


        bool flag=0;

        for(int i=0;i<3*n;i++)
        {
            int cnt=1;
            int pos=1+i;

            while(a[pos]>a[pos-1] && pos < 3*n)
            {
                cnt++;
                pos++;
            }

            if(cnt>=n)
            {
                flag=true;
                break;
            }

            cnt=1;
            pos=i+1;

            while(a[pos]<a[pos-1] && pos < 3*n)
            {
                cnt++;
                pos++;
            }

            if(cnt>=n)
            {
                flag=true;
                break;
            }
        }

        if(flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

You are given 4n sticks, the length of the i-th stick is a_i.

You have to create n rectangles, each rectangle will consist of exactly 4 sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.

You want to all rectangles to have equal area. The area of the rectangle with sides a and b is a*b.

Your task is to say if it is possible to create exactly n rectangles of equal area or not.

You have to answer q independent queries.

Input
The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. Then q queries follow.

The first line of the query contains one integer n (1 ≤ n ≤ 100) — the number of rectangles.

The second line of the query contains 4n integers a_1, a_2, …, a_{4n} (1 ≤ a_i ≤ 10^4), where a_i is the length of the i-th stick.

Output
For each query print the answer to it. If it is impossible to create exactly n rectangles of equal area using given sticks, print “NO”. Otherwise print “YES”.

SampleInput
5
1
1 1 10 10
2
10 5 2 10 1 1 2 5
2
10 5 1 10 5 1 1 1
2
1 1 1 1 1 1 1 1
1
10000 10000 10000 10000
SampleOutput
YES
YES
NO
YES
YES

题意:给你4n条边 问其是否能组成n个面积相同的矩形
思路:模拟 提醒一下自己 当数据范围不大是 可以用STL来简化模拟过程

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int q;

    cin >> q;

    while(q--)
    {
        int n;

        cin >> n;

        map<int,int>vis;
        vector<int>res;

        for(int i=0;i<4*n;i++)
        {
            int x;
            cin >> x;

            vis[x]++;///标记

            if(vis[x]%2==0)//如果边出现两次
                res.push_back(x);
        }

        if(res.size()!=2*n)///当边不足2n条时不能构成矩形
        {
            cout << "NO" << endl;
            continue;
        }

        sort(res.begin(),res.end());

        int m=res.size();

        int t=res[0]*res[m-1];

        int i=1,j=m-2;

        while(i<j&&res[i]*res[j]==t)///贪心  取最小和最大的边
            i++,j--;

        if(i>j)
            cout << "YES"  << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}

发布了54 篇原创文章 · 获赞 0 · 访问量 1222

猜你喜欢

转载自blog.csdn.net/weixin_44144278/article/details/99667136