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 "aliens.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
struct Line{
LL m, c, i;
Line(LL m = 0, LL c = 0, int i = 0): m(m), c(c), i(i) {}
LL get(LL x){return m * x + c;}
};
struct ConvexHull{
int sz, B, fr;
Line *hull;
ConvexHull(int n): sz(0), B(0), fr(0){
hull = new Line[++n];
}
bool is_bad(int curr, int prev, int next){
Line c = hull[curr], p = hull[prev], n = hull[next];
return (c.c - n.c) * (c.m - p.m) <= (p.c - c.c) * (n.m - c.m);
}
void add_line(LL m, LL c, int idx){
hull[sz++] = Line(m, c, idx);
while (sz - fr > 2 && is_bad(sz-2, sz-3, sz-1)){
hull[sz-2] = hull[sz-1]; sz--;
}
}
pair<LL, int> query(LL x){
while (sz - fr > 1 && hull[fr].get(x) >= hull[fr+1].get(x)) fr++;
return make_pair(hull[fr].get(x), hull[fr].i);
}
};
const LL LINF = 1e18;
const int MX = 1e5;
int n, m, k;
vector<pii> ori;
LL L[MX + 5], R[MX + 5], nx[MX + 5];
LL dp[MX + 5];
LL dp2[2][MX + 5];
int cnt[MX + 5];
int par[MX + 5];
ConvexHull ch(MX + 5);
bool cmp(pii a, pii b){
return a.fi != b.fi ? a.fi < b.fi : a.se > b.se;
}
/*
dp[k][n] = min(dp[k-1][n], min{j<=n} (dp[k-1][j-1] + (L[j]^2-2L[j]) + max(0,R[j-1]-L[j]+1)^2 - 2L[j]•R[n]) + R[n]^2 + 2R[n] + 1;
( c1 ) ( c2 ) ( m ) ( x ) ( extra )
(R[n] - L[j] + 1) ^ 2 = (R[n] + 1) ^ 2 - 2L[j](R[n] + 1) + L[j] ^ 2
*/
LL check(LL C){
ch.sz = 0; ch.fr = 0;
cnt[0] = 0;
for (int j = 1; j <= n; j++){
ch.add_line(-2LL * L[j], dp[j-1] + L[j] * L[j] - nx[j] * nx[j], j-1);
pair<LL, int> res = ch.query(R[j] + 1);
dp[j] = res.first + (R[j] + 1) * (R[j] + 1) + C;
par[j] = res.second;
cnt[j] = cnt[par[j]] + 1;
// cout << "J/" << j << " : " << dp[j] << ' ' << cnt[j] << ' ' << par[j] << '\n';
}
// cout << "Checking C : " << C << " with result " << dp[n] << ' ' << cnt[n] << '\n';
return cnt[n];
}
long long take_photos(int N, int M, int K, std::vector<int> r, std::vector<int> c) {
n = N, m = M, k = K;
for (int i = 0; i < n; i++){
ori.emplace_back(r[i], c[i]);
if (ori.back().fi > ori.back().se) swap(ori.back().fi, ori.back().se);
}
sort(ori.begin(), ori.end());
int idx = 0;
for (int i = 0; i < n; i++){
if (idx && L[idx] <= ori[i].fi && R[idx] >= ori[i].se) continue;
while (idx && ori[i].fi <= L[idx] && ori[i].se >= R[idx]) idx--;
idx++; tie(L[idx], R[idx]) = ori[i];
}
n = idx;
k = min(k, idx);
// for (int i = 1; i <= n; i++) printf("(%d, %d)%c", L[i], R[i], " \n"[i==n]);
L[0] = R[0] = -LINF;
for (int i = 1; i <= n; i++)
nx[i] = max(0LL, R[i-1] - L[i] + 1);
// for (int i = 1; i <= n; i++) printf("%lld%c", nx[i], " \n"[i==n]);
// for (int j = 1; j <= n; j++) dp2[0][j] = LINF;
// for (int i = 1; i <= k; i++){
// ch.sz = ch.fr = 0;
// for (int j = 1; j <= n; j++){
// ch.add_line(-2LL * L[j], dp2[i&1^1][j-1] + L[j] * L[j] - nx[j] * nx[j], cnt[j - 1]);
// pair<LL, int> res = ch.query(R[j] + 1);
// dp2[i&1][j] = res.first + (R[j] + 1) * (R[j] + 1);
// cnt[j] = res.second + 1;
// // cout << "J/" << j << " : " << dp[j] << ' ' << cnt[j] << '\n';
// }
// }
// return dp2[k&1][n];
LL lo = -1e12, hi = 1e12, md, ans = -1e12;
//increase cost then k decreases
// cout << "LOOK FOR K : " << k << '\n';
while (lo <= hi){
md = (lo + hi) >> 1;
if (check(md) <= k){
ans = md; hi = md - 1;
} else lo = md + 1;
}
int kk = check(ans);
// cout << ans << ' ' << kk << '\n';
return dp[n] - ans * kk - 1LL * (k - kk) * (ans - 1);
}
# | 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... |