Codeforces 1312B Bogosort (逆序证明)

Example

input

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

output

7
1 5 1 3
2 4 6 1 3 5

看题的时候发现和sort有关,但一定要逆序排序

证明:

ai>=aj   逆序
i<j
 
-i>-j
上式两边同时加上ai,ai-i>ai-j   (1)
 
ai>=aj
上式两边同时减去j,ai-j>=aj-j   (2)
 
综合(1),(2)
ai-i>ai-j>=aj-j
即ai-i>aj-j
 
即ai-i!=aj-j
也即j-aj!=i-ai
 
逆序正确,得证。

AC代码:

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a, int b) { return a > b; }
int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int _; cin >> _; while (_--) {
		int n; cin >> n; int a[n + 1];
		for (int i = 1; i <= n; ++i)cin >> a[i];
		sort(a + 1, a + 1 + n,cmp);
		for (int i = 1; i <= n; ++i)cout << a[i] << " ";
		cout << endl;
	}
}

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/13708022.html