Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Rewrite the bad condition a little: if for some we have , then what does that say about ?
From , you get But for , the right-hand side is negative. So a bad pair forces .
So ask yourself: what if the array had the opposite property everywhere β namely, for all , we had ?
In a non-increasing array, for we have , hence Both terms are nonnegative, and .
That expression is actually strictly positive: So equality is impossible. Therefore any array sorted in decreasing order is good. Just sort descending and print it. Done. Bogosort my ass.
The trick is to stop staring at the condition like it insulted your mother and just algebra it.
The array is bad if there exists a pair such that
Rearrange it:
Now look at the signs:
That means a bad pair can only happen when a later element is strictly larger than an earlier one.
So what arrangement completely kills that possibility?
A non-increasing arrangement.
Suppose we sort the array so that
Take any . Then , so . Now compute
Here:
Therefore,
So it is never equal to , which means
for every pair . Exactly what we need.
So the whole problem is hilariously simple:
The statement says a solution always exists. Cool. We donβt even need that promise, because descending order is always a solution.
For each test case:
Sorting dominates:
per test case, with extra space apart from the array.
Given , this is absurdly safe.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
sort(a.rbegin(), a.rend());
for (int i = 0; i < n; ++i) {
if (i) cout << ' ';
cout << a[i];
}
cout << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
sort(a.rbegin(), a.rend());
for (int i = 0; i < n; ++i) {
if (i) cout << ' ';
cout << a[i];
}
cout << '\n';
}
return 0;
}