#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void maxup(ll &a, ll b) {
a = max(a, b);
}
long long max_weights(int n, int cnt_fish, std::vector<int> XX, std::vector<int> YY, std::vector<int> WW) {
const ll inf = (ll) 1e18 + 7;
n += 7;
vector<set<int>> interestingSet(n);
vector<vector<int>> interesting(n);
vector<vector<vector<ll>>> dp(2, vector<vector<ll>> (n));
vector<vector<ll>> both(n);
vector<ll> dp_zero(n, 0LL);
vector<vector<pair<int, ll>>> pe(n);
assert((int) XX.size() == cnt_fish);
assert((int) YY.size() == cnt_fish);
assert((int) WW.size() == cnt_fish);
for (int i = 0; i < cnt_fish; i++) {
int x = XX[i] + 5, y = YY[i] + 1, w = WW[i];
pe[x].push_back({y, w});
vector<int> xs = {x - 1, x + 1};
for (auto &i : xs) {
interestingSet[i].insert(y);
}
}
for (int x = 0; x < n; x++) {
interestingSet[x].insert(0);
for (auto &y : interestingSet[x]) {
interesting[x].push_back(y);
}
dp[0][x].resize((int) interesting[x].size(), 0LL);
dp[1][x].resize((int) interesting[x].size(), 0LL);
both[x].resize((int) interesting[x].size(), 0LL);
sort(pe[x].begin(), pe[x].end());
ll sum = 0;
for (auto &it : pe[x]) {
sum += it.second;
it.second = sum;
}
}
function<ll(int, int)> get = [&] (int x, int y) {
if (x == n) {
return 0LL;
}
assert(0 <= x && x < n);
assert(0 <= y && y < n);
ll sol = 0;
int low = 0, high = (int) pe[x].size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (pe[x][mid].first <= y) {
sol = pe[x][mid].second;
low = mid + 1;
} else {
high = mid - 1;
}
}
return sol;
};
for (int i = 5; i < n - 2; i++) {
{
maxup(dp_zero[i], dp_zero[i - 1]);
for (int ant_j = 0; ant_j < (int) interesting[i - 1].size(); ant_j++) {
int h2 = interesting[i - 1][ant_j];
maxup(dp_zero[i], both[i - 1][ant_j]);
}
}
{
vector<ll> dp_ant0((int) interesting[i].size());
{
int q = 0, w = 0;
ll Fpref = -inf, Spref = -inf;
for (int j = 0; j < (int) interesting[i].size(); j++) {
int h = interesting[i][j];
while (q < (int) interesting[i - 2].size() && interesting[i - 2][q] < h) {
maxup(Fpref, both[i - 2][q]);
q++;
}
while (w < (int) interesting[i - 1].size() && interesting[i - 1][w] < h) {
int h2 = interesting[i - 1][w];
maxup(Spref, dp[1][i - 1][w] - get(i, h2) - get(i - 1, h2));
w++;
}
maxup(dp_ant0[j], Fpref + get(i + 1, h));
maxup(dp_ant0[j], dp_zero[i - 2] + get(i - 1, h) + get(i + 1, h));
maxup(dp[1][i][j], Spref + get(i - 1, h) + get(i + 1, h));
}
}
{
int q = (int) interesting[i - 2].size(), w = (int) interesting[i - 1].size();
ll Fsuf = -inf, Ssuf = -inf;
for (int j = (int) interesting[i].size() - 1; j >= 0; j--) {
int h = interesting[i][j];
while (q - 1 >= 0 && interesting[i - 2][q - 1] >= h) {
q--;
maxup(Fsuf, both[i - 2][q] + - get(i - 1, interesting[i - 2][q]));
}
while (w - 1 >= 0 && interesting[i - 1][w - 1] >= h) {
w--;
maxup(Ssuf, both[i - 1][w]);
}
maxup(dp_ant0[j], Fsuf + get(i - 1, h) + get(i + 1, h));
maxup(dp[0][i][j], Ssuf - get(i, h) + get(i + 1, h));
}
}
for (int j = 0; j < (int) interesting[i].size(); j++) {
maxup(dp[0][i][j], dp_ant0[j]);
maxup(dp[1][i][j], dp_ant0[j]);
both[i][j] = max(dp[0][i][j], dp[1][i][j]);
}
}
}
ll sol = 0;
for (int i = 0; i < n; i++) {
maxup(sol, dp_zero[i]);
for (int j = 0; j < (int) interesting[i].size(); j++) {
maxup(sol, both[i][j]);
}
}
return sol;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:82:13: warning: unused variable 'h2' [-Wunused-variable]
82 | int h2 = interesting[i - 1][ant_j];
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
47080 KB |
Output is correct |
2 |
Correct |
212 ms |
54332 KB |
Output is correct |
3 |
Correct |
72 ms |
34660 KB |
Output is correct |
4 |
Correct |
70 ms |
34732 KB |
Output is correct |
5 |
Correct |
733 ms |
91860 KB |
Output is correct |
6 |
Correct |
739 ms |
92032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
419 ms |
62400 KB |
Output is correct |
3 |
Correct |
469 ms |
73124 KB |
Output is correct |
4 |
Correct |
169 ms |
47076 KB |
Output is correct |
5 |
Correct |
215 ms |
54320 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
57 ms |
34724 KB |
Output is correct |
11 |
Correct |
57 ms |
34656 KB |
Output is correct |
12 |
Correct |
185 ms |
47272 KB |
Output is correct |
13 |
Correct |
230 ms |
54336 KB |
Output is correct |
14 |
Correct |
180 ms |
46784 KB |
Output is correct |
15 |
Correct |
222 ms |
53252 KB |
Output is correct |
16 |
Correct |
181 ms |
48020 KB |
Output is correct |
17 |
Correct |
204 ms |
53156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
34640 KB |
Output is correct |
2 |
Correct |
58 ms |
34732 KB |
Output is correct |
3 |
Correct |
103 ms |
36876 KB |
Output is correct |
4 |
Correct |
93 ms |
38516 KB |
Output is correct |
5 |
Correct |
169 ms |
44876 KB |
Output is correct |
6 |
Correct |
165 ms |
44828 KB |
Output is correct |
7 |
Correct |
157 ms |
44864 KB |
Output is correct |
8 |
Correct |
159 ms |
44836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
62 ms |
6692 KB |
Output is correct |
18 |
Correct |
60 ms |
6732 KB |
Output is correct |
19 |
Correct |
51 ms |
6732 KB |
Output is correct |
20 |
Correct |
46 ms |
6228 KB |
Output is correct |
21 |
Correct |
44 ms |
6204 KB |
Output is correct |
22 |
Correct |
109 ms |
11992 KB |
Output is correct |
23 |
Correct |
14 ms |
2004 KB |
Output is correct |
24 |
Correct |
50 ms |
5532 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
12 ms |
1924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
62 ms |
6692 KB |
Output is correct |
18 |
Correct |
60 ms |
6732 KB |
Output is correct |
19 |
Correct |
51 ms |
6732 KB |
Output is correct |
20 |
Correct |
46 ms |
6228 KB |
Output is correct |
21 |
Correct |
44 ms |
6204 KB |
Output is correct |
22 |
Correct |
109 ms |
11992 KB |
Output is correct |
23 |
Correct |
14 ms |
2004 KB |
Output is correct |
24 |
Correct |
50 ms |
5532 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
12 ms |
1924 KB |
Output is correct |
27 |
Correct |
4 ms |
1748 KB |
Output is correct |
28 |
Correct |
401 ms |
30688 KB |
Output is correct |
29 |
Correct |
686 ms |
50976 KB |
Output is correct |
30 |
Correct |
776 ms |
60988 KB |
Output is correct |
31 |
Correct |
805 ms |
60552 KB |
Output is correct |
32 |
Correct |
461 ms |
41144 KB |
Output is correct |
33 |
Correct |
803 ms |
61336 KB |
Output is correct |
34 |
Correct |
809 ms |
61248 KB |
Output is correct |
35 |
Correct |
217 ms |
23120 KB |
Output is correct |
36 |
Correct |
789 ms |
58404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
34640 KB |
Output is correct |
2 |
Correct |
58 ms |
34732 KB |
Output is correct |
3 |
Correct |
103 ms |
36876 KB |
Output is correct |
4 |
Correct |
93 ms |
38516 KB |
Output is correct |
5 |
Correct |
169 ms |
44876 KB |
Output is correct |
6 |
Correct |
165 ms |
44828 KB |
Output is correct |
7 |
Correct |
157 ms |
44864 KB |
Output is correct |
8 |
Correct |
159 ms |
44836 KB |
Output is correct |
9 |
Correct |
216 ms |
49700 KB |
Output is correct |
10 |
Correct |
128 ms |
26920 KB |
Output is correct |
11 |
Correct |
317 ms |
53432 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 |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
57 ms |
34708 KB |
Output is correct |
19 |
Correct |
57 ms |
34684 KB |
Output is correct |
20 |
Correct |
57 ms |
34696 KB |
Output is correct |
21 |
Correct |
60 ms |
34740 KB |
Output is correct |
22 |
Correct |
243 ms |
50748 KB |
Output is correct |
23 |
Correct |
404 ms |
65316 KB |
Output is correct |
24 |
Correct |
411 ms |
68424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
47080 KB |
Output is correct |
2 |
Correct |
212 ms |
54332 KB |
Output is correct |
3 |
Correct |
72 ms |
34660 KB |
Output is correct |
4 |
Correct |
70 ms |
34732 KB |
Output is correct |
5 |
Correct |
733 ms |
91860 KB |
Output is correct |
6 |
Correct |
739 ms |
92032 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
419 ms |
62400 KB |
Output is correct |
9 |
Correct |
469 ms |
73124 KB |
Output is correct |
10 |
Correct |
169 ms |
47076 KB |
Output is correct |
11 |
Correct |
215 ms |
54320 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 |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
57 ms |
34724 KB |
Output is correct |
17 |
Correct |
57 ms |
34656 KB |
Output is correct |
18 |
Correct |
185 ms |
47272 KB |
Output is correct |
19 |
Correct |
230 ms |
54336 KB |
Output is correct |
20 |
Correct |
180 ms |
46784 KB |
Output is correct |
21 |
Correct |
222 ms |
53252 KB |
Output is correct |
22 |
Correct |
181 ms |
48020 KB |
Output is correct |
23 |
Correct |
204 ms |
53156 KB |
Output is correct |
24 |
Correct |
58 ms |
34640 KB |
Output is correct |
25 |
Correct |
58 ms |
34732 KB |
Output is correct |
26 |
Correct |
103 ms |
36876 KB |
Output is correct |
27 |
Correct |
93 ms |
38516 KB |
Output is correct |
28 |
Correct |
169 ms |
44876 KB |
Output is correct |
29 |
Correct |
165 ms |
44828 KB |
Output is correct |
30 |
Correct |
157 ms |
44864 KB |
Output is correct |
31 |
Correct |
159 ms |
44836 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
3 ms |
596 KB |
Output is correct |
48 |
Correct |
62 ms |
6692 KB |
Output is correct |
49 |
Correct |
60 ms |
6732 KB |
Output is correct |
50 |
Correct |
51 ms |
6732 KB |
Output is correct |
51 |
Correct |
46 ms |
6228 KB |
Output is correct |
52 |
Correct |
44 ms |
6204 KB |
Output is correct |
53 |
Correct |
109 ms |
11992 KB |
Output is correct |
54 |
Correct |
14 ms |
2004 KB |
Output is correct |
55 |
Correct |
50 ms |
5532 KB |
Output is correct |
56 |
Correct |
2 ms |
596 KB |
Output is correct |
57 |
Correct |
12 ms |
1924 KB |
Output is correct |
58 |
Correct |
4 ms |
1748 KB |
Output is correct |
59 |
Correct |
401 ms |
30688 KB |
Output is correct |
60 |
Correct |
686 ms |
50976 KB |
Output is correct |
61 |
Correct |
776 ms |
60988 KB |
Output is correct |
62 |
Correct |
805 ms |
60552 KB |
Output is correct |
63 |
Correct |
461 ms |
41144 KB |
Output is correct |
64 |
Correct |
803 ms |
61336 KB |
Output is correct |
65 |
Correct |
809 ms |
61248 KB |
Output is correct |
66 |
Correct |
217 ms |
23120 KB |
Output is correct |
67 |
Correct |
789 ms |
58404 KB |
Output is correct |
68 |
Correct |
216 ms |
49700 KB |
Output is correct |
69 |
Correct |
128 ms |
26920 KB |
Output is correct |
70 |
Correct |
317 ms |
53432 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
57 ms |
34708 KB |
Output is correct |
78 |
Correct |
57 ms |
34684 KB |
Output is correct |
79 |
Correct |
57 ms |
34696 KB |
Output is correct |
80 |
Correct |
60 ms |
34740 KB |
Output is correct |
81 |
Correct |
243 ms |
50748 KB |
Output is correct |
82 |
Correct |
404 ms |
65316 KB |
Output is correct |
83 |
Correct |
411 ms |
68424 KB |
Output is correct |
84 |
Correct |
901 ms |
88652 KB |
Output is correct |
85 |
Correct |
927 ms |
91684 KB |
Output is correct |
86 |
Correct |
732 ms |
87784 KB |
Output is correct |
87 |
Correct |
758 ms |
90532 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
763 ms |
91052 KB |
Output is correct |
90 |
Correct |
705 ms |
86096 KB |
Output is correct |
91 |
Correct |
554 ms |
76556 KB |
Output is correct |