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.
The operation never invents a new value. The inserted value is the median of marked elements, so it was already one of the marked elements.
Do not obsess over making a long final array. If both arrays can ever become the same array, they can then both be reduced further to the same one-element array.
For one array, ask this smaller question: which values can this array be reduced to as a singleton?
In an odd-length sorted array with length greater than , a value that appears only at the very first position or only at the very last position is screwed: it can never be the median of a nontrivial marked set while preserving itself.
The reachable singleton values are exactly the values in the array after deleting the first and last positions, except that a length- array keeps its only value. So the whole problem is just: do these two trimmed sorted ranges intersect?
Research note: I checked the Codeforces problem statement and the Kotlin Heroes 14 editorial. The official tutorial text for 2199D is still listed as Tutorial is loading..., but the published official solution by Roms checks exactly the intersection of the arrays after trimming the first and last positions. The proof below is the actual reason that tiny check works.
First, ignore positions except when comparing final equality. The median depends only on the multiset of chosen values, and for a singleton final array, order is irrelevant anyway.
Two basic facts:
marked_count - 1, which is even, so every array always has odd length.Define as the set of values such that array can be transformed into the one-element array .
The key lemma is:
For a sorted odd-length array ,
Necessity first. Suppose and a value does not appear in the middle positions. Then is either the unique minimum or the unique maximum . Consider the unique minimum case; the maximum case is symmetric.
As long as is present, every other value is greater than . If a real operation marks together with at least two other elements, then is the smallest marked element, so it cannot be the median. If the operation marks only , nothing changes. Therefore any operation that actually reduces the array and includes deletes forever, while operations not including keep it as the unique minimum. The final reducing operation cannot output . So a unique endpoint value is impossible. Brutal, but clean.
Now sufficiency. Take any occurrence of that is not the first or last position. Keep this occurrence alive. It has at least one element on its left and at least one element on its right in sorted order.
While the length is greater than :
Eventually the array has exactly three elements: one , the kept , and one . Mark all three; their median is . Done. This is why the answer is not just the original median. You can burn junk from one side until the target becomes the median. Math, not magic.
Now connect the lemma to the two-array problem.
If the two arrays share some value in their sets, reduce both arrays to and print YES.
Conversely, suppose the arrays can be made equal to some array . If has length , its only value is reachable from both originals. If has length greater than , then by the lemma can be reduced further to some value from its middle positions. Since both originals can reach , both originals can also reach that singleton. So their sets must intersect.
So the entire problem collapses to this:
Use two pointers because the arrays are already sorted.
Edge cases:
YES iff the two values are equal.Complexity is per test case, and memory for storing the arrays. Across all tests this is , easily inside the limits.
#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, m;
cin >> n >> m;
vector<ll> a(n), b(m);
for (ll &x : a) cin >> x;
for (ll &x : b) cin >> x;
int al = (n == 1 ? 0 : 1);
int ar = (n == 1 ? 0 : n - 2);
int bl = (m == 1 ? 0 : 1);
int br = (m == 1 ? 0 : m - 2);
bool ok = false;
int i = al, j = bl;
while (i <= ar && j <= br) {
if (a[i] == b[j]) {
ok = true;
break;
}
if (a[i] < b[j]) ++i;
else ++j;
}
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, m;
cin >> n >> m;
vector<ll> a(n), b(m);
for (ll &x : a) cin >> x;
for (ll &x : b) cin >> x;
int al = (n == 1 ? 0 : 1);
int ar = (n == 1 ? 0 : n - 2);
int bl = (m == 1 ? 0 : 1);
int br = (m == 1 ? 0 : m - 2);
bool ok = false;
int i = al, j = bl;
while (i <= ar && j <= br) {
if (a[i] == b[j]) {
ok = true;
break;
}
if (a[i] < b[j]) ++i;
else ++j;
}
cout << (ok ? "YES" : "NO") << '\n';
}
}