Submission #963024

#TimeUsernameProblemLanguageResultExecution timeMemory
963024penguin133Aliens (IOI16_aliens)C++17
25 / 100
13 ms11868 KiB
#include <bits/stdc++.h> using namespace std; #include "aliens.h" //#define int long long typedef long long ll; #define pi pair<ll, ll> #define pii pair<int, pi> #define fi first #define se second #ifdef _WIN32 #define getchar_unlocked _getchar_nolock #endif mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); deque<pii>dq; ll f(pi x, ll y){ return x.fi * y + x.se; } long double intersect(ll a, ll b, ll c, ll d){ return (long double)(d-b)/(a-c); } void ins(ll m, ll c, ll in){ while(dq.size() > 1){ int s = dq.size(); if(dq[s-1].fi == m){ if(dq[s-1].se.fi < c)dq.pop_back(); else break; continue; } if(intersect(dq[s-1].fi, dq[s-1].se.fi, m, c) <= intersect(dq[s-2].fi, dq[s-2].se.fi, m, c))dq.pop_back(); else break; } dq.push_back({m, {c, in}}); } pi query(ll x){ while(dq.size() > 1){ if(f({dq[0].fi, dq[0].se.fi}, x) < f({dq[1].fi, dq[1].se.fi}, x))dq.pop_front(); else break; } return {f({dq[0].fi, dq[0].se.fi}, x), dq[0].se.se}; } ll mn[1000005], val[100005], N; pi dp[100005]; pi stuf[1000005]; pi cst(ll x){ dq.clear(); for(int i = 0; i < N; i++){ ins(mn[i] * 2, -((i ? dp[i - 1].fi : 0) - 2 * mn[i] + mn[i] * mn[i] - (i ? max(0ll, stuf[i - 1].fi - mn[i] + 1) * (stuf[i - 1].fi - mn[i] + 1) : 0)), (i ? dp[i - 1].se : 0)); pi res = query(stuf[i].fi); res.fi = -res.fi; res.fi += stuf[i].fi * stuf[i].fi + 2 * stuf[i].fi + 1; res.fi += x; res.se++; dp[i] = res; } return dp[N - 1]; } long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) { N = n; for(int i = 0; i <= m; i++)mn[i] = 1e18; for(int i = 0; i < n; i++){ if(r[i] > c[i])swap(r[i], c[i]); mn[c[i]] = min(mn[c[i]], (ll)r[i]); stuf[i] = {c[i], r[i]}; } sort(stuf, stuf + n); for(int i = 0; i < n; i++)mn[i] = stuf[i].se, val[i] = 4e18; val[n] = 1e18; for(int i = n - 2; i >= 0; i--)mn[i] = min(mn[i], mn[i + 1]); //return 0; /* ll dp[n + 1][k + 1]; for(int i = 0; i <= n; i++)dp[i][0] = 1e18; for(int j = 1; j <= k; j++){ for(int i = 0; i < n; i++){ ins(mn[i] * 2, -((i ? dp[i - 1][j - 1] : 0) - 2 * mn[i] + mn[i] * mn[i] - (i ? max(0ll, stuf[i - 1].fi - mn[i] + 1) * (stuf[i - 1].fi - mn[i] + 1) : 0))); dp[i][j] = -query(stuf[i].fi) + stuf[i].fi * stuf[i].fi + 2 * stuf[i].fi + 1; dp[i][j] = 1e18; for(int a = i; a >= 0; a--){ dp[i][j] = min(dp[i][j], (a ? dp[a - 1][j - 1] - max(0ll, stuf[a - 1].fi - mn[a] + 1) * (stuf[a - 1].fi - mn[a] + 1) : 0) + (stuf[i].fi - mn[a] + 1) * (stuf[i].fi - mn[a] + 1)); } //cout << dp[i][j] << ' '; } //cout << '\n'; dq.clear(); } */ ll lo = 0, hi = 1e13; ll ans = 5e18; while(lo <= hi){ ll mid = (lo + hi) >> 1; pi res = cst(mid); if(res.se <= k)ans = min(ans, res.fi - mid * res.se); if(res.se <= n){ val[res.se] = min(val[res.se], res.fi - mid * res.se); } if(res.se > k)lo = mid + 1; else hi = mid - 1; } if(val[k] < 4e18)return ans; int lft = k, rgt = k; while(lft >= 0 && val[lft] >= (int)4e18)lft--; while(rgt <= n && val[rgt] >= (int)4e18)rgt++; if(lft >= 0 && rgt <= n)ans = min(ans, val[lft] + (val[rgt] - val[lft]) * (k - lft) / (rgt - lft)); 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...