이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using i32 = int;
using i64 = long long;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<V<V<T>>>;
#define OVERLOAD4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, l, r) for (i32 i = (i32)(l); i < (i32)(r); ++i)
#define REP(...) OVERLOAD4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, l, r) for (i32 i = (i32)(r)-1; i >= (i32)(l); --i)
#define PER(...) OVERLOAD4(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32) size(x)
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
void dbg(i32 x) { cerr << x; }
void dbg(i64 x) { cerr << x; }
template <typename T>
void dbg(const V<T> &arr) {
cerr << "[";
REP(i, LEN(arr)) {
if (i) {
cerr << ", ";
}
dbg(arr[i]);
}
cerr << "]";
}
void debug() {}
template <typename Head, typename... Tail>
void debug(const Head &head, const Tail &...tail) {
dbg(head);
if (sizeof...(Tail)) {
cerr << ", ";
} else {
cerr << '\n';
}
debug(tail...);
}
#ifdef DEBUGF
#define DBG(...) \
do { \
cerr << #__VA_ARGS__ << " : "; \
debug(__VA_ARGS__); \
} while (false)
#else
#define DBG(...) (void)0
#endif
#include "wiring.h"
constexpr i64 INF = 3003003003003003003;
i64 min_total_length(V<i32> r, V<i32> b) {
i32 n = LEN(r), m = LEN(b);
V<pair<i64, bool>> pts;
{
i32 rit = 0, bit = 0;
while (LEN(pts) < n + m) {
if (bit == m || (rit < n && r[rit] < b[bit])) {
pts.emplace_back(r[rit++], false);
} else {
pts.emplace_back(b[bit++], true);
}
}
}
V<i64> dp(2 * (n + m) + 1, INF);
dp[n + m] = 0;
REP(i, n + m) {
auto [x, b] = pts[i];
V<i64> ndp(LEN(dp), INF);
if (b) {
REP(j, LEN(dp)) {
REP(k, j) {
chmin(ndp[k], dp[j]);
}
}
} else {
REP(j, LEN(dp)) {
REP(k, j + 1, LEN(dp)) {
chmin(ndp[k], dp[j]);
}
}
}
if (i != n + m - 1) {
i64 nxt = pts[i + 1].first;
REP(j, LEN(ndp)) {
ndp[j] += abs(j - (n + m)) * (nxt - x);
}
}
dp = ndp;
DBG(dp);
}
i64 ans = dp[n + m];
return ans;
}
# | 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... |