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.
A remaining array is just a subsequence of , because deletions preserve order.
Try thinking about the smallest useful length. A length- array can never work, since sorting it changes nothing.
For length , suppose the subsequence is . Its sorted version is .
That length- subsequence is a derangement exactly when . If , it is already sorted; if , both positions match.
So the whole problem is just: does contain an inversion with ? If yes, output those two values. If no, every subsequence is non-decreasing and therefore cannot work.
We need delete some elements so the remaining array is a derangement under this custom definition: make a sorted copy , and require
for every position .
The key thing: after deletions, the remaining array is a subsequence of the original array. We do not get to reorder anything.
Length 1 is useless
If , then its sorted copy is also . So , meaning it is never a derangement.
So the smallest possible useful answer has length .
Analyze length 2
Take some subsequence .
Its sorted version is:
There are three cases:
So any decreasing pair immediately gives a valid answer of length . That's the whole trick. No need to build some giant fancy subsequence. This is an 800, not a tax form.
When is it impossible?
If there is no pair with , then the array is non-decreasing.
And every subsequence of a non-decreasing array is also non-decreasing. That means every possible remaining array is already sorted, so its sorted copy is identical to itself. Therefore every position matches, and it cannot be a derangement.
So the condition is exactly:
YES, then print the subsequence .NO.Algorithm
For each test case:
YES2a_i a_jNO.The constraints are tiny: , so is completely fine.
Correctness proof
Suppose the algorithm prints YES. It found indices with and outputs . Since , this is a valid subsequence. Its sorted copy is , because . Thus and . Therefore is a derangement.
Suppose the algorithm prints NO. Then there is no pair such that , so is non-decreasing. Every subsequence of a non-decreasing array is also non-decreasing. Therefore any remaining array equals its sorted copy, so at least one position matches; in fact, every position matches. Hence no valid non-empty derangement can be formed.
Therefore the algorithm is correct.
#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;
bool found = false;
for (int i = 0; i < n && !found; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
cout << "YES\n";
cout << 2 << '\n';
cout << a[i] << ' ' << a[j] << '\n';
found = true;
break;
}
}
}
if (!found) {
cout << "NO\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;
bool found = false;
for (int i = 0; i < n && !found; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
cout << "YES\n";
cout << 2 << '\n';
cout << a[i] << ' ' << a[j] << '\n';
found = true;
break;
}
}
}
if (!found) {
cout << "NO\n";
}
}
return 0;
}