#include "candies.h"
#include "bits/stdc++.h"
using namespace std;
#ifndef EVAL
#include "grader.cpp"
#endif
typedef long long ll;
#define ar array
const ll INF = 1e18;
const int inf = 1e9 + 5;
const int N = 2e5 + 5;
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size();
int q = l.size();
vector<int> p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j){
return c[i] < c[j];
});
set<int> ss;
set<ar<ll, 2>> tt;
ss.insert(-1);
vector<ll> a(q);
for(int i=0;i<q;i++){
a[i] = min(a[i], (ll)c[p[0]]);
a[i] = max(a[i], 0ll);
a[i] += v[i];
if(i + 1 < q) a[i + 1] = a[i];
if(a[i] > c[p[0]]){
a[i] -= c[p[0]];
if(!ss.empty() || v[*--ss.end()] < 0){
tt.insert({a[i], i});
}
ss.insert(i);
} else if(a[i] < 0){
a[i] = 0 - a[i];
if(!ss.empty() && v[(*--ss.end())] > 0){
tt.insert({a[i], i});
}
ss.insert(i);
}
}
vector<ll> suff(q + 1);
for(int i=q-1;~i;i--){
suff[i] = suff[i + 1] + v[i];
}
vector<int> res(n);
int j = 0;
while(!tt.empty()){
auto [x, i] = (*tt.begin());
tt.erase(tt.begin());
while(j < n && c[p[j]] < x + c[p[0]]){
int k = *--ss.end();
res[p[j]] = suff[k + 1] + (~k && v[k] > 0 ? c[p[j]] : 0);
j++;
}
assert(ss.count(i));
ss.erase(i);
auto it = ss.lower_bound(i);
if(it != ss.end()){
int k = *it;
if((v[k] > 0) == (v[i] > 0)){
tt.insert({a[k], k});
} else {
tt.erase({a[k], k});
a[k] -= x;
}
}
}
while(j < n){
int k = *--ss.end();
res[p[j]] = suff[k + 1] + (~k && v[k] > 0 ? c[p[j]] : 0);
j++;
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
710 ms |
33164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |