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 fr(i,n) for(int i = 0; i<n; i++)
#define sz(v) (int)(v.size())
#define prin(a) cout << #a << " = " << a << endl
#define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl
#define all(v) (v).begin(),(v).end()
typedef long long ll;
#define rmin(a,b) a = min<ll>(a,b)
#define rmax(a,b) a = max<ll>(a,b)
#define fi first
#define se second
struct Line{
// y = m*x + k
ll m,k,qnt;
ll eval(ll x){
return m*x+k;
}
Line(){}
Line(ll _m, ll _k, ll _qnt){
m = _m, k = _k, qnt = _qnt;
}
};
//querys em ordem de x decrescente
//insercoes de linhas com m (slopes) em ordem decrescente
struct ChtDeque{
vector<Line> dq;
int l, r; // [l,r)
ChtDeque(){}
ChtDeque(int n){ //n eh o maximo de linhas a serem inseridas
n+=5;
dq.resize(n+5);
l = r = n;
}
ll div(ll a, ll b){
return a/b - ( (a^b)<0 and a%b);
}
bool tirar(Line &l1, Line &l2, Line &l3){
if(l1.m==l2.m){
return l1.k>=l2.k;
}
return div(l2.k-l3.k,l3.m-l2.m)<=div(l1.k-l2.k,l2.m-l1.m);
}
void add(ll m, ll k, ll qnt){
Line line(m,k,qnt);
while(r-l>=2){
if(tirar(line,dq[l],dq[l+1])) l++;
else break;
}
if(l<r and dq[l].m==m){
if(k>dq[l].k) dq[l] = line;
return;
}
dq[--l] = line;
}
pair<ll,ll> query(ll x){
assert(r>l);
while(r-l>=2){
if(dq[l].eval(x)<=dq[l+1].eval(x)) l++;
else break;
}
return {dq[l].eval(x),dq[l].qnt};
}
};
vector<pair<ll,ll>> vp;
//ans min, k used
pair<ll,ll> f(ll c){
ChtDeque cht(sz(vp)+1);
ll udp = 0, uqnt = 0;
fr(i,sz(vp)){
ll ri,ci; tie(ri,ci) = vp[i];
ci++;
cht.add(-2*ri,ri*ri+udp,uqnt);
ll dp, qnt; tie(dp,qnt) = cht.query(ci);
dp+=ci*ci+c;
qnt++;
udp = dp, uqnt = qnt;
}
return {udp,uqnt};
}
ll take_photos(int n, int m, int k, vector<int> vr, vector<int> vc){
vp.clear();
fr(i,n){
if(vc[i]<vr[i]) swap(vc[i],vr[i]);
vp.emplace_back(vr[i],vc[i]);
}
{
sort(all(vp));
reverse(all(vp));
vector<pair<ll,ll>> aux;
for(auto &[i,j] : vp){
if(!aux.empty() and aux.back().fi==i) continue;
while(!aux.empty() and j>=aux.back().se) aux.pop_back();
aux.emplace_back(i,j);
}
reverse(all(aux));
vp = aux;
}
/*
for(auto &[i,j] : vp){
prin(i);
prin(j);
cout << endl;
}
*/
ll lo = 0, hi = m*m;
while(lo<hi){
ll mid = (lo+hi+1)/2;
if(f(mid).se<=k) lo = mid;
else hi = mid-1;
}
//prin(lo);
auto par = f(lo);
return par.fi-par.se*lo;
}
# | 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... |