#include "fish.h"
#include <vector>
#include <algorithm>
#include <iostream>
#define int long long
using namespace std;
vector<pair<int, int>> adj[100005];
vector<pair<int, int>> prf[100005];
const long long inf = 1e18;
int _N;
int get_prfsum(int X, int to)
{
if (X <= 0 || _N+1 <= X) return 0;
return (*--lower_bound(prf[X].begin(), prf[X].end(), make_pair(to, inf))).second;
}
vector<int> dp[100005][3];
vector<int> suf[100005];
vector<int> prf0[100005], prf1[100005], suf0[100005];
vector<int> cpx[100005];
long long max_weights(signed N, signed M, std::vector<signed> X, std::vector<signed> Y,
std::vector<signed> W) {
_N= N;
for (int i = 0; i < M; i++) {
adj[X[i]+1].push_back(make_pair(Y[i] + 2, W[i]));
}
for (int i = 1; i <= N; i++) sort(adj[i].begin(), adj[i].end());
for (int i = 1; i <= N; i++) {
prf[i].resize(adj[i].size() + 1);
prf[i][0] = make_pair(0, 0);
for (int k = 1; k <= adj[i].size(); k++) {
prf[i][k] = make_pair(adj[i][k-1].first, adj[i][k-1].second + prf[i][k-1].second);
}
}
cpx[0].push_back(0);
for (int i = 1; i <= N; i++) {
cpx[i].push_back(0);
for (pair<int, int> p:adj[i]) cpx[i].push_back(p.first - 1);
cpx[i].push_back(N+1);
}
for (int i = 1; i <= N; i++) {
dp[i][0].resize(cpx[i].size());
dp[i][1].resize(cpx[i].size());
dp[i][2].resize(cpx[i-1].size());
suf[i].resize(dp[i][0].size());
prf0[i].resize(dp[i][0].size());
prf1[i].resize(dp[i][2].size());
suf0[i].resize(dp[i][2].size());
}
for (int i = 1; i <= N; i++) {
if (i >= 2) {
for (int j = 0; j < (int)dp[i-1][2].size(); j++) dp[i][2][0] = max(dp[i][2][0], dp[i-1][2][j]);
for (int j = 1; j < (int)dp[i][2].size(); j++) {
dp[i][2][j] = max(dp[i-1][0][j], dp[i-1][1][j]) + get_prfsum(i, cpx[i-1][j]);
}
for (int j = 1; j < (int)dp[i][1].size(); j++) {
int height = cpx[i][j]; //height까지 끌어올리기
//height보다 더 '큰' 최초의 인덱스 k를 찾자
int low = upper_bound(cpx[i-1].begin(), cpx[i-1].end(), height) - cpx[i-1].begin();
if (low == cpx[i-1].size()) {
dp[i][1][j] = -inf;
continue;
}
dp[i][1][j] = suf[i-1][low] - get_prfsum(i, cpx[i][j]);
}
for (int j = 1; j < (int)dp[i][0].size(); j++) {
int ord = upper_bound(cpx[i-1].begin(), cpx[i-1].end(), cpx[i][j]) - cpx[i-1].begin();
int M1 = ord == 1 ? -inf : (prf0[i-1][ord-1] + get_prfsum(i-1, cpx[i][j]));
int ord2 = upper_bound(cpx[i-2].begin(), cpx[i-2].end(), cpx[i][j]) - cpx[i-2].begin();
int M2 = prf1[i-1][ord2-1] + get_prfsum(i-1, cpx[i][j]);
int M3 = (i == 2 || ord2 >= cpx[i-2].size()) ? -inf : suf0[i-1][ord2];
dp[i][0][j] = max({M1, M2, M3});
}
}
for (int j = 1; j < (int)dp[i][1].size(); j++) {
prf0[i][j] = dp[i][0][j] - get_prfsum(i, cpx[i][j]);
suf[i][j] = max(dp[i][0][j], dp[i][1][j]) + get_prfsum(i+1, cpx[i][j]);
}
for (int j = 0; j < (int)dp[i][2].size(); j++) {
prf1[i][j] = dp[i][2][j] - get_prfsum(i, cpx[i-1][j]);
suf0[i][j] = dp[i][2][j];
}
for (int j = 2; j < (int)dp[i][1].size(); j++) prf0[i][j] = max(prf0[i][j], prf0[i][j-1]);
for (int j = 1; j < (int)dp[i][2].size(); j++) prf1[i][j] = max(prf1[i][j], prf1[i][j-1]);
for (int j = (int)dp[i][2].size() - 2; j >= 0; j--) suf0[i][j] = max(suf0[i][j], suf0[i][j+1]);
for (int j = (int)dp[i][1].size() - 2; j >= 1; j--) suf[i][j] = max(suf[i][j], suf[i][j+1]);
}
int ans = 0;
for (int i = 1; i < (int)dp[N][0].size(); i++) {
ans = max(ans, dp[N][0][i]);
}
for (int i = 1; i < (int)dp[N][1].size(); i++) ans = max(ans, dp[N][1][i]);
for (int i = 0; i < (int)dp[N][2].size(); i++) ans = max(ans, dp[N][2][i]);
return ans;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:32:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int k = 1; k <= adj[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~
fish.cpp:61:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if (low == cpx[i-1].size()) {
| ~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:72:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | int M3 = (i == 2 || ord2 >= cpx[i-2].size()) ? -inf : suf0[i-1][ord2];
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
60032 KB |
Output is correct |
2 |
Correct |
148 ms |
65464 KB |
Output is correct |
3 |
Correct |
73 ms |
51936 KB |
Output is correct |
4 |
Correct |
89 ms |
51916 KB |
Output is correct |
5 |
Correct |
262 ms |
90752 KB |
Output is correct |
6 |
Correct |
337 ms |
92928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
208 ms |
71000 KB |
Output is correct |
3 |
Correct |
212 ms |
78952 KB |
Output is correct |
4 |
Correct |
110 ms |
59964 KB |
Output is correct |
5 |
Correct |
183 ms |
65384 KB |
Output is correct |
6 |
Correct |
15 ms |
23672 KB |
Output is correct |
7 |
Correct |
14 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23712 KB |
Output is correct |
9 |
Correct |
13 ms |
23788 KB |
Output is correct |
10 |
Correct |
71 ms |
51900 KB |
Output is correct |
11 |
Correct |
90 ms |
51892 KB |
Output is correct |
12 |
Correct |
125 ms |
60044 KB |
Output is correct |
13 |
Correct |
135 ms |
65424 KB |
Output is correct |
14 |
Correct |
120 ms |
60160 KB |
Output is correct |
15 |
Correct |
142 ms |
64052 KB |
Output is correct |
16 |
Correct |
129 ms |
60032 KB |
Output is correct |
17 |
Correct |
153 ms |
64060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
52044 KB |
Output is correct |
2 |
Correct |
73 ms |
51888 KB |
Output is correct |
3 |
Correct |
103 ms |
54628 KB |
Output is correct |
4 |
Correct |
96 ms |
55648 KB |
Output is correct |
5 |
Correct |
119 ms |
62188 KB |
Output is correct |
6 |
Correct |
124 ms |
61632 KB |
Output is correct |
7 |
Correct |
136 ms |
62028 KB |
Output is correct |
8 |
Correct |
121 ms |
62104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23696 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
14 ms |
23792 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23768 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
15 ms |
23700 KB |
Output is correct |
9 |
Correct |
14 ms |
23884 KB |
Output is correct |
10 |
Correct |
15 ms |
24148 KB |
Output is correct |
11 |
Correct |
13 ms |
23892 KB |
Output is correct |
12 |
Correct |
13 ms |
23924 KB |
Output is correct |
13 |
Correct |
14 ms |
23788 KB |
Output is correct |
14 |
Correct |
13 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23696 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
14 ms |
23792 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23768 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
15 ms |
23700 KB |
Output is correct |
9 |
Correct |
14 ms |
23884 KB |
Output is correct |
10 |
Correct |
15 ms |
24148 KB |
Output is correct |
11 |
Correct |
13 ms |
23892 KB |
Output is correct |
12 |
Correct |
13 ms |
23924 KB |
Output is correct |
13 |
Correct |
14 ms |
23788 KB |
Output is correct |
14 |
Correct |
13 ms |
23928 KB |
Output is correct |
15 |
Correct |
16 ms |
23784 KB |
Output is correct |
16 |
Correct |
15 ms |
24056 KB |
Output is correct |
17 |
Correct |
53 ms |
30096 KB |
Output is correct |
18 |
Correct |
47 ms |
30960 KB |
Output is correct |
19 |
Correct |
41 ms |
30688 KB |
Output is correct |
20 |
Correct |
40 ms |
30668 KB |
Output is correct |
21 |
Correct |
51 ms |
30572 KB |
Output is correct |
22 |
Correct |
80 ms |
37500 KB |
Output is correct |
23 |
Correct |
19 ms |
25156 KB |
Output is correct |
24 |
Correct |
38 ms |
27988 KB |
Output is correct |
25 |
Correct |
15 ms |
24064 KB |
Output is correct |
26 |
Correct |
20 ms |
25028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23696 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
14 ms |
23792 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23768 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
15 ms |
23700 KB |
Output is correct |
9 |
Correct |
14 ms |
23884 KB |
Output is correct |
10 |
Correct |
15 ms |
24148 KB |
Output is correct |
11 |
Correct |
13 ms |
23892 KB |
Output is correct |
12 |
Correct |
13 ms |
23924 KB |
Output is correct |
13 |
Correct |
14 ms |
23788 KB |
Output is correct |
14 |
Correct |
13 ms |
23928 KB |
Output is correct |
15 |
Correct |
16 ms |
23784 KB |
Output is correct |
16 |
Correct |
15 ms |
24056 KB |
Output is correct |
17 |
Correct |
53 ms |
30096 KB |
Output is correct |
18 |
Correct |
47 ms |
30960 KB |
Output is correct |
19 |
Correct |
41 ms |
30688 KB |
Output is correct |
20 |
Correct |
40 ms |
30668 KB |
Output is correct |
21 |
Correct |
51 ms |
30572 KB |
Output is correct |
22 |
Correct |
80 ms |
37500 KB |
Output is correct |
23 |
Correct |
19 ms |
25156 KB |
Output is correct |
24 |
Correct |
38 ms |
27988 KB |
Output is correct |
25 |
Correct |
15 ms |
24064 KB |
Output is correct |
26 |
Correct |
20 ms |
25028 KB |
Output is correct |
27 |
Correct |
18 ms |
24812 KB |
Output is correct |
28 |
Correct |
188 ms |
56092 KB |
Output is correct |
29 |
Correct |
248 ms |
67740 KB |
Output is correct |
30 |
Correct |
282 ms |
67444 KB |
Output is correct |
31 |
Correct |
241 ms |
67428 KB |
Output is correct |
32 |
Correct |
253 ms |
69408 KB |
Output is correct |
33 |
Correct |
281 ms |
67452 KB |
Output is correct |
34 |
Correct |
239 ms |
67344 KB |
Output is correct |
35 |
Correct |
96 ms |
41548 KB |
Output is correct |
36 |
Correct |
275 ms |
69520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
52044 KB |
Output is correct |
2 |
Correct |
73 ms |
51888 KB |
Output is correct |
3 |
Correct |
103 ms |
54628 KB |
Output is correct |
4 |
Correct |
96 ms |
55648 KB |
Output is correct |
5 |
Correct |
119 ms |
62188 KB |
Output is correct |
6 |
Correct |
124 ms |
61632 KB |
Output is correct |
7 |
Correct |
136 ms |
62028 KB |
Output is correct |
8 |
Correct |
121 ms |
62104 KB |
Output is correct |
9 |
Correct |
127 ms |
61860 KB |
Output is correct |
10 |
Correct |
103 ms |
52300 KB |
Output is correct |
11 |
Correct |
209 ms |
80996 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23700 KB |
Output is correct |
15 |
Correct |
11 ms |
23784 KB |
Output is correct |
16 |
Correct |
16 ms |
23764 KB |
Output is correct |
17 |
Correct |
17 ms |
23764 KB |
Output is correct |
18 |
Correct |
75 ms |
51840 KB |
Output is correct |
19 |
Correct |
79 ms |
51944 KB |
Output is correct |
20 |
Correct |
83 ms |
51940 KB |
Output is correct |
21 |
Correct |
89 ms |
51920 KB |
Output is correct |
22 |
Correct |
183 ms |
65312 KB |
Output is correct |
23 |
Correct |
218 ms |
81000 KB |
Output is correct |
24 |
Correct |
245 ms |
81412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
60032 KB |
Output is correct |
2 |
Correct |
148 ms |
65464 KB |
Output is correct |
3 |
Correct |
73 ms |
51936 KB |
Output is correct |
4 |
Correct |
89 ms |
51916 KB |
Output is correct |
5 |
Correct |
262 ms |
90752 KB |
Output is correct |
6 |
Correct |
337 ms |
92928 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
208 ms |
71000 KB |
Output is correct |
9 |
Correct |
212 ms |
78952 KB |
Output is correct |
10 |
Correct |
110 ms |
59964 KB |
Output is correct |
11 |
Correct |
183 ms |
65384 KB |
Output is correct |
12 |
Correct |
15 ms |
23672 KB |
Output is correct |
13 |
Correct |
14 ms |
23764 KB |
Output is correct |
14 |
Correct |
14 ms |
23712 KB |
Output is correct |
15 |
Correct |
13 ms |
23788 KB |
Output is correct |
16 |
Correct |
71 ms |
51900 KB |
Output is correct |
17 |
Correct |
90 ms |
51892 KB |
Output is correct |
18 |
Correct |
125 ms |
60044 KB |
Output is correct |
19 |
Correct |
135 ms |
65424 KB |
Output is correct |
20 |
Correct |
120 ms |
60160 KB |
Output is correct |
21 |
Correct |
142 ms |
64052 KB |
Output is correct |
22 |
Correct |
129 ms |
60032 KB |
Output is correct |
23 |
Correct |
153 ms |
64060 KB |
Output is correct |
24 |
Correct |
70 ms |
52044 KB |
Output is correct |
25 |
Correct |
73 ms |
51888 KB |
Output is correct |
26 |
Correct |
103 ms |
54628 KB |
Output is correct |
27 |
Correct |
96 ms |
55648 KB |
Output is correct |
28 |
Correct |
119 ms |
62188 KB |
Output is correct |
29 |
Correct |
124 ms |
61632 KB |
Output is correct |
30 |
Correct |
136 ms |
62028 KB |
Output is correct |
31 |
Correct |
121 ms |
62104 KB |
Output is correct |
32 |
Correct |
13 ms |
23696 KB |
Output is correct |
33 |
Correct |
13 ms |
23756 KB |
Output is correct |
34 |
Correct |
14 ms |
23792 KB |
Output is correct |
35 |
Correct |
14 ms |
23764 KB |
Output is correct |
36 |
Correct |
13 ms |
23768 KB |
Output is correct |
37 |
Correct |
13 ms |
23764 KB |
Output is correct |
38 |
Correct |
13 ms |
23764 KB |
Output is correct |
39 |
Correct |
15 ms |
23700 KB |
Output is correct |
40 |
Correct |
14 ms |
23884 KB |
Output is correct |
41 |
Correct |
15 ms |
24148 KB |
Output is correct |
42 |
Correct |
13 ms |
23892 KB |
Output is correct |
43 |
Correct |
13 ms |
23924 KB |
Output is correct |
44 |
Correct |
14 ms |
23788 KB |
Output is correct |
45 |
Correct |
13 ms |
23928 KB |
Output is correct |
46 |
Correct |
16 ms |
23784 KB |
Output is correct |
47 |
Correct |
15 ms |
24056 KB |
Output is correct |
48 |
Correct |
53 ms |
30096 KB |
Output is correct |
49 |
Correct |
47 ms |
30960 KB |
Output is correct |
50 |
Correct |
41 ms |
30688 KB |
Output is correct |
51 |
Correct |
40 ms |
30668 KB |
Output is correct |
52 |
Correct |
51 ms |
30572 KB |
Output is correct |
53 |
Correct |
80 ms |
37500 KB |
Output is correct |
54 |
Correct |
19 ms |
25156 KB |
Output is correct |
55 |
Correct |
38 ms |
27988 KB |
Output is correct |
56 |
Correct |
15 ms |
24064 KB |
Output is correct |
57 |
Correct |
20 ms |
25028 KB |
Output is correct |
58 |
Correct |
18 ms |
24812 KB |
Output is correct |
59 |
Correct |
188 ms |
56092 KB |
Output is correct |
60 |
Correct |
248 ms |
67740 KB |
Output is correct |
61 |
Correct |
282 ms |
67444 KB |
Output is correct |
62 |
Correct |
241 ms |
67428 KB |
Output is correct |
63 |
Correct |
253 ms |
69408 KB |
Output is correct |
64 |
Correct |
281 ms |
67452 KB |
Output is correct |
65 |
Correct |
239 ms |
67344 KB |
Output is correct |
66 |
Correct |
96 ms |
41548 KB |
Output is correct |
67 |
Correct |
275 ms |
69520 KB |
Output is correct |
68 |
Correct |
127 ms |
61860 KB |
Output is correct |
69 |
Correct |
103 ms |
52300 KB |
Output is correct |
70 |
Correct |
209 ms |
80996 KB |
Output is correct |
71 |
Correct |
11 ms |
23764 KB |
Output is correct |
72 |
Correct |
11 ms |
23764 KB |
Output is correct |
73 |
Correct |
11 ms |
23700 KB |
Output is correct |
74 |
Correct |
11 ms |
23784 KB |
Output is correct |
75 |
Correct |
16 ms |
23764 KB |
Output is correct |
76 |
Correct |
17 ms |
23764 KB |
Output is correct |
77 |
Correct |
75 ms |
51840 KB |
Output is correct |
78 |
Correct |
79 ms |
51944 KB |
Output is correct |
79 |
Correct |
83 ms |
51940 KB |
Output is correct |
80 |
Correct |
89 ms |
51920 KB |
Output is correct |
81 |
Correct |
183 ms |
65312 KB |
Output is correct |
82 |
Correct |
218 ms |
81000 KB |
Output is correct |
83 |
Correct |
245 ms |
81412 KB |
Output is correct |
84 |
Correct |
382 ms |
90708 KB |
Output is correct |
85 |
Correct |
365 ms |
92932 KB |
Output is correct |
86 |
Correct |
429 ms |
91444 KB |
Output is correct |
87 |
Correct |
409 ms |
94540 KB |
Output is correct |
88 |
Correct |
15 ms |
23768 KB |
Output is correct |
89 |
Correct |
453 ms |
94656 KB |
Output is correct |
90 |
Correct |
372 ms |
94480 KB |
Output is correct |
91 |
Correct |
311 ms |
95028 KB |
Output is correct |