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 creates a number smaller than both chosen numbers or larger than both chosen numbers. So think about what bounds can never be escaped.
After any merge, the new number lies between two values that already came from the sequence. That means the overall minimum and maximum of the current sequence can only move inward, never outward.
If is outside the initial range , it is immediately impossible. No amount of legal merging can summon values from the void.
The other direction is the key: if is inside the initial range, find one element and one element . Somehow, when those two values become adjacent, they can merge into exactly .
Because every merge between two numbers on the same side of can preserve that side, you can reduce everything until the final merge has one value and one value . Then merge them into . So the whole answer is just whether .
The operation looks like it might care about adjacency, order, or some galaxy-brain interval DP. It does not. This is an 800, so the trick is noticing the whole game collapses to one range check.
Let
The answer is:
Why outside the range is impossible
When you merge two adjacent values and , the replacement value must satisfy
So this merge cannot create a value smaller than both inputs, and it cannot create a value larger than both inputs.
That means no operation can ever create a number outside the initial global range . The minimum can only increase or stay the same, and the maximum can only decrease or stay the same. So if or , the final value being is impossible. Done.
Why every value inside the range is possible
Now suppose
Then there exists at least one original element and at least one original element . Maybe one element is already exactly , which is even easier.
Here is the useful way to think about merging:
So values have two “sides” relative to : low side and high side. Merging two values on the same side can keep you on that side. Merging across the two sides lets you pick exactly.
Since the sequence is finite, keep merging adjacent elements while preserving the fact that there is still enough information to eventually have one low-side value and one high-side value. At the end, you can reduce the entire sequence to two values: one and one . Those two are adjacent because only two values remain. Then merge them into .
No need to construct the actual operations in code. The existence argument is enough.
Edge case:
If there is only one number, then . The condition becomes , which is exactly correct.
Algorithm
For each test case:
YES if lies between them, otherwise print NO.Complexity
Each test case takes time and extra memory. Basically free, unless your computer is powered by a potato with commitment issues.
#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;
ll mn = LLONG_MAX, mx = LLONG_MIN;
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
mn = min(mn, a);
mx = max(mx, a);
}
ll x;
cin >> x;
cout << (mn <= x && x <= mx ? "YES" : "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;
ll mn = LLONG_MAX, mx = LLONG_MIN;
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
mn = min(mn, a);
mx = max(mx, a);
}
ll x;
cin >> x;
cout << (mn <= x && x <= mx ? "YES" : "NO") << '\n';
}
return 0;
}