이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
using ll = long long;
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
ll min_total_length(vector<int> arr_r, vector<int> arr_b) {
vector<pair<int, bool>> arr;
for (auto& a : arr_r) {
arr.emplace_back(a, false);
}
for (auto& a : arr_b) {
arr.emplace_back(a, true);
}
sort(begin(arr), end(arr));
int n = sz(arr);
long dp[n + 1];
memset(dp, 0x3f, sizeof(dp));
dp[n] = 0;
deque<int> inds[2];
for (int i = n - 1; i >= 0; i--) {
if (i + 1 < n) {
inds[arr[i + 1].second].push_front(i + 1);
}
auto [t, ty] = arr[i];
auto &me = inds[ty], &other = inds[!ty];
auto get_w = [&](int u, int v) -> long {
return abs(arr[u].first - arr[v].first);
};
auto get = [&](const deque<int>& arr, int ind) -> int {
if (ind == sz(arr)) {
return n;
}
return arr[ind];
};
if (!sz(other)) {
continue;
}
if (get(other, 0) < get(me, 0)) {
long csum = 0;
for (int j = 0; j < sz(other) && get(other, j) < get(me, 0); j++) {
csum += get_w(i, other[j]);
dp[i] = min(dp[i], csum + min(dp[other[j]], dp[other[j] + 1]));
}
} else {
dp[i] = dp[i + 1] + get_w(i, other[0]);
long csum = 0;
me.push_front(i);
for (int j = 0; j < sz(other) && j < sz(me); j++) {
csum += get_w(me[j], other[j]);
dp[i] = min(dp[i],
csum + dp[min(get(other, j + 1), get(me, j + 1))]);
}
me.pop_front();
}
}
return dp[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... |