• 洛谷——CF1750A Indirect Sort



    Indirect Sort

    题目描述

    You are given a permutation a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an of size n n n , where each integer from 1 1 1 to n n n appears exactly once.

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

    • Choose any three indices i , j , k i, j, k i,j,k ( 1 ≤ i < j < k ≤ n 1 \le i < j < k \le n 1i<j<kn ).
    • If a i > a k a_i > a_k ai>ak , replace a i a_i ai with a i + a j a_i + a_j ai+aj . Otherwise, swap a j a_j aj and a k a_k ak .

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

    输入格式

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

    The first line of each test case contains a single integer n n n ( 3 ≤ n ≤ 10 3 \le n \le 10 3n10 ) — the length of the array a a a .

    The second line contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\dots,a_n a1,a2,,an( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain , a i ≠ a j a_i \neq a_j ai=aj if i ≠ j i \neq j i=j ) — the elements of the array a a a .

    输出格式

    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

    7
    3
    1 2 3
    3
    1 3 2
    7
    5 3 4 7 6 2 1
    7
    7 6 5 4 3 2 1
    5
    2 1 4 5 3
    5
    2 1 3 4 5
    7
    1 2 6 7 4 3 5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    样例输出 #1

    Yes
    Yes
    No
    No
    No
    No
    Yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    提示

    In the first test case, [ 1 , 2 , 3 ] [1,2,3] [1,2,3] is already sorted in non-descending order.

    In the second test case, we can choose i = 1 , j = 2 , k = 3 i = 1,j = 2,k = 3 i=1,j=2,k=3 . Since a 1 ≤ a 3 a_1 \le a_3 a1a3 , swap a 2 a_2 a2 and a 3 a_3 a3 , the array then becomes [ 1 , 2 , 3 ] [1,2,3] [1,2,3] , which is sorted in non-descending order.

    In the seventh test case, we can do the following operations successively:

    • Choose i = 5 , j = 6 , k = 7 i = 5,j = 6,k = 7 i=5,j=6,k=7 . Since a 5 ≤ a 7 a_5 \le a_7 a5a7 , swap a 6 a_6 a6 and a 7 a_7 a7 , the array then becomes [ 1 , 2 , 6 , 7 , 4 , 5 , 3 ] [1,2,6,7,4,5,3] [1,2,6,7,4,5,3] .
    • Choose i = 5 , j = 6 , k = 7 i = 5,j = 6,k =7 i=5,j=6,k=7 . Since a 5 > a 7 a_5 > a_7 a5>a7 , replace a 5 a_5 a5 with a 5 + a 6 = 9 a_5+a_6=9 a5+a6=9 , the array then becomes [ 1 , 2 , 6 , 7 , 9 , 5 , 3 ] [1,2,6,7,9,5,3] [1,2,6,7,9,5,3] .
    • Choose i = 2 , j = 5 , k = 7 i = 2,j = 5,k = 7 i=2,j=5,k=7 . Since a 2 ≤ a 7 a_2 \le a_7 a2a7 , swap a 5 a_5 a5 and a 7 a_7 a7 , the array then becomes [ 1 , 2 , 6 , 7 , 3 , 5 , 9 ] [1,2,6,7,3,5,9] [1,2,6,7,3,5,9] .
    • Choose i = 2 , j = 4 , k = 6 i = 2,j = 4,k = 6 i=2,j=4,k=6 . Since $ a_2 \le a_6 $ , swap a 4 a_4 a4 and a 6 a_6 a6 , the array then becomes [ 1 , 2 , 6 , 5 , 3 , 7 , 9 ] [1,2,6,5,3,7,9] [1,2,6,5,3,7,9] .
    • Choose i = 1 , j = 3 , k = 5 i = 1,j = 3,k = 5 i=1,j=3,k=5 . Since a 1 ≤ a 5 a_1 \le a_5 a1a5 , swap a 3 a_3 a3 and a 5 a_5 a5 , the array then becomes [ 1 , 2 , 3 , 5 , 6 , 7 , 9 ] [1,2,3,5,6,7,9] [1,2,3,5,6,7,9] , which is sorted in non-descending order.

    In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order.

    代码

    #include
    using namespace std;
    int main() {
        int t,n,a,b,i;
        cin>>t;
        while(t--) {
            cin>>n>>b;
            for(i=2;i<=n;i++)
            cin>>a;
            if(b==1) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    使用 MoveIt 控制自己的真实机械臂【3】——封装 ROS 驱动
    k8spod使用gpu
    Java基础知识全览
    使用python量化交易接口有哪些分析指标和策略?
    【解决】自定义conda环境安装位置,三种解决方法
    企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图
    Docker Compose使用教程
    【Node.js】深度解析node的包和强大的包管理工具
    算法通过村第十四关-堆|青铜笔记|堆结构
    C++ Reference: Standard C++ Library reference: C Library: ctime: time
  • 原文地址:https://blog.csdn.net/zjx120307/article/details/127835335