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<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
#define ll long long
#define tili tuple<int,ll,int>
const int mxn=1e5+5;
vector<pair<int,int>>p;
int n,m;
pair<ll,int> calc(tili f,int x){
return {1LL*get<0>(f)*x+get<1>(f),get<2>(f)+1};
}
bool ck(tili l1,tili l2,tili l){
auto [m1,b1,c1]=l1;
auto [m2,b2,c2]=l2;
auto [m,b,c]=l;
//l1*l2<l2*l
//(b1-b2)/(m2-m1)>(b1-b)/(m-m1)
//(b1-b2)*(m-m1)>=(b1-b)*(m2-m1)
return make_pair(1LL*(b1-b)*(m2-m1),c)<=make_pair(1LL*(b1-b2)*(m-m1),c2);
}
pair<ll,int> solve(ll cost){
deque<tili>st;
st.pb({-2*p[1].F,1LL*p[1].F*p[1].F,0});
pair<ll,int>dp;
for(int i=1;i<=n;i++){
while(sz(st)>1 and calc(st[0],p[i].S)>=calc(st[1],p[i].S)){
st.pop_front();
}
dp=calc(st.front(),p[i].S);
dp.F+=1LL*p[i].S*p[i].S+cost;
if(i==n)
break;
int d=max(0,p[i].S-p[i+1].F);
tili l={-2*p[i+1].F,dp.F+1LL*p[i+1].F*p[i+1].F-1LL*d*d,dp.S};
while(sz(st)>1 and ck(st.end()[-2],st.back(),l)){
st.pop_back();
}
st.pb(l);
}
return dp;
}
bool cmp(pair<int,int>a,pair<int,int>b){
return (a.F==b.F?a.S>b.S:a.F<b.F);
}
ll take_photos(int N,int M,int k,vector<int>R,vector<int>c){
n=N;
m=M;
vector<pii>v;
for(int i=0;i<n;i++){
if(c[i]<R[i])swap(R[i],c[i]);
v.pb({R[i],c[i]+1});
}
sort(all(v),cmp);
p.pb({-1,-1});
for(int i=0;i<n;i++){
if(p.empty() or v[i].S>p.back().S)p.pb(v[i]);
}
n=sz(p)-1;
ll l=0,r=1LL*m*m;
while(l<r){
ll mm=l+r>>1;
pair<ll,int> x=solve(mm);
if(x.S>k){
l=mm+1;
}
else{
r=mm;
}
}
return solve(l).F-1LL*l*k;
}
#ifdef local
signed main(){
freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);
int n,m,k;
cin>>n>>m>>k;
vector<int> r(n),c(n);
for(int i=0;i<n;i++){
cin>>r[i]>>c[i];
}
cout<<take_photos(n,m,k,r,c)<<'\n';
}
#endif
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:74:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
74 | ll mm=l+r>>1;
| ~^~
# | 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... |