CF1670A.Prof. Slim
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
One day Prof. Slim decided to leave the kingdom of the GUC to join the kingdom of the GIU. He was given an easy online assessment to solve before joining the GIU. Citizens of the GUC were happy sad to see the prof leaving, so they decided to hack into the system and change the online assessment into a harder one so that he stays at the GUC. After a long argument, they decided to change it into the following problem.
Given an array of n integers a1,a2,…,an, where ai=0, check if you can make this array sorted by using the following operation any number of times (possibly zero). An array is sorted if its elements are arranged in a non-decreasing order.
- select two indices i and j (1≤i,j≤n) such that ai and aj have different signs. In other words, one must be positive and one must be negative.
- swap the signs of ai and aj. For example if you select ai=3 and aj=−2, then they will change to ai=−3 and aj=2.
Prof. Slim saw that the problem is still too easy and isn't worth his time, so he decided to give it to you to solve.
一天,斯利姆教授决定离开GUC王国,加入GIU王国。在加入GIU之前,他被要求在线完成一道简单的评估题。GUC王国的居民既高兴又悲伤地目送教授离开,于是他们决定黑入系统,将这道在线评估题改成一道更难的题目,以挽留教授留在GUC。经过一番激烈争论,他们最终将其改成了如下问题:
给定一个包含 n 个整数的数组 a1,a2,…,an,其中每个 ai=0,请判断:能否通过任意次数(包括零次)执行以下操作,使得该数组变为非递减有序(即“排序”)?
操作定义如下:
- 选择两个下标 i 和 j(满足 1≤i,j≤n),使得 ai 与 aj 符号相反(即一正一负);
- 交换 ai 与 aj 的符号。例如,若选中 ai=3 与 aj=−2,则它们将分别变为 ai=−3 与 aj=2。
斯利姆教授发现这道题依然过于简单,不值得他亲自花费时间,于是决定将它交由你来解答。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The first line of each test case contains a single integer n (1≤n≤105) — the length of the array a.
The next line contain n integers a1,a2,…,an (−109≤ai≤109, ai=0) separated by spaces describing elements of the array a.
It is guaranteed that the sum of n over all test cases doesn't exceed 105.
第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。接下来是 t 个测试用例。
每个测试用例的第一行包含一个整数 n(1≤n≤105)—— 表示数组 a 的长度。
下一行包含 n 个整数 a1,a2,…,an(−109≤ai≤109,且 ai=0),以空格分隔,用于描述数组 a 的元素。
保证所有测试用例的 n 之和不超过 105。
输出格式
For each test case, print "YES" if the array can be sorted in the non-decreasing order, otherwise print "NO". You can print each letter in any case (upper or lower).
对于每个测试用例,如果数组能够被排序为非递减顺序,则输出 "YES";否则输出 "NO"。你可以以任意大小写(大写或小写)输出每个字母。
输入输出样例
输入#1
4 7 7 3 2 -11 -13 -17 -23 6 4 10 25 47 71 96 6 71 -35 7 -4 -11 -25 6 -45 9 -48 -67 -55 7
输出#1
NO YES YES NO
说明/提示
In the first test case, there is no way to make the array sorted using the operation any number of times.
In the second test case, the array is already sorted.
In the third test case, we can swap the sign of the 1-st element with the sign of the 5-th element, and the sign of the 3-rd element with the sign of the 6-th element, this way the array will be sorted.
In the fourth test case, there is no way to make the array sorted using the operation any number of times.
在第一个测试用例中,无论进行多少次该操作,都无法使数组变为有序。
在第二个测试用例中,数组本身已经有序。
在第三个测试用例中,我们可以将第 1 个元素的符号与第 5 个元素的符号交换,并将第 3 个元素的符号与第 6 个元素的符号交换,这样数组便会变为有序。
在第四个测试用例中,无论进行多少次该操作,都无法使数组变为有序。
输入解题思路,AI测评打分。不知道怎么写?