#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();
pii a[NMAX];
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;
}
for(int i = 0; i < n; ++i) {
a[i].fi = r[i];
a[i].se = 0;
}
for(int i = 0; i < m; ++i) {
a[i+n].fi = b[i];
a[i+n].se = 1;
}
int nm = n + m;
sort(a, a + nm);
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, x[NMAX], y[NMAX], f[NMAX];
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;
for(int i = 0; i < n; ++i)
cin >> x[i];
for(int i = 0; i < m; ++i)
cin >> y[i];
cout << min_total_length(x, y);
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
8184 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '41560' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
8316 KB |
3rd lines differ - on the 1st token, expected: '904', found: '949' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
8316 KB |
3rd lines differ - on the 1st token, expected: '316', found: '364' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
8316 KB |
3rd lines differ - on the 1st token, expected: '27', found: '29' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
8184 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '41560' |
2 |
Halted |
0 ms |
0 KB |
- |