Submission #465725

#TimeUsernameProblemLanguageResultExecution timeMemory
465725alexxela12345Distributing Candies (IOI21_candies)C++17
3 / 100
5098 ms25436 KiB
#include "candies.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; struct mdata { vector<pair<int, int>> arr; int size(); int get(int); }; int mdata::size() { return arr.size(); } int mdata::get(int i) { return arr[i].second; } void erase(mdata &mda, int i, int x) { mda.arr.erase(find(mda.arr.begin(), mda.arr.end(), pair<int, int>{i, x})); } void add(mdata &mda, int i, int x) { mda.arr.push_back({i, x}); sort(mda.arr.begin(), mda.arr.end()); } int get_fst_diff(mdata &mda, int mx_diff) { // returns index to first arrow after which everything contains in a segment of size mx int cur = 0; int mn = 0, mx = 0; for (int i = mda.size() - 1; i >= 0; i--) { int a = mda.get(i); cur += a; mn = min(mn, cur); mx = max(mx, cur); if (mx - mn >= mx_diff) { return i + 1; } } return 0; } int get_min_suf(mdata &mda, int fst) { int cur = 0; int mn = 0; for (int i = mda.size() - 1; i >= fst; i--) { cur += mda.get(i); if (cur < mn) { mn = cur; } } return mn; } int get_max_suf(mdata &mda, int fst) { int cur = 0; int mn = 0; for (int i = mda.size() - 1; i >= fst; i--) { cur += mda.get(i); if (cur > mn) { mn = cur; } } return mn; } int get_ans(mdata &mda, int mx) { int fst = get_fst_diff(mda, mx); int start = 0; if (fst == 0) { start = 0; } else { int a = mda.get(fst - 1); if (a > 0) { start = mx; } else { start = 0; } } if (start == 0) { int ans = get_max_suf(mda, fst); return start + ans; } else { int ans = get_min_suf(mda, fst); return start + ans; } } vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) { int n = c.size(); vector<vector<int>> add_evs(n + 1), rem_evs(n + 1); for (int i = 0; i < (int) l.size(); i++) { add_evs[l[i]].push_back(i); rem_evs[r[i] + 1].push_back(i); } mdata mda; vector<int> ans; ans.reserve(n); for (int i = 0; i < n; i++) { for (int el : rem_evs[i]) { erase(mda, el, v[el]); } for (int el : add_evs[i]) { add(mda, el, v[el]); } ans.push_back(get_ans(mda, c[i])); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...