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;
typedef long double ld;
const int N = 1000100;
const int K = 110;
const ll OO = 1e18;
const ld E = 1e-9;
vector<pii> vc, seg;
int n, m, k, kl[N];
ld f[N], val[N];
ll sqr(ll x) { return x * x; }
struct line{
ld k, b;
int kol;
line(): k(0), b(0), kol(0) {}
line(ld _b, ld _k, int kl): k(_k), b(_b), kol(kl) {}
};
vector<line> cht;
ld get_cross_point(line a, line b){
ld K = a.k - b.k;
ld B = b.b - a.b;
return B / K;
}
void insert_to_cht(line nw){
while (sz(cht) > 1){
ld fi = get_cross_point(nw, cht.back());
ld se = get_cross_point(cht.back(), cht[sz(cht) - 2]);
if (se + E < fi) break;
cht.pop_back();
}
cht.PB(nw);
}
int calc(ld extra){
f[0] = 0;
cht.clear();
cht.PB({f[0] + (ld)sqr(seg[0].ft), -2 * ld(seg[0].ft), 0});
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]) + E < ld(seg[i - 1].sd))
ptr++;
ld nw = cht[ptr].b + cht[ptr].k * ld(seg[i - 1].sd) + (ld)sqr(seg[i - 1].sd);
f[i] = nw + extra;
kl[i] = cht[ptr].kol + 1;
if (i < n)
insert_to_cht({f[i] + val[i] + (ld)sqr(seg[i].ft), -2 * ld(seg[i].ft), kl[i]});
}
return kl[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++)
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);
ld l1 = 0, r1 = ll(1e12);
for (int it = 0; it < 60; it++){
ld md = (l1 + r1) / 2.0;
if (calc(md) <= k)
r1 = md;
else l1 = md;
}
calc(l1);
return ll(round(f[n] - l1 * ld(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... |