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>
#include "aliens.h"
#define sz(x) ((int)x.size())
#define pii pair<int, int>
#define MP make_pair
#define PB push_back
#define ft first
#define sd second
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
const int N = 50100;
const int K = 110;
const ll OO = 1e18;
vector<pii> vc, seg;
int n, m, k;
ll f[N][K], val[N];
ll sqr(ll x) { return x * x; }
struct line{
ll k, b;
line(): k(0), b(0) {}
line(ll _b, ll _k): k(_k), b(_b) {}
};
vector<line> cht;
ll get_cross_point(line a, line b){
ll K = a.k - b.k;
ll B = b.b - a.b;
if ((B <= 0 && K < 0) || (B >= 0 && K > 0))
return B / K + bool((B % K) != 0);
else return B / K;
}
void insert_to_cht(line nw){
while (sz(cht) > 1){
ll fi = get_cross_point(nw, cht.back());
ll se = get_cross_point(cht.back(), cht[sz(cht) - 2]);
if (se < fi) break;
cht.pop_back();
}
cht.PB(nw);
}
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++)
vc.PB(MP(min(r[i], c[i]), -max(r[i], c[i])));
sort(all(vc));
int lst = -vc[0].sd;
seg.PB(vc[0]);
seg.back().sd *= -1;
seg.back().ft--;
for (int i = 1; i < sz(vc); i++){
pii cr = vc[i];
if (-cr.sd <= lst) continue;
lst = -cr.sd;
seg.PB(cr);
seg.back().sd *= -1;
seg.back().ft--;
}
n = sz(seg);
k = min(n, k);
for (int j = 1; j <= n; j++)
if (seg[j - 1].sd > seg[j].ft)
val[j] = -sqr(seg[j - 1].sd - seg[j].ft);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= k; j++)
f[i][j] = OO;
f[0][0] = 0;
for (int kl = 1; kl <= k; kl++) {
cht.clear();
cht.PB({f[0][kl - 1] + sqr(seg[0].ft), -2 * ll(seg[0].ft)});
int ptr = 0;
for (int i = 1; i <= n; i++){
ptr = min(ptr, sz(cht) - 1);
while (ptr + 1 < sz(cht) && get_cross_point(cht[ptr], cht[ptr + 1]) <= ll(seg[i - 1].sd))
ptr++;
ll nw = cht[ptr].b + cht[ptr].k * ll(seg[i - 1].sd) + sqr(seg[i - 1].sd);
f[i][kl] = min(f[i][kl], nw);
if (i < n)
insert_to_cht({f[i][kl - 1] + val[i] + sqr(seg[i].ft), -2 * ll(seg[i].ft)});
}
}
return f[n][k];
}
# | 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... |