# include "wiring.h"
# include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
// pairs
using pii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
#define mp make_pair
#define f first
#define s second
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pl>;
using vpd = V<pd>;
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const int MOD = 998244353;
const int MX = 2e5+5;
const ll BIG = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4]{1,0,-1,0}, dy[4]{0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
struct DSU {
vi e; void init(int N) { e = vi(N,-1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool sameSet(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) { // union by size
x = get(x), y = get(y); if (x == y) return 0;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y]; e[y] = x; return 1;
}
};
/*
inline namespace Helpers {
//////////// is_iterable
// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
// this gets used only when we can call begin() and end() on that type
tcT, class = void> struct is_iterable : false_type {};
tcT> struct is_iterable<T, void_t<decltype(begin(declval<T>())),
decltype(end(declval<T>()))
>
> : true_type {};
tcT> constexpr bool is_iterable_v = is_iterable<T>::value;
//////////// is_readable
tcT, class = void> struct is_readable : false_type {};
tcT> struct is_readable<T,
typename std::enable_if_t<
is_same_v<decltype(cin >> declval<T&>()), istream&>
>
> : true_type {};
tcT> constexpr bool is_readable_v = is_readable<T>::value;
//////////// is_printable
// // https://nafe.es/posts/2020-02-29-is-printable/
tcT, class = void> struct is_printable : false_type {};
tcT> struct is_printable<T,
typename std::enable_if_t<
is_same_v<decltype(cout << declval<T>()), ostream&>
>
> : true_type {};
tcT> constexpr bool is_printable_v = is_printable<T>::value;
}*/
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
// pairs
using pii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
#define mp make_pair
#define f first
#define s second
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pl>;
using vpd = V<pd>;
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
/*
inline namespace Helpers {
//////////// is_iterable
// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
// this gets used only when we can call begin() and end() on that type
tcT, class = void> struct is_iterable : false_type {};
tcT> struct is_iterable<T, void_t<decltype(begin(declval<T>())),
decltype(end(declval<T>()))
>
> : true_type {};
tcT> constexpr bool is_iterable_v = is_iterable<T>::value;
//////////// is_readable
tcT, class = void> struct is_readable : false_type {};
tcT> struct is_readable<T,
typename std::enable_if_t<
is_same_v<decltype(cin >> declval<T&>()), istream&>
>
> : true_type {};
tcT> constexpr bool is_readable_v = is_readable<T>::value;
//////////// is_printable
// // https://nafe.es/posts/2020-02-29-is-printable/
tcT, class = void> struct is_printable : false_type {};
tcT> struct is_printable<T,
typename std::enable_if_t<
is_same_v<decltype(cout << declval<T>()), ostream&>
>
> : true_type {};
tcT> constexpr bool is_printable_v = is_printable<T>::value;
}*/
const int N = 3e5 + 5;
const long long inf = 1e15;
long long a[N],dp[N],sf[N],pr[N];
long long min_total_length(std::vector<int> r, std::vector<int> b) {
int n = r.size() + b.size();
int x = r.size(); int y = b.size();
int prty = -1;
int curx = 0, cury = 0;
int curty;
vector < pii > seg;
// Divide the combined array into blocks
while (curx < x || cury < y) {
//cout<<curx<<" "<<a[curx]<<" "<<cury<<" "<<b[cury]<<"\n";
if(curx >= x || (cury < y && b[cury] <= r[curx])) {
a[curx + cury] = b[cury];
cury++;
curty = 0;
} else {
a[curx + cury] = r[curx];
curx++;
curty = 1;
}
if (curty != prty) {
prty = curty;
seg.pb({curx + cury - 1,curx + cury - 1});
} else seg.back().s++;
}
for (int i = 0; i <= n; i++) dp[i] = inf;
// Only consecutive blocks should be connected
// [1 1 1] [0 0 ||| 0 0] [1 1 1]
// Zeroes that are before "|||" are connected to the left part and ...
for (int i = 0; i < (int)seg.size(); i++) {
int st = seg[i].f;
int nd = seg[i].s;
long long val = 0;
if (i) {
for (int j = seg[i].f; j <= seg[i].s; j++) {
int cnt = j - seg[i].f + 1;
val += a[j] - a[st];
// If size(seg[i].f --- j) >= size(prev, seg[i - 1].s);
int mx = max(st - 1 - cnt + 1, seg[i - 1].f);
dp[j] = min(dp[j], sf[mx] + cnt * (a[st] - a[st - 1]) + val);
// If size(seg[i].f --- j) < size(prev, seg[i - 1].s);
if(st - cnt >= seg[i - 1].f) {
dp[j] = min(dp[j], pr[st - cnt] + val);
}
}
for (int j = seg[i].s; j >= seg[i].f - 1; j--) {
if (j != -1)
dp[j] = min(dp[j], dp[j + 1]);
}
}
if (i == (int)seg.size() - 1) continue;
val = 0;
for (int j = seg[i].s; j >= seg[i].f; j--) {
int cnt = seg[i].s - j + 1;
val += a[seg[i].s] - a[j];
sf[j] = val + dp[j - 1];
pr[j] = val + cnt * (a[seg[i + 1].f] - a[nd]) + dp[j - 1];
}
for (int j = seg[i].s - 1; j >= seg[i].f; j--) sf[j] = min(sf[j], sf[j + 1]);
for (int j = seg[i].f + 1; j <= seg[i].s; j++) pr[j] = min(pr[j], pr[j - 1]);
}
return dp[n - 1];
}
/*
int main() {
int n, m;
assert(2 == scanf("%d %d", &n, &m));
vector<int> r(n), b(m);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &r[i]));
for(int i = 0; i < m; i++)
assert(1 == scanf("%d", &b[i]));
long long res = min_total_length(r, b);
printf("%lld\n", res);
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
19 ms |
5332 KB |
Output is correct |
4 |
Correct |
19 ms |
6760 KB |
Output is correct |
5 |
Correct |
18 ms |
6004 KB |
Output is correct |
6 |
Correct |
25 ms |
8492 KB |
Output is correct |
7 |
Correct |
25 ms |
8496 KB |
Output is correct |
8 |
Correct |
27 ms |
8524 KB |
Output is correct |
9 |
Correct |
26 ms |
8492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
26 ms |
8652 KB |
Output is correct |
4 |
Correct |
25 ms |
10032 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
0 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
28 ms |
10544 KB |
Output is correct |
19 |
Correct |
28 ms |
10564 KB |
Output is correct |
20 |
Correct |
26 ms |
10416 KB |
Output is correct |
21 |
Correct |
27 ms |
10532 KB |
Output is correct |
22 |
Correct |
27 ms |
10544 KB |
Output is correct |
23 |
Correct |
28 ms |
10548 KB |
Output is correct |
24 |
Correct |
27 ms |
10564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
22 ms |
8016 KB |
Output is correct |
3 |
Correct |
22 ms |
8392 KB |
Output is correct |
4 |
Correct |
22 ms |
8028 KB |
Output is correct |
5 |
Correct |
21 ms |
8396 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
32 ms |
7820 KB |
Output is correct |
19 |
Correct |
25 ms |
9276 KB |
Output is correct |
20 |
Correct |
31 ms |
9276 KB |
Output is correct |
21 |
Correct |
22 ms |
9272 KB |
Output is correct |
22 |
Correct |
24 ms |
9016 KB |
Output is correct |
23 |
Correct |
26 ms |
8780 KB |
Output is correct |
24 |
Correct |
30 ms |
8896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
19 ms |
5332 KB |
Output is correct |
20 |
Correct |
19 ms |
6760 KB |
Output is correct |
21 |
Correct |
18 ms |
6004 KB |
Output is correct |
22 |
Correct |
25 ms |
8492 KB |
Output is correct |
23 |
Correct |
25 ms |
8496 KB |
Output is correct |
24 |
Correct |
27 ms |
8524 KB |
Output is correct |
25 |
Correct |
26 ms |
8492 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
26 ms |
8652 KB |
Output is correct |
29 |
Correct |
25 ms |
10032 KB |
Output is correct |
30 |
Correct |
1 ms |
308 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
312 KB |
Output is correct |
38 |
Correct |
0 ms |
312 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
316 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
308 KB |
Output is correct |
43 |
Correct |
28 ms |
10544 KB |
Output is correct |
44 |
Correct |
28 ms |
10564 KB |
Output is correct |
45 |
Correct |
26 ms |
10416 KB |
Output is correct |
46 |
Correct |
27 ms |
10532 KB |
Output is correct |
47 |
Correct |
27 ms |
10544 KB |
Output is correct |
48 |
Correct |
28 ms |
10548 KB |
Output is correct |
49 |
Correct |
27 ms |
10564 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
22 ms |
8016 KB |
Output is correct |
52 |
Correct |
22 ms |
8392 KB |
Output is correct |
53 |
Correct |
22 ms |
8028 KB |
Output is correct |
54 |
Correct |
21 ms |
8396 KB |
Output is correct |
55 |
Correct |
0 ms |
212 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
340 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
32 ms |
7820 KB |
Output is correct |
68 |
Correct |
25 ms |
9276 KB |
Output is correct |
69 |
Correct |
31 ms |
9276 KB |
Output is correct |
70 |
Correct |
22 ms |
9272 KB |
Output is correct |
71 |
Correct |
24 ms |
9016 KB |
Output is correct |
72 |
Correct |
26 ms |
8780 KB |
Output is correct |
73 |
Correct |
30 ms |
8896 KB |
Output is correct |
74 |
Correct |
34 ms |
10040 KB |
Output is correct |
75 |
Correct |
23 ms |
9652 KB |
Output is correct |
76 |
Correct |
28 ms |
10032 KB |
Output is correct |
77 |
Correct |
23 ms |
9524 KB |
Output is correct |
78 |
Correct |
23 ms |
9400 KB |
Output is correct |
79 |
Correct |
26 ms |
9356 KB |
Output is correct |
80 |
Correct |
35 ms |
9388 KB |
Output is correct |
81 |
Correct |
35 ms |
9532 KB |
Output is correct |
82 |
Correct |
24 ms |
9388 KB |
Output is correct |
83 |
Correct |
23 ms |
8628 KB |
Output is correct |
84 |
Correct |
27 ms |
9128 KB |
Output is correct |
85 |
Correct |
36 ms |
10056 KB |
Output is correct |
86 |
Correct |
26 ms |
9532 KB |
Output is correct |
87 |
Correct |
25 ms |
9400 KB |
Output is correct |
88 |
Correct |
28 ms |
10044 KB |
Output is correct |
89 |
Correct |
27 ms |
10048 KB |
Output is correct |
90 |
Correct |
34 ms |
9900 KB |
Output is correct |
91 |
Correct |
26 ms |
10036 KB |
Output is correct |
92 |
Correct |
26 ms |
10036 KB |
Output is correct |
93 |
Correct |
26 ms |
10044 KB |
Output is correct |
94 |
Correct |
30 ms |
9908 KB |
Output is correct |
95 |
Correct |
28 ms |
10040 KB |
Output is correct |
96 |
Correct |
26 ms |
9416 KB |
Output is correct |
97 |
Correct |
26 ms |
9932 KB |
Output is correct |
98 |
Correct |
37 ms |
9768 KB |
Output is correct |
99 |
Correct |
25 ms |
9812 KB |
Output is correct |
100 |
Correct |
27 ms |
10072 KB |
Output is correct |
101 |
Correct |
25 ms |
10032 KB |
Output is correct |
102 |
Correct |
26 ms |
10056 KB |
Output is correct |