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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m,k;
vector<ii> v;
ll extra[100005];
ll sq(ll i){return i*i;}
struct line{
ll m,c,g;
ii eval(ll x){
return ii(m*x+c,g);
}
};
struct hull{
deque<line> dq;
double POI(line i,line j){
return (long double)(i.c-j.c)/(j.m-i.m);
}
void add(line i){
while (sz(dq)>=2 && POI(dq.back(),i)<=POI(dq[sz(dq)-2],dq.back())) dq.pop_back();
dq.push_back(i);
}
ii query(ll i){
while (sz(dq)>=2 && dq[1].eval(i).fi<dq[0].eval(i).fi) dq.pop_front();
return dq.front().eval(i);
}
};
ii param(ll lambda){
hull h;
ii val=ii(0,0);
rep(x,0,sz(v)){
h.add({-2*v[x].fi,val.fi-extra[x]+sq(v[x].fi)+lambda,val.se});
val=h.query(v[x].se);
val.se++;
val.fi+=sq(v[x].se);
}
return val;
}
long long take_photos(int N, int M, int K, std::vector<int> R, std::vector<int> C) {
n=N,m=M,k=K;
rep(x,0,n){
int a=R[x],b=C[x];
if (a>b) swap(a,b);
v.push_back(ii(a,b+1));
}
sort(all(v),[](ii i,ii j){
if (i.fi!=j.fi) return i.fi<j.fi;
else return i.se>j.se;
});
vector<ii> temp;
for (auto &it:v){
if (temp.empty() || temp.back().se<it.se) temp.push_back(it);
}
swap(v,temp);
//for (auto &it:v) cout<<it.fi<<" "<<it.se<<endl;
rep(x,1,sz(v)) extra[x]=sq(max(0LL,v[x-1].se-v[x].fi));
//rep(x,0,sz(v)) cout<<extra[x]<<" "; cout<<endl;
k=min(k,sz(v));
ll lo=0,hi=1e12,mi;
//lo>=k, k>hi
while (hi-lo>1){
mi=hi+lo>>1;
if (param(mi).se>=k) lo=mi;
else hi=mi;
}
auto val=param(lo);
return val.fi-val.se*lo+(val.se-k)*hi;
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:106:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
106 | mi=hi+lo>>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... |