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.
For a marble with value , Bob only scores it if . Equal distance is useless for Bob because ties go to Alice.
Think about where Bob places relative to Alice's number . If , Bob can only win marbles on the right side. If , he can only win marbles on the left side.
If Bob chooses , the dividing line is halfway between and . Moving farther right moves this line right too, which can only make Bob lose possible marbles.
So if Bob wants the right side, his best choice is the closest integer to the right: . That wins exactly all marbles with value .
Similarly, wins exactly all marbles with value . Therefore compare the count of marbles less than and greater than , then output or accordingly. Values equal to are dead weight for Bob. Brutal, but true.
We need choose Bob's integer to maximize how many marbles are closer to than to Alice's fixed integer .
The important detail is the tie rule: if a marble is exactly equally close to both numbers, Alice gets it. So Bob needs a strictly smaller distance.
For a marble value , Bob scores it iff
What if Bob chooses ?
Then Alice is on the left, Bob is on the right. The boundary between them is the midpoint:
Bob wins values strictly to the right of that midpoint:
Now here's the whole trick: if Bob moves farther right, then increases, so the midpoint moves right, and Bob can only win fewer marbles. Moving farther away does not magically help. It just drags the boundary away from Alice. Classic overthinking trap.
So if Bob wants to win right-side marbles, the best possible choice is the closest integer to the right:
Then Bob wins all integer marble values satisfying
which is exactly
So choosing gives Bob every marble greater than .
What if Bob chooses ?
Symmetric story. Bob wins values strictly to the left of the midpoint:
To maximize this, Bob should choose the closest integer to the left:
Then Bob wins all integer values satisfying
which is exactly
So choosing gives Bob every marble less than .
Can Bob ever win a marble equal to ?
Nope. If , Alice's distance is . Bob cannot beat distance . If Bob also chooses , it is a tie, and Alice still gets it. Those marbles are completely unwinnable for Bob. Sad little zeros.
So the entire problem reduces to:
Both outputs are always valid: since , we have and .
The array is sorted, so we can use binary search:
lower_bound(v.begin(), v.end(), a) gives the first index with value , so that index is the number of values ;upper_bound(v.begin(), v.end(), a) gives the first index with value , so is the number of values .The complexity is per test case after reading the array, and reading all input already costs total. Plenty fast.
#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;
ll a;
cin >> n >> a;
vector<ll> v(n);
for (ll &x : v) cin >> x;
int less_count = lower_bound(v.begin(), v.end(), a) - v.begin();
int greater_count = v.end() - upper_bound(v.begin(), v.end(), a);
if (less_count > greater_count) cout << a - 1 << '\n';
else cout << a + 1 << '\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;
ll a;
cin >> n >> a;
vector<ll> v(n);
for (ll &x : v) cin >> x;
int less_count = lower_bound(v.begin(), v.end(), a) - v.begin();
int greater_count = v.end() - upper_bound(v.begin(), v.end(), a);
if (less_count > greater_count) cout << a - 1 << '\n';
else cout << a + 1 << '\n';
}
return 0;
}