# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
122919 | baqargam | Wiring (IOI17_wiring) | 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>
#include "wiring.h"
using namespace std;
long long n,m,bs[200005],bn[200005],sum[200005],dp[200005],j;
vector<pair<long long,long long> >w;
/**/
long long cnt(long long l,long long r) {
long long m=bs[r];
long long res=(long long)(w[m].first-w[m-1].first)*max(r-m+1,m-l);
res+=w[m-1].first*(m-l);
res-=(sum[m-1]-sum[l-1]);
res+=sum[r]-sum[m-1];
res-=w[m].first*(r-m+1);
res+=dp[l-1];
return res;
}
void preprnt(){
for(long long i=1;i<=n+m;i++)
cout<<w[i].first<<" "; cout<<" position"<<endl;
for(long long i=1;i<=n+m;i++)
cout<<w[i].second<<" "; cout<<" color"<<endl;
for(long long i=1;i<=n+m;i++)
cout<<bn[i]<<" "; cout<<" block number"<<endl;
for(long long i=1;i<=n+m;i++)
cout<<bs[i]<<" "; cout<<" block start"<<endl;
for(long long i=1;i<=n+m;i++)
cout<<sum[i]<<" "; cout<<endl;
cout<<" ";
}
long long min_total_length(vector<long long> r, vector<long long> b) {
n=r.size();
m=b.size();
for(long long i=0;i<n;i++)
w.push_back({r[i],0});
for(long long i=0;i<m;i++)
w.push_back({b[i],1});
w.push_back({-100000,-100000});
sort(w.begin(),w.end());
for(long long i=1;i<=n+m;i++){
sum[i]=w[i].first;
bn[i]=bn[i-1];bs[i]=bs[i-1];
sum[i]+=sum[i-1];
if(w[i].second!=w[i-1].second || i==0)
{
bn[i]++;
bs[i]=i;
}
}
// preprnt();
//bn[0]=1;
for(long long i=1;i<=n+m;i++){
if(bn[i]==1)
{
dp[i]=1000000000000*i;
continue;
}
if(bn[i]!=bn[i-1])
j=i-1;
dp[i]=cnt(j,i);
while(cnt(j-1,i)<dp[i] && bn[j]==bn[i]-1){
j--;
dp[i]=cnt(j,i);
}
//cout<<dp[i]<<" "<<i<<" "<<j<<endl;
}
//cout<<endl;
return dp[n+m];
}