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>
#define fi first
#define se second
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef vector<int> vim;
typedef vector<ll> vlm;
int n, m;
pii im[100010]; vim chk(100010);
vector<pll> ar;
ll sq(ll i) {return i*i;}
pll stk[100010]; ll cnt[100010]; int fr, tp;
ll lin(ll i, ll x) { return stk[i].fi*x+stk[i].se;}
pll cal(ll x) {
while (fr<=tp-2 && lin(fr, x)>=lin(fr+1, x)) fr++;
return {lin(fr, x), cnt[fr]};
}
void add(ll a, ll b, ll c) {
while (fr<=tp-2 && (stk[tp-1].fi-a)*(stk[tp-1].se-stk[tp-2].se) > (stk[tp-2].fi-stk[tp-1].fi)*(b-stk[tp-1].se)) tp--;
stk[tp]={a, b};
cnt[tp]=c;
tp++;
}
pll f(ll cost) {
fr=tp=0;
vlm Dp(n+1), c(n+1);
add(-2*(ar[1].se-1), sq(ar[1].se-1)+cost, 1);
Dp[1]=cal(ar[1].fi).fi+sq(ar[1].fi);
c[1]=1;
pll pl;
for (int i=2; i<=n; i++) {
add(-2*(ar[i].se-1), sq(ar[i].se-1)-sq( max(0ll, (ar[i-1].fi-ar[i].se+1)) )+Dp[i-1]+cost, c[i-1]+1);
pl=cal(ar[i].fi);
Dp[i]=sq(ar[i].fi)+pl.fi, c[i]=pl.se;
}
return {c[n], Dp[n]-c[n]*cost};
}
ll take_photos(int n_, int m_, int k, vim r, vim c) { //(r-c+1)^2
n=n_; m=m_;
for (int i=0; i<n; i++) r[i]++, c[i]++;
for (int i=0; i<n; i++) {if (r[i]<c[i]) swap(r[i], c[i]);}
for (int i=0; i<n; i++) im[i]={r[i],c[i]};
sort(im, im+n, [](pii pi1, pii pi2){return pi1.fi==pi2.fi?pi1.se<pi2.se:pi1.fi>pi2.fi;});
int mn=m+1;
ar.push_back({0,0});
for (int i=0; i<n; i++) {
if (mn<=im[i].se) continue;
ar.push_back({(ll)im[i].fi, (ll)im[i].se});
mn=min(mn, im[i].se);
}
n=ar.size()-1;
sort(ar.begin(), ar.end());
ll s=0, e=((ll)m*m);
ll ret;
ll x1, x2, y1, y2;
x1=y1=y2=0; x2=e;
k=min(k, n);
while (s<=e) {
ll md=(s+e)/2;
pll pl=f(md);
if (pl.fi==k) return pl.se;
else if (pl.fi<k) {
e=md-1;
if (x1<pl.fi) x1=pl.fi, y1=pl.se;
}
else {
s=md+1;
if (x2>pl.fi) x2=pl.fi, y2=pl.se;
}
}
return y2+(y1-y2)/(x2-x1)*(x2-k);
}
Compilation message (stderr)
aliens.cpp: In function 'll take_photos(int, int, int, vim, vim)':
aliens.cpp:70:5: warning: unused variable 'ret' [-Wunused-variable]
ll ret;
^~~
# | 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... |