#include "fish.h"
#include <climits>
#include <algorithm>
#include <numeric>
#include <utility>
#include <vector>
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
std::vector<std::vector<std::pair<int, long long>>> fish(N + 2);
for (int i = 0; i < M; ++i) {
fish[X[i] + 1].emplace_back(Y[i], W[i]);
}
fish[0] = {{-1, 0LL}};
for (int i = 1; i <= N; ++i) {
fish[i].emplace_back(-1, 0LL);
std::sort(fish[i].begin(), fish[i].end());
for (int j = 1; j < static_cast<int>(fish[i].size()); ++j) {
fish[i][j].second += fish[i][j - 1].second;
}
}
fish[N + 1] = {{-1, 0LL}};
auto getRowWeight = [&](int row, int col) {
return (upper_bound(fish[row].begin(), fish[row].end(), std::make_pair(col, LLONG_MAX)) - 1)->second;
};
std::vector<std::vector<int>> indices(N + 2);
indices[0] = {-1};
for (int i = 1; i <= N; ++i) {
for (std::pair<int, int> f : fish[i - 1]) {
indices[i].push_back(f.first);
}
for (std::pair<int, int> f : fish[i + 1]) {
indices[i].push_back(f.first);
}
std::sort(indices[i].begin(), indices[i].end());
indices[i].erase(std::unique(indices[i].begin(), indices[i].end()), indices[i].end());
}
indices[N + 1] = {-1};
std::vector<long long> dpInc = {0LL}, dpDec = {0LL};
for (int i = 1; i <= N + 1; ++i) {
std::vector<long long> ndpInc(indices[i].size());
long long pref = 0LL;
int pidx = 0;
for (int j = 0; j < static_cast<int>(indices[i].size()); ++j) {
int col = indices[i][j];
while (pidx < static_cast<int>(indices[i - 1].size()) && indices[i - 1][pidx] <= col) {
pref = std::max(pref, dpInc[pidx] - getRowWeight(i - 1, indices[i - 1][pidx]));
++pidx;
}
ndpInc[j] = std::max(pref + getRowWeight(i - 1, col), dpDec[0]);
}
std::vector<long long> ndpDec(indices[i].size());
long long suff = 0;
int sidx = indices[i - 1].size();
for (int j = static_cast<int>(indices[i].size()) - 1; j >= 0; --j) {
int col = indices[i][j];
while (sidx > 0 && indices[i - 1][sidx - 1] >= col) {
--sidx;
suff = std::max(suff, std::max(dpInc[sidx], dpDec[sidx]) + getRowWeight(i, indices[i - 1][sidx]));
}
ndpDec[j] = suff - getRowWeight(i, col);
}
std::swap(dpInc, ndpInc);
std::swap(dpDec, ndpDec);
}
return std::max(dpInc[0], dpDec[0]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
16324 KB |
Output is correct |
2 |
Correct |
47 ms |
18884 KB |
Output is correct |
3 |
Correct |
17 ms |
11396 KB |
Output is correct |
4 |
Correct |
16 ms |
11172 KB |
Output is correct |
5 |
Correct |
126 ms |
33672 KB |
Output is correct |
6 |
Correct |
135 ms |
34644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
93 ms |
22832 KB |
Output is correct |
3 |
Correct |
107 ms |
26992 KB |
Output is correct |
4 |
Correct |
38 ms |
16332 KB |
Output is correct |
5 |
Correct |
45 ms |
18880 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 |
15 ms |
11152 KB |
Output is correct |
11 |
Correct |
15 ms |
11352 KB |
Output is correct |
12 |
Correct |
49 ms |
16656 KB |
Output is correct |
13 |
Correct |
52 ms |
19392 KB |
Output is correct |
14 |
Correct |
42 ms |
16628 KB |
Output is correct |
15 |
Correct |
57 ms |
18364 KB |
Output is correct |
16 |
Correct |
44 ms |
16532 KB |
Output is correct |
17 |
Correct |
55 ms |
18364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
11280 KB |
Output is correct |
2 |
Correct |
22 ms |
11180 KB |
Output is correct |
3 |
Correct |
29 ms |
13136 KB |
Output is correct |
4 |
Correct |
24 ms |
13400 KB |
Output is correct |
5 |
Correct |
44 ms |
17332 KB |
Output is correct |
6 |
Correct |
38 ms |
16744 KB |
Output is correct |
7 |
Correct |
44 ms |
17244 KB |
Output is correct |
8 |
Correct |
44 ms |
17232 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 |
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 |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
0 ms |
420 KB |
Output is correct |
14 |
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 |
0 ms |
348 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 |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
0 ms |
420 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
420 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
14 ms |
3388 KB |
Output is correct |
18 |
Correct |
14 ms |
4328 KB |
Output is correct |
19 |
Correct |
13 ms |
3932 KB |
Output is correct |
20 |
Correct |
15 ms |
4012 KB |
Output is correct |
21 |
Correct |
13 ms |
3932 KB |
Output is correct |
22 |
Correct |
24 ms |
7764 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
10 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
860 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 |
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 |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
0 ms |
420 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
420 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
14 ms |
3388 KB |
Output is correct |
18 |
Correct |
14 ms |
4328 KB |
Output is correct |
19 |
Correct |
13 ms |
3932 KB |
Output is correct |
20 |
Correct |
15 ms |
4012 KB |
Output is correct |
21 |
Correct |
13 ms |
3932 KB |
Output is correct |
22 |
Correct |
24 ms |
7764 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
10 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
83 ms |
17260 KB |
Output is correct |
29 |
Correct |
161 ms |
23380 KB |
Output is correct |
30 |
Correct |
102 ms |
22352 KB |
Output is correct |
31 |
Correct |
103 ms |
22352 KB |
Output is correct |
32 |
Correct |
79 ms |
24912 KB |
Output is correct |
33 |
Correct |
115 ms |
22372 KB |
Output is correct |
34 |
Correct |
99 ms |
22480 KB |
Output is correct |
35 |
Correct |
37 ms |
9560 KB |
Output is correct |
36 |
Correct |
126 ms |
24660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
11280 KB |
Output is correct |
2 |
Correct |
22 ms |
11180 KB |
Output is correct |
3 |
Correct |
29 ms |
13136 KB |
Output is correct |
4 |
Correct |
24 ms |
13400 KB |
Output is correct |
5 |
Correct |
44 ms |
17332 KB |
Output is correct |
6 |
Correct |
38 ms |
16744 KB |
Output is correct |
7 |
Correct |
44 ms |
17244 KB |
Output is correct |
8 |
Correct |
44 ms |
17232 KB |
Output is correct |
9 |
Correct |
43 ms |
16980 KB |
Output is correct |
10 |
Correct |
34 ms |
13144 KB |
Output is correct |
11 |
Correct |
69 ms |
26180 KB |
Output is correct |
12 |
Correct |
0 ms |
344 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 |
17 ms |
11392 KB |
Output is correct |
19 |
Correct |
17 ms |
11356 KB |
Output is correct |
20 |
Correct |
17 ms |
11188 KB |
Output is correct |
21 |
Correct |
14 ms |
11356 KB |
Output is correct |
22 |
Correct |
49 ms |
18296 KB |
Output is correct |
23 |
Correct |
92 ms |
26192 KB |
Output is correct |
24 |
Correct |
85 ms |
26708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
16324 KB |
Output is correct |
2 |
Correct |
47 ms |
18884 KB |
Output is correct |
3 |
Correct |
17 ms |
11396 KB |
Output is correct |
4 |
Correct |
16 ms |
11172 KB |
Output is correct |
5 |
Correct |
126 ms |
33672 KB |
Output is correct |
6 |
Correct |
135 ms |
34644 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
93 ms |
22832 KB |
Output is correct |
9 |
Correct |
107 ms |
26992 KB |
Output is correct |
10 |
Correct |
38 ms |
16332 KB |
Output is correct |
11 |
Correct |
45 ms |
18880 KB |
Output is correct |
12 |
Correct |
0 ms |
344 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 |
15 ms |
11152 KB |
Output is correct |
17 |
Correct |
15 ms |
11352 KB |
Output is correct |
18 |
Correct |
49 ms |
16656 KB |
Output is correct |
19 |
Correct |
52 ms |
19392 KB |
Output is correct |
20 |
Correct |
42 ms |
16628 KB |
Output is correct |
21 |
Correct |
57 ms |
18364 KB |
Output is correct |
22 |
Correct |
44 ms |
16532 KB |
Output is correct |
23 |
Correct |
55 ms |
18364 KB |
Output is correct |
24 |
Correct |
14 ms |
11280 KB |
Output is correct |
25 |
Correct |
22 ms |
11180 KB |
Output is correct |
26 |
Correct |
29 ms |
13136 KB |
Output is correct |
27 |
Correct |
24 ms |
13400 KB |
Output is correct |
28 |
Correct |
44 ms |
17332 KB |
Output is correct |
29 |
Correct |
38 ms |
16744 KB |
Output is correct |
30 |
Correct |
44 ms |
17244 KB |
Output is correct |
31 |
Correct |
44 ms |
17232 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 |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 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 |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
436 KB |
Output is correct |
44 |
Correct |
0 ms |
420 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
420 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
14 ms |
3388 KB |
Output is correct |
49 |
Correct |
14 ms |
4328 KB |
Output is correct |
50 |
Correct |
13 ms |
3932 KB |
Output is correct |
51 |
Correct |
15 ms |
4012 KB |
Output is correct |
52 |
Correct |
13 ms |
3932 KB |
Output is correct |
53 |
Correct |
24 ms |
7764 KB |
Output is correct |
54 |
Correct |
3 ms |
1116 KB |
Output is correct |
55 |
Correct |
10 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
4 ms |
860 KB |
Output is correct |
58 |
Correct |
1 ms |
860 KB |
Output is correct |
59 |
Correct |
83 ms |
17260 KB |
Output is correct |
60 |
Correct |
161 ms |
23380 KB |
Output is correct |
61 |
Correct |
102 ms |
22352 KB |
Output is correct |
62 |
Correct |
103 ms |
22352 KB |
Output is correct |
63 |
Correct |
79 ms |
24912 KB |
Output is correct |
64 |
Correct |
115 ms |
22372 KB |
Output is correct |
65 |
Correct |
99 ms |
22480 KB |
Output is correct |
66 |
Correct |
37 ms |
9560 KB |
Output is correct |
67 |
Correct |
126 ms |
24660 KB |
Output is correct |
68 |
Correct |
43 ms |
16980 KB |
Output is correct |
69 |
Correct |
34 ms |
13144 KB |
Output is correct |
70 |
Correct |
69 ms |
26180 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
17 ms |
11392 KB |
Output is correct |
78 |
Correct |
17 ms |
11356 KB |
Output is correct |
79 |
Correct |
17 ms |
11188 KB |
Output is correct |
80 |
Correct |
14 ms |
11356 KB |
Output is correct |
81 |
Correct |
49 ms |
18296 KB |
Output is correct |
82 |
Correct |
92 ms |
26192 KB |
Output is correct |
83 |
Correct |
85 ms |
26708 KB |
Output is correct |
84 |
Correct |
177 ms |
32132 KB |
Output is correct |
85 |
Correct |
181 ms |
32692 KB |
Output is correct |
86 |
Correct |
147 ms |
32592 KB |
Output is correct |
87 |
Correct |
132 ms |
33732 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
130 ms |
33756 KB |
Output is correct |
90 |
Correct |
115 ms |
33504 KB |
Output is correct |
91 |
Correct |
97 ms |
33612 KB |
Output is correct |