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;
const int MAXN = 2e5 + 10;
long long ps[MAXN];
long long f(int l1, int r1, int l2, int r2) {
long long cntB = r2 - l2 + 1;
long long cntR = r1 - l1 + 1;
long long sumB = ps[r2] - ps[l2 - 1];
long long sumR = ps[r1] - ps[l1 - 1];
if(cntR <= cntB) {
return sumB - sumR - (cntB - cntR) * (ps[r1] - ps[r1 - 1]);
}
else {
return sumB - sumR + (cntR - cntB) * (ps[l2] - ps[l2 - 1]);
}
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
vector<pair<long long, char> > all;
all.push_back({-1, 'N'});
for(int i=0; i<r.size(); i++) all.push_back({r[i], 'R'});
for(int i=0; i<b.size(); i++) all.push_back({b[i], 'B'});
sort(all.begin(), all.end());
int n = all.size() - 1;
long long dp[n+1];
ps[0] = 0;
for(int i=1; i<=n; i++) ps[i] = ps[i-1] + all[i].first;
dp[0] = 0;
for(int i=1; i<=n; i++) {
dp[i] = 1e18;
bool active = 0;
int cntsame = 0, cntdiff = 0;
for(int j=i; j>=1; j--) {
if(all[j].second == all[i].second && active) {
break;
}
if(all[j].second == all[i].second) cntsame++;
if(all[j].second != all[i].second) {
active = 1; // use [j, i], reference dp[j-1]
cntdiff++;
dp[i] = min(dp[i], dp[j-1] + f(j, j+cntdiff-1, j+cntdiff, i));
}
}
}
return dp[n];
}
Compilation message (stderr)
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:22:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0; i<r.size(); i++) all.push_back({r[i], 'R'});
| ~^~~~~~~~~
wiring.cpp:23:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=0; i<b.size(); i++) all.push_back({b[i], 'B'});
| ~^~~~~~~~~
# | 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... |