이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
using L = long long;
long long min_total_length(std::vector<int> r, std::vector<int> b)
{
vector<vector<L>> a;
auto it = r.begin(), jt = b.begin();
while (it != r.end() || jt != b.end())
{
a.push_back({});
if (it != r.end() && (jt == b.end() || *it < *jt))
{
while (it != r.end() && (jt == b.end() || *it < *jt))
a.back().push_back(*it), ++it;
}
else
{
while (jt != b.end() && (it == r.end() || *jt < *it))
a.back().push_back(*jt), ++jt;
}
}
vector<vector<L>> p = a;
for (auto &x : p)
for (size_t i = 1; i < x.size(); ++i)
x[i] += x[i - 1];
// dp state, suffix min, prefix min - k * d
vector<vector<L>> f(a.size()), g(a.size()), h(a.size());
for (size_t i = 0; i < a.size(); ++i)
{
f[i].resize(a[i].size() + 1);
g[i].resize(a[i].size() + 1);
h[i].resize(a[i].size() + 1);
fill(f[i].begin(), f[i].end(), LLONG_MAX / 4);
fill(g[i].begin(), g[i].end(), LLONG_MAX / 4);
fill(h[i].begin(), h[i].end(), LLONG_MAX / 4);
for (L j = 0; j <= a[i].size(); ++j)
{
if ((!i && j) || (i == a.size() - 1 && j != a[i].size()))
continue;
L d = !i ? 0 : a[i][0] - a[i - 1].back();
f[i][j] = (j ? p[i][j - 1] : 0) - j * a[i][0];
if (i)
{
f[i][j] += min(j < a[i - 1].size() ? g[i - 1][j] : LLONG_MAX / 4,
j ? (h[i - 1][min<size_t>(j - 1, a[i - 1].size())] + j * d) : LLONG_MAX / 4);
}
if (i + 1 < a.size())
{
L right_cost = -(p[i].back() - (j ? p[i][j - 1] : 0)) + (L)(a[i].size() - j) * a[i + 1][0];
g[i][j] = f[i][j] + right_cost;
h[i][j] = f[i][j] + right_cost - j * (a[i + 1][0] - a[i].back());
if (j)
h[i][j] = min(h[i][j], h[i][j - 1]);
}
}
for (size_t j = a[i].size() - 1; j < a[i].size(); --j)
g[i][j] = min(g[i][j], g[i][j + 1]);
}
return f.back().back();
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:42:25: warning: comparison of integer expressions of different signedness: 'L' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (L j = 0; j <= a[i].size(); ++j)
| ~~^~~~~~~~~~~~~~
wiring.cpp:44:54: warning: comparison of integer expressions of different signedness: 'L' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | if ((!i && j) || (i == a.size() - 1 && j != a[i].size()))
| ~~^~~~~~~~~~~~~~
wiring.cpp:50:34: warning: comparison of integer expressions of different signedness: 'L' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | f[i][j] += min(j < a[i - 1].size() ? g[i - 1][j] : LLONG_MAX / 4,
| ~~^~~~~~~~~~~~~~~~~
# | 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... |