This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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], N;
pi dp[100005];
pi stuf[1000005];
pi cst(int 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;
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;
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 = 1e12;
ll ans = 1e18;
while(lo <= hi){
int mid = (lo + hi) >> 1;
pi res = cst(mid);
ans = min(ans, res.fi - mid * res.se);
if(res.se > mid)lo = mid + 1;
else hi = mid - 1;
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |