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 all(x) begin(x),end(x)
#define sz(x) int(x.size())
#define forn(i,n) for(int i=0;i<int(n);i++)
using vi=vector<int>;
using ll=long long;
using ld=long double;
using ii=pair<int,int>;
#define fst first
#define snd second
struct Line {
ll m, b;
int k;
ll operator()(ll x) {
return m*x+b;
}
ld intersectX(Line &o) {
return ld(b-o.b)/(o.m-m);
}
};
struct LineContainer:deque<Line>{
void add(ll m, ll b, int k){
Line l=Line{m, b, k};
while(size()>=2&&l.intersectX(begin()[0])<=l.intersectX(begin()[1])) pop_front();
push_front(l);
}
pair<ll,int> query(ll x){
while(size()>=2&&end()[-1](x)>=end()[-2](x)) pop_back();
return {end()[-1](x),end()[-1].k};
}
};
vector<ii> p;
ll sq(ll x){
return x*x;
}
pair<ll,int> check(ll lmd){
LineContainer dp;
dp.add(-2*(p[0].snd-1),sq(p[0].snd-1),0);
ll res=0;
int cnt=0;
forn(i,sz(p)){
tie(res,cnt)=dp.query(p[i].fst);
res+=sq(p[i].fst)+lmd,cnt++;
if(i!=sz(p)-1) dp.add(-2*(p[i+1].snd-1),res-sq(max(p[i].fst-p[i+1].snd+1,0))+sq(p[i+1].snd-1),cnt);
}
return {res,cnt};
}
ll take_photos(int n, int /*m*/, int k, vi r, vi c){
vector<ii> v(n);
forn(i,n){
if(r[i]>c[i]) v[i]={r[i],c[i]};
else v[i]={c[i],r[i]};
}
sort(all(v));
forn(i,n){
while(!p.empty()&&p.back().snd>=v[i].snd) p.pop_back();
p.push_back(v[i]);
}
ll lo=0, hi=1e18;
ll ans=0;
while(hi-lo>1){
ll mid=(lo+hi)/2;
auto [res,cnt]=check(mid);
if(cnt<=k){
hi=mid;
ans=res-cnt*mid;
}else{
lo=mid;
}
}
return ans;
}
Compilation message (stderr)
aliens.cpp: In function 'll take_photos(int, int, int, vi, vi)':
aliens.cpp:73:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | auto [res,cnt]=check(mid);
| ^
# | 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... |