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;
long long min_total_length(vector<int> r, vector<int> b)
{
vector<array<long long, 2>> a;
int i = 0, j = 0;
while (i < r.size() && j < b.size())
{
if (r[i] <= b[j]) a.push_back({r[i++], 0});
else a.push_back({b[j++], 1});
}
while (i < r.size()) a.push_back({r[i++], 0});
while (j < b.size()) a.push_back({b[j++], 1});
vector<long long> dp(a.size(), 1e18);
vector<long long> mn(a.size(), 1e18);
long long rl = -1e18, bl = -1e18;
for (int i = 0; i < a.size(); i++)
{
if (a[i][1] == 0) mn[i] = min(mn[i], abs(a[i][0] - bl)), rl = a[i][0];
else mn[i] = min(mn[i], abs(a[i][0] - rl)), bl = a[i][0];
}
rl = 1e18, bl = 1e18;
for (int i = (int)a.size() - 1; i >= 0; i--)
{
if (a[i][1] == 0) mn[i] = min(mn[i], abs(a[i][0] - bl)), rl = a[i][0];
else mn[i] = min(mn[i], abs(a[i][0] - rl)), bl = a[i][0];
}
vector<int> f(a.size() * 2 + 100, -2);
vector<int> pr(a.size(), 0), pb(a.size(), 0);
f[a.size()] = -1;
int c = 0;
for (int i = 0; i < a.size(); i++)
{
if (a[i][1] == 0) c++;
else c--;
pr[i] = (i ? pr[i - 1] : 0);
pb[i] = (i ? pb[i - 1] : 0);
if (a[i][1] == 0) pr[i] += a[i][0];
else pb[i] += a[i][0];
dp[i] = (i ? dp[i - 1] : 0) + mn[i];
long long cntr = 0, cntb = 0, d = 0;
for (int j = i; j >= 0; j--)
{
if (a[j][1] == 0)
{
if (cntb > cntr) d -= a[j][0];
else d += a[j][0];
cntr++;
}
else
{
if (cntr > cntb) d -= a[j][0];
else d += a[j][0];
cntb++;
}
if (cntr == cntb)
{
assert(j == f[c + a.size()] + 1);
dp[i] = min(dp[i], (j ? dp[j - 1] : 0) + d);
break;
}
}
// if (f[c + a.size()] != -2)
// {
// long long ind = f[c + a.size()], sumr = pr[i], sumb = pb[i];
// if (ind >= 0) sumr -= pr[ind], sumb -= pb[ind];
// dp[i] = min(dp[i], (ind >= 0 ? dp[ind] : 0) + abs(sumr - sumb));
// }
f[c + a.size()] = i;
}
return dp[(int)a.size() - 1];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:10:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | while (i < r.size() && j < b.size())
| ~~^~~~~~~~~~
wiring.cpp:10:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | while (i < r.size() && j < b.size())
| ~~^~~~~~~~~~
wiring.cpp:15:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | while (i < r.size()) a.push_back({r[i++], 0});
| ~~^~~~~~~~~~
wiring.cpp:16:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | while (j < b.size()) a.push_back({b[j++], 1});
| ~~^~~~~~~~~~
wiring.cpp:20:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
wiring.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
# | 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... |