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;
struct CHT{
vector<pair<ll,ll>>lines;
void add(pair<ll,ll>line){
lines.push_back(line);
}
ll get(ll x){
ll res=1e18;
for(auto line:lines){
res=min(res,line.first*x+line.second);
}
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;
for(int i=0;i<=n;i++){
dp[i][j]=dp[i][j-1];
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);
}
/*for(int i2=0;i2<i;i2++){
ll curr=r[i-1]*r[i-1]-2*l[i2]*r[i-1]+l[i2]*l[i2]+dp[i2][j-1];
if(i2&&r[i2-1]>l[i2]){
curr-=sq(r[i2-1]-l[i2]);
}
dp[i][j]=min(dp[i][j],curr);
}*/
}
}
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... |