【CodeForces】 Codeforces Round # 672 (Div. 2) A Cubes Sorting 【思维题】

A. Cubes Sorting

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

For god’s sake, you’re boxes with legs! It is literally your only purpose! Walking onto buttons! How can you not do the one thing you were designed for?
Oh, that’s funny, is it? Oh it’s funny? Because we’ve been at this for twelve hours and you haven’t solved it either, so I don’t know why you’re laughing. You’ve got one hour! Solve it!

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 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⋅104) — number of cubes.

The second line contains n positive integers ai (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”.


题意:

类似于冒泡排序的过程,与相邻数交换,成为不下降序列时的交换次数最大次数比较,如果大于最大次数,输出NO,否则,输出YES


思路:

一开始想用冒泡排序试试能过吗,结果很显然TLE了

在这里插入图片描述
发现这个最多的次数就是n*(n-1)/2,也就是

(n-1)+(n-2)+ … + 1 + 0

只有当这个序列处于降序时 (此时每个数需要交换n-1,n-2,n-3…1,0,与所给最大次数刚好符合),才会达到最多次数,此时输出NO,否则输出YES

而题目需要的是不下降序列,因此只需要判断所有相邻两个数,只要有一组相邻是不需要交换的,那么就输出YES,否则输出NO


代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e4+50;
int t,n,x;
int main()
{
    
    	   
	scanf("%d",&t);
	while(t--)
	{
    
    
		int a[maxn]={
    
    0};
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		
		int flag=0;
		for(int i=1;i<n;i++)
		{
    
    
			if(a[i-1]<=a[i]) //不降序,前一个 小于等于 后一个
			{
    
    
				flag=1; //有一个满足条件,不需要交换,直接break,输出YES
				break;
			}
		}
		
		if(!flag) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45260385/article/details/110733388
今日推荐