이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
using namespace std;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
using ll = long long;
constexpr ll inf = 1e18;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int N = r.size(), M = b.size();
vector<pair<ll, int>> A(N + M);
rep (i, N) A[i] = {r[i], 0};
rep (i, M) A[N + i] = {b[i], 1};
sort(all(A));
vector<int> nxt(N + M + 1);
nxt[N + M] = N + M;
nxt[N + M - 1] = N + M;
rrep (i, N + M - 1) {
if (A[i].second == A[i + 1].second) nxt[i] = nxt[i + 1];
else nxt[i] = i + 1;
}
vector<ll> cum1(N + M), cum2(N + M);
rep (i, N + M - 1) cum1[i + 1] = cum1[i] + (A[i + 1].first - A[i].first) * i;
vector<ll> dp(N + M + 1, inf);
dp[0] = 0;
rep (i, N + M) {
int m = nxt[i];
int r = nxt[m];
if (m != r) {
rep2 (j, m, r) {
if (m - i >= j - m + 1) {
ll sm = 0;
sm += cum1[m] - cum1[i];
sm += (A[m].first - A[i].first) * (-i + 1);
sm -= cum1[j] - cum1[m];
sm += (A[j].first - A[m].first) * j;
chmin(dp[j + 1], dp[i] + sm);
}
else {
ll sm = 0;
sm += cum1[m - 1] - cum1[i];
sm += (A[m - 1].first - A[i].first) * (-i + 1);
sm -= cum1[j] - cum1[m - 1];
sm += (A[j].first - A[m - 1].first) * j;
chmin(dp[j + 1], dp[i] + sm);
}
}
}
if (i != 0 && A[i].second != A[i - 1].second) {
rep2 (j, i, m) {
ll sm = 0;
rep2 (k, i - 1, j) sm += (A[k + 1].first - A[k].first) * (j - k);
chmin(dp[j + 1], dp[i] + sm);
}
}
}
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... |