#pragma GCC optimize("O3")
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, ans;
vector<int> fullList;
unordered_map<int, bool> isRed, isBlue;
vector<vector<int>> segments;
vector<vector<int>> prefixR;
vector<vector<int>> prefixL;
int calcWire(int s, int l, int r) {
int res = 0;
res += prefixL[s][l];
res += (segments[s].size()-l) * (segments[s+1][0]-segments[s].back());
res += prefixR[s+1][r];
res += (r+1) * (segments[s+1][0]-segments[s].back());
res -= min((int)segments[s].size()-l, r+1)*(segments[s+1][0]-segments[s].back());
return res;
}
int shash(int a, int b) {
return a*1000000 + b;
}
unordered_map<int, int> dp;
long long min_total_length(vector<signed> r, vector<signed> b) {
dp.reserve(32768); dp.max_load_factor(0.25);
n = r.size();
m = b.size();
fullList.resize(n+m);
merge(r.begin(), r.end(), b.begin(), b.end(), fullList.begin());
for(auto x: r)
isRed[x]=1;
for(auto x: b)
isBlue[x]=1;
for(int i = 0; i < m+n; i++) {
if(i==0 || isRed[fullList[i]] != isRed[fullList[i-1]]) {
segments.push_back(vector<int>(0));
prefixL.push_back(vector<int>(0));
prefixR.push_back(vector<int>(0));
segments.back().push_back(fullList[i]);
} else {
segments.back().push_back(fullList[i]);
}
}
for(int s = 0; s < segments.size(); s++) {
prefixR[s].push_back(0);
for(int i = 1; i < segments[s].size(); i++)
prefixR[s].push_back(prefixR[s][i-1]+segments[s][i]-segments[s][0]);
reverse(segments[s].begin(), segments[s].end());
prefixL[s].push_back(0);
for(int i = 1; i < segments[s].size(); i++)
prefixL[s].push_back(prefixL[s][i-1]+segments[s][0]-segments[s][i]);
reverse(prefixL[s].begin(), prefixL[s].end());
reverse(segments[s].begin(), segments[s].end());
}
for(int j = 0; j < segments[1].size(); j++) {
dp[shash(1,j)] = calcWire(0, 0, j);
}
for(int s = 2; s < segments.size(); s++) {
for(int i = 0; i < segments[s].size(); i++) {
dp[shash(s, i)] = dp[shash(s-1, 0)] + calcWire(s-1, 0, i);
if(dp[shash(s-2, segments[s-2].size()-1)] != 0)
dp[shash(s, i)] = min(dp[shash(s, i)], dp[shash(s-2, segments[s-2].size()-1)] + calcWire(s-1, 0, i));
int lo = 1, hi = max(1ll,(int)segments[s-1].size()-1), j, m1, m2, r1, r2;
while(lo+1 < hi) {
m1 = (lo+hi)/2;
r1 = dp[shash(s-1, m1-1)] + calcWire(s-1, m1, i);
m2 = m1+1;
r2 = dp[shash(s-1, m2-1)] + calcWire(s-1, m2, i);
if(r1==r2)
lo = m1, hi = m2;
else if(r1 < r2)
hi = m2-1;
else
lo = m1+1;
}
dp[shash(s, i)] = min(dp[shash(s, i)], dp[shash(s-1, lo-1)] + calcWire(s-1, lo, i));
dp[shash(s, i)] = min(dp[shash(s, i)], dp[shash(s-1, hi-1)] + calcWire(s-1, hi, i));
// cerr << s << " " << i << ": " << dp[shash(s, i)] << endl;
}
}
return dp[shash(segments.size()-1, segments.back().size()-1)];
if(segments.size() == 2)
return calcWire(0, 0, m-1);
return -1;
}
Compilation message
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:51:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int s = 0; s < segments.size(); s++) {
~~^~~~~~~~~~~~~~~~~
wiring.cpp:53:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < segments[s].size(); i++)
~~^~~~~~~~~~~~~~~~~~~~
wiring.cpp:57:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < segments[s].size(); i++)
~~^~~~~~~~~~~~~~~~~~~~
wiring.cpp:63:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < segments[1].size(); j++) {
~~^~~~~~~~~~~~~~~~~~~~
wiring.cpp:67:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int s = 2; s < segments.size(); s++) {
~~^~~~~~~~~~~~~~~~~
wiring.cpp:68:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < segments[s].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~~
wiring.cpp:72:59: warning: unused variable 'j' [-Wunused-variable]
int lo = 1, hi = max(1ll,(int)segments[s-1].size()-1), j, m1, m2, r1, r2;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
612 KB |
Output is correct |
3 |
Correct |
3 ms |
720 KB |
Output is correct |
4 |
Correct |
2 ms |
772 KB |
Output is correct |
5 |
Correct |
3 ms |
892 KB |
Output is correct |
6 |
Correct |
3 ms |
912 KB |
Output is correct |
7 |
Correct |
3 ms |
920 KB |
Output is correct |
8 |
Correct |
3 ms |
920 KB |
Output is correct |
9 |
Correct |
2 ms |
920 KB |
Output is correct |
10 |
Correct |
4 ms |
920 KB |
Output is correct |
11 |
Correct |
3 ms |
920 KB |
Output is correct |
12 |
Correct |
3 ms |
920 KB |
Output is correct |
13 |
Correct |
3 ms |
920 KB |
Output is correct |
14 |
Correct |
3 ms |
924 KB |
Output is correct |
15 |
Correct |
2 ms |
928 KB |
Output is correct |
16 |
Correct |
3 ms |
932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
936 KB |
Output is correct |
2 |
Correct |
3 ms |
940 KB |
Output is correct |
3 |
Correct |
128 ms |
19428 KB |
Output is correct |
4 |
Correct |
135 ms |
19428 KB |
Output is correct |
5 |
Correct |
156 ms |
25636 KB |
Output is correct |
6 |
Correct |
208 ms |
29284 KB |
Output is correct |
7 |
Correct |
213 ms |
29284 KB |
Output is correct |
8 |
Correct |
201 ms |
29284 KB |
Output is correct |
9 |
Correct |
183 ms |
29284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
29284 KB |
Output is correct |
2 |
Correct |
3 ms |
29284 KB |
Output is correct |
3 |
Correct |
359 ms |
46704 KB |
Output is correct |
4 |
Correct |
293 ms |
46704 KB |
Output is correct |
5 |
Correct |
2 ms |
46704 KB |
Output is correct |
6 |
Correct |
2 ms |
46704 KB |
Output is correct |
7 |
Correct |
4 ms |
46704 KB |
Output is correct |
8 |
Correct |
3 ms |
46704 KB |
Output is correct |
9 |
Correct |
3 ms |
46704 KB |
Output is correct |
10 |
Correct |
3 ms |
46704 KB |
Output is correct |
11 |
Correct |
3 ms |
46704 KB |
Output is correct |
12 |
Correct |
2 ms |
46704 KB |
Output is correct |
13 |
Correct |
2 ms |
46704 KB |
Output is correct |
14 |
Correct |
3 ms |
46704 KB |
Output is correct |
15 |
Correct |
3 ms |
46704 KB |
Output is correct |
16 |
Correct |
2 ms |
46704 KB |
Output is correct |
17 |
Correct |
3 ms |
46704 KB |
Output is correct |
18 |
Correct |
311 ms |
50288 KB |
Output is correct |
19 |
Correct |
304 ms |
52236 KB |
Output is correct |
20 |
Correct |
335 ms |
52236 KB |
Output is correct |
21 |
Correct |
307 ms |
56200 KB |
Output is correct |
22 |
Correct |
299 ms |
57264 KB |
Output is correct |
23 |
Correct |
338 ms |
59276 KB |
Output is correct |
24 |
Correct |
316 ms |
61112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
61112 KB |
Output is correct |
2 |
Correct |
214 ms |
61112 KB |
Output is correct |
3 |
Correct |
226 ms |
61112 KB |
Output is correct |
4 |
Correct |
207 ms |
61112 KB |
Output is correct |
5 |
Correct |
200 ms |
61764 KB |
Output is correct |
6 |
Correct |
3 ms |
61764 KB |
Output is correct |
7 |
Correct |
3 ms |
61764 KB |
Output is correct |
8 |
Correct |
3 ms |
61764 KB |
Output is correct |
9 |
Correct |
3 ms |
61764 KB |
Output is correct |
10 |
Correct |
3 ms |
61764 KB |
Output is correct |
11 |
Correct |
3 ms |
61764 KB |
Output is correct |
12 |
Correct |
2 ms |
61764 KB |
Output is correct |
13 |
Correct |
2 ms |
61764 KB |
Output is correct |
14 |
Correct |
3 ms |
61764 KB |
Output is correct |
15 |
Correct |
2 ms |
61764 KB |
Output is correct |
16 |
Correct |
3 ms |
61764 KB |
Output is correct |
17 |
Correct |
3 ms |
61764 KB |
Output is correct |
18 |
Correct |
158 ms |
61764 KB |
Output is correct |
19 |
Correct |
244 ms |
61764 KB |
Output is correct |
20 |
Correct |
212 ms |
62360 KB |
Output is correct |
21 |
Correct |
223 ms |
62764 KB |
Output is correct |
22 |
Correct |
145 ms |
64016 KB |
Output is correct |
23 |
Correct |
160 ms |
64016 KB |
Output is correct |
24 |
Correct |
128 ms |
64016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
612 KB |
Output is correct |
3 |
Correct |
3 ms |
720 KB |
Output is correct |
4 |
Correct |
2 ms |
772 KB |
Output is correct |
5 |
Correct |
3 ms |
892 KB |
Output is correct |
6 |
Correct |
3 ms |
912 KB |
Output is correct |
7 |
Correct |
3 ms |
920 KB |
Output is correct |
8 |
Correct |
3 ms |
920 KB |
Output is correct |
9 |
Correct |
2 ms |
920 KB |
Output is correct |
10 |
Correct |
4 ms |
920 KB |
Output is correct |
11 |
Correct |
3 ms |
920 KB |
Output is correct |
12 |
Correct |
3 ms |
920 KB |
Output is correct |
13 |
Correct |
3 ms |
920 KB |
Output is correct |
14 |
Correct |
3 ms |
924 KB |
Output is correct |
15 |
Correct |
2 ms |
928 KB |
Output is correct |
16 |
Correct |
3 ms |
932 KB |
Output is correct |
17 |
Correct |
2 ms |
936 KB |
Output is correct |
18 |
Correct |
3 ms |
940 KB |
Output is correct |
19 |
Correct |
128 ms |
19428 KB |
Output is correct |
20 |
Correct |
135 ms |
19428 KB |
Output is correct |
21 |
Correct |
156 ms |
25636 KB |
Output is correct |
22 |
Correct |
208 ms |
29284 KB |
Output is correct |
23 |
Correct |
213 ms |
29284 KB |
Output is correct |
24 |
Correct |
201 ms |
29284 KB |
Output is correct |
25 |
Correct |
183 ms |
29284 KB |
Output is correct |
26 |
Correct |
3 ms |
29284 KB |
Output is correct |
27 |
Correct |
3 ms |
29284 KB |
Output is correct |
28 |
Correct |
359 ms |
46704 KB |
Output is correct |
29 |
Correct |
293 ms |
46704 KB |
Output is correct |
30 |
Correct |
2 ms |
46704 KB |
Output is correct |
31 |
Correct |
2 ms |
46704 KB |
Output is correct |
32 |
Correct |
4 ms |
46704 KB |
Output is correct |
33 |
Correct |
3 ms |
46704 KB |
Output is correct |
34 |
Correct |
3 ms |
46704 KB |
Output is correct |
35 |
Correct |
3 ms |
46704 KB |
Output is correct |
36 |
Correct |
3 ms |
46704 KB |
Output is correct |
37 |
Correct |
2 ms |
46704 KB |
Output is correct |
38 |
Correct |
2 ms |
46704 KB |
Output is correct |
39 |
Correct |
3 ms |
46704 KB |
Output is correct |
40 |
Correct |
3 ms |
46704 KB |
Output is correct |
41 |
Correct |
2 ms |
46704 KB |
Output is correct |
42 |
Correct |
3 ms |
46704 KB |
Output is correct |
43 |
Correct |
311 ms |
50288 KB |
Output is correct |
44 |
Correct |
304 ms |
52236 KB |
Output is correct |
45 |
Correct |
335 ms |
52236 KB |
Output is correct |
46 |
Correct |
307 ms |
56200 KB |
Output is correct |
47 |
Correct |
299 ms |
57264 KB |
Output is correct |
48 |
Correct |
338 ms |
59276 KB |
Output is correct |
49 |
Correct |
316 ms |
61112 KB |
Output is correct |
50 |
Correct |
2 ms |
61112 KB |
Output is correct |
51 |
Correct |
214 ms |
61112 KB |
Output is correct |
52 |
Correct |
226 ms |
61112 KB |
Output is correct |
53 |
Correct |
207 ms |
61112 KB |
Output is correct |
54 |
Correct |
200 ms |
61764 KB |
Output is correct |
55 |
Correct |
3 ms |
61764 KB |
Output is correct |
56 |
Correct |
3 ms |
61764 KB |
Output is correct |
57 |
Correct |
3 ms |
61764 KB |
Output is correct |
58 |
Correct |
3 ms |
61764 KB |
Output is correct |
59 |
Correct |
3 ms |
61764 KB |
Output is correct |
60 |
Correct |
3 ms |
61764 KB |
Output is correct |
61 |
Correct |
2 ms |
61764 KB |
Output is correct |
62 |
Correct |
2 ms |
61764 KB |
Output is correct |
63 |
Correct |
3 ms |
61764 KB |
Output is correct |
64 |
Correct |
2 ms |
61764 KB |
Output is correct |
65 |
Correct |
3 ms |
61764 KB |
Output is correct |
66 |
Correct |
3 ms |
61764 KB |
Output is correct |
67 |
Correct |
158 ms |
61764 KB |
Output is correct |
68 |
Correct |
244 ms |
61764 KB |
Output is correct |
69 |
Correct |
212 ms |
62360 KB |
Output is correct |
70 |
Correct |
223 ms |
62764 KB |
Output is correct |
71 |
Correct |
145 ms |
64016 KB |
Output is correct |
72 |
Correct |
160 ms |
64016 KB |
Output is correct |
73 |
Correct |
128 ms |
64016 KB |
Output is correct |
74 |
Correct |
220 ms |
64016 KB |
Output is correct |
75 |
Correct |
327 ms |
70096 KB |
Output is correct |
76 |
Correct |
306 ms |
70096 KB |
Output is correct |
77 |
Correct |
255 ms |
73292 KB |
Output is correct |
78 |
Correct |
235 ms |
74576 KB |
Output is correct |
79 |
Correct |
298 ms |
76032 KB |
Output is correct |
80 |
Correct |
215 ms |
76032 KB |
Output is correct |
81 |
Correct |
210 ms |
76032 KB |
Output is correct |
82 |
Correct |
271 ms |
76032 KB |
Output is correct |
83 |
Correct |
225 ms |
76032 KB |
Output is correct |
84 |
Correct |
220 ms |
76032 KB |
Output is correct |
85 |
Correct |
295 ms |
85772 KB |
Output is correct |
86 |
Correct |
271 ms |
85772 KB |
Output is correct |
87 |
Correct |
228 ms |
85772 KB |
Output is correct |
88 |
Correct |
301 ms |
91584 KB |
Output is correct |
89 |
Correct |
268 ms |
93376 KB |
Output is correct |
90 |
Correct |
308 ms |
95028 KB |
Output is correct |
91 |
Correct |
233 ms |
95028 KB |
Output is correct |
92 |
Correct |
307 ms |
99548 KB |
Output is correct |
93 |
Correct |
255 ms |
99548 KB |
Output is correct |
94 |
Correct |
352 ms |
103016 KB |
Output is correct |
95 |
Correct |
351 ms |
104856 KB |
Output is correct |
96 |
Correct |
285 ms |
104856 KB |
Output is correct |
97 |
Correct |
209 ms |
104856 KB |
Output is correct |
98 |
Correct |
244 ms |
104856 KB |
Output is correct |
99 |
Correct |
259 ms |
104856 KB |
Output is correct |
100 |
Correct |
361 ms |
114864 KB |
Output is correct |
101 |
Correct |
261 ms |
114864 KB |
Output is correct |
102 |
Correct |
314 ms |
114864 KB |
Output is correct |