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;
const long long INF = 2e18;
long long getcost(vector<int> r, vector<int> b) {
int n = r.size();
int m = b.size();
long long ret = 0;
if (n < m) {
for (int i = 0; i < n; ++i) {
ret -= r[i];
}
ret -= (m - n) * 1ll * r[n - 1];
for (int i = 0; i < m; ++i) {
ret += b[i];
}
} else {
for (int i = 0; i < n; ++i) {
ret -= r[i];
}
ret += (n - m) * 1ll * b[0];
for (int i = 0; i < m; ++i) {
ret += b[i];
}
}
return ret;
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int n = r.size();
int m = b.size();
vector<pair<int, int>> kek;
for (int i = 0; i < n; ++i) {
kek.push_back({r[i], 0});
}
for (int i = 0; i < m; ++i) {
kek.push_back({b[i], 1});
}
sort(kek.begin(), kek.end());
vector<long long> dp(n + m + 1, INF);
vector<long long> dp1(n + m + 1, INF), dp2(n + m + 1, INF);
dp[0] = 0;
for (int i = 0; i < n + m; ++i) {
dp[i] = min(dp[i], dp[i + 1]);
if (i < n + m - 1 && kek[i].second != kek[i + 1].second) {
int prv = i;
while (prv > 0 && kek[prv - 1].second == kek[i].second) {
--prv;
}
int nxt = i + 1;
while (nxt < n + m - 1 && kek[nxt + 1].second == kek[i + 1].second) {
++nxt;
}
for (int j = i + 2; j <= nxt + 1; ++j) {
dp1[j] = INF;
dp2[j] = INF;
}
long long sum = 0;
for (int j = 0; i + j + 1 <= nxt && i - j >= prv; ++j) {
sum -= kek[i - j].first;
sum += kek[i + j + 1].first;
dp1[i + j + 2] = min(dp1[i + j + 2], dp[i - j] + sum);
dp2[i + j + 2] = min(dp2[i + j + 2], dp[i - j] + sum);
}
if (nxt - i < i - prv + 1) {
int d = nxt - i;
for (int j = i - d; j >= prv; --j) {
sum -= kek[j].first;
sum += kek[i + 1].first;
dp2[nxt + 1] = min(dp2[nxt + 1], dp[j] + sum);
}
}
for (int j = i + 2; j + 1 <= nxt + 1; ++j) {
if (dp1[j] != INF) {
dp1[j + 1] = min(dp1[j + 1], dp1[j] + kek[j].first - kek[i].first);
}
}
for (int j = i + 3; j <= nxt + 1; ++j) {
if (dp2[j] != INF) {
dp2[j - 1] = min(dp2[j - 1], dp2[j] - kek[j - 1].first + kek[i + 1].first);
}
}
for (int j = i + 2; j <= nxt + 1; ++j) {
dp[j] = min(dp[j], dp1[j]);
dp[j] = min(dp[j], dp2[j]);
if (j > 0) {
dp[j - 1] = min(dp[j - 1], dp[j]);
dp[j - 1] = min(dp[j - 1], dp[j]);
}
}
}
// for (int j = 0; j <= n + m; ++j) {
// cout << dp[j] << " " << kek[j].second << " ";
// }
// cout << endl;
// bool was = 0;
// vector<int> r1, b1;
// for (int j = i; j < n + m; ++j) {
// if (was) {
// if (kek[j].second == kek[i].second) {
// break;
// }
// }
// if (kek[j].second == kek[i].second) {
// r1.push_back(kek[j].first);
// } else {
// was = 1;
// b1.push_back(kek[j].first);
// }
// if (was) {
// long long cost = getcost(r1, b1);
// dp[j + 1] = min(dp[j + 1], dp[i] + cost);
// }
// }
}
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... |