Educational Codeforces Round 83 (Rated for Div. 2) B. Bogosort

B. Bogosort

题目链接-B. Bogosort
在这里插入图片描述
题目大意
输入一个数组a[],做任意多次交换操作,使得对于任意j < i,恒有j−a[j]≠i−a[i]

解题思路

  • 因为j−a[j]≠i−a[i] ,移项得j - i≠a[j]-a[i],易得递减序列就是满足这个条件的
  • 递减队列对任意j < i,都有j-i<0,a[j]-a[i]≥0,所以满足条件
  • sort+逆序输出即可

附上代码

#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
int a[110];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		for(int i=0;i<n;i++)
			cin>>a[i];
		sort(a,a+n);
		for(int i=n-1;i>=0;i--)
			cout<<a[i]<<" ";
		cout<<endl;
	}
	return 0;
}

发布了123 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/104788316