이 제출은 이전 버전의 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) {
ll tmp1 = (dp[i] + cum1[m] * 2 - cum1[i] + (A[m].first - A[i].first) * (-i + 1));
ll tmp2 = (dp[i] + cum1[m - 1] * 2 - cum1[i] + (A[m - 1].first - A[i].first) * (-i + 1));
int mm = clamp(2 * m - i, m, r);
rep2 (j, m, mm) {
ll sm = 0;
sm += - A[m].first * j + tmp1;
chmin(dp[j + 1], sm);
}
rep2 (j, mm, r) {
ll sm = 0;
sm += - A[m - 1].first * j + tmp2;
chmin(dp[j + 1], sm);
}
}
if (i != 0 && A[i].second != A[i - 1].second) {
ll tmp = dp[i] + cum1[i - 1];
rep2 (j, i, m) {
ll sm = 0;
sm += - A[i - 1].first * j + tmp;
chmin(dp[j + 1], sm);
}
}
dp[i + 1] += A[i].first * i - cum1[i];
}
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... |