이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
#include "wiring.h"
using namespace std;
const long long inf = 1e15;
vector <long long> dp[200010];
vector <int> group[200010];
vector <long long> first[200010], last[200010];
typedef pair <int, int> pii;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int g = 0;
int n = r.size();
int m = b.size();
vector <pii> v;
for(int i = 0; i < n; i++) {
v.emplace_back(r[i], 0);
}
for(int i = 0; i < m; i++) {
v.emplace_back(b[i], 1);
}
sort(v.begin(), v.end());
int prevc = -1;
group[0].push_back(-1);
for(int i = 0; i < n+m; i++) {
if(prevc == v[i].second) {
group[g].push_back(v[i].first);
} else {
++g;
prevc = v[i].second;
group[g].push_back(-1);
group[g].push_back(v[i].first);
}
}
for(int i = 0; i <= g; i++) {
int sz = group[i].size() - 1;
first[i].resize(sz + 1);
last[i].resize(sz + 1);
for(int j = 1; j <= sz; j++) {
first[i][j] = first[i][j - 1] + group[i][j];
last[i][j] = last[i][j - 1] + group[i][sz - j + 1];
}
}
for(int i = 1; i <= g+1; i++) {
int sz = group[i - 1].size() - 1;
dp[i].resize(sz + 1);
for(int j = 0; j <= sz; j++) {
dp[i][j] = inf;
}
}
dp[1][0] = 0;
for(int i = 1; i <= g; i++) {
int sz_prev = group[i - 1].size() - 1;
int sz_cur = group[i].size() - 1;
for(int j = sz_prev; j > 0; j--) {
dp[i][j - 1] = min(dp[i][j - 1], dp[i][j] + abs(group[i - 1][sz_prev - j + 1] - group[i][1]));
}
for(int j = 0; j <= min(sz_cur, sz_prev); j++) {
dp[i + 1][sz_cur - j] = min(dp[i + 1][sz_cur - j], dp[i][j] + abs(last[i - 1][j] - first[i][j]));
}
for(int j = sz_cur; j > 0; j--) {
if(i == 1) break;
dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i + 1][j] + abs(group[i][sz_cur - j + 1] - group[i - 1].back()));
}
}
return dp[g + 1][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... |