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.
Think in terms of what elements are forced to stay. Deleting everything is not always possible: when the current array has fewer than elements, operations stop immediately.
Let be the -th smallest value in the original array. Elements smaller than are scary: there are fewer than elements , so none of them can ever be the -th smallest of any valid segment. They are undeletable.
Elements larger than are harmless. The real fight is among values : smaller-than- values are mandatory, while copies of are optional-ish.
Since all values are stuck forever, operations must stop with at least elements remaining that are . So among the 's, at least must survive.
Build the subsequence of all elements with value . Values must be kept, values may be kept/deleted, but at least copies of must remain. Use two pointers: when ends mismatch, the only legal skip is an . Count skipped 's.
Let be the -th smallest value of the original array.
This value is the entire problem. Everything else is noise wearing a fake mustache.
What can never be deleted?
Any value smaller than is impossible to delete.
Why? Suppose . In the whole array, there are fewer than elements with value at most , because is the -th smallest. Any subarray has no more such elements than the whole array. So inside any valid chosen segment, cannot be the -th smallest. It is too small. It is stuck forever.
So every element must appear in the final palindrome, in the same relative order.
What about values bigger than ?
They do not need to survive. In the intended final array, we can delete all of them. They are not part of the hard core.
The only delicate value is exactly . Some copies of can be deleted, but not all of them.
Let
Since is the -th smallest, . Once the current array has fewer than elements, no operation is possible. Also, the undeletable elements always remain. Therefore the final array must contain at least elements whose values are at most ; otherwise we would have deleted too many copies of .
So the number of copies of that must remain is
This is always nonnegative.
Now ignore every value . Those can be deleted. Consider the filtered sequence
In this sequence:
So the problem becomes:
Can we delete at most
copies of from so that becomes a palindrome?
That is now just a two-pointer problem.
Put pointers and at the ends of .
So:
Count how many 's you skip. If the number skipped is at most the allowed amount, answer YES.
There is one tiny center case: when , one element remains in the middle, which is always fine for palindrome purposes. If that middle element is , we keep it, not skip it. No drama.
Why this is sufficient
The two-pointer procedure constructs a palindromic subsequence of containing all mandatory values and enough copies of . All values are discarded, and the skipped copies of are also discarded.
Those deletions are achievable because we never try to delete a value smaller than , and we leave at least elements with value at most alive. Values greater than and the extra copies of can be removed in some order while the required small core remains. The operation’s annoying wording basically enforces exactly this threshold behavior.
So the whole solution is:
Sorting costs per test case, and the two-pointer scan is linear. With total , this is comfortably AC.
#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, k;
cin >> n >> k;
vector<int> a(n), s;
for (int &x : a) cin >> x;
s = a;
sort(s.begin(), s.end());
int pivot = s[k - 1];
int smaller = 0, equal = 0;
vector<int> b;
for (int x : a) {
if (x < pivot) smaller++;
if (x == pivot) equal++;
if (x <= pivot) b.push_back(x);
}
int mustKeepPivot = k - 1 - smaller;
int canSkipPivot = equal - mustKeepPivot;
int skipped = 0;
int l = 0, r = (int)b.size() - 1;
bool ok = true;
while (l < r) {
if (b[l] == b[r]) {
l++;
r--;
} else if (b[l] == pivot) {
skipped++;
l++;
} else if (b[r] == pivot) {
skipped++;
r--;
} else {
ok = false;
break;
}
}
if (skipped > canSkipPivot) ok = false;
cout << (ok ? "YES" : "NO") << '\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, k;
cin >> n >> k;
vector<int> a(n), s;
for (int &x : a) cin >> x;
s = a;
sort(s.begin(), s.end());
int pivot = s[k - 1];
int smaller = 0, equal = 0;
vector<int> b;
for (int x : a) {
if (x < pivot) smaller++;
if (x == pivot) equal++;
if (x <= pivot) b.push_back(x);
}
int mustKeepPivot = k - 1 - smaller;
int canSkipPivot = equal - mustKeepPivot;
int skipped = 0;
int l = 0, r = (int)b.size() - 1;
bool ok = true;
while (l < r) {
if (b[l] == b[r]) {
l++;
r--;
} else if (b[l] == pivot) {
skipped++;
l++;
} else if (b[r] == pivot) {
skipped++;
r--;
} else {
ok = false;
break;
}
}
if (skipped > canSkipPivot) ok = false;
cout << (ok ? "YES" : "NO") << '\n';
}
}