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>
using namespace std;
typedef long long ll;
struct segment{
int l, r;
segment(int _l, int _r){
l = _l;
r = _r;
}
};
struct line{
ll a, b;
line(ll _a = 0, ll _b = 0){
a = _a;
b = _b;
}
};
struct convexHull{
vector <line> d;
int ptr;
void reset(){
d.clear();
ptr = 0;
}
ll calcY(const line &l, ll x){
return l.a * x + l.b;
}
bool bad(const line &l1, const line &l2, const line &l3){
return (l2.a - l3.a) * (l2.b - l1.b) >= (l1.a - l2.a) * (l3.b - l2.b);
}
void add(const line &newLine){
while (d.size() > 1){
if (bad(d[d.size() - 2], d[d.size() - 1], newLine))
d.pop_back();
else
break;
}
d.push_back(newLine);
}
ll getMin(ll x){
assert(!d.empty());
ptr = min(ptr, (int) d.size() - 1);
while (ptr < (int) d.size() - 1)
if (calcY(d[ptr], x) >= calcY(d[ptr + 1], x))
ptr++;
else
break;
return calcY(d[ptr], x);
}
} cvh;
int n, m, k;
vector <segment> segs;
vector <vector <ll> > dp;
void prepare(){
sort(segs.begin(), segs.end(), [](const segment &a, const segment &b){
if (a.l != b.l)
return a.l < b.l;
return a.r > b.r;
});
vector <segment> tmp;
int maxR = -1;
for(segment s : segs){
if (s.r > maxR)
tmp.push_back(s);
maxR = max(maxR, s.r);
}
segs = tmp;
n = segs.size();
}
ll cost(int l, int r){
assert(l <= r || l > 0);
int w = segs[r].r - segs[l].l + 1;
int intersect = max(0, segs[l - 1].r - segs[l].l + 1);
return (ll) w * w - (ll) intersect * intersect;
}
ll solve(){
segs.insert(segs.begin(), segment(-1, -1));
dp.resize(2, vector<ll>(n + 1));
dp[0][0] = dp[1][0] = 0;
for(int i = 1; i <= n; i++)
dp[1][i] = cost(1, i);
for(int i = 2; i <= k; i++){
int id = i & 1;
cvh.reset();
cvh.add(line(-2 * segs[1].l, (ll) segs[1].l * segs[1].l));
for(int j = 1; j <= n; j++){
dp[id][j] = cvh.getMin(segs[j].r + 1) + (ll) (segs[j].r + 1) * (segs[j].r + 1);
cvh.add(line(-2 * segs[j + 1].l, (ll) segs[j + 1].l * segs[j + 1].l + dp[id ^ 1][j]
- (ll) max(0, segs[j].r - segs[j + 1].l + 1) * max(0, segs[j].r - segs[j + 1].l + 1)));
}
}
return dp[k & 1][n];
}
ll take_photos(int _n, int _m, int _k, vector<int> r, vector<int> c){
n = _n;
m = _m;
k = _k;
for(int i = 0; i < n; i++)
segs.push_back(segment(min(r[i], c[i]), max(r[i], c[i])));
prepare();
return solve();
}
# | 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... |