CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) A. Indirect Sort 解题报告

原题链接:

Problem - A - Codeforces (Unofficial mirror by Menci)

题目描述:

You are given a permutation a1,a2,…,ana1,a2,…,an of size nn, where each integer from 11 to nn appears exactly once.

You can do the following operation any number of times (possibly, zero):

  • Choose any three indices i,j,ki,j,k (1≤i
  • If ai>akai>ak, replace aiai with ai+ajai+aj. Otherwise, swap ajaj and akak.

Determine whether you can make the array aa sorted in non-descending order.

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤50001≤t≤5000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (3≤n≤103≤n≤10) — the length of the array aa.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n, ai≠ajai≠aj if i≠ji≠j) — the elements of the array aa.

Output

For each test case, output "Yes" (without quotes) if the array can be sorted in non-descending order, and "No" (without quotes) otherwise.

You can output "Yes" and "No" in any case (for example, strings "YES", "yEs" and "yes" will be recognized as a positive response).

解题思路:

要使用题目要求的方式进行排序的话,数组第一位必须是1,否则无解。

只要数组第一位是1,总能用这个1把整个数组都排好。

代码(CPP):

#include 
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int INF = 0x3fffffff;

/*
    要使用题目要求的方式进行排序的话,数组第一位必须是1,否则无解。
    只要数组第一位是1,总能用这个1把整个数组都排好。
*/

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int first;
        cin >> first;
        for (int i = 2, x; i <= n; i++)
        {
            cin >> x;
        }
        if(first == 1)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    return 0;
}

你可能感兴趣的:(#,Codeforces,算法)