이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e18;
long long cost(vector<int> a, vector<int> b) {
reverse(a.begin(), a.end());
int n = a.size();
int m = b.size();
long long ret = 0;
if (n < m) {
for (int i = 0; i < n; ++i) {
ret -= a[i];
}
ret -= (m - n) * 1ll * a[n - 1];
for (int i = 0; i < m; ++i) {
ret += b[i];
}
} else {
for (int i = 0; i < n; ++i) {
ret -= a[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<vector<int>> a;
for (int i = 0, j = 0; i < n + m; i = j) {
while (kek[j].second == kek[i].second) {
++j;
}
a.push_back({});
for (int t = i; t < j; ++t) {
a.back().push_back(kek[t].first);
}
}
n = a.size();
vector<int> sz(n);
for (int i = 0; i < n; ++i) {
sz[i] = a[i].size();
}
vector<long long> dp(sz[0] + 1, INF);
dp[0] = 0;
for (int rofl = 1; rofl < n; ++rofl) {
vector<long long> ndp(sz[rofl] + 1, INF);
ndp[0] = min(ndp[0], dp[sz[rofl - 1]]);
vector<int> lol1;
for (int i = sz[rofl - 1] - 1; i >= 0; --i) {
lol1.push_back(a[rofl - 1][i]);
vector<int> lol2;
for (int j = 1; j <= sz[rofl]; ++j) {
lol2.push_back(a[rofl][j - 1]);
ndp[j] = min(ndp[j], dp[i] + cost(lol1, lol2));
}
}
dp = ndp;
for (int i = sz[rofl]; i > 0; --i) {
dp[i - 1] = min(dp[i - 1], dp[i]);
}
}
return dp[sz[n - 1]];
}
# | 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... |