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;
#define pii pair<int, int>
#define oo 1e9
long long min_total_length(std::vector<int> r, std::vector<int> b) {
#define int long long
int n = r.size() + b.size();
vector<pii> v = {{0, 0}};
for(int a : r) v.push_back({a, 0});
for(int a : b) v.push_back({a, 1});
sort(v.begin() + 1, v.end());
vector<int> pre(n + 1, 0), prex(n + 1, 0), dp(n + 1, 0);
map<int, int> mp = {{0, 0}};
for(int i = 1; i <= n; i++){
pre[i] = pre[i - 1] + ((v[i].second)? -1 : 1);
prex[i] = prex[i - 1] + ((v[i].second)? -1 : 1) * v[i].first;
int dist = oo;
if(v[i].second){
int id = lower_bound(r.begin(), r.end(), v[i].first) - r.begin();
// cout << v[i].first << "B " << id << '\n';
if(id != r.size()) dist = min(dist, r[id] - v[i].first);
if(id - 1 != -1) dist = min(dist, v[i].first - r[id - 1]);
}
else{
int id = lower_bound(b.begin(), b.end(), v[i].first) - b.begin();
// cout << v[i].first << "R " << id << '\n';
if(id != b.size()) dist = min(dist, b[id] - v[i].first);
if(id - 1 != -1) dist = min(dist, v[i].first - b[id - 1]);
}
dp[i] = dp[i - 1] + dist;
if(mp.count(pre[i])){
int id = mp[pre[i]];
dp[i] = min(dp[id] + abs(prex[i] - prex[id]), dp[i]);
}
mp[pre[i]] = i;
}
#undef int
return dp[n];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:23:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if(id != r.size()) dist = min(dist, r[id] - v[i].first);
| ~~~^~~~~~~~~~~
wiring.cpp:29:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(id != b.size()) dist = min(dist, b[id] - v[i].first);
| ~~~^~~~~~~~~~~
# | 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... |