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>
using namespace std;
long long min_total_length(vector<int> s, vector<int> t) {
int n = s.size(), m = t.size(), si = 0, ti = 0;
vector<long long> ss(n+m+1), st(n+m+1), dp(n+m+1), c(2*n+2*m+2, -1);
vector<pair<long long, int>> v;
v.push_back({-1, 0});
for (int i = 0; i < n; i++) {
v.push_back({s[i], 0});
}
for (int i = 0; i < m; i++) {
v.push_back({t[i], 1});
}
sort(v.begin(), v.end());
int x = n+m;
c[n+m] = 0;
for (int i = 1; i <= n+m; i++) {
ss[i] = ss[i-1];
st[i] = st[i-1];
long long tmp = 0;
if(v[i].second == 1){
si = lower_bound(s.begin(), s.end(), v[i].first) - s.begin();
if(si == n) si--;
if(si > 0 && s[si] > v[i].first) si--;
if(si < n-1) {
tmp = min(abs(s[si] - v[i].first), abs(s[si+1] - v[i].first));
} else {
tmp = abs(s[si] - v[i].first);
}
x++;
ss[i] += v[i].first;
} else {
ti = lower_bound(t.begin(), t.end(), v[i].first) - t.begin();
if(ti == m) ti--;
if(ti > 0 && t[ti] > v[i].first) ti--;
if(ti < m-1) {
tmp = min(abs(t[ti] - v[i].first), abs(t[ti+1] - v[i].first));
} else {
tmp = abs(t[ti] - v[i].first);
}
x--;
st[i] += v[i].first;
}
dp[i] = dp[i-1] + tmp;
if(c[x] != -1){
dp[i] = min(dp[i], dp[c[x]] + abs(ss[i] - ss[c[x]] + st[c[x]] - st[i]));
}
c[x] = i;
}
return dp[n+m];
}
# | 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... |