This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = sz(c), q = sz(v);
vector<int> a(c.begin(), c.end());
sort(a.rbegin(), a.rend());
vector<ll> ps(q);
for (int i = 0; i < q; i++) {
ps[i] = v[i];
if (i) ps[i] += ps[i-1];
}
vector<ll> mn(q), mx(q);
for (int i = q-1; i >= 0; i--) {
mn[i] = mx[i] = ps[i];
if (i < q-1) mn[i] = min(mn[i], mn[i+1]), mx[i] = max(mx[i], mx[i+1]);
}
map<int, ll> store;
int last = -1;
for (int x : a) {
while (last < q-1) {
ll cur = (last < 0 || v[last] < 0 ? 0 : x);
ll cur_ps = last >= 0 ? ps[last] : 0;
if (mn[last + 1] - cur_ps + cur < 0 || mx[last + 1] - cur_ps + cur >= x) {
int use = last + 1;
do {
cur += v[use++];
} while (0 < cur && cur < x);
last = use - 1;
} else break;
}
ll cur = (last < 0 || v[last] < 0 ? 0 : x);
ll cur_ps = last >= 0 ? ps[last] : 0;
store[x] = ps.back() - cur_ps + cur;
}
vector<int> ans(n);
for (int i = 0; i < n; i++)
ans[i] = store[c[i]];
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |