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<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>Line;
ll eval(Line l,ll x){
return l.first*x+l.second;
}
struct CHT{
int l,r;
CHT*ch1,*ch2;
Line cl;
CHT(int l,int r):l(l),r(r),ch1(nullptr),ch2(nullptr){
cl={0,1e18};
}
void add(Line line){
int mid=(l+r)/2;
if(eval(line,mid)<eval(cl,mid))
swap(line,cl);
if(l<r-1){
if(eval(line,l)<eval(cl,l)){
if(ch1==nullptr)
ch1=new CHT(l,mid);
ch1->add(line);
}
if(eval(line,r)<eval(cl,r)){
if(ch2==nullptr)
ch2=new CHT(mid,r);
ch2->add(line);
}
}
}
ll get(ll x){
int mid=(l+r)/2;
ll res=eval(cl,x);
//cout<<"res = "<<res<<"\n";
//cout<<"line = "<<cl.first<<" "<<cl.second<<"\n";
if(x>=mid&&ch2)
res=min(res,ch2->get(x));
if(x<mid&&ch1)
res=min(res,ch1->get(x));
//cout<<"res2 = "<<res<<"\n";
return res;
}
};
int n;
vector<ll>l,r;
ll dp[5000][5000];
ll sq(ll x){
return x*x;
}
bool cmp(pair<int,int>a,pair<int,int>b){
if(a.first==b.first)
return a.second>b.second;
return a.first<b.first;
}
ll take_photos(int N,int m,int k,vector<int>R,vector<int>C){
n=N;
vector<pair<int,int>>intervals_sorted;
for(int i=0;i<n;i++){
if(R[i]>C[i])
swap(R[i],C[i]);
intervals_sorted.emplace_back(R[i],C[i]);
}
sort(intervals_sorted.begin(),intervals_sorted.end(),cmp);
int right_done=-1;
for(auto i:intervals_sorted){
if(i.second>right_done){
l.push_back(i.first);
r.push_back(i.second+1);
right_done=i.second;
}
}
n=(int)l.size();
// cheapest way to cover first i segments with j squares
for(int i=1;i<=n;i++){
dp[i][0]=1e12;
}
for(int j=1;j<=k;j++){
CHT cht(0,1<<20);
for(int i=0;i<=n;i++){
dp[i][j]=dp[i][j-1];
//cout<<"get "<<cht.get(r[i-1])<<"\n";
dp[i][j]=min(dp[i][j-1],cht.get(r[i-1])+r[i-1]*r[i-1]);
if(i!=n){
pair<ll,ll>el;
el.first=-2*l[i];
el.second=l[i]*l[i]+dp[i][j-1];
if(i&&r[i-1]>l[i])
el.second-=sq(r[i-1]-l[i]);
cht.add(el);
}
}
}
return dp[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... |