# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
680489 | ngrace | Aliens (IOI16_aliens) | C++14 | 0 ms | 0 KiB |
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 v vector
#define ll long long
#define ii pair<long long,long long>
#define fi first
#define se second
v<ii> ph;
ll C(int i, int j){ // i>j, j is exclusive.
ll len = ph[i].se - ph[j+1].fi + 1;
ll intersect = max(0LL, ph[j].se - ph[j+1].fi + 1);
return len*len - intersect*intersect;
}
v<ll> dplast, dpcur;
void comp(int l, int r, int optl, int optr){
if(l>r) return;
int m=(l+r)/2;
dpcur[m] = 1e18;
int opt;
for(int i=optl; i<=min(m-1, optr); i++){
ll cur = dplast[i] + C(m,i);
if(cur<dpcur[m]){
dpcur[m]=cur;
opt=i;
}
}
comp(l,m-1,optl,opt);
comp(m+1,r,opt,optr);
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
set<pii> tmp;
for(int i=0;i<n;i++){
tmp.insert({min(r[i],c[i]),-max(c[i],r[i])});
} cin.tie(0);
ph.push_back({-1,-1});
for(ii it:tmp){
if(ph.back().se < -it.se) ph.push_back({it.fi, -it.se});
}
n=ph.size()-1;
if(n<=4000){//knuths
v<v<int>> opt(k+1, v<int>(n+2, 0));
for(int h=0; h<=k; h++) opt[h][n+1]=n;
v<v<ll>> dp(k+1, v<ll>(n+1, 1e18));
for(int h=0; h<=k; h++) dp[h][0] = 0;
for(int h=1; h<=k; h++){
for(int i=n; i>0; i--){
for(int j=opt[h-1][i]; j<=min(i-1, opt[h][i+1]); j++){
ll cost = dp[h-1][j] + C(i,j);
if(cost<dp[h][i]){
dp[h][i] = cost;
opt[h][i]=j;
}
}
}
}
cout<<dp[k][n]<<endl;
}
else{//d&c
dplast = v<ll>(n+2,0);
dpcur = v<ll>(n+2,0);
for(int i=1; i<=n; i++) dplast[i] = C(i,0);
for(int h=2; h<=k; h++){
comp(1, n, 0, n);
dplast = dpcur;
}
cout<<dplast[n]<<endl;
}
}