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 "wiring.h"
#include <bits/stdc++.h>
#define sz(a) a.size()
using namespace std;
typedef long long int ll;
const ll INF = 1e16;
const int MAXN = 2e5+5;
ll pre[MAXN],suff[MAXN]; //pre and suff min
ll min_total_length(vector<int> R,vector<int> B) {
vector<array<int,2>>a;
for(int x:R)a.push_back({x,0});
for(int x:B)a.push_back({x,1});
sort(a.begin(),a.end());
vector<vector<int>>b;
int n = sz(a);
for(int i=0;i<n;i++){
int j = i;
b.push_back({a[i][0]});
while(j+1<n && a[j][1] == a[j+1][1]){
j++;
b.back().push_back(a[j][0]);
}
i = j;
}
vector<int>s;
for(auto x:b)s.push_back(sz(x));
for(int i=0;i<=n;i++)suff[i] = pre[i] = INF;
pre[s[0]] = suff[s[0]] = 0;
for(int i=0;i<s[0];i++){
pre[s[0]] += b[0].back() - b[0][i];
suff[s[0]] += b[0].back() - b[0][i];
}
//compute prefix and suffix min
suff[s[0]] += 1LL * s[0] * (b[1][0] - b[0].back()); //distance in between
int t = max(s[0],s[1]);
for(int i=1;i<=t;i++)pre[i] = min(pre[i],pre[i-1]);
for(int i=t-1;i>=0;i--)suff[i] = min(suff[i],suff[i+1]);
/*
for(auto x:b){
for(int y:x)cout<<y<<" ";
cout<<'\n';
}*/
for(int i=1;i<sz(s);i++){
vector<ll>tmp(s[i]+1,0);
//tmp[i] = min cost if i guys from this batch is assigned to the left
ll cur = 0;
for(int j=0;j<=s[i];j++){
if(j)cur += b[i][j-1] - b[i][0];
ll x = pre[j] + 1LL * j * (b[i][0] - b[i-1].back()); //distance in between
ll y = suff[j];
tmp[j] = cur + min(x,y);
}
cur = 0;
reverse(tmp.begin(),tmp.end());
for(int j=0;j<=s[i];j++){
if(j)cur += b[i].back() - b[i][s[i]-j];
tmp[j] += cur;
}
/*
cout<<"at "<<i<<'\n';
for(ll x:tmp)cout<<x<<" ";
cout<<'\n';*/
//tmp[i] = min cost if i guys assigned to the right
if(i == sz(s)-1)return tmp[0];
int t = max(s[i],s[i+1]);
for(int j=0;j<=t;j++)pre[j] = suff[j] = INF;
for(int j=0;j<=s[i];j++){
pre[j] = suff[j] = tmp[j];
suff[j] += 1LL * j * (b[i+1][0] - b[i].back());
}
for(int j=1;j<=t;j++)pre[j] = min(pre[j],pre[j-1]);
for(int j=t-1;j>=0;j--)suff[j] = min(suff[j],suff[j+1]);
}
return 0;
}
Compilation message (stderr)
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:47:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=1;i<sz(s);i++){
| ^
wiring.cpp:69:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if(i == sz(s)-1)return tmp[0];
| ~~^~~~~~~~~~
# | 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... |