#include <bits/stdc++.h>
#include "wiring.h"
using namespace std;
const int MAX_N = 100000;
const long long INF = (1LL<<30) * MAX_N;
struct Range {
int l, r, color;
Range(int _l, int _r, int _color):l(_l), r(_r), color(_color) {}
};
vector<pair<int, int> > node;
vector<Range> ranges;
long long _dp[1+2*MAX_N], *dp = _dp + 1;
long long _sum[1+2*MAX_N], *sum = _sum + 1;
void pushRange(int poz, int color, int x) {
if(ranges.back().color == color)
ranges.back().r = poz;
else
ranges.push_back(Range(poz, poz, color));
node.push_back(make_pair(x, color));
}
void mergeNodes(const vector<int> &r, const vector<int> &b) {
int i, j;
i = j = 0;
if(r[0] < b[0])
ranges.push_back({0, 0, 1});
else
ranges.push_back({0, 0, 2});
while(i < r.size() || j < b.size()) {
if(j == b.size() || (j < b.size() && i < r.size() && r[i] < b[j])) {
pushRange(i + j, 1, r[i]);
++i;
} else {
pushRange(i + j, 2, b[j]);
++j;
}
}
for(int i = 0; i < node.size(); ++i)
sum[i] = sum[i - 1] + node[i].first;
}
long long sumRange(int a, int b) {
return sum[b] - sum[a - 1];
}
void initDp() {
dp[-1] = 0;
for(int i = ranges[0].l; i <= ranges[0].r; ++i)
dp[i] = INF;
}
void doDp(int i) {
int l, r;
long long rgang = INF, lazy = 0LL;
priority_queue<pair<long long, int>,
vector<pair<long long, int> >,
std::greater<pair<long long, int> > > lgang;
l = ranges[i - 1].r;
r = ranges[i].l;
for(int j = ranges[i - 1].l; j <= ranges[i - 1].r; ++j)
lgang.push(make_pair(dp[j - 1] - sumRange(j, ranges[i - 1].r) + (long long)(ranges[i - 1].r - i + 1) * node[ranges[i].l].first, j));
while(r <= ranges[i].r) {
if(l >= ranges[i - 1].l)
rgang = min(dp[l - 1] - sumRange(l, ranges[i - 1].r), rgang - node[ranges[i - 1].r].first);
else
rgang -= node[ranges[i - 1].r].first;
lazy -= node[ranges[i].l].first;
while(!lgang.empty() && ranges[i - 1].r - lgang.top().second <= r - ranges[i].l)
lgang.pop();
dp[r] = rgang + sumRange(ranges[i].l, r);
if(!lgang.empty())
dp[r] = min(dp[r], lgang.top().first + sumRange(ranges[i].l, r) + lazy);
--l;
++r;
}
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
mergeNodes(r, b);
initDp();
for(int i = 1; i < ranges.size(); ++i)
doDp(i);
return dp[node.size() - 1];
}
Compilation message
wiring.cpp: In function 'void mergeNodes(const std::vector<int>&, const std::vector<int>&)':
wiring.cpp:36:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(i < r.size() || j < b.size()) {
~~^~~~~~~~~~
wiring.cpp:36:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(i < r.size() || j < b.size()) {
~~^~~~~~~~~~
wiring.cpp:37:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j == b.size() || (j < b.size() && i < r.size() && r[i] < b[j])) {
~~^~~~~~~~~~~
wiring.cpp:37:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j == b.size() || (j < b.size() && i < r.size() && r[i] < b[j])) {
~~^~~~~~~~~~
wiring.cpp:37:44: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j == b.size() || (j < b.size() && i < r.size() && r[i] < b[j])) {
~~^~~~~~~~~~
wiring.cpp:46:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < node.size(); ++i)
~~^~~~~~~~~~~~~
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < ranges.size(); ++i)
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
504 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '23725' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '904', found: '848' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
292 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '17703', found: '17583' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '27', found: '22' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
504 KB |
3rd lines differ - on the 1st token, expected: '25859', found: '23725' |
2 |
Halted |
0 ms |
0 KB |
- |