#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep = ""; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename A> ostream& operator<<(ostream &os, const set<A> &s) { os << '{'; string sep = ""; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << '{'; string sep = ""; for (const auto &x : m) os << sep << x.first << " -> " << x.second, sep = ", "; return os << '}'; }
void debug() { cerr << endl; }
template<typename Ini, typename... Fim> void debug(Ini I, Fim... F) { cerr << I; if(sizeof...(F)) cerr << ", "; debug(F...);}
#define db(...) cerr << "DEBUG (" << #__VA_ARGS__ << ") == ", debug(__VA_ARGS__)
constexpr int inf = 0x3f3f3f3f;
// subtask l = 0, r = n-1 for all events
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = (int)(c.size());
vector<int> ans(n);
vector<pair<int,int>> td;
for(int i = 0; i < n; i++)
td.push_back({c[i], i});
sort(td.begin(), td.end(), greater<pair<int,int>>());
vector<int> seq;
int add = 0;
for(int i = 0; i < n; i++) {
if((v[i] > 0) == (add > 0) || !add) add += v[i], add = max(-inf, min(add, inf));
else seq.push_back(max(0, min(inf, add + (seq.size() ? seq.back() : 0)))), add = v[i];
}
// db(add);
if(add) seq.push_back(max(0, min(inf, add + (seq.size() ? seq.back() : 0))));
// db(seq);
vector<pair<int,int>> gld;
int mn = inf;
for(int i = (int)(seq.size())-1; i >= 0; i--) {
mn = min(mn, seq[i]);
gld.push_back({seq[i], seq[i] - mn}); // intervalo que fica ativo
}
sort(gld.begin(), gld.end());
reverse(gld.begin(), gld.end());
int acabado = 0;
set<pair<int,int>> atv, maior;
for(int i = 0, j = 0; i < n; i++) {
for(; j < (int)(gld.size()) && gld[j].first >= td[i].first; j++)
atv.insert({gld[j].second, gld[j].first}), maior.insert(gld[j]);
while(atv.size() && atv.begin()->first >= td[i].first) {
acabado = max(acabado, atv.begin()->second - atv.begin()->first);
maior.erase({atv.begin()->second, atv.begin()->first});
atv.erase(atv.begin());
}
// db(atv, maior, td[i]);
ans[td[i].second] = seq.back() - acabado;
// db(acabado, seq.back());
if(maior.size()) ans[td[i].second] = min(ans[td[i].second], seq.back() - (maior.rbegin()->first - td[i].first));
}
// db(ans);
// exit(0);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
162 ms |
9512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |