Submission #38307

#TimeUsernameProblemLanguageResultExecution timeMemory
38307romanasaMountain Trek Route (IZhO12_route)C++14
0 / 100
0 ms2024 KiB
#include <bits/stdc++.h> #define err(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) using namespace std; typedef long long ll; #define TASK "g" /* const int MAXMEM = (int)5e7; char MEM[MAXMEM]; int mpos; inline void* operator new(size_t n) { char *res = MEM + mpos; mpos += n; return (void*)res; } inline void operator delete(void*) {} */ const int base = (int)1e7; ll get(pair<int, int> x) {return 1ll * x.first * base + x.second; } typedef pair<int, ll> segment; //#define val second.first //#define len second.second #define pos first set<segment> S; priority_queue<pair<int, int> > T; void calc(int pos, int mx, vector<int> &x) { int n = (int)x.size(); vector<pair<int, int> > cur; for (int i = 0; i < n; i++) { int ind = (pos + i); if (ind >= n) ind -= n; if (cur.empty() || cur.back().first != x[ind]) cur.push_back({ x[ind], 1 }); else cur.back().second++; } for (int i = 0; i < (int)cur.size(); i++) { auto c = cur[i]; int pr = cur[i ? i - 1 : (int)cur.size() - 1].first; int nt = cur[(i + 1) % (int)cur.size()].first; S.insert(make_pair(pos, get(c))); if (c.first < pr && c.first < nt) T.push({ -c.second, pos }); pos = (pos + c.second); if (pos >= n) pos -= n; } } void add(segment c) { auto it = S.insert(c).first; auto prv = it; if (it == S.begin()) prv = --S.end(); else --prv; auto nxt = it; if (it == --S.end()) nxt = S.begin(); else ++nxt; if (c.second / base < nxt->second / base && c.second / base < prv->second / base) T.push({ -c.second % base, c.first }); } int main() { #ifndef HOME freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<int> x(n); for (int i = 0; i < n; i++) cin >> x[i]; int pos = -1; for (int i = 0; i < n; i++) { if (x[i] != x[(i + 1) % n]) pos = (i + 1) % n; } if (pos == -1) return 0 * puts("0"); int mx = *max_element(x.begin(), x.end()); calc(pos, mx, x); int ans = 0; while (T.size() && k >= -T.top().first) { k -= (-T.top().first); auto it = S.lower_bound(make_pair(T.top().second, -1)); T.pop(); ans += 2; if (S.size() == 1) break; auto prv = it; if (it == S.begin()) prv = --S.end(); else --prv; auto nxt = it; if (it == --S.end()) nxt = S.begin(); else ++nxt; if (prv == nxt) { if (it->second / base + 1 < prv->second / base) { auto c = *it; S.erase(it); //c.val++; c.second += base; add(c); } else { auto c = *it; c.second += base; c.second += prv->second % base; //c.val++; //c.len += prv->len; S.erase(it); S.erase(prv); add(c); } } else { if (it->second / base + 1 < prv->second / base && it->second / base + 1 < nxt->second / base) { auto c = *it; S.erase(it); //c.val++; c.second += base; add(c); } else if (it->second / base + 1 < prv->second / base) { auto c = *it; //c.len += nxt->len; //c.val++; c.second += base; c.second += nxt->second % base; S.erase(it); S.erase(nxt); add(c); } else if (it->second / base + 1 < nxt->second / base) { auto c = *it; //c.len += prv->len; //c.pos = prv->pos; //c.val++; c.second += prv->second % base; c.second += base; c.first = prv->first; S.erase(it); S.erase(prv); add(c); } else { auto c = *it; //c.len += prv->len + nxt->len; //c.pos = prv->pos; //c.val++; c.second += prv->second % base + nxt->second % base; c.second += base; c.first = prv->first; S.erase(it); S.erase(prv); S.erase(nxt); add(c); } } } cout << ans << "\n"; return 0; }

Compilation message (stderr)

route.cpp: In function 'int main()':
route.cpp:78:69: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
                                                                     ^
route.cpp:78:69: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
#Verdict Execution timeMemoryGrader output
Fetching results...