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;
struct point
{
int ty;
long long pos;
point() {}
point(int a, long long b)
{
ty=a;
pos=b;
}
};
bool cmp(point p, point q)
{
return p.pos<q.pos;
}
int n;
point t[200005];
long long ans[200005];
long long sp[200005];
long long pref[200005];
long long ri_r[200005], ri_b[200005];
long long val[200005];
int id[800005];
void update(int v, int le, int ri, int pos, long long st)
{
if(ri<pos || le>pos) return;
if(le==ri)
{
val[le]=st;
id[v]=le;
}
else
{
int mid=(le+ri)/2;
update(2*v, le, mid, pos, st);
update(2*v+1, mid+1, ri, pos, st);
if(val[id[2*v]]<val[id[2*v+1]]) id[v]=id[2*v];
else id[v]=id[2*v+1];
}
}
int query(int v, int le, int ri, int be, int en)
{
if(ri<be || le>en) return -1;
if(be<=le && ri<=en) return id[v];
else
{
int mid=(le+ri)/2;
int id1=query(2*v, le, mid, be, en);
int id2=query(2*v+1, mid+1, ri, be, en);
if(id1==-1) return id2;
if(id2==-1) return id1;
if(val[id1]<val[id2]) return id1;
return id2;
}
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
n=r.size()+b.size();
for(int i=0; i<r.size(); i++) t[i+1]=point(1, r[i]);
for(int i=0; i<b.size(); i++) t[i+r.size()+1]=point(0, b[i]);
sort(t+1, t+n+1, cmp);
int idb=n+1, idr=n+1;
for(int i=1; i<=n; i++) pref[i]=pref[i-1]+t[i].pos;
for(int i=n; i>=1; i--)
{
int ty=t[i].ty;
if(ty==1)
{
if(idb==n+1) sp[i]=0;
else sp[i]=t[idb].pos-t[i].pos;
idr=i;
}
else
{
if(idr==n+1) sp[i]=0;
else sp[i]=t[idr].pos-t[i].pos;
idb=i;
}
}
for(int i=1; i<=n; i++) sp[i]+=sp[i-1];
for(int i=1; i<=n; i++) update(1, 0, n+2, i, 1e18);
update(1, 0, n+2, 0, 0);
update(1, 0, n+2, 1, 1e18);
ans[1]=1e18;
int le1=1, ri1=1;
while(ri1+1<=n && t[ri1+1].ty==t[ri1].ty)
{
ri1++;
ans[ri1]=1e18;
update(1, 0, n+2, ri1, 1e18);
}
//cout<<n<<endl;
for(int j=ri1+1; j<=n; j++)
{
/////cout<<j<<" ";
//cout<<j<<" c "<<le1<<" "<<ri1<<endl;
if(t[j].ty!=t[j-1].ty && j!=ri1+1)
{
le1=ri1+1;
ri1=j-1;
}
if(j-ri1>ri1-le1+1)
{
ans[j]=ans[j-1]+t[j].pos-t[ri1].pos;
/// //cout<<j<<" "<<ans[j]<<" "<<ans[j]-sp[j]<<endl;
update(1, 0, n+2, j, ans[j]-sp[j]);
}
else if(j-ri1==ri1-le1+1)
{
//cout<<j<<" tuka: ";
ans[j]=ans[j-1]+t[j].pos-t[ri1].pos;
ans[j]=min(ans[j], min(ans[le1-1]+pref[j]-2*pref[ri1]+pref[le1-1], ans[le1]+pref[j]-2*pref[ri1]+pref[le1-1]));
//cout<<j<<" "<<ans[j]<<" "<<ans[j]-sp[j]<<endl;
//cout<<ans[le1-1]+pref[j]-2*pref[ri1]+pref[le1-1]<<" "<<ans[le1]+pref[j]-2*pref[ri1]+pref[le1-1]<<endl;
update(1, 0, n+2, j, ans[j]-sp[j]);
}
else
{
ans[j]=ans[j-1]+t[j].pos-t[ri1].pos;
int tid=query(1, 0, n+2, le1-1, ri1-j+ri1);
//cout<<ans[j]<<" "<<tid<<endl;
long long calc2=ans[tid];
//cout<<calc2<<" ";
tid++;
calc2+=t[ri1+1].pos*(ri1-tid-(j-(ri1+1)));
//cout<<calc2<<" ";
calc2+=pref[j]-2*pref[ri1]+pref[tid-1];
//cout<<calc2<<endl;
if(calc2<ans[j]) ans[j]=calc2;
//cout<<j<<" "<<ans[j]<<" "<<ans[j]-sp[j]<<endl;
update(1, 0, n+2, j, ans[j]-sp[j]);
}
//cout<<ans[j]<<endl;
}
return ans[n];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:69:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i=0; i<r.size(); i++) t[i+1]=point(1, r[i]);
| ~^~~~~~~~~
wiring.cpp:70:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=0; i<b.size(); i++) t[i+r.size()+1]=point(0, b[i]);
| ~^~~~~~~~~
# | 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... |