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.
Do not turn this into LIS. The game is not asking for the maximum sorted subsequence; it asks how small the array can be when the forced stopping rule finally triggers.
If the array is already non-decreasing at the start, the first rule ends the game immediately. You do not get even one deletion.
If the array is not non-decreasing, then by definition there is some adjacent bad pair with .
Try keeping one bad adjacent pair alive while deleting other elements. As long as both elements of that pair remain, the current array is still not non-decreasing.
So an unsorted array can be reduced to two decreasing elements, then one more deletion leaves exactly one element. Final answer: if initially sorted, otherwise .
The trick is the forced stop condition. You may delete only while the current array is not non-decreasing. Once it becomes non-decreasing, the game ends immediately. So the common thought "find the shortest non-decreasing subsequence" is the wrong rabbit hole. Tiny problem, tiny answer: either or .
Let the initial array be .
If is already non-decreasing, then the game checks the first rule before any deletion. The game ends right there, so no element can be removed. The answer is therefore .
Now suppose is not non-decreasing. Then there exists an index such that
Keep these two adjacent elements. Delete every other element first, in any order. During all those deletions, this same bad pair still appears in the remaining array, with before , and since originally they were adjacent, nothing can sneak between them. Therefore the array is still not non-decreasing, so the game does not stop early.
Eventually the remaining array can be just
and this is still decreasing because . Since the game has not ended yet, we are allowed to delete one of these two elements. After that, exactly one element remains.
A one-element array is always non-decreasing, so the game ends with element. We cannot end with elements, because after reaching length , the game stops before another deletion is allowed. That is the whole thing. No DP, no greedy gymnastics, no fake complexity tax.
So:
Edge cases:
Implementation just checks whether every . In C++, is_sorted(a.begin(), a.end()) does exactly that for non-decreasing order.
Complexity per test case is time and memory for the input array. With , this is basically free.
Research checked: the official Codeforces statement, the official round editorial page whose B implementation uses exactly this sorted-check split, the public Codeforces status page showing accepted B submissions, and a short Codeforces explanation of the same -or- observation.
#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;
cout << (is_sorted(a.begin(), a.end()) ? n : 1) << '\n';
}
}#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;
cout << (is_sorted(a.begin(), a.end()) ? n : 1) << '\n';
}
}