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 ll long long
const ll INF=1e12;
const ll MAXN=1e5+6;
struct line
{
ll a,b;
ll c;
ll eval(ll x)
{
return x*a+b;
}
ll operator+(const line& other)
{
if(a==other.a)assert(false);
return (other.b-b)/(a-other.a);
}
};
struct CHT
{
deque<line>dq;
bool bad(line ezdp)
{
if(dq.size()==1)return 0;
line l1=dq.end()[-2];
line l2=dq.end()[-1];
//(l1.b-ezdp.b)/(ezdp.a-l1.a)<(l1.b-l2.b)/(l2.a-l1.a)
// <=> (l1.b-ezdp.b)*(l2.a-l1.a)/(ezdp.a-l1.a)<(l1.b-l2.b)
return (l1.b-ezdp.b)*(l2.a-l1.a)<(l1.b-l2.b)*(ezdp.a-l1.a);
}
void push(line ezdp)
{
while(dq.size()>1&&bad(ezdp))
{
dq.pop_back();
}
dq.push_back(ezdp);
}
pair<ll,ll> query(ll x)
{
while(dq.size()>1&&(dq[0].eval(x)>dq[1].eval(x)||(dq[0].eval(x)==dq[1].eval(x)&&dq[0].c>dq[1].c)))
{
dq.pop_front();
}
//cout<<"&& "<<dq[0].a<<" "<<dq[0].b<<" "<<dq[0].c<<endl;
return {dq[0].eval(x),dq[0].c};
}
void init()
{
dq.clear();
}
}t;
struct poll
{
ll x,y;
bool operator<(poll const& other)const
{
return make_pair(x,-y)<make_pair(other.x,-other.y);
}
};
vector<poll >v;
vector<poll >aux;
pair<ll,ll> dp[MAXN];
pair<ll,ll> calc(vector<poll >v,ll pen)
{
t.init();
for(ll i=0;i<MAXN;i++)
{
dp[i]={INF,INF};
}
/*
const - x[i]*x[i]+pen+1+2*x[i]
a - -2*y[j+1]
b - max(x[j]-y[j+1]+1,0)*max(x[j]-y[j+1]+1,0)+dp[j]+y[j+1]*y[j+1]-2*y[j+1]
c - dp[j].second
*/
dp[0]={(v[0].x-v[0].y+1)*(v[0].x-v[0].y+1)+pen,1};
//cout<<"areve "<<max(v[0].x-v[1].y+1,0ll)*max(v[0].x-v[1].y+1,0ll)<<endl;
//cout<<max(v[0].x-v[1].y+1,0ll)*max(v[0].x-v[1].y+1,0ll)+dp[0].first+v[1].y*v[1].y-2*v[1].y<<" "<<-max(v[0].x-v[1].y+1,0ll)*max(v[0].x-v[1].y+1,0ll)+dp[0].first+v[1].y*v[1].y-2*v[1].y<<endl;
t.push({-2*v[1].y,-max(v[0].x-v[1].y+1,0ll)*max(v[0].x-v[1].y+1,0ll)+dp[0].first+v[1].y*v[1].y-2*v[1].y,1});
//cout<<"dp\n";
for(ll i=1;i<v.size();i++)
{
//cout<<"CHT is:\n";
/*for(auto xd:t.dq)
{
cout<<xd.a<<" "<<xd.b<<" "<<xd.c<<endl;
}
cout<<"--\n";*/
pair<ll,ll> gg=t.query(v[i].x);
dp[i]={v[i].x*v[i].x+pen+1+2*v[i].x+gg.first,gg.second+1};
dp[i]=min(dp[i],{(v[i].x-v[0].y+1)*(v[i].x-v[0].y+1)+pen,1});
if(i+1<v.size())t.push({-2*v[i+1].y,-max(v[i].x-v[i+1].y+1,0ll)*max(v[i].x-v[i+1].y+1,0ll)+dp[i].first+v[i+1].y*v[i+1].y-2*v[i+1].y,dp[i].second});
//cout<<dp[i].first<<" "<<dp[i].second<<endl;
}
//cout<<"----\n";
return dp[v.size()-1];
}
long long take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
for(ll i=0;i<r.size();i++)
{
if(r[i]<c[i])swap(r[i],c[i]);
aux.push_back({r[i],c[i]});
}
sort(aux.begin(),aux.end());
for(auto xd:aux)
{
while(v.size()>0&&v.back().y>=xd.y)
{
v.pop_back();
}
v.push_back(xd);
}
/*cout<<"v:\n";
for(auto xd:v)
{
cout<<xd.x<<" "<<xd.y<<endl;
}*/
//cout<<"bs:\n";
ll low=0,high=INF,mid,ans=-1;
while(low<=high)
{
mid=(low+high)/2;
ll t=calc(v,mid).second;
//cout<<mid<<" "<<t<<endl;
if(t<=k)
{
ans=mid;
high=mid-1;
}
else
{
low=mid+1;
}
}
//cout<<"------";
pair<ll,ll> f=calc(v,ans);
//cout<<f.first<<" "<<ans<<endl;
return f.first-k*ans;
return 0;
}
Compilation message (stderr)
aliens.cpp: In function 'std::pair<long long int, long long int> calc(std::vector<poll>, long long int)':
aliens.cpp:84:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<poll>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(ll i=1;i<v.size();i++)
| ~^~~~~~~~~
aliens.cpp:95:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<poll>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | if(i+1<v.size())t.push({-2*v[i+1].y,-max(v[i].x-v[i+1].y+1,0ll)*max(v[i].x-v[i+1].y+1,0ll)+dp[i].first+v[i+1].y*v[i+1].y-2*v[i+1].y,dp[i].second});
| ~~~^~~~~~~~~
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:102:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(ll i=0;i<r.size();i++)
| ~^~~~~~~~~
# | 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... |