Submission #684064

#TimeUsernameProblemLanguageResultExecution timeMemory
684064MilosMilutinovicMountain Trek Route (IZhO12_route)C++14
100 / 100
296 ms29572 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } if (*min_element(a.begin(), a.end()) == *max_element(a.begin(), a.end())) { cout << 0 << '\n'; return 0; } vector<int> par(n); vector<int> L(n); vector<int> R(n); vector<int> sz(n); for (int i = 0; i < n; i++) { par[i] = i; L[i] = i; R[i] = i; sz[i] = 1; } function<int(int)> Get = [&](int x) { return par[x] == x ? x : par[x] = Get(par[x]); }; auto Unite = [&](int x, int y) { x = Get(x); y = Get(y); if (Get((R[y] + 1) % n) == x) { swap(x, y); } sz[x] += sz[y]; par[y] = x; R[x] = R[y]; }; for (int i = 0; i < n; i++) { if (a[(i + 1) % n] == a[i]) { Unite(i, (i + 1) % n); } } auto Valid = [&](int i) { i = Get(i); int lx = Get((L[i] - 1 + n) % n); int rx = Get((R[i] + 1) % n); return a[i] < min(a[lx], a[rx]); }; set<pair<int, int>> st; for (int i = 0; i < n; i++) { if (i == Get(i) && Valid(i)) { st.emplace(sz[i], i); } } auto Fix = [&](int x) { int lx = Get((L[x] - 1 + n) % n); int rx = Get((R[x] + 1) % n); int mn = min(a[lx], a[rx]); a[x] = mn; if (a[lx] == mn) { Unite(lx, x); } if (a[rx] == mn) { Unite(rx, x); } lx = Get(lx); rx = Get(rx); x = Get(x); if (Valid(lx)) { st.emplace(sz[lx], lx); } if (Valid(rx)) { st.emplace(sz[rx], rx); } if (Valid(x)) { st.emplace(sz[x], x); } }; auto MaxOps = [&](int x) { int lx = Get((L[x] - 1 + n) % n); int rx = Get((R[x] + 1) % n); return min(a[lx], a[rx]) - a[x]; }; int ans = 0; while (!st.empty()) { auto it = st.begin(); int i = Get(it->second); st.erase(it); int x = min(k / sz[i], MaxOps(i)); k -= x * sz[i]; ans += 2 * x; Fix(i); } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...