#include <bits/stdc++.h> //uh may be changing style make it better ?
#ifndef LOCAL
#include <fish.h>
#endif // LOCAL
using namespace std;
#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
template<typename T> bool maximize(T& a, const T& b){
if(a < b) return a = b, true; return false;
}
long long max_weights(int sizeBoard, int nFishes, vector<int> column, vector<int> row, vector<int> weight){
vector<vector<pair<int, int>>> fishes(sizeBoard);
for(int i = 0; i < nFishes; ++i){
fishes[column[i]].emplace_back(row[i], weight[i]);
}
vector<vector<long long>> pref(sizeBoard);
vector<vector<long long>> up(sizeBoard), down(sizeBoard);
for(int i = 0; i < sizeBoard; ++i){
fishes[i].emplace_back(sizeBoard, 0);
sort(all(fishes[i]));
pref[i].resize(sz(fishes[i]));
for(int j = 0; j < sz(fishes[i]); ++j){
pref[i][j] = (j > 0 ? pref[i][j - 1] : 0) + fishes[i][j].second;
}
up[i].resize(sz(fishes[i]));
down[i].resize(sz(fishes[i]));
}
auto prefixSum = [&](int c, int pos) -> long long{
int l = 0, r = sz(fishes[c]) - 1;
long long sum = 0;
assert(!fishes[c].empty());
while(l <= r){
int mid = l + r >> 1;
if(fishes[c][mid].first < pos) sum = pref[c][mid], l = mid + 1;
else r = mid - 1;
}
return sum;
};
auto maxAll = [&](int col) -> long long{
return max(*max_element(all(up[col])), *max_element(all(down[col])));
};
//let h[i] be the height of the chosen pier at column i
for(int i = 1; i < sizeBoard; ++i){
//calculate the down[i] (case h[i - 1] > h[i])
long long mx = 0;
for(int j = sz(fishes[i]) - 1, ptr = sz(fishes[i - 1]) - 1; j >= 0; --j){
while(ptr >= 0 && fishes[i - 1][ptr].first >= fishes[i][j].first){
maximize(mx, max(up[i - 1][ptr], down[i - 1][ptr]) + prefixSum(i, fishes[i - 1][ptr].first));
--ptr;
}
maximize(down[i][j], mx - prefixSum(i, fishes[i][j].first));
}
//consider h[i - 1] < h[i] and case h[i - 2] > h[i - 1] < h[i]
mx = (i > 1 ? maxAll(i - 2) : 0);
for(int j = 0, ptr = 0; j < sz(fishes[i]); ++j){
while(ptr < sz(fishes[i - 1]) && fishes[i - 1][ptr].first <= fishes[i][j].first){
maximize(mx, up[i - 1][ptr] - prefixSum(i - 1, fishes[i - 1][ptr].first));
++ptr;
}
maximize(up[i][j], mx + prefixSum(i - 1, fishes[i][j].first));
}
}
long long ans = 0;
for(int i = 0; i < sizeBoard; ++i){
for(int j = 0; j < sz(fishes[i]); ++j){
// assert(j < sz(up[i]));
// assert(j < sz(down[i]));
// cout << "(" << i << ", " << fishes[i][j].first << ") = " << up[i][j] << ' ' << down[i][j] << '\n';
maximize(ans, max(up[i][j], down[i][j]));
}
}
return ans;
}
#ifdef LOCAL
int main(){
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
int n, m;
cin >> n >> m;
vector<int> x(m), y(m), z(m);
for(int i = 0; i < m; ++i) cin >> x[i] >> y[i] >> z[i];
cout << max_weights(n, m, x, y, z) << '\n';
return 0;
}
#endif // LOCAL
Compilation message
fish.cpp: In function 'bool maximize(T&, const T&)':
fish.cpp:12:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
12 | if(a < b) return a = b, true; return false;
| ^~
fish.cpp:12:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
12 | if(a < b) return a = b, true; return false;
| ^~~~~~
fish.cpp: In lambda function:
fish.cpp:40:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
25844 KB |
Output is correct |
2 |
Correct |
70 ms |
29576 KB |
Output is correct |
3 |
Correct |
24 ms |
22352 KB |
Output is correct |
4 |
Correct |
24 ms |
22352 KB |
Output is correct |
5 |
Correct |
136 ms |
44972 KB |
Output is correct |
6 |
Correct |
120 ms |
41108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
89 ms |
31708 KB |
Output is correct |
3 |
Correct |
104 ms |
36800 KB |
Output is correct |
4 |
Correct |
51 ms |
25920 KB |
Output is correct |
5 |
Correct |
56 ms |
29496 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
32 ms |
22100 KB |
Output is correct |
11 |
Correct |
28 ms |
22352 KB |
Output is correct |
12 |
Correct |
51 ms |
26048 KB |
Output is correct |
13 |
Correct |
63 ms |
29372 KB |
Output is correct |
14 |
Correct |
50 ms |
24412 KB |
Output is correct |
15 |
Correct |
59 ms |
27172 KB |
Output is correct |
16 |
Correct |
62 ms |
26048 KB |
Output is correct |
17 |
Correct |
61 ms |
28604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
22308 KB |
Output is correct |
2 |
Correct |
27 ms |
22352 KB |
Output is correct |
3 |
Correct |
42 ms |
22304 KB |
Output is correct |
4 |
Correct |
38 ms |
23624 KB |
Output is correct |
5 |
Correct |
58 ms |
26192 KB |
Output is correct |
6 |
Correct |
54 ms |
25672 KB |
Output is correct |
7 |
Correct |
67 ms |
26132 KB |
Output is correct |
8 |
Correct |
76 ms |
24648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
19 ms |
3664 KB |
Output is correct |
18 |
Correct |
19 ms |
3152 KB |
Output is correct |
19 |
Correct |
17 ms |
3664 KB |
Output is correct |
20 |
Correct |
17 ms |
3664 KB |
Output is correct |
21 |
Correct |
18 ms |
3780 KB |
Output is correct |
22 |
Correct |
28 ms |
7364 KB |
Output is correct |
23 |
Correct |
4 ms |
1104 KB |
Output is correct |
24 |
Correct |
11 ms |
2640 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
4 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
19 ms |
3664 KB |
Output is correct |
18 |
Correct |
19 ms |
3152 KB |
Output is correct |
19 |
Correct |
17 ms |
3664 KB |
Output is correct |
20 |
Correct |
17 ms |
3664 KB |
Output is correct |
21 |
Correct |
18 ms |
3780 KB |
Output is correct |
22 |
Correct |
28 ms |
7364 KB |
Output is correct |
23 |
Correct |
4 ms |
1104 KB |
Output is correct |
24 |
Correct |
11 ms |
2640 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
4 ms |
1104 KB |
Output is correct |
27 |
Correct |
2 ms |
1104 KB |
Output is correct |
28 |
Correct |
92 ms |
17412 KB |
Output is correct |
29 |
Correct |
157 ms |
24136 KB |
Output is correct |
30 |
Correct |
137 ms |
23772 KB |
Output is correct |
31 |
Correct |
104 ms |
23780 KB |
Output is correct |
32 |
Correct |
97 ms |
24392 KB |
Output is correct |
33 |
Correct |
112 ms |
23644 KB |
Output is correct |
34 |
Correct |
120 ms |
23780 KB |
Output is correct |
35 |
Correct |
42 ms |
9712 KB |
Output is correct |
36 |
Correct |
132 ms |
24392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
22308 KB |
Output is correct |
2 |
Correct |
27 ms |
22352 KB |
Output is correct |
3 |
Correct |
42 ms |
22304 KB |
Output is correct |
4 |
Correct |
38 ms |
23624 KB |
Output is correct |
5 |
Correct |
58 ms |
26192 KB |
Output is correct |
6 |
Correct |
54 ms |
25672 KB |
Output is correct |
7 |
Correct |
67 ms |
26132 KB |
Output is correct |
8 |
Correct |
76 ms |
24648 KB |
Output is correct |
9 |
Correct |
56 ms |
25928 KB |
Output is correct |
10 |
Correct |
46 ms |
16176 KB |
Output is correct |
11 |
Correct |
83 ms |
32072 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
29 ms |
22348 KB |
Output is correct |
19 |
Correct |
29 ms |
22352 KB |
Output is correct |
20 |
Correct |
26 ms |
22520 KB |
Output is correct |
21 |
Correct |
28 ms |
22364 KB |
Output is correct |
22 |
Correct |
61 ms |
27208 KB |
Output is correct |
23 |
Correct |
81 ms |
31948 KB |
Output is correct |
24 |
Correct |
91 ms |
32544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
25844 KB |
Output is correct |
2 |
Correct |
70 ms |
29576 KB |
Output is correct |
3 |
Correct |
24 ms |
22352 KB |
Output is correct |
4 |
Correct |
24 ms |
22352 KB |
Output is correct |
5 |
Correct |
136 ms |
44972 KB |
Output is correct |
6 |
Correct |
120 ms |
41108 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
89 ms |
31708 KB |
Output is correct |
9 |
Correct |
104 ms |
36800 KB |
Output is correct |
10 |
Correct |
51 ms |
25920 KB |
Output is correct |
11 |
Correct |
56 ms |
29496 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
32 ms |
22100 KB |
Output is correct |
17 |
Correct |
28 ms |
22352 KB |
Output is correct |
18 |
Correct |
51 ms |
26048 KB |
Output is correct |
19 |
Correct |
63 ms |
29372 KB |
Output is correct |
20 |
Correct |
50 ms |
24412 KB |
Output is correct |
21 |
Correct |
59 ms |
27172 KB |
Output is correct |
22 |
Correct |
62 ms |
26048 KB |
Output is correct |
23 |
Correct |
61 ms |
28604 KB |
Output is correct |
24 |
Correct |
38 ms |
22308 KB |
Output is correct |
25 |
Correct |
27 ms |
22352 KB |
Output is correct |
26 |
Correct |
42 ms |
22304 KB |
Output is correct |
27 |
Correct |
38 ms |
23624 KB |
Output is correct |
28 |
Correct |
58 ms |
26192 KB |
Output is correct |
29 |
Correct |
54 ms |
25672 KB |
Output is correct |
30 |
Correct |
67 ms |
26132 KB |
Output is correct |
31 |
Correct |
76 ms |
24648 KB |
Output is correct |
32 |
Correct |
1 ms |
500 KB |
Output is correct |
33 |
Correct |
1 ms |
504 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
2 ms |
760 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
2 ms |
592 KB |
Output is correct |
48 |
Correct |
19 ms |
3664 KB |
Output is correct |
49 |
Correct |
19 ms |
3152 KB |
Output is correct |
50 |
Correct |
17 ms |
3664 KB |
Output is correct |
51 |
Correct |
17 ms |
3664 KB |
Output is correct |
52 |
Correct |
18 ms |
3780 KB |
Output is correct |
53 |
Correct |
28 ms |
7364 KB |
Output is correct |
54 |
Correct |
4 ms |
1104 KB |
Output is correct |
55 |
Correct |
11 ms |
2640 KB |
Output is correct |
56 |
Correct |
1 ms |
336 KB |
Output is correct |
57 |
Correct |
4 ms |
1104 KB |
Output is correct |
58 |
Correct |
2 ms |
1104 KB |
Output is correct |
59 |
Correct |
92 ms |
17412 KB |
Output is correct |
60 |
Correct |
157 ms |
24136 KB |
Output is correct |
61 |
Correct |
137 ms |
23772 KB |
Output is correct |
62 |
Correct |
104 ms |
23780 KB |
Output is correct |
63 |
Correct |
97 ms |
24392 KB |
Output is correct |
64 |
Correct |
112 ms |
23644 KB |
Output is correct |
65 |
Correct |
120 ms |
23780 KB |
Output is correct |
66 |
Correct |
42 ms |
9712 KB |
Output is correct |
67 |
Correct |
132 ms |
24392 KB |
Output is correct |
68 |
Correct |
56 ms |
25928 KB |
Output is correct |
69 |
Correct |
46 ms |
16176 KB |
Output is correct |
70 |
Correct |
83 ms |
32072 KB |
Output is correct |
71 |
Correct |
1 ms |
336 KB |
Output is correct |
72 |
Correct |
1 ms |
336 KB |
Output is correct |
73 |
Correct |
1 ms |
336 KB |
Output is correct |
74 |
Correct |
1 ms |
336 KB |
Output is correct |
75 |
Correct |
1 ms |
336 KB |
Output is correct |
76 |
Correct |
1 ms |
336 KB |
Output is correct |
77 |
Correct |
29 ms |
22348 KB |
Output is correct |
78 |
Correct |
29 ms |
22352 KB |
Output is correct |
79 |
Correct |
26 ms |
22520 KB |
Output is correct |
80 |
Correct |
28 ms |
22364 KB |
Output is correct |
81 |
Correct |
61 ms |
27208 KB |
Output is correct |
82 |
Correct |
81 ms |
31948 KB |
Output is correct |
83 |
Correct |
91 ms |
32544 KB |
Output is correct |
84 |
Correct |
160 ms |
43048 KB |
Output is correct |
85 |
Correct |
157 ms |
44540 KB |
Output is correct |
86 |
Correct |
117 ms |
39876 KB |
Output is correct |
87 |
Correct |
127 ms |
41952 KB |
Output is correct |
88 |
Correct |
1 ms |
508 KB |
Output is correct |
89 |
Correct |
123 ms |
41892 KB |
Output is correct |
90 |
Correct |
137 ms |
43080 KB |
Output is correct |
91 |
Correct |
116 ms |
43864 KB |
Output is correct |