#include "candies.h"
#include <bits/stdc++.h>
typedef std::pair<int, int> ii;
void push_to_stack(std::stack<int> &st, int toAdd){
if(toAdd > 0){
while(!st.empty() && st.top() + toAdd >= 0){
toAdd += st.top();
st.pop();
}
if(toAdd > 0) st.push(toAdd);
}
else if(toAdd < 0){
while(!st.empty() && st.top() + toAdd <= 0){
toAdd += st.top();
st.pop();
}
if(!st.empty() && toAdd < 0) st.push(toAdd);
}
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n = c.size();
int q = l.size();
std::vector<int> s(n);
std::vector<ii> orders;
std::stack<int> st;
int buffer = 0;
for(int i = 0; i < q; i++) push_to_stack(st, v[i]);
for(int i = 0; i < n; i++) orders.push_back(ii(c[i], i));
sort(orders.begin(), orders.end());
int cur = 0;
for(auto it = orders.begin(); it != orders.end(); it++){
while(it->first >= abs(st.top())){
cur += st.top();
st.pop();
}
if(st.size()%2) s[it->second] = cur + it->first;
else s[it->second] = cur;
}
return s;
}
//int main() {
// freopen("test.inp","r",stdin);
// int n;
// assert(1 == scanf("%d", &n));
// std::vector<int> c(n);
// for(int i = 0; i < n; ++i) {
// assert(scanf("%d", &c[i]) == 1);
// }
//
// int q;
// assert(1 == scanf("%d", &q));
// std::vector<int> l(q), r(q), v(q);
// for(int i = 0; i < q; ++i) {
// assert(scanf("%d %d %d", &l[i], &r[i], &v[i]) == 3);
// }
//
// std::vector<int> ans = distribute_candies(c, l, r, v);
//
// for(int i = 0; i < n; ++i) {
// if (i > 0) {
// printf(" ");
// }
// printf("%d", ans[i]);
// }
// printf("\n");
// fclose(stdout);
// return 0;
//}
Compilation message
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:31:9: warning: unused variable 'buffer' [-Wunused-variable]
31 | int buffer = 0;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
300 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
124 ms |
13884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
300 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |