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 <bits/stdc++.h>
//#include "grader.cpp"
#include "wiring.h"
using namespace std;
#define ll long long
#define mk make_pair
#define pb push_back
#define fr first
#define sc second
#define pi pair < int, int >
#define vi vector < int >
#define vii vector < pi >
const int N = 2e5 + 7;
const ll INF = 1e18 + 7;
ll dp[N], pr[N];
int bal[N];
int n, m;
int cb = (int) 1e5 + 1, s, f;
vii v;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
n = r.size();
m = b.size();
v.pb(mk(0, 0));
int i = 0, j = 0;
while (i < n && j < m){
if (r[i] < b[j]){
v.pb(mk(r[i], 1));
i++;
}
else {
v.pb(mk(-b[j], -1));
j++;
}
}
while (i < n) v.pb(mk(r[i], 1)), i++;
while (j < m) v.pb(mk(-b[j], -1)), j++;
for (int i = 1; i < v.size(); i++)
pr[i] = pr[i - 1] + v[i].fr;
memset(bal, -1, sizeof bal);
bal[cb] = 0;
for (int i = 1; i < v.size(); i++){
cb += v[i].sc;
ll r1, r2;
r1 = r2 = INF;
if (v[i].sc == 1){
if (s)
r1 = dp[i - 1] + abs(r[f] - b[s - 1]);
if (s < b.size())
r2 = dp[i - 1] + abs(r[f] - b[s]);
f++;
}
else {
if (f)
r1 = dp[i - 1] + abs(b[s] - r[f - 1]);
if (f < r.size())
r2 = dp[i - 1] + abs(b[s] - r[f]);
s++;
}
dp[i] = min(r1, r2);
if (bal[cb] != -1)
dp[i] = min(dp[i], dp[bal[cb]] + abs(pr[i] - pr[bal[cb]]));
bal[cb] = i;
}
return dp[v.size() - 1];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < v.size(); i++)
~~^~~~~~~~~~
wiring.cpp:48:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < v.size(); i++){
~~^~~~~~~~~~
wiring.cpp:55:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (s < b.size())
~~^~~~~~~~~~
wiring.cpp:62:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (f < r.size())
~~^~~~~~~~~~
# | 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... |