제출 #586717

#제출 시각아이디문제언어결과실행 시간메모리
586717SeDunion휴가 (IOI14_holiday)C++17
100 / 100
890 ms4948 KiB
#include"holiday.h" #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; using ll = long long; const int N = 1e5 + 123; int n; pair<int,int>pp[N]; int cnt[N << 2]; ll sum[N << 2]; void upd(int pos, int type, int l = 0, int r = n, int v = 1) { if (r - l == 1) { cnt[v] += type, sum[v] += type * pp[l].first; return; } int m = (l + r) >> 1; if (pos < m) upd(pos, type, l, m, v << 1); else upd(pos, type, m, r, v<<1|1); cnt[v] = cnt[v << 1] + cnt[v<<1|1]; sum[v] = sum[v << 1] + sum[v<<1|1]; } ll get(int y, int l = 0, int r = n, int v = 1) { if (r - l == 1) return y ? sum[v] : 0; int m = (l + r) >> 1; if (cnt[v<<1|1] >= y) return get(y, m, r, v<<1|1); else return get(y - cnt[v<<1|1], l, m, v << 1) + sum[v<<1|1]; } int pos[N]; ll ans; int start, d; int L = 1, R = 0; void moveL(int s) { while (L < s) { upd(pos[L++], -1); } while (L > s) { upd(pos[--L], +1); } } void moveR(int s) { while (R < s) { upd(pos[++R], +1); } while (R > s) { upd(pos[R--], -1); } } void solve(int l, int r, int opl, int opr) { if (l > r) return; int m = (l + r) >> 1; int opm = -1; moveL(m); int x = d - (start - m); ll cur = 0; opm = opl; for (int i = opl ; i <= opr ; ++ i) { moveR(i); int y = x - (i - m); if (y > 0) { ll temp = get(y); if (temp > cur) { opm = i; cur = temp; } } } ans = max(ans, cur); solve(l, m - 1, opl, opm); solve(m + 1, r, opm, opr); } ll findMaxAttraction(int n_, int start_, int d_, int attraction[]) { n = n_, start = start_, d = d_; for (int i = 0 ; i < n ; ++ i) { pp[i] = {attraction[i], i}; } sort(pp, pp + n); for (int i = 0 ; i < n ; ++ i) { pos[pp[i].second] = i; } for (int _ = 0 ; _ < 2 ; ++ _) { solve(0, start, start, n - 1); moveL(1); moveR(0); for (int i = 0 ; i < n ; ++ i) { pp[i].second = n - pp[i].second - 1; pos[pp[i].second] = i; } reverse(attraction, attraction + n); start = n - start - 1; } 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...