이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll inf = 1E16;
long long min_total_length(vector<int> R, vector<int> B) {
int N = R.size();
int M = B.size();
vector<array<int, 2>> T;
for (int x : R) {
T.push_back({x, 0});
}
for (int x : B) {
T.push_back({x, 1});
}
sort(T.begin(), T.end());
vector<ll> pfs(N + M + 1);
for (int i = 0; i < N + M; i++) {
pfs[i + 1] = pfs[i] + T[i][0];
}
vector<int> d(N + M, -1);
vector<ll> dp(N + M + 1, inf);
dp[0] = 0;
for (int i = 0; i < N + M; i++) {
if (i > 0) {
if (T[i][1] == T[i - 1][1]) {
d[i] = d[i - 1];
} else {
d[i] = i - 1;
}
}
if (d[i] == -1) {
continue;
}
int j = d[i];
dp[i + 1] = min(dp[i + 1], dp[j + 1] + (pfs[i + 1] - pfs[j + 1]) - 1LL * (i - j) * T[j][0]);
for (int p = d[j] + 1; p <= j; p++) {
int m = min(j - p + 1, i - j);
ll s = (pfs[i + 1] - pfs[i - m + 1]) - (pfs[p + m] - pfs[p]);
if (m <= j - p) {
s += 1LL * T[j + 1][0] * ((j + 1) - (p + m)) - (pfs[j + 1] - pfs[p + m]);
}
if (m < i - j) {
s -= 1LL * T[j][0] * ((i - m + 1) - (j + 1)) - (pfs[i - m + 1] - pfs[j + 1]);
}
dp[i + 1] = min(dp[i + 1], dp[p] + s);
}
}
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... |