Codeforce 1312B—— Bogosort 思维

Codeforce 1312B Bogosort

题目

You are given an array a1,a2,…,ana1,a2,…,an. Array is good if for each pair of indexes i<j the condition j−aj≠i−ai holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).

For example, if a=[1,1,3,5], then shuffled arrays [1,3,5,1], [3,5,1,1] and [5,3,1,1] are good, but shuffled arrays [3,1,5,1], [1,1,3,5] and [1,1,5,3] aren’t.

It’s guaranteed that it’s always possible to shuffle an array to meet this condition.


Input

The first line contains one integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains one integer n (1≤n≤100) — the length of array aa.

The second line of each test case contains n integers a1,a2,…,an(1≤ai≤100).


Output

For each test case print the shuffled version of the array aa which is good.


Sample Input

3
1
7
4
1 1 3 5
6
3 2 1 5 6 4


Sample Output

7
1 5 1 3
2 4 6 1 3 5

题目大意:
大致就是 给一组数,它的索引(下标)减去他这个值不等于另一个的下标减去其对应的值,

题目分析:
我们想,对于一组数,如果我们从下到大,那么前面小的下标减去较小的数,于较大的下标减去较大的数总有可能相等,如果一个一个排的话也不好实现。所以我们把他们从大到小排一下,这样较小的下标,减去一个较大的数,和较大的下标减去较小的数总是不会相等的,所以我们只用把他们逆序输出一下

AC代码

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int a[105];
int cmp(int a,int b){
	return a>b;
}
int main(){
	int t,n,i;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		sort(a+1,a+1+n,cmp);//逆序
		for(i=1;i<n;i++)printf("%d ",a[i]);
		printf("%d\n",a[n]);
	}
	return 0;
}
发布了50 篇原创文章 · 获赞 50 · 访问量 2734

猜你喜欢

转载自blog.csdn.net/weixin_45691686/article/details/104784913