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<j<k≤n1≤i<j<k≤n).
  • 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 <bits/stdc++.h>
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;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值