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;
#define int long long
const int inf=1e18;
struct Line{
mutable int k, m, p;
bool operator<(const Line &a) const { return k<a.k; }
bool operator<(int x) const { return p<x; }
};
struct LineContainer: multiset<Line, less<>>{
int div(int x, int y){
return x/y-((x^y)<0 && (x%y));
}
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(int k, int m){
k=-k, m=-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, erase(y));
while ((y=x)!=begin() && (--x)->p>=y->p) isect(x, erase(y));
}
int query(int x){
auto l=*lower_bound(x);
return (-l.k)*x+(-l.m);
}
};
long long take_photos(int32_t n, int32_t m, int32_t k, vector<int32_t> r, vector<int32_t> c) {
vector<pair<int, int>> v;
for (int i=0; i<n; ++i){
v.emplace_back(min(r[i], c[i]), max(r[i], c[i]));
}
sort(v.begin(), v.end());
vector<pair<int, int>> v2;
for (int i=0; i<(int)v.size(); ++i){
int j=i;
while (j+1<(int)v.size() && v[j+1].first==v[i].first) ++j;
if (v2.empty() || v2.back().second<v[j].second) v2.push_back(v[j]);
i=j;
}
n=v2.size();
for (auto &i:v2) ++i.second;
vector<vector<int>> f(n+1, vector<int>(k+1, inf));
vector<LineContainer> cht(k);
f[0][0]=0;
for (int i=1; i<=n; ++i){
int x=-2*v2[i-1].first, y=v2[i-1].first*v2[i-1].first;
if (i!=1 && v2[i-2].second>v2[i-1].first) y-=(v2[i-2].second-v2[i-1].first)*(v2[i-2].second-v2[i-1].first);
for (int j=1; j<=k; ++j){
if (f[i-1][j-1]!=inf) cht[j-1].add(x, y+f[i-1][j-1]);
if (cht[j-1].size()) f[i][j]=min(f[i][j], v2[i-1].second*v2[i-1].second+cht[j-1].query(v2[i-1].second));
}
}
return *min_element(f[n].begin(), f[n].end());
}
# | 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... |