#include "candies.h"
#include <vector>
#include <algorithm>
using namespace std;
struct Move {
long long o;
long long n;
long long c;
};
vector<Move> V;
long long cnt;
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
cnt = 0; V.clear();
int n = (int)c.size(), q = (int)v.size();
long long globalMin = 0, globalMax = 0;
vector<int> s(n);
for (int t = 0; t < q; t++) {
long long tmp_curr = v[t];
if (tmp_curr > 0) {
long long curr = tmp_curr;
while (!V.empty()) {
if (V.back().n > V.back().o) {
if (V.back().o < cnt) {
tmp_curr = cnt + tmp_curr - V.back().o;
cnt = V.back().o;
}
V.pop_back();
} else if (V.back().c <= curr) V.pop_back();
else break;
}
} else {
long long curr = -tmp_curr;
while (!V.empty()) {
if (V.back().n < V.back().o) {
if (V.back().o > cnt) {
tmp_curr = cnt + tmp_curr - V.back().o;
cnt = V.back().o;
}
V.pop_back();
} else if (V.back().c <= curr) V.pop_back();
else break;
}
}
V.push_back(Move{ cnt, cnt + tmp_curr, tmp_curr > 0 ? tmp_curr : -tmp_curr });
cnt += tmp_curr;
globalMin = min(globalMin, cnt), globalMax = max(globalMax, cnt);
}
//for (auto &i: V) printf("%lld %lld %d\n", i.o, i.n, i.c);
for (int i = 0; i < n; i++) {
long long l = globalMin, r = globalMax;
if (V.front().c >= c[i]) {
Move x = *(upper_bound(V.begin(), V.end(), Move{ 0LL, 0LL, c[i] }, [](const Move &a, const Move &b) {
return a.c > b.c;
}) - 1);
if (x.o > x.n) l = x.n, r = l + c[i];
else r = x.n, l = r - c[i];
//printf("%lld %lld %d\n", x.o, x.n, x.c);
}
//printf("! %lld %lld\n", l, r);
if (l < 0) s[i] = cnt - l;
else if (r > c[i]) s[i] = cnt - r + c[i];
else s[i] = cnt;
}
return s;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
129 ms |
7248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
62 ms |
5824 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |