当前位置: 首页 > news >正文

Codeforces 1750A. Indirect Sort

A. Indirect Sort

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

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

Choose any three indices i,j,k (1≤i<j<k≤n).
If ai>ak, replace ai with ai+aj. Otherwise, swap aj and ak.
Determine whether you can make the array a sorted in non-descending order.

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

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

The second line contains n integers a1,a2,…,an (1≤ai≤n, ai≠aj if i≠j) — the elements of the array a.

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).

Example
inputCopy
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
outputCopy
Yes
Yes
No
No
No
No
Yes
Note
In the first test case, [1,2,3] is already sorted in non-descending order.

In the second test case, we can choose i=1,j=2,k=3. Since a1≤a3, swap a2 and a3, the array then becomes [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. Since a5≤a7, swap a6 and a7, the array then becomes [1,2,6,7,4,5,3].
Choose i=5,j=6,k=7. Since a5>a7, replace a5 with a5+a6=9, the array then becomes [1,2,6,7,9,5,3].
Choose i=2,j=5,k=7. Since a2≤a7, swap a5 and a7, the array then becomes [1,2,6,7,3,5,9].
Choose i=2,j=4,k=6. Since a2≤a6, swap a4 and a6, the array then becomes [1,2,6,5,3,7,9].
Choose i=1,j=3,k=5. Since a1≤a5, swap a3 and a5, the array then becomes [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.
由题意易知,当数组第一个数为1即可满足,无需理会后续操作

#include<bits/stdc++.h>
using namespace std;
const int N = 12;
void solve() {
	int n;
	cin >> n;
	int a[N];
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	if (a[0]==1) {
		cout << "YES" << endl;
	}
	else {
		cout << "NO" << endl;
	}
}
int main(void) {
	int t; 
	cin >> t;
	while (t--) {
		solve();
	}
}
//code by 01100_10111;
//code by yxisme;

相关文章:

  • 7.10 操作系统的启动
  • 项目计划要趁早
  • 【阅读笔记】理解表驱动设计
  • [C语言、C++]数据结构作业:用递归实现走迷宫(打印正确通路即可)
  • JVM垃圾回收系列之垃圾收集器二
  • 2023年,前端开发未来可期
  • mysql 常用查询优化策略详解
  • 区块链的认识
  • Kubernetes中的yaml文件
  • Python编程挑战赛
  • Windows下安装RabbitMQ的步骤
  • 欢迎女神科学家颜宁回国,并祝她如愿以偿
  • 学习笔记-IPC$(Internet Process Connection)
  • 第三站:函数(第二幕)
  • 信贷审批中拒绝原因码的分布与监控设计
  • Mybatis之foreach
  • 【JavaEE基础与高级 第60章】Java中的反射获取成员方法Method、获取成员变量Field(下篇)
  • 2023秋招感悟
  • Hadoop学习总结
  • 集合(Set)和有序集合(ZSet)的基本使用方法详解【Redis】
  • “五一”假期首日国铁郑州局迎大客流,预计发送旅客逾95万人次
  • 澎湃读报丨解放日报8个版聚焦:牢记嘱托,砥砺奋进
  • 澎湃回声丨23岁小伙“被精神病”8年续:今日将被移出“重精”管理系统
  • 制定出台民营经济促进法有何重大意义?全国人大常委会法工委回应
  • 不准打小孩:童年逆境经历视角下的生育友好社会
  • 看见“看得见的手”,看见住房与土地——读《央地之间》