Submission #371411

#TimeUsernameProblemLanguageResultExecution timeMemory
371411SortingGap (APIO16_gap)C++17
30 / 100
49 ms1260 KiB
#include "gap.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; using namespace std; void check_max(ll &a, ll b){ a = (b > a) ? b : a; } const ll INF = 1e18; const int INT_INF = 1e9; int t, n; ll solve_1(){ ll l, r, ans = 0; MinMax(0, INF, &l, &r); ll l2, r2; n -= 2; while(n > 0){ MinMax(l + 1, r - 1, &l2, &r2); ans = max(ans, max(l2 - l, r - r2)); l = l2, r = r2; n -= 2; } ans = max(ans, r - l); return ans; } pair<ll, int> solve(ll l, ll r, int cnt, ll curr_mx){ if(r - l <= curr_mx) return {0, -1}; if(!cnt) return {0, 0}; ll mid = (l + r) >> 1, l2, r2, l3, r3; MinMax(l + 1, mid, &l2, &r2); MinMax(mid + 1, r - 1, &l3, &r3); if(l2 == -1 && l3 == -1) return {r - l, 0}; if(l2 != -1) curr_mx = max(l2 - l, curr_mx); else curr_mx = max(l3 - l, curr_mx); if(r3 != -1) curr_mx = max(r - r3, curr_mx); else curr_mx = max(r - r2, curr_mx); if(l2 != -1 && l3 != -1) curr_mx = max(l3 - r2, curr_mx); int new_cnt = 0; if(l2 != -1){ auto p = solve(l2, r2, -1, curr_mx); check_max(curr_mx, p.first); if(p.second == -1){ new_cnt -= INT_INF; cnt = -1; } if(cnt != -1) cnt -= p.second; else new_cnt += p.second; } if(l3 != -1){ auto p = solve(l2, r2, cnt, curr_mx); check_max(curr_mx, p.first); if(p.second == -1){ new_cnt -= INT_INF; cnt = -1; } if(cnt != -1) cnt -= p.second; else new_cnt += p.second; } if(new_cnt < 0) new_cnt = -1; return {curr_mx, new_cnt}; } ll findGap(int _t, int _n){ t = _t, n = _n; if(t == 1) return solve_1(); if(n <= 1) return 0; ll l, r; MinMax(0, INF, &l, &r); ll curr_mx = ((r - l) / (n - 1)) + !!((r - l) % (n - 1)); return max(solve(l, r, n - 2, curr_mx).first, curr_mx); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...