# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
618160 | rrrr10000 | 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;
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pb emplace_back
template<class T> void out(T a){cout<<a<<endl;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
const ll inf=1001001001001001001;
long long take_photos1(int n, int m, int K, std::vector<int> r, std::vector<int> c) {
vp srt(n);
rep(i,n)srt[i]=P(max(r[i],c[i]),min(r[i],c[i]));
sort(all(srt));
vp v;v.pb(-1,-1);
for(auto x:srt){
while(v.size()&&v.back().fi>=x.se)v.pop_back();
if(v.size()==0||v.back().se<x.fi)v.pb(x.se,x.fi);
}
n=v.size();
vvi dp(n,vi(K+1,inf));
dp[0][0]=0;
rep(i,n)rep(j,K)if(dp[i][j]!=inf){
chmin(dp[i][j+1],dp[i][j]);
REP(t,i+1,n)chmin(dp[t][j+1],dp[i][j]+(v[t].se-v[i+1].fi+1)*(v[t].se-v[i+1].fi+1)-max(0ll,v[i].se-v[i+1].fi+1)*max(0ll,v[i].se-v[i+1].fi+1));
}
return dp[n-1][K];
}
struct CHT{
deque<P> deq;
void insert(ll a,ll b){
while(deq.size()>1){
long double a1,b1,a2,b2;tie(a1,b1)=deq[deq.size()-2];tie(a2,b2)=deq.back();
long double x=(b1-b)/(a-a1);
if(a2*x+b2<a*x+b)break;
deq.pop_back();
}
deq.pb(a,b);
}
ll sol(ll x){
if(!deq.size())return inf;
while(deq.size()>1){
if(deq[0].fi*x+deq[0].se<deq[1].fi*x+deq[1].se)break;
deq.pop_front();
}
return deq[0].fi*x+deq[0].se;
}
};
struct CHT_{
deque<PP> deq;
void insert(ll id,ll a,ll b){
ll tmp;
while(deq.size()>1){
long double a1,b1,a2,b2;tie(a1,b1,tmp)=deq[deq.size()-2];tie(a2,b2,tmp)=deq.back();
long double x=(b1-b)/(a-a1);
if(a2*x+b2<a*x+b)break;
deq.pop_back();
}
deq.pb(a,b,id);
}
P sol(ll x){
while(deq.size()>1){
if(get<0>(deq[0])*x+get<1>(deq[0])<get<0>(deq[1])*x+get<1>(deq[1]))break;
deq.pop_front();
}
return P(get<0>(deq[0])*x+get<1>(deq[0]),get<2>(deq[0]));
}
};
long long take_photos2(int n, int m, int K, std::vector<int> r, std::vector<int> c) {
vp srt(n);
rep(i,n)srt[i]=P(max(r[i],c[i]),min(r[i],c[i]));
sort(all(srt));
vp v;v.pb(-1,-1);
for(auto x:srt){
while(v.size()&&v.back().fi>=x.se)v.pop_back();
if(v.size()==0||v.back().se<x.fi)v.pb(x.se,x.fi);
}
n=v.size();chmin(K,n-1);
vvi dp(K+1,vi(n,inf));
dp[0][0]=0;
rep(k,K){
CHT cht;
rep(i,n){
dp[k+1][i]=cht.sol(v[i].se)+v[i].se*v[i].se;
if(i<n-1)cht.insert(2*(-v[i+1].fi+1),dp[k][i]+(-v[i+1].fi+1)*(-v[i+1].fi+1)-max(0ll,v[i].se-v[i+1].fi+1)*max(0ll,v[i].se-v[i+1].fi+1));
}
}
// for(auto x:v)cout<<x.fi<<' '<<x.se<<endl;
// for(auto x:dp)outv(x);
return dp[K][n-1];
}
long long take_photos(int n, int m, int K, std::vector<int> r, std::vector<int> c) {
vp srt(n);
rep(i,n)srt[i]=P(max(r[i],c[i]),min(r[i],c[i]));
sort(all(srt));
vp v;v.pb(-1,-1);
for(auto x:srt){
while(v.size()&&v.back().fi>=x.se)v.pop_back();
if(v.size()==0||v.back().se<x.fi)v.pb(x.se,x.fi);
}
n=v.size();chmin(K,n-1);
auto sol=[&](ll lambda){
vp dp(n);
CHT_ cht;
dp[0]=P(0,0);
rep(i,n){
if(i){
auto res=cht.sol(v[i].se);
dp[i].fi=res.fi+v[i].se*v[i].se+lambda;
dp[i].se=dp[res.se].se+1;
}
if(i<n-1)cht.insert(i,2*(-v[i+1].fi+1),dp[i].fi+(-v[i+1].fi+1)*(-v[i+1].fi+1)-max(0ll,v[i].se-v[i+1].fi+1)*max(0ll,v[i].se-v[i+1].fi+1));
}
return dp[n-1];
};
if(K==n-1)return sol(0);
ll ok=m*m+5,ng=-1;
while(ok-ng>1){
ll md=(ok+ng)/2;
auto res=sol(md);
if(res.se<=K)ok=md;
else ng=md;
}
P r1=sol(ok),r2=sol(ng);
r1.fi-=r1.se*ok;r2.fi-=r2.se*ng;
// cout<<ok<<' '<<r1.fi<<' '<<r1.se<<endl;
// cout<<ng<<' '<<r2.fi<<' '<<r2.se<<endl;
if(r1.se+r1.fi-(ng*(r2.se-r1.se)+r2.fi)>=K)return r1.fi-ok*(K-r1.se);
return r2.fi+ng*(r2.se-K);
}
int main(){
int n=8,m=10,K=3;
vector<int> a={1,9,8,8,5,4,0,7},b={7,4,9,3,8,9,6,3};
out(take_photos(n,m,K,a,b));
out(take_photos1(n,m,K,a,b));
out(take_photos2(n,m,K,a,b));
// return 0;
rep(tt,100000){
int n=rand()%1000+1,m=rand()%1000+1,K=rand()%n+1;
vector<int> a(n),b(n);
rep(i,n)a[i]=rand()%m;
rep(i,n)b[i]=rand()%m;
ll r1=take_photos2(n,m,K,a,b);
ll r2=take_photos(n,m,K,a,b);
if(r1!=r2){
out(tt);
out(n);out(m);out(K);
outv(a);outv(b);
out(r1);out(r2);break;
}
}
}