A. Cubes Sorting
Wheatley decided to try to make a test chamber. He made a nice test chamber, but there was only one detail absent — cubes.
For completing the chamber Wheatley needs n cubes. i-th cube has a volume ai.
Wheatley has to place cubes in such a way that they would be sorted in a non-decreasing order by their volume. Formally, for each i>1, ai−1≤ai must hold.
To achieve his goal, Wheatley can exchange two neighbouring cubes. It means that for any i>1 you can exchange cubes on positions i−1 and i.
But there is a problem: Wheatley is very impatient. If Wheatley needs more than n ⋅ ( n − 1 ) 2 − 1 \frac{n⋅(n−1)}{2}−1 2n⋅(n−1)−1 exchange operations, he won’t do this boring work.
Wheatly wants to know: can cubes be sorted under this conditions?
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1≤t≤1000), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer n ( 2 ≤ n ≤ 5 ⋅ 1 0 4 2≤n≤5⋅10^4 2≤n≤5⋅104) — number of cubes.
The second line contains n positive integers ai ( 1 ≤ a i ≤ 1 0 9 1≤ai≤10^9 1≤ai≤109) — volumes of cubes.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case, print a word in a single line: “YES” (without quotation marks) if the cubes can be sorted and “NO” (without quotation marks) otherwise.
Example
input
3
5
5 3 2 1 4
6
2 2 2 2 2 2
2
2 1
output
YES
YES
NO
Note
In the first test case it is possible to sort all the cubes in 7 exchanges.
In the second test case the cubes are already sorted.
In the third test case we can make 0 exchanges, but the cubes are not sorted yet, so the answer is “NO”.
题意: 输入t组测试数据,每组测试数据分为2行,第一行代表的是这一组测试数据有n个数,第二行输入这n个数。问这n个数经过少于 n ⋅ ( n − 1 ) 2 − 1 \frac{n⋅(n−1)}{2}−1 2n⋅(n−1)−1 次相邻的数的交换之后能否变成严格单调递增的。如果能,则输入“YES",若不能,则输出”NO”。
题解: 只要给的数不是严格单调递减的,都能在 n ⋅ ( n − 1 ) 2 − 1 \frac{n⋅(n−1)}{2}−1 2n⋅(n−1)−1 次内变成严格单调递增的。
c++ AC 代码
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn = 5e4 + 10;
ll a[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
bool flag = 0;
int n;
scanf("%d",&n);
scanf("%d",&a[0]);
for(int i=1;i<n;i++)
{
scanf("%d",a+i);
if(a[i] >= a[i-1])
flag = 1;
}
if(flag) puts("YES");
else puts("NO");
}
// system("pause");
return 0;
}