#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAX_N = 3e5 + 1;
vector<ll> dp[MAX_N];
vector<ll> up[MAX_N];
vector<ll> down[MAX_N];
vector<ll> sum[MAX_N];
vector<int> h[MAX_N];
// f(rom), j'th block of x=i place
ll ps(int i, int j, int f) {
int H = h[i][j] - 1;
int idx = upper_bound(h[f].begin(), h[f].end(), H) - h[f].begin();
// cout << "ps: " << i << ' ' << j << ' ' << f << ' ' << sum[f][idx] << endl;
return sum[f][idx];
}
// ub() is minimum exceeding h
int ub(int i, int j, int f) {
int H = h[i][j];
int idx = upper_bound(h[f].begin(), h[f].end(), H) - h[f].begin();
// cout << "ub: " << i << ' ' << j << ' ' << f << ' ' << idx << endl;
return idx;
}
ll max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> W) {
for (int i=0; i<m; ++i) h[X[i]].push_back(Y[i]);
for (int i=0; i<n; ++i) {
h[i].push_back(n);
sort(h[i].begin(), h[i].end());
int sz = h[i].size() + 1;
dp[i].resize(sz, 0);
up[i].resize(sz, 0);
down[i].resize(sz, 0);
sum[i].resize(sz, 0);
}
for (int i=0; i<m; ++i) {
int y = lower_bound(h[X[i]].begin(), h[X[i]].end(), Y[i]) - h[X[i]].begin() + 1;
sum[X[i]][y] = W[i];
}
for (int i=0; i<n; ++i) {
for (int j=1; j<h[i].size(); ++j) {
sum[i][j] += sum[i][j-1];
}
}
// i = 1
{
int N = h[1].size(), n1 = h[0].size();
vector<ll> ps1(n1+1, 0), ps2(n1);
ps2[0] = up[0][0];
for (int j=n1-1; j>=0; --j) ps1[j] = max(ps1[j+1], dp[0][j] + ps(0, j, 1));
for (int j=1; j<n1; ++j) ps2[j] = max(ps2[j-1], up[0][j] - sum[0][j]);
for (int j=0; j<N; ++j) {
down[1][j] = ps1[ub(1, j, 0)] - sum[1][j];
if (ub(1, j, 0) > 0) up[1][j] = ps2[ub(1, j, 0)-1] + ps(1, j, 0);
dp[1][j] = max(down[1][j], up[1][j]);
// cout << j << ' ' << down[1][j] << ' ' << up[1][j] << endl;
}
}
for (int i=2; i<n; ++i) {
int N = h[i].size(), n1 = h[i-1].size(), n2 = h[i-2].size();
vector<ll> ps1(n1+1, 0), ps2(n1), ps3(n2), ps4(n2+1, 0);
ps2[0] = up[i-1][0];
ps3[0] = dp[i-2][0];
for (int j=n1-1; j>=0; --j) ps1[j] = max(ps1[j+1], dp[i-1][j] + ps(i-1, j, i));
for (int j=1; j<n1; ++j) ps2[j] = max(ps2[j-1], up[i-1][j] - sum[i-1][j]);
for (int j=1; j<n2; ++j) ps3[j] = max(ps3[j-1], dp[i-2][j]);
for (int j=n2-1; j>=0; --j) ps4[j] = max(ps4[j+1], dp[i-2][j] + ps(i-2, j, i-1));
for (int j=0; j<N; ++j) {
down[i][j] = ps1[ub(i, j, i-1)] - sum[i][j];
up[i][j] = ps4[ub(i, j, i-2)];
if (ub(i, j, i-1) > 0) up[i][j] = max(up[i][j], ps2[ub(i, j, i-1)-1] + ps(i, j, i-1));
if (ub(i, j, i-2) > 0) up[i][j] = max(up[i][j], ps3[ub(i, j, i-2)-1] + ps(i, j, i-1));
dp[i][j] = max(down[i][j], up[i][j]);
}
}
ll ans = *max_element(dp[n-1].begin(), dp[n-1].end());
return ans;
}
Compilation message
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:49:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int j=1; j<h[i].size(); ++j) {
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
55556 KB |
Output is correct |
2 |
Correct |
121 ms |
60344 KB |
Output is correct |
3 |
Correct |
61 ms |
51164 KB |
Output is correct |
4 |
Correct |
64 ms |
51044 KB |
Output is correct |
5 |
Correct |
232 ms |
76584 KB |
Output is correct |
6 |
Correct |
260 ms |
74576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35412 KB |
Output is correct |
2 |
Correct |
158 ms |
64412 KB |
Output is correct |
3 |
Correct |
190 ms |
69584 KB |
Output is correct |
4 |
Correct |
98 ms |
56936 KB |
Output is correct |
5 |
Correct |
110 ms |
60360 KB |
Output is correct |
6 |
Correct |
19 ms |
35508 KB |
Output is correct |
7 |
Correct |
18 ms |
35504 KB |
Output is correct |
8 |
Correct |
19 ms |
35412 KB |
Output is correct |
9 |
Correct |
19 ms |
35412 KB |
Output is correct |
10 |
Correct |
63 ms |
51244 KB |
Output is correct |
11 |
Correct |
60 ms |
51160 KB |
Output is correct |
12 |
Correct |
120 ms |
57036 KB |
Output is correct |
13 |
Correct |
112 ms |
60352 KB |
Output is correct |
14 |
Correct |
120 ms |
57004 KB |
Output is correct |
15 |
Correct |
118 ms |
59516 KB |
Output is correct |
16 |
Correct |
100 ms |
57080 KB |
Output is correct |
17 |
Correct |
109 ms |
59412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
51156 KB |
Output is correct |
2 |
Correct |
76 ms |
51132 KB |
Output is correct |
3 |
Correct |
95 ms |
51928 KB |
Output is correct |
4 |
Correct |
78 ms |
52664 KB |
Output is correct |
5 |
Correct |
122 ms |
55112 KB |
Output is correct |
6 |
Correct |
115 ms |
54480 KB |
Output is correct |
7 |
Correct |
128 ms |
55080 KB |
Output is correct |
8 |
Correct |
130 ms |
55028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35524 KB |
Output is correct |
2 |
Correct |
19 ms |
35528 KB |
Output is correct |
3 |
Correct |
18 ms |
35460 KB |
Output is correct |
4 |
Correct |
20 ms |
35532 KB |
Output is correct |
5 |
Correct |
21 ms |
35412 KB |
Output is correct |
6 |
Correct |
17 ms |
35412 KB |
Output is correct |
7 |
Correct |
17 ms |
35428 KB |
Output is correct |
8 |
Correct |
20 ms |
35488 KB |
Output is correct |
9 |
Correct |
19 ms |
35564 KB |
Output is correct |
10 |
Correct |
20 ms |
35628 KB |
Output is correct |
11 |
Correct |
20 ms |
35552 KB |
Output is correct |
12 |
Correct |
19 ms |
35540 KB |
Output is correct |
13 |
Correct |
18 ms |
35520 KB |
Output is correct |
14 |
Correct |
19 ms |
35540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35524 KB |
Output is correct |
2 |
Correct |
19 ms |
35528 KB |
Output is correct |
3 |
Correct |
18 ms |
35460 KB |
Output is correct |
4 |
Correct |
20 ms |
35532 KB |
Output is correct |
5 |
Correct |
21 ms |
35412 KB |
Output is correct |
6 |
Correct |
17 ms |
35412 KB |
Output is correct |
7 |
Correct |
17 ms |
35428 KB |
Output is correct |
8 |
Correct |
20 ms |
35488 KB |
Output is correct |
9 |
Correct |
19 ms |
35564 KB |
Output is correct |
10 |
Correct |
20 ms |
35628 KB |
Output is correct |
11 |
Correct |
20 ms |
35552 KB |
Output is correct |
12 |
Correct |
19 ms |
35540 KB |
Output is correct |
13 |
Correct |
18 ms |
35520 KB |
Output is correct |
14 |
Correct |
19 ms |
35540 KB |
Output is correct |
15 |
Correct |
19 ms |
35488 KB |
Output is correct |
16 |
Correct |
20 ms |
35652 KB |
Output is correct |
17 |
Correct |
62 ms |
38988 KB |
Output is correct |
18 |
Correct |
55 ms |
39136 KB |
Output is correct |
19 |
Correct |
47 ms |
39112 KB |
Output is correct |
20 |
Correct |
44 ms |
39072 KB |
Output is correct |
21 |
Correct |
50 ms |
39108 KB |
Output is correct |
22 |
Correct |
77 ms |
42688 KB |
Output is correct |
23 |
Correct |
23 ms |
36168 KB |
Output is correct |
24 |
Correct |
37 ms |
37892 KB |
Output is correct |
25 |
Correct |
18 ms |
35540 KB |
Output is correct |
26 |
Correct |
32 ms |
36144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35524 KB |
Output is correct |
2 |
Correct |
19 ms |
35528 KB |
Output is correct |
3 |
Correct |
18 ms |
35460 KB |
Output is correct |
4 |
Correct |
20 ms |
35532 KB |
Output is correct |
5 |
Correct |
21 ms |
35412 KB |
Output is correct |
6 |
Correct |
17 ms |
35412 KB |
Output is correct |
7 |
Correct |
17 ms |
35428 KB |
Output is correct |
8 |
Correct |
20 ms |
35488 KB |
Output is correct |
9 |
Correct |
19 ms |
35564 KB |
Output is correct |
10 |
Correct |
20 ms |
35628 KB |
Output is correct |
11 |
Correct |
20 ms |
35552 KB |
Output is correct |
12 |
Correct |
19 ms |
35540 KB |
Output is correct |
13 |
Correct |
18 ms |
35520 KB |
Output is correct |
14 |
Correct |
19 ms |
35540 KB |
Output is correct |
15 |
Correct |
19 ms |
35488 KB |
Output is correct |
16 |
Correct |
20 ms |
35652 KB |
Output is correct |
17 |
Correct |
62 ms |
38988 KB |
Output is correct |
18 |
Correct |
55 ms |
39136 KB |
Output is correct |
19 |
Correct |
47 ms |
39112 KB |
Output is correct |
20 |
Correct |
44 ms |
39072 KB |
Output is correct |
21 |
Correct |
50 ms |
39108 KB |
Output is correct |
22 |
Correct |
77 ms |
42688 KB |
Output is correct |
23 |
Correct |
23 ms |
36168 KB |
Output is correct |
24 |
Correct |
37 ms |
37892 KB |
Output is correct |
25 |
Correct |
18 ms |
35540 KB |
Output is correct |
26 |
Correct |
32 ms |
36144 KB |
Output is correct |
27 |
Correct |
22 ms |
35996 KB |
Output is correct |
28 |
Correct |
196 ms |
52804 KB |
Output is correct |
29 |
Correct |
251 ms |
59820 KB |
Output is correct |
30 |
Correct |
233 ms |
59640 KB |
Output is correct |
31 |
Correct |
219 ms |
59700 KB |
Output is correct |
32 |
Correct |
258 ms |
59600 KB |
Output is correct |
33 |
Correct |
265 ms |
59720 KB |
Output is correct |
34 |
Correct |
245 ms |
59756 KB |
Output is correct |
35 |
Correct |
107 ms |
44944 KB |
Output is correct |
36 |
Correct |
258 ms |
59756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
51156 KB |
Output is correct |
2 |
Correct |
76 ms |
51132 KB |
Output is correct |
3 |
Correct |
95 ms |
51928 KB |
Output is correct |
4 |
Correct |
78 ms |
52664 KB |
Output is correct |
5 |
Correct |
122 ms |
55112 KB |
Output is correct |
6 |
Correct |
115 ms |
54480 KB |
Output is correct |
7 |
Correct |
128 ms |
55080 KB |
Output is correct |
8 |
Correct |
130 ms |
55028 KB |
Output is correct |
9 |
Correct |
123 ms |
54856 KB |
Output is correct |
10 |
Correct |
93 ms |
50504 KB |
Output is correct |
11 |
Correct |
197 ms |
65520 KB |
Output is correct |
12 |
Correct |
19 ms |
35412 KB |
Output is correct |
13 |
Correct |
19 ms |
35520 KB |
Output is correct |
14 |
Correct |
18 ms |
35412 KB |
Output is correct |
15 |
Correct |
20 ms |
35408 KB |
Output is correct |
16 |
Correct |
19 ms |
35476 KB |
Output is correct |
17 |
Correct |
19 ms |
35412 KB |
Output is correct |
18 |
Correct |
77 ms |
51160 KB |
Output is correct |
19 |
Correct |
64 ms |
51168 KB |
Output is correct |
20 |
Correct |
67 ms |
51164 KB |
Output is correct |
21 |
Correct |
57 ms |
51160 KB |
Output is correct |
22 |
Correct |
150 ms |
57796 KB |
Output is correct |
23 |
Correct |
235 ms |
65636 KB |
Output is correct |
24 |
Correct |
174 ms |
66124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
55556 KB |
Output is correct |
2 |
Correct |
121 ms |
60344 KB |
Output is correct |
3 |
Correct |
61 ms |
51164 KB |
Output is correct |
4 |
Correct |
64 ms |
51044 KB |
Output is correct |
5 |
Correct |
232 ms |
76584 KB |
Output is correct |
6 |
Correct |
260 ms |
74576 KB |
Output is correct |
7 |
Correct |
19 ms |
35412 KB |
Output is correct |
8 |
Correct |
158 ms |
64412 KB |
Output is correct |
9 |
Correct |
190 ms |
69584 KB |
Output is correct |
10 |
Correct |
98 ms |
56936 KB |
Output is correct |
11 |
Correct |
110 ms |
60360 KB |
Output is correct |
12 |
Correct |
19 ms |
35508 KB |
Output is correct |
13 |
Correct |
18 ms |
35504 KB |
Output is correct |
14 |
Correct |
19 ms |
35412 KB |
Output is correct |
15 |
Correct |
19 ms |
35412 KB |
Output is correct |
16 |
Correct |
63 ms |
51244 KB |
Output is correct |
17 |
Correct |
60 ms |
51160 KB |
Output is correct |
18 |
Correct |
120 ms |
57036 KB |
Output is correct |
19 |
Correct |
112 ms |
60352 KB |
Output is correct |
20 |
Correct |
120 ms |
57004 KB |
Output is correct |
21 |
Correct |
118 ms |
59516 KB |
Output is correct |
22 |
Correct |
100 ms |
57080 KB |
Output is correct |
23 |
Correct |
109 ms |
59412 KB |
Output is correct |
24 |
Correct |
63 ms |
51156 KB |
Output is correct |
25 |
Correct |
76 ms |
51132 KB |
Output is correct |
26 |
Correct |
95 ms |
51928 KB |
Output is correct |
27 |
Correct |
78 ms |
52664 KB |
Output is correct |
28 |
Correct |
122 ms |
55112 KB |
Output is correct |
29 |
Correct |
115 ms |
54480 KB |
Output is correct |
30 |
Correct |
128 ms |
55080 KB |
Output is correct |
31 |
Correct |
130 ms |
55028 KB |
Output is correct |
32 |
Correct |
19 ms |
35524 KB |
Output is correct |
33 |
Correct |
19 ms |
35528 KB |
Output is correct |
34 |
Correct |
18 ms |
35460 KB |
Output is correct |
35 |
Correct |
20 ms |
35532 KB |
Output is correct |
36 |
Correct |
21 ms |
35412 KB |
Output is correct |
37 |
Correct |
17 ms |
35412 KB |
Output is correct |
38 |
Correct |
17 ms |
35428 KB |
Output is correct |
39 |
Correct |
20 ms |
35488 KB |
Output is correct |
40 |
Correct |
19 ms |
35564 KB |
Output is correct |
41 |
Correct |
20 ms |
35628 KB |
Output is correct |
42 |
Correct |
20 ms |
35552 KB |
Output is correct |
43 |
Correct |
19 ms |
35540 KB |
Output is correct |
44 |
Correct |
18 ms |
35520 KB |
Output is correct |
45 |
Correct |
19 ms |
35540 KB |
Output is correct |
46 |
Correct |
19 ms |
35488 KB |
Output is correct |
47 |
Correct |
20 ms |
35652 KB |
Output is correct |
48 |
Correct |
62 ms |
38988 KB |
Output is correct |
49 |
Correct |
55 ms |
39136 KB |
Output is correct |
50 |
Correct |
47 ms |
39112 KB |
Output is correct |
51 |
Correct |
44 ms |
39072 KB |
Output is correct |
52 |
Correct |
50 ms |
39108 KB |
Output is correct |
53 |
Correct |
77 ms |
42688 KB |
Output is correct |
54 |
Correct |
23 ms |
36168 KB |
Output is correct |
55 |
Correct |
37 ms |
37892 KB |
Output is correct |
56 |
Correct |
18 ms |
35540 KB |
Output is correct |
57 |
Correct |
32 ms |
36144 KB |
Output is correct |
58 |
Correct |
22 ms |
35996 KB |
Output is correct |
59 |
Correct |
196 ms |
52804 KB |
Output is correct |
60 |
Correct |
251 ms |
59820 KB |
Output is correct |
61 |
Correct |
233 ms |
59640 KB |
Output is correct |
62 |
Correct |
219 ms |
59700 KB |
Output is correct |
63 |
Correct |
258 ms |
59600 KB |
Output is correct |
64 |
Correct |
265 ms |
59720 KB |
Output is correct |
65 |
Correct |
245 ms |
59756 KB |
Output is correct |
66 |
Correct |
107 ms |
44944 KB |
Output is correct |
67 |
Correct |
258 ms |
59756 KB |
Output is correct |
68 |
Correct |
123 ms |
54856 KB |
Output is correct |
69 |
Correct |
93 ms |
50504 KB |
Output is correct |
70 |
Correct |
197 ms |
65520 KB |
Output is correct |
71 |
Correct |
19 ms |
35412 KB |
Output is correct |
72 |
Correct |
19 ms |
35520 KB |
Output is correct |
73 |
Correct |
18 ms |
35412 KB |
Output is correct |
74 |
Correct |
20 ms |
35408 KB |
Output is correct |
75 |
Correct |
19 ms |
35476 KB |
Output is correct |
76 |
Correct |
19 ms |
35412 KB |
Output is correct |
77 |
Correct |
77 ms |
51160 KB |
Output is correct |
78 |
Correct |
64 ms |
51168 KB |
Output is correct |
79 |
Correct |
67 ms |
51164 KB |
Output is correct |
80 |
Correct |
57 ms |
51160 KB |
Output is correct |
81 |
Correct |
150 ms |
57796 KB |
Output is correct |
82 |
Correct |
235 ms |
65636 KB |
Output is correct |
83 |
Correct |
174 ms |
66124 KB |
Output is correct |
84 |
Correct |
296 ms |
74240 KB |
Output is correct |
85 |
Correct |
318 ms |
75320 KB |
Output is correct |
86 |
Correct |
245 ms |
71416 KB |
Output is correct |
87 |
Correct |
260 ms |
72908 KB |
Output is correct |
88 |
Correct |
20 ms |
35448 KB |
Output is correct |
89 |
Correct |
254 ms |
72960 KB |
Output is correct |
90 |
Correct |
241 ms |
73380 KB |
Output is correct |
91 |
Correct |
242 ms |
73904 KB |
Output is correct |