#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(x) ((int) (x).size())
const ll inf = 1LL << 60;
struct divide_and_conquer_optimization {
int N, K, p[2] = {0, 0};
ll s = 0, mdp = -inf;
vector<int> &A, &B, omax;
vector<ll> dp;
multiset<int> h, l;
vector<vector<int>> sv, ev;
void addToSet(int v) {
if (l.empty() || v >= *h.begin()) {
h.insert(v), s += v;
if (sz(h) > K)
l.insert(v = *h.begin()), h.erase(h.begin()), s -= v;
} else
l.insert(v);
}
void removeFromSet(int v) {
if (l.empty() || v >= *h.begin()) {
h.erase(h.find(v)), s -= v;
if (sz(h) < K && !l.empty())
h.insert(v = *(--l.end())), s += v, l.erase(--l.end());
} else
l.erase(l.find(v));
}
ll compute(int i, int j) {
while (p[0] > j)
addToSet(A[--p[0]]), s -= B[p[0]];
while (p[1] < i + 1)
addToSet(A[p[1]]), s -= B[p[1]++];
while (p[0] < j)
removeFromSet(A[p[0]]), s += B[p[0]++];
while (p[1] > i + 1)
removeFromSet(A[--p[1]]), s += B[p[1]];
return s;
}
divide_and_conquer_optimization(int N, int K, vector<int> & A, vector<int> & B) : N(N), K(K), A(A), B(B), omax(N), dp(N, -inf), sv(N), ev(N) {
solvemax(K - 1, N, 0, N);
int lomax = 0;
for (int i = 0; i < N; i++) {
if (dp[i] != mdp)
continue;
for (int j = lomax; j <= omax[i]; j++) {
ll v = compute(i, j);
if (v == mdp)
sv[j].push_back(*h.begin()), ev[i].push_back(*h.begin());
}
lomax = omax[i];
}
}
void solvemax(int l, int r, int lo, int ro) {
if (l >= r)
return;
int m = (l + r) / 2;
for (int i = lo; i < min(ro, m - K + 2); i++) {
ll v = compute(m, i);
if (v >= dp[m])
dp[m] = v, omax[m] = i, mdp = max(mdp, v);
}
solvemax(m + 1, r, omax[m], ro);
solvemax(l, m, lo, omax[m] + 1);
}
};
int main() {
int N, K;
cin >> N >> K;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++)
cin >> B[i];
for (int i = 0; i < N; i++)
cin >> A[i];
divide_and_conquer_optimization dcf(N, K, A, B);
cout << dcf.mdp << "\n";
multiset<int> v;
for (int i = 0; i < N; i++) {
for (int u : dcf.sv[i])
v.insert(u);
cout << (!v.empty() && *v.begin() <= A[i]);
for (int u : dcf.ev[i])
v.erase(v.find(u));
}
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
13 ms |
984 KB |
Output is correct |
25 |
Correct |
22 ms |
1012 KB |
Output is correct |
26 |
Correct |
22 ms |
1104 KB |
Output is correct |
27 |
Correct |
12 ms |
1116 KB |
Output is correct |
28 |
Correct |
9 ms |
992 KB |
Output is correct |
29 |
Correct |
13 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
452 ms |
24980 KB |
Output is correct |
3 |
Correct |
635 ms |
23124 KB |
Output is correct |
4 |
Correct |
761 ms |
29020 KB |
Output is correct |
5 |
Correct |
818 ms |
24656 KB |
Output is correct |
6 |
Correct |
760 ms |
22940 KB |
Output is correct |
7 |
Correct |
1900 ms |
29480 KB |
Output is correct |
8 |
Correct |
797 ms |
26016 KB |
Output is correct |
9 |
Correct |
639 ms |
23196 KB |
Output is correct |
10 |
Correct |
710 ms |
23120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
452 ms |
24980 KB |
Output is correct |
3 |
Correct |
635 ms |
23124 KB |
Output is correct |
4 |
Correct |
761 ms |
29020 KB |
Output is correct |
5 |
Correct |
818 ms |
24656 KB |
Output is correct |
6 |
Correct |
760 ms |
22940 KB |
Output is correct |
7 |
Correct |
1900 ms |
29480 KB |
Output is correct |
8 |
Correct |
797 ms |
26016 KB |
Output is correct |
9 |
Correct |
639 ms |
23196 KB |
Output is correct |
10 |
Correct |
710 ms |
23120 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
434 ms |
24908 KB |
Output is correct |
13 |
Correct |
644 ms |
23136 KB |
Output is correct |
14 |
Correct |
819 ms |
28824 KB |
Output is correct |
15 |
Correct |
827 ms |
24660 KB |
Output is correct |
16 |
Correct |
750 ms |
22936 KB |
Output is correct |
17 |
Correct |
1891 ms |
29652 KB |
Output is correct |
18 |
Correct |
754 ms |
25944 KB |
Output is correct |
19 |
Correct |
619 ms |
23132 KB |
Output is correct |
20 |
Correct |
618 ms |
22936 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
950 ms |
28492 KB |
Output is correct |
32 |
Correct |
801 ms |
22952 KB |
Output is correct |
33 |
Correct |
1430 ms |
30120 KB |
Output is correct |
34 |
Correct |
1400 ms |
23120 KB |
Output is correct |
35 |
Correct |
1254 ms |
23132 KB |
Output is correct |
36 |
Correct |
2097 ms |
29008 KB |
Output is correct |
37 |
Correct |
817 ms |
23200 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
1116 KB |
Output is correct |
26 |
Correct |
13 ms |
984 KB |
Output is correct |
27 |
Correct |
22 ms |
1012 KB |
Output is correct |
28 |
Correct |
22 ms |
1104 KB |
Output is correct |
29 |
Correct |
12 ms |
1116 KB |
Output is correct |
30 |
Correct |
9 ms |
992 KB |
Output is correct |
31 |
Correct |
13 ms |
1116 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
452 ms |
24980 KB |
Output is correct |
34 |
Correct |
635 ms |
23124 KB |
Output is correct |
35 |
Correct |
761 ms |
29020 KB |
Output is correct |
36 |
Correct |
818 ms |
24656 KB |
Output is correct |
37 |
Correct |
760 ms |
22940 KB |
Output is correct |
38 |
Correct |
1900 ms |
29480 KB |
Output is correct |
39 |
Correct |
797 ms |
26016 KB |
Output is correct |
40 |
Correct |
639 ms |
23196 KB |
Output is correct |
41 |
Correct |
710 ms |
23120 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
434 ms |
24908 KB |
Output is correct |
44 |
Correct |
644 ms |
23136 KB |
Output is correct |
45 |
Correct |
819 ms |
28824 KB |
Output is correct |
46 |
Correct |
827 ms |
24660 KB |
Output is correct |
47 |
Correct |
750 ms |
22936 KB |
Output is correct |
48 |
Correct |
1891 ms |
29652 KB |
Output is correct |
49 |
Correct |
754 ms |
25944 KB |
Output is correct |
50 |
Correct |
619 ms |
23132 KB |
Output is correct |
51 |
Correct |
618 ms |
22936 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
950 ms |
28492 KB |
Output is correct |
63 |
Correct |
801 ms |
22952 KB |
Output is correct |
64 |
Correct |
1430 ms |
30120 KB |
Output is correct |
65 |
Correct |
1400 ms |
23120 KB |
Output is correct |
66 |
Correct |
1254 ms |
23132 KB |
Output is correct |
67 |
Correct |
2097 ms |
29008 KB |
Output is correct |
68 |
Correct |
817 ms |
23200 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
438 ms |
24988 KB |
Output is correct |
71 |
Correct |
688 ms |
23136 KB |
Output is correct |
72 |
Correct |
825 ms |
29024 KB |
Output is correct |
73 |
Correct |
807 ms |
24656 KB |
Output is correct |
74 |
Correct |
777 ms |
22940 KB |
Output is correct |
75 |
Correct |
1975 ms |
29644 KB |
Output is correct |
76 |
Correct |
732 ms |
26016 KB |
Output is correct |
77 |
Correct |
562 ms |
23116 KB |
Output is correct |
78 |
Correct |
641 ms |
23116 KB |
Output is correct |
79 |
Correct |
1 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
348 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
0 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
1095 ms |
28324 KB |
Output is correct |
90 |
Correct |
814 ms |
22936 KB |
Output is correct |
91 |
Correct |
1345 ms |
30120 KB |
Output is correct |
92 |
Correct |
1403 ms |
22944 KB |
Output is correct |
93 |
Correct |
1213 ms |
23140 KB |
Output is correct |
94 |
Correct |
1910 ms |
28940 KB |
Output is correct |
95 |
Correct |
816 ms |
23376 KB |
Output is correct |
96 |
Correct |
3 ms |
1116 KB |
Output is correct |
97 |
Correct |
13 ms |
992 KB |
Output is correct |
98 |
Correct |
22 ms |
1072 KB |
Output is correct |
99 |
Correct |
30 ms |
1116 KB |
Output is correct |
100 |
Correct |
19 ms |
1116 KB |
Output is correct |
101 |
Correct |
9 ms |
860 KB |
Output is correct |
102 |
Correct |
19 ms |
1116 KB |
Output is correct |
103 |
Correct |
1780 ms |
28940 KB |
Output is correct |
104 |
Correct |
1264 ms |
23200 KB |
Output is correct |
105 |
Correct |
1388 ms |
22948 KB |
Output is correct |
106 |
Correct |
1894 ms |
25576 KB |
Output is correct |
107 |
Correct |
189 ms |
29008 KB |
Output is correct |
108 |
Correct |
1790 ms |
29904 KB |
Output is correct |
109 |
Correct |
859 ms |
38372 KB |
Output is correct |
110 |
Correct |
1252 ms |
32944 KB |
Output is correct |
111 |
Correct |
465 ms |
25956 KB |
Output is correct |
112 |
Correct |
1218 ms |
28872 KB |
Output is correct |