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>
using namespace std;
const long long L = 1000000000000000LL;
long long min_total_length(vector<int> r, vector<int> b) {
vector<array<int, 2>> points;
for (auto i : r) {
points.push_back({i, 0});
}
for (auto i : b) {
points.push_back({i, 1});
}
sort(points.begin(), points.end());
vector<long long> cost;
for (int i = 0, j = 0; i < (int) points.size(); i = j) {
while (j < (int) points.size() && points[j][1] == points[i][1]) {
++j;
}
if (i == 0) {
cost.resize(j + 1);
for (int k = 0; k < j; ++k) {
cost[j] += points[j - 1][0] - points[k][0];
cost[k] = L;
}
} else {
long long gap = points[i][0] - points[i - 1][0];
vector<long long> prefix = cost;
for (int k = 1; k < (int) cost.size(); ++k) {
prefix[k] = min(prefix[k], prefix[k - 1]);
}
vector<long long> suffix = cost;
for (int k = cost.size() - 1; k >= 0; --k) {
suffix[k] = max(suffix[k] + k * gap, suffix[k + 1]);
}
long long sum = 0;
cost.resize(j - i + 1);
for (int k = 0; k <= j - i; ++k) {
sum += k > 0 ? points[i + k - 1][0] - points[i][0] : 0;
if (k < (int) prefix.size()) {
cost[k] = sum + min(suffix[k], prefix[k] + k * gap);
} else {
cost[k] = sum + prefix.back() + k * gap;
}
}
sum = 0;
for (int k = j - i - 1; k >= 0; --k) {
sum += points[j - 1][0] - points[i + k][0];
cost[k] += sum;
}
reverse(cost.begin(), cost.end());
}
}
return cost[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... |