Submission #685459

#TimeUsernameProblemLanguageResultExecution timeMemory
685459pragmatistMountain Trek Route (IZhO12_route)C++17
0 / 100
1 ms340 KiB
/* #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") #pragma GCC target ("avx2") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") */ #include<bits/stdc++.h> #define ld long double #define sz(v) (int)v.size() #define ll long long #define pb push_back #define x first #define y second #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define nl "\n" using namespace std; const int N = (int)1e6 + 7; // make sure this is right const int M = (int)1e6 + 7; const int inf = (int)2e9 + 7; const ll INF = (ll)3e18 + 7; const ll MOD = (ll)998244353; // make sure this is right bool bit(int x, int i) { return x >> i & 1; } int sum(int x, int y) { x += y; if(x >= MOD) x -= MOD; return x; } int mult(int x, int y) { return 1ll * x * y % MOD; } int n, k, a[N], b[N]; int l[N], r[N], sz[N], p[N]; int get(int v) { return (v == p[v] ? v : p[v] = get(p[v])); } void comb(int u, int v) { u = get(u); v = get(v); if(u == v) return; l[v] = min(l[v], l[u]); r[v] = max(r[v], r[u]); sz[v] += sz[u]; p[u] = v; } void solve() { cin >> n >> k; int mx = 0; for(int i = 1; i <= n; ++i) { cin >> a[i]; if(a[i] > a[mx]) mx = i; } for(int i = mx; i <= n; ++i) b[i - mx + 1] = a[i]; for(int i = 1; i < mx; ++i) b[n - mx + i + 1] = a[i]; for(int i = 1; i <= n; ++i) a[i] = b[i]; for(int i = 1; i <= n; ++i) { l[i] = r[i] = p[i] = i; sz[i] = 1; } for(int i = 2; i <= n; ++i) { if(a[i] == a[i - 1]) { comb(i, i - 1); } } if(a[1] == a[n]) comb(1, n); set<pair<int, int> > s; for(int i = 2; i <= n; ++i) { s.insert({sz[i], get(i)}); } ll ans = 0; while(!s.empty()) { int i = s.begin() -> y, x = get(i); s.erase(s.begin()); if(sz[x] == n) break; int L = (l[x] == 1 ? n : l[x] - 1), R = (r[x] == n ? 1 : r[x] + 1); if(a[x] < a[L] && a[x] < a[R]) { int can = min(k / sz[x], min(a[L], a[R]) - a[x]); ans += 2 * can; k -= can * sz[x]; a[x] += can; if(a[x] != a[L] && a[x] != a[R]) continue; if(a[x] == a[L]) comb(x, L); if(a[x] == a[R]) comb(x, R); x = get(x); s.insert({sz[x], x}); } } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int test = 1; //cin >> test; for(int i = 1; i <= test; ++i) { //cout << "Case #" << i << ": "; solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...