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>
#define fi first
#define se second
#define NAME "wiring"
using namespace std;
typedef long long LL;
typedef pair <int, int> pii;
typedef double DB;
typedef pair <LL, LL> pLL;
const int NMAX = 1e6+7;
int64_t min_total_length(vector <int> r, vector <int> b) {
int64_t res = 0;
int n = r.size();
int m = b.size();
if(r[n-1] < b[0]) {
for(int i = 0; i < n; ++i)
res += abs(b[0] - r[i]);
for(int i = 0; i < m; ++i)
res += abs(r[n-1] - b[i]);
return res;
}
vector <pii> a;
for(int i = 0; i < n; ++i) {
a.push_back(pii(r[i], i));
}
for(int i = 0; i < m; ++i) {
a.push_back(pii(b[i], i));
}
int nm = n + m;
sort(a.begin(), a.end());
int i = 0, j = 0, ck = 0;
while(i < nm) {
while(j+1 < nm && a[j+1].se == a[i].se) j++;
int pos1, pos2;
if(i > 0) pos1 = a[i-1].fi;
else pos1 = INT_MAX;
if(j < nm-1) pos2 = a[j+1].fi;
else pos2 = INT_MAX;
for(int t = i; t <= j; ++t)
res += min(abs(pos1 - a[t].fi), abs(pos2 - a[t].fi));
if(i > 0 && ck == 1)
res -= abs(pos1 - a[i].fi);
if(abs(pos1 - a[j].fi) > abs(pos2 - a[j].fi)) ck = 1;
else ck = 0;
j++; i = j;
}
return res;
}
int n, m;
vector <int> ver, veb;
/*int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
freopen(NAME".inp", "r", stdin);
//freopen(NAME".out", "w", stdout);
cin >> n >> m;
int x;
for(int i = 0; i < n; ++i) {
cin >> x;
ver.push_back(x);
}
for(int i = 0; i < m; ++i) {
cin >> x;
veb.push_back(x);
}
cout << min_total_length(ver, veb);
}*/
# | 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... |