#include <bits/stdc++.h>
#include "candies.h"
typedef long long int64;
using namespace std;
// int64 find (int n, int64 c, vector <int64> &mx, vector <int64> &mn) {
// int l = 1, r = n-1;
// }
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size();
int m = v.size();
v.insert(v.begin(), -1);
vector <int64> pref (m + 2);
vector <int64> pref_pos (m + 2);
vector <pair <int64, int64> > mn (m + 2, {1e18, -1}), mx (m + 2, {-1e18, -1});
for (int i = 1; i <= m; i++){
pref[i] = pref[i-1] + v[i];
pref_pos[i] = max(0ll, pref_pos[i-1] + v[i]);
}
for (int i = m; i >= 1; i--){
mx[i] = mx[i + 1], mn[i] = mn[i + 1];
if (pref[i] > mx[i].first) {
mx[i] = {pref[i], i};
}
if (pref[i] < mn[i].first) {
mn[i] = {pref[i], i};
}
}
// for (int i = 1; i <= m; i++){
// cout << pref[i] << " ";
// }
// cout << "\n";
// for (int i = 1; i <= m; i++){
// cout << mn[i].first << " ";
// }
// cout << "\n";
// for (int i = 1; i <= m; i++){
// cout << mx[i].first << " ";
// }
// cout << "\n";
vector <int> rs (n);
for (int i = 0; i < n; i++){
int l = 1, r = m, f = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (mx[mid].first - mn[mid].first >= c[i]) {
f = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
if (f == -1) rs[i] = pref_pos[m];
else {
if (mn[f].second > mx[f].second) rs[i] = pref[m] - pref[mn[f].second];
else rs[i] = c[i] + pref[m] - pref[mx[f].second];
}
}
return rs;
}
# |
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 |
115 ms |
17716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
58 ms |
15476 KB |
Output is correct |
4 |
Correct |
46 ms |
3824 KB |
Output is correct |
5 |
Correct |
103 ms |
17752 KB |
Output is correct |
6 |
Correct |
102 ms |
17724 KB |
Output is correct |
7 |
Correct |
108 ms |
17764 KB |
Output is correct |
8 |
Correct |
100 ms |
17764 KB |
Output is correct |
9 |
Correct |
106 ms |
17812 KB |
Output is correct |
# |
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 |
- |