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 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;
}
// (points, num of pictures) if pictures are unlimited but have a specific cost
// num of picures will be minimal while still having optimal points
pair<ll,int>get(int cost){
vector<ll>dp(n+1); // optimum for first i intervals
vector<int>num_pic(n+1);
for(int i=1;i<=n;i++){
dp[i]=1e18;
for(int j=0;j<i;j++){
ll curr=sq(r[i-1]-l[j]);
curr+=dp[j];
curr+=cost;
if(j)
curr-=sq(max(0LL,r[j-1]-l[j]));
if(curr<dp[i]){
dp[i]=curr;
num_pic[i]=num_pic[j]+1;
}else if(curr==dp[i]){
num_pic[i]=min(num_pic[i],num_pic[j]+1);
}
}
}
return{dp[n],num_pic[n]};
}
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();
int l=0,r=m*m;
while(l<r-1){
int mid=(l+r)/2;
auto res=get(mid);
//cout<<mid<<": "<<res.first<<" "<<res.second<<"\n";
if(res.second>k)
l=mid;
else
r=mid;
}
auto res=get(r);
return res.first-r*res.second;
}
# | 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... |