This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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][a[i].size() - j] = f[i][j] + right_cost;
h[i][a[i].size() - j] = f[i][j] + right_cost - (L)(a[i].size() - j) * (a[i + 1][0] - a[i].back());
}
}
for (size_t j = a[i].size() - 1; j < a[i].size(); --j)
g[i][j] = min(g[i][j], g[i][j + 1]);
for (size_t j = 1; j < h[i].size(); ++j)
h[i][j] = min(h[i][j], h[i][j - 1]);
}
return f.back().back();
}
Compilation message (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... |