This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "aliens.h"
using namespace std;
#define ve vector
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9 + 10;
const ll LLINF = (ll)1e18 + 10;
ve<pii> P;
ve<ll> L;
int a;
struct segment {
ll m, b, l, r, k;
};
double intersect(ll m1, ll m2, ll b1, ll b2) {
return (double) (b1 - b2) / (double) (m2 - m1);
}
ll sq(int x) { return (ll)x*x; }
pair<ll, ll> calc(ll lambda) {
ve<ll> DP(a), K(a);
list<segment> H;
DP[0] = sq(P[0].second - P[0].first + 1) - lambda;
K[0] = 1;
for (int i = 1; i < a; i++) {
ll x = -P[i].first + 1;
ll b = DP[i - 1] - L[i] + x * x;
ll m = 2 * x;
if (H.size() == 0) H.push_back({m, b, -LLINF, LLINF, K[i - 1]});
else {
while (intersect(H.back().m, m, H.back().b, b) < (double)H.back().l) {
H.pop_back();
}
double z = intersect(H.back().m, m, H.back().b, b);
if (z == (double)H.back().l) {
assert(false);
}
ll l = (ll)ceil(z);
H.back().r = (ll)floor(z);
H.push_back({m, b, l, LLINF, K[i - 1]});
}
while (H.begin()->r < P[i].second) {
H.pop_front();
}
H.begin()->l = -LLINF;
ll q = H.begin()->m * P[i].second + H.begin()->b;
ll v = q + sq(P[i].second) - lambda;
DP[i] = sq(P[i].second - P[0].first + 1) - lambda;
K[i] = 1;
if (DP[i] > v) {
DP[i] = v;
K[i] = H.begin()->k + 1;
}
}
return {DP[a - 1], K[a - 1]};
}
ll take_photos(int n, int m, int k, ve<int> R, ve<int> C) {
map<int, int> M;
for (int i = 0; i < n; i++) {
int r = R[i];
int c = C[i];
if (c < r) swap(r, c);
if (M.find(r) == M.end()) M[r] = c;
else M[r] = max(M[r], c);
}
int cOld = -1;
a = 0;
for (pii p : M) {
int r, c;
tie(r, c) = p;
if (c > cOld) {
cOld = c;
P.push_back({r, c});
a++;
}
}
L = ve<ll>(a);
for (int i = 1; i < a; i++) {
if (P[i - 1].second >= P[i].first) {
L[i] = sq(P[i - 1].second - P[i].first + 1);
}
}
ll lo = 0;
ll hi = (ll)1e9;
while (true) {
ll m = (lo + hi) / 2;
ll v, t;
tie(v, t) = calc(m);
if (t > min(a, k)) lo = m + 1;
else if (t < min(a, k)) hi = m - 1;
else return v + t * m;
}
}
# | 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... |