# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
680486 | ngrace | Aliens (IOI16_aliens) | C++17 | 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);
}
int main(){
cin.tie(0);
iostream::sync_with_stdio(0);
ll n,m,K;
cin>>n>>m>>K;//n points, mxm grid, k photo's allowed
set<ii> tmp;
//min photo needed per point represented as min(a,b), max(a,b) points on diagonal.
for(int i=0; i<n; i++){
int a,b;
cin>>a>>b;
tmp.insert({min(a,b),-max(a,b)});
}
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 k=0; k<=K; k++) opt[k][n+1]=n;
v<v<ll>> dp(K+1, v<ll>(n+1, 1e18));
for(int k=0; k<=K; k++) dp[k][0] = 0;
for(int k=1; k<=K; k++){
for(int i=n; i>0; i--){
for(int j=opt[k-1][i]; j<=min(i-1, opt[k][i+1]); j++){
ll cost = dp[k-1][j] + C(i,j);
if(cost<dp[k][i]){
dp[k][i] = cost;
opt[k][i]=j;
}
}
}
}
cout<<dp[K][n]<<endl;
}
else if(k<=100){//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 k=2; k<=K; k++){
comp(1, n, 0, n);
dplast = dpcur;
}
cout<<dplast[n]<<endl;
}
else{
}
}