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 single accepted offer, the payoff is secretly linear. Claiming gives for every , and claiming gives for every . The truth-condition wording is basically a costume.
After all choices, your total payoff is one line: , where is #(p >= a_i) minus #(p <= a_i). The minimum over is therefore at if , and at if .
So split the problem into two versions. For guaranteeing at , you need at least as many $p\ge a_i$ choices as $p\le a_i$ choices. Each offer becomes choosing , choosing , or ignoring it.
In that transformed version with values , choosing increases the “balance” and choosing spends one balance. If , taking is always good. If , taking is free balance. Positive values are the only interesting part.
Sort the positive . Use existing balance on the largest positives for free profit. Then pair the smallest remaining positive as the forced negative choice with the largest remaining positive as the positive choice, gaining large - small while it is positive. Do this once for and once for , then take the max.
The bookmaker offers look like inequalities, but the payoff is much simpler than it first seems.
If you accept offer by claiming :
So this action always contributes
Similarly, claiming always contributes
Ignoring contributes . The truth of the claim does not create cases in the final formula. Nice little scam by the statement.
The total payoff is a line
Suppose we choose some offers as type upper, meaning claim , and some as type lower, meaning claim .
The total payoff is
This is a linear function of :
where
Since can be any value in , the worst case is:
So every strategy belongs to one of two worlds:
We solve both and take the maximum.
Solving the left-end version
Assume the worst point is , so .
At :
upper gives ;lower gives .Let
Now each offer lets us choose one of:
upper choice;lower choice;Constraint:
Think of every lower choice as giving one unit of balance, and every upper choice as spending one unit of balance.
Now classify values .
If , then choosing gives positive profit and also increases balance. Always take it.
If , choosing gives no profit but increases balance for free. Always take it too.
If , choosing is profitable but spends balance. Choosing loses money but creates balance. So positives are where all the actual decision-making lives.
Let balance be the number of already chosen non-positive values. Let ans contain the profit from negative values.
Sort all positive increasingly.
First, while we have balance, spend it on the largest positive values. This is pure profit, so obviously we want the biggest ones.
After balance is gone, the only way to take another positive as is to also take some positive as to pay for the count constraint. A pair gives
To maximize this, use the smallest remaining positive as the forced negative choice and the largest remaining positive as the positive choice. If the difference is not positive, there is no point continuing.
That gives the best possible score whose worst case is .
Solving the right-end version
Do the exact same thing from the right side.
For worst point , the condition is .
At :
lower gives ;upper gives .So define
and run the same helper. Again, choosing is the side that creates the required balance, and choosing spends it.
Finally:
Why the greedy is correct
All non-positive should be taken as :
For positive values, every chosen either uses existing balance or must be matched with some chosen .
Existing balance should go to the largest positives, because each such choice has no downside.
For matched positive pairs, a pair contributes , where is chosen as positive and is chosen as negative. If two positive values are paired, the larger one should always be the positive one. Across many pairs, the best pairing is therefore smallest negatives against largest positives. Any other pairing can be swapped to not decrease the sum.
So the two-pointer greedy is optimal.
Complexity
Each test case sorts values twice, so the complexity is
per test case, with total . Totally fine.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll calc(const vector<ll>& b) {
ll ans = 0;
int balance = 0;
vector<ll> pos;
for (ll x : b) {
if (x < 0) {
ans += -x;
balance++;
} else if (x == 0) {
balance++;
} else {
pos.push_back(x);
}
}
sort(pos.begin(), pos.end());
int left = 0, right = (int)pos.size() - 1;
while (balance > 0 && left <= right) {
ans += pos[right];
right--;
balance--;
}
while (left < right) {
if (pos[right] <= pos[left]) break;
ans += pos[right] - pos[left];
left++;
right--;
}
return ans;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
ll l, r;
cin >> n >> l >> r;
vector<ll> leftVals(n), rightVals(n);
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
leftVals[i] = a - l;
rightVals[i] = r - a;
}
cout << max(calc(leftVals), calc(rightVals)) << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll calc(const vector<ll>& b) {
ll ans = 0;
int balance = 0;
vector<ll> pos;
for (ll x : b) {
if (x < 0) {
ans += -x;
balance++;
} else if (x == 0) {
balance++;
} else {
pos.push_back(x);
}
}
sort(pos.begin(), pos.end());
int left = 0, right = (int)pos.size() - 1;
while (balance > 0 && left <= right) {
ans += pos[right];
right--;
balance--;
}
while (left < right) {
if (pos[right] <= pos[left]) break;
ans += pos[right] - pos[left];
left++;
right--;
}
return ans;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
ll l, r;
cin >> n >> l >> r;
vector<ll> leftVals(n), rightVals(n);
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
leftVals[i] = a - l;
rightVals[i] = r - a;
}
cout << max(calc(leftVals), calc(rightVals)) << '\n';
}
}