제출 #438652

#제출 시각아이디문제언어결과실행 시간메모리
438652prvocislo휴가 (IOI14_holiday)C++17
100 / 100
465 ms7868 KiB
#include "holiday.h" #include <bits/stdc++.h> typedef long long ll; using namespace std; const int maxn = 1e5 + 5, maxd = 1 << 17; //const int maxn = 8, maxd = 8; ll st[2][maxd * 2]; ll a[maxn]; int id[maxn], n, s, d; // atrakcie a ich indexy v striedenom poli void upd(int t, int i, int val) { for (i += maxd; i > 0; i >>= 1) st[t][i] += val; } void change(int i, int delta) { upd(0, id[i], delta), upd(1, id[i], delta * a[i]); } ll max_k(int k) { if (k <= 0) return 0; int vr = 2; ll ans = 0; for (;; vr = vr * 2) { if (k > st[0][vr]) { k -= st[0][vr]; ans += st[1][vr]; vr++; } if (vr >= maxd) return ans + st[1][vr]; }//return ans + st[1][vr]; } int L, R; // interval (vratane) ktory ma teraz obsadeny divide&conquer procedura ll query(int l, int r) // nahod vsetky cisla v intervale l az r, najprv pojdeme doprava, potom sa otocime a pojdeme dolava { while (L > l) change(--L, 1); while (R < r) change(++R, 1); while (L < l) change(L++, -1); while (R > r) change(R--, -1); return max_k(d - (r - l) - (r - s)); } ll ans = 0; void rek(int l, int r, int optl, int optr) { if (r < l) return; int m = (l + r) >> 1; int maxid = m; ll maxi = -1; for (int i = optl; i <= optr; i++) { ll val = query(i, m); if (val >= maxi) maxi = val, maxid = i; } ans = max(ans, maxi); rek(l, m - 1, optl, maxid), rek(m + 1, r, maxid, optr); } long long int findMaxAttraction(int N, int S, int D, int A[]) { memset(st, 0, sizeof(st)), memset(a, 0, sizeof(a)), memset(id, 0, sizeof(id)); n = N, s = S, d = D; ans = 0; vector<pair<int, int> > v; for (int i = 0; i < n; i++) a[i] = A[i], v.push_back({a[i], i}); sort(v.begin(), v.end(), greater<pair<int, int> >()); for (int i = 0; i < n; i++) id[v[i].second] = i; L = 0, R = -1; rek(s, n - 1, 0, s); reverse(a, a + n), reverse(id, id + n); memset(st, 0, sizeof(st)); s = n - s - 1; L = 0, R = -1; rek(s, n - 1, 0, s); 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...