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"
using namespace std;
using ll = long long;
const ll inf = 2e18;
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
LineContainer cht;
void ins(ll k, ll m) {
cht.add(-k, -m);
}
ll que(ll x) {
return -cht.query(x);
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
for(int i = 0;i<n;i++) {
if(r[i] > c[i]) {
swap(r[i], c[i]);
}
}
vector<pair<long long,long long>> v;
{
vector<pair<long long, long long>> d;
for(int i = 0;i<n;i++)d.push_back(make_pair(r[i], -c[i]-1));
sort(d.begin(), d.end());
for(int i = 0;i<n;i++)d[i].second*=-1;
long long mxr = -inf;
for(int i = 0; i<n; i++) {
if(d[i].second > mxr) {
v.push_back(make_pair(d[i].second, d[i].first));
mxr = d[i].second;
}
}
}
sort(v.begin(), v.end());
n = v.size();
vector<long long> dp[2];
for(int i = 0;i<2;i++)dp[i].resize(n+1);
for(int i = 0;i<=n;i++)dp[0][i] = inf;
dp[0][0] = 0;
for(int p = 1;p<=k;p++) {
int j = 1;
cht.clear();
for(int i = 1;i<=n;i++) {
dp[j][i] = inf;
long long f = -2*v[i-1].second;
long long s = v[i-1].second*v[i-1].second+dp[j-1][i-1];
if(i!=1 and v[i-2].first > v[i-1].second) {
s-=(v[i-2].first-v[i-1].second)*(v[i-2].first-v[i-1].second);
}
ins(f, s);
dp[j][i] = que(v[i-1].first) + v[i-1].first*v[i-1].first;
}
swap(dp[1], dp[0]);
}
return dp[0][n];
}
# | 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... |