#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
#include "wiring.h"
ll min_total_length(vector<int> r_g, vector<int> b_g) {
vi r, b;
for (int x : r_g)
r.pb(x);
for (int x : b_g)
b.pb(x);
int N = r.size();
int M = b.size();
vector<ii> vals;
for (int i = 0; i < N; i++) {
vals.pb({r[i], 0});
}
for (int i = 0; i < M; i++) {
vals.pb({b[i], 1});
}
vals.pb({-1e9, -1});
sort(vals.begin(), vals.end());
/* cout<<"vals: ";
for (ii p : vals)
cout<<"("<<p.fi<<", "<<p.se<<"); ";
cout<<endl;*/
int K = N + M;
vals[0].se = 1 - vals[1].se; // opposite start as placeholder
vi pfs(K + 1, 0);
for (int i = 1; i <= K; i++) {
pfs[i] = pfs[i - 1] + vals[i].fi;
}
/* cout<<"pfs: ";
for (ll x : pfs)
cout<<x<<" ";
cout<<endl;*/
vi dp(K + 1, 0);
ll prev = 1;
for (ll i = 1; i <= K; i++) { // i = start of comp
ll j = i; // j = end of comp
while (j < K && vals[j].se == vals[j + 1].se)
j++;
for (int k = prev; k < i; k++) { // try connecting last comp to i
dp[k] = min(dp[k], dp[k - 1] + (vals[i].fi - vals[k].fi));
}
ll minv = dp[i - 1];
ll sum = 0; // total connection cost to i - 1
for (int k = i; k <= j; k++) {
sum += (vals[k].fi - vals[i - 1].fi);
ll vc = 2 * i - k - 1; // paired (vc, k), (vc + 1, k - 1), up to (i - 1, i)
if (vc >= prev) {
ll toadd = vals[i - 1].fi * (i - vc) + (pfs[vc - 1] - pfs[i - 1]);
// toadd: extra connection cost from pairing w/ vc, vc+1, ... instead of all w/ i-1
minv = min(minv, dp[vc - 1] + toadd);
}
dp[k] = minv + sum;
}
prev = i;
i = j;
}
return dp[K];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
432 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
504 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
22 ms |
9320 KB |
Output is correct |
4 |
Correct |
23 ms |
9676 KB |
Output is correct |
5 |
Correct |
24 ms |
10448 KB |
Output is correct |
6 |
Correct |
34 ms |
11764 KB |
Output is correct |
7 |
Correct |
30 ms |
11720 KB |
Output is correct |
8 |
Correct |
30 ms |
12496 KB |
Output is correct |
9 |
Correct |
30 ms |
11716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
31 ms |
11544 KB |
Output is correct |
4 |
Correct |
35 ms |
12192 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
436 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
31 ms |
11716 KB |
Output is correct |
19 |
Correct |
31 ms |
11724 KB |
Output is correct |
20 |
Correct |
45 ms |
11716 KB |
Output is correct |
21 |
Correct |
31 ms |
12236 KB |
Output is correct |
22 |
Correct |
31 ms |
11724 KB |
Output is correct |
23 |
Correct |
35 ms |
11676 KB |
Output is correct |
24 |
Correct |
30 ms |
11976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
23 ms |
10952 KB |
Output is correct |
3 |
Correct |
32 ms |
10948 KB |
Output is correct |
4 |
Correct |
30 ms |
10948 KB |
Output is correct |
5 |
Correct |
29 ms |
10948 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
29 ms |
11052 KB |
Output is correct |
19 |
Correct |
30 ms |
10948 KB |
Output is correct |
20 |
Correct |
24 ms |
11980 KB |
Output is correct |
21 |
Correct |
30 ms |
10952 KB |
Output is correct |
22 |
Correct |
30 ms |
11728 KB |
Output is correct |
23 |
Correct |
25 ms |
11680 KB |
Output is correct |
24 |
Correct |
23 ms |
11564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
432 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
504 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
22 ms |
9320 KB |
Output is correct |
20 |
Correct |
23 ms |
9676 KB |
Output is correct |
21 |
Correct |
24 ms |
10448 KB |
Output is correct |
22 |
Correct |
34 ms |
11764 KB |
Output is correct |
23 |
Correct |
30 ms |
11720 KB |
Output is correct |
24 |
Correct |
30 ms |
12496 KB |
Output is correct |
25 |
Correct |
30 ms |
11716 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
31 ms |
11544 KB |
Output is correct |
29 |
Correct |
35 ms |
12192 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
436 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
31 ms |
11716 KB |
Output is correct |
44 |
Correct |
31 ms |
11724 KB |
Output is correct |
45 |
Correct |
45 ms |
11716 KB |
Output is correct |
46 |
Correct |
31 ms |
12236 KB |
Output is correct |
47 |
Correct |
31 ms |
11724 KB |
Output is correct |
48 |
Correct |
35 ms |
11676 KB |
Output is correct |
49 |
Correct |
30 ms |
11976 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
23 ms |
10952 KB |
Output is correct |
52 |
Correct |
32 ms |
10948 KB |
Output is correct |
53 |
Correct |
30 ms |
10948 KB |
Output is correct |
54 |
Correct |
29 ms |
10948 KB |
Output is correct |
55 |
Correct |
0 ms |
344 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
29 ms |
11052 KB |
Output is correct |
68 |
Correct |
30 ms |
10948 KB |
Output is correct |
69 |
Correct |
24 ms |
11980 KB |
Output is correct |
70 |
Correct |
30 ms |
10952 KB |
Output is correct |
71 |
Correct |
30 ms |
11728 KB |
Output is correct |
72 |
Correct |
25 ms |
11680 KB |
Output is correct |
73 |
Correct |
23 ms |
11564 KB |
Output is correct |
74 |
Correct |
31 ms |
11716 KB |
Output is correct |
75 |
Correct |
29 ms |
11444 KB |
Output is correct |
76 |
Correct |
32 ms |
11720 KB |
Output is correct |
77 |
Correct |
30 ms |
11460 KB |
Output is correct |
78 |
Correct |
24 ms |
11216 KB |
Output is correct |
79 |
Correct |
22 ms |
11172 KB |
Output is correct |
80 |
Correct |
30 ms |
11468 KB |
Output is correct |
81 |
Correct |
29 ms |
11464 KB |
Output is correct |
82 |
Correct |
24 ms |
11464 KB |
Output is correct |
83 |
Correct |
30 ms |
11204 KB |
Output is correct |
84 |
Correct |
34 ms |
11720 KB |
Output is correct |
85 |
Correct |
24 ms |
11716 KB |
Output is correct |
86 |
Correct |
31 ms |
11576 KB |
Output is correct |
87 |
Correct |
26 ms |
11728 KB |
Output is correct |
88 |
Correct |
31 ms |
11692 KB |
Output is correct |
89 |
Correct |
31 ms |
11716 KB |
Output is correct |
90 |
Correct |
32 ms |
11612 KB |
Output is correct |
91 |
Correct |
27 ms |
11728 KB |
Output is correct |
92 |
Correct |
32 ms |
11780 KB |
Output is correct |
93 |
Correct |
33 ms |
11720 KB |
Output is correct |
94 |
Correct |
33 ms |
11692 KB |
Output is correct |
95 |
Correct |
31 ms |
11728 KB |
Output is correct |
96 |
Correct |
38 ms |
12312 KB |
Output is correct |
97 |
Correct |
31 ms |
11716 KB |
Output is correct |
98 |
Correct |
26 ms |
12488 KB |
Output is correct |
99 |
Correct |
26 ms |
12460 KB |
Output is correct |
100 |
Correct |
27 ms |
11720 KB |
Output is correct |
101 |
Correct |
31 ms |
11616 KB |
Output is correct |
102 |
Correct |
32 ms |
12448 KB |
Output is correct |