#include "fish.h"
#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
const int maxn = 1e5+10;
// const int inf = 1e18+10;
int n, m;
vector<vector<ll>> dp[maxn];
map<int,ll> pf[maxn], pfmx0[maxn], pfmx1[maxn], sfmx0[maxn];
vector<pair<int,int>> fish[maxn]; // fishs from column i
vector<int> hr[maxn]; // important heights from column i, the ones that can be chosen
long long max_weights(int32_t N, int32_t M, vector<int32_t> X, vector<int32_t> Y, vector<int32_t> W) {
n = N;
m = M;
for(int i = 0; i < m; i++) {
fish[X[i]+1].pb(mp(Y[i]+1,W[i]));
hr[X[i]+1].pb(Y[i]);
}
for(int i = 0; i <= n; i++) {
hr[i].pb(0);
hr[i].pb(n);
sort(all(hr[i]));
sort(all(fish[i]));
hr[i].erase(unique(all(hr[i])),hr[i].end());
// Store only the relevant heights
for(int j = 0; j < hr[i].size(); j++) {
dp[i].pb({0,0});
}
}
for(int i = 1; i <= n; i++) {
for(auto j : hr[i]) pf[i][j] = 0;
for(auto aux : fish[i]) {
int y = aux.fr;
int w = aux.sc;
pf[i][y]+= w;
}
// Do a prefix sum on the weight of the fishs
pf[i][0] = 0;
for(auto it = next(pf[i].begin()); it != pf[i].end(); it++) {
it->sc+= prev(it)->sc;
}
}
// dp[i][h0][0/1] = until i, with h[i] = hr[i][h0]
// and it's greater/smaller tha the previous
// dp[1][h0][0/1] = 0
for(int h0 = 0; h0 < hr[1].size(); h0++) {
dp[1][h0][0] = 0;
dp[1][h0][1] = 0;
pfmx0[1][hr[1][h0]] = max(pfmx0[1][hr[1][h0]],dp[1][h0][0]-pf[1][hr[1][h0]]);
sfmx0[1][hr[1][h0]] = max(sfmx0[1][hr[1][h0]],dp[1][h0][0]+pf[1+1][hr[1][h0]]);
sfmx0[1][hr[1][h0]] = max(sfmx0[1][hr[1][h0]],dp[1][h0][1]+pf[1+1][hr[1][h0]]);
}
for(auto it = next(pfmx0[1].begin()); it != pfmx0[1].end(); it++) {
it->sc = max(it->sc,prev(it)->sc);
}
auto it = prev(prev(sfmx0[1].end()));
while(true) {
it->sc = max(it->sc,next(it)->sc);
if(it == sfmx0[1].begin()) break;
it = prev(it);
}
for(int i = 2; i <= n; i++) {
for(int h0 = 0; h0 < hr[i].size(); h0++) {
// It's greater than the previous
auto it = prev(upper_bound(all(hr[i-1]),hr[i][h0]));
auto itpfh1 = prev(pf[i-1].upper_bound(hr[i][h0]));
dp[i][h0][0] = pfmx0[i-1][*it] + itpfh1->sc;
if(i != 2) {
it = prev(upper_bound(all(hr[i-2]),hr[i][h0]));
dp[i][h0][0] = max(dp[i][h0][0],pfmx1[i-2][*it] + itpfh1->sc);
}
// It's smaller than the previous
it = lower_bound(all(hr[i-1]),hr[i][h0]);
auto itpfh0 = prev(pf[i].upper_bound(hr[i][h0]));
dp[i][h0][1] = sfmx0[i-1][*it] - itpfh0->sc;
if(i == n) continue;
auto itpfh11 = prev(pf[i+1].upper_bound(hr[i][h0]));
pfmx0[i][hr[i][h0]] = max(pfmx0[i][hr[i][h0]],dp[i][h0][0]-pf[i][hr[i][h0]]);
pfmx1[i][hr[i][h0]] = max(pfmx1[i][hr[i][h0]],max(dp[i][h0][1],dp[i][h0][0]));
sfmx0[i][hr[i][h0]] = max(sfmx0[i][hr[i][h0]],max(dp[i][h0][0],dp[i][h0][1])+itpfh11->sc);
}
if(i == n) continue;
for(auto it = next(pfmx0[i].begin()); it != pfmx0[i].end(); it++) {
it->sc = max(it->sc,prev(it)->sc);
}
for(auto it = next(pfmx1[i].begin()); it != pfmx1[i].end(); it++) {
it->sc = max(it->sc,prev(it)->sc);
}
auto it = prev(prev(sfmx0[i].end()));
while(true) {
it->sc = max(it->sc,next(it)->sc);
if(it == sfmx0[i].begin()) break;
it = prev(it);
}
}
ll ans = 0;
for(int h0 = 0; h0 < hr[n].size(); h0++) {
ans = max(ans, dp[n][h0][0]);
ans = max(ans, dp[n][h0][1]);
}
return ans;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int32_t, int32_t, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:38:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int j = 0; j < hr[i].size(); j++) {
| ~~^~~~~~~~~~~~~~
fish.cpp:62:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int h0 = 0; h0 < hr[1].size(); h0++) {
| ~~~^~~~~~~~~~~~~~
fish.cpp:81:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int h0 = 0; h0 < hr[i].size(); h0++) {
| ~~~^~~~~~~~~~~~~~
fish.cpp:120:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for(int h0 = 0; h0 < hr[n].size(); h0++) {
| ~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
113336 KB |
Output is correct |
2 |
Correct |
329 ms |
125884 KB |
Output is correct |
3 |
Correct |
124 ms |
91984 KB |
Output is correct |
4 |
Correct |
110 ms |
91764 KB |
Output is correct |
5 |
Correct |
802 ms |
196164 KB |
Output is correct |
6 |
Correct |
559 ms |
218184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
26068 KB |
Output is correct |
2 |
Correct |
448 ms |
136436 KB |
Output is correct |
3 |
Correct |
587 ms |
153812 KB |
Output is correct |
4 |
Correct |
292 ms |
113200 KB |
Output is correct |
5 |
Correct |
332 ms |
125908 KB |
Output is correct |
6 |
Correct |
14 ms |
26032 KB |
Output is correct |
7 |
Correct |
14 ms |
26068 KB |
Output is correct |
8 |
Correct |
14 ms |
26068 KB |
Output is correct |
9 |
Correct |
13 ms |
26068 KB |
Output is correct |
10 |
Correct |
112 ms |
91852 KB |
Output is correct |
11 |
Correct |
112 ms |
91852 KB |
Output is correct |
12 |
Correct |
269 ms |
113388 KB |
Output is correct |
13 |
Correct |
323 ms |
125916 KB |
Output is correct |
14 |
Correct |
277 ms |
113388 KB |
Output is correct |
15 |
Correct |
306 ms |
120376 KB |
Output is correct |
16 |
Correct |
307 ms |
113472 KB |
Output is correct |
17 |
Correct |
337 ms |
123048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
91868 KB |
Output is correct |
2 |
Correct |
136 ms |
91864 KB |
Output is correct |
3 |
Correct |
158 ms |
91528 KB |
Output is correct |
4 |
Correct |
154 ms |
96024 KB |
Output is correct |
5 |
Correct |
218 ms |
103536 KB |
Output is correct |
6 |
Correct |
202 ms |
103604 KB |
Output is correct |
7 |
Correct |
212 ms |
103548 KB |
Output is correct |
8 |
Correct |
228 ms |
103600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
26104 KB |
Output is correct |
2 |
Correct |
13 ms |
26104 KB |
Output is correct |
3 |
Correct |
14 ms |
26068 KB |
Output is correct |
4 |
Correct |
15 ms |
26076 KB |
Output is correct |
5 |
Correct |
14 ms |
26020 KB |
Output is correct |
6 |
Correct |
14 ms |
26068 KB |
Output is correct |
7 |
Correct |
14 ms |
26068 KB |
Output is correct |
8 |
Correct |
14 ms |
26056 KB |
Output is correct |
9 |
Correct |
16 ms |
26324 KB |
Output is correct |
10 |
Correct |
20 ms |
27092 KB |
Output is correct |
11 |
Correct |
15 ms |
26452 KB |
Output is correct |
12 |
Correct |
19 ms |
26724 KB |
Output is correct |
13 |
Correct |
15 ms |
26196 KB |
Output is correct |
14 |
Correct |
16 ms |
26616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
26104 KB |
Output is correct |
2 |
Correct |
13 ms |
26104 KB |
Output is correct |
3 |
Correct |
14 ms |
26068 KB |
Output is correct |
4 |
Correct |
15 ms |
26076 KB |
Output is correct |
5 |
Correct |
14 ms |
26020 KB |
Output is correct |
6 |
Correct |
14 ms |
26068 KB |
Output is correct |
7 |
Correct |
14 ms |
26068 KB |
Output is correct |
8 |
Correct |
14 ms |
26056 KB |
Output is correct |
9 |
Correct |
16 ms |
26324 KB |
Output is correct |
10 |
Correct |
20 ms |
27092 KB |
Output is correct |
11 |
Correct |
15 ms |
26452 KB |
Output is correct |
12 |
Correct |
19 ms |
26724 KB |
Output is correct |
13 |
Correct |
15 ms |
26196 KB |
Output is correct |
14 |
Correct |
16 ms |
26616 KB |
Output is correct |
15 |
Correct |
15 ms |
26452 KB |
Output is correct |
16 |
Correct |
17 ms |
26852 KB |
Output is correct |
17 |
Correct |
87 ms |
42356 KB |
Output is correct |
18 |
Correct |
91 ms |
42916 KB |
Output is correct |
19 |
Correct |
82 ms |
42636 KB |
Output is correct |
20 |
Correct |
82 ms |
42684 KB |
Output is correct |
21 |
Correct |
83 ms |
42580 KB |
Output is correct |
22 |
Correct |
154 ms |
58812 KB |
Output is correct |
23 |
Correct |
26 ms |
30036 KB |
Output is correct |
24 |
Correct |
63 ms |
38008 KB |
Output is correct |
25 |
Correct |
15 ms |
26708 KB |
Output is correct |
26 |
Correct |
25 ms |
29652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
26104 KB |
Output is correct |
2 |
Correct |
13 ms |
26104 KB |
Output is correct |
3 |
Correct |
14 ms |
26068 KB |
Output is correct |
4 |
Correct |
15 ms |
26076 KB |
Output is correct |
5 |
Correct |
14 ms |
26020 KB |
Output is correct |
6 |
Correct |
14 ms |
26068 KB |
Output is correct |
7 |
Correct |
14 ms |
26068 KB |
Output is correct |
8 |
Correct |
14 ms |
26056 KB |
Output is correct |
9 |
Correct |
16 ms |
26324 KB |
Output is correct |
10 |
Correct |
20 ms |
27092 KB |
Output is correct |
11 |
Correct |
15 ms |
26452 KB |
Output is correct |
12 |
Correct |
19 ms |
26724 KB |
Output is correct |
13 |
Correct |
15 ms |
26196 KB |
Output is correct |
14 |
Correct |
16 ms |
26616 KB |
Output is correct |
15 |
Correct |
15 ms |
26452 KB |
Output is correct |
16 |
Correct |
17 ms |
26852 KB |
Output is correct |
17 |
Correct |
87 ms |
42356 KB |
Output is correct |
18 |
Correct |
91 ms |
42916 KB |
Output is correct |
19 |
Correct |
82 ms |
42636 KB |
Output is correct |
20 |
Correct |
82 ms |
42684 KB |
Output is correct |
21 |
Correct |
83 ms |
42580 KB |
Output is correct |
22 |
Correct |
154 ms |
58812 KB |
Output is correct |
23 |
Correct |
26 ms |
30036 KB |
Output is correct |
24 |
Correct |
63 ms |
38008 KB |
Output is correct |
25 |
Correct |
15 ms |
26708 KB |
Output is correct |
26 |
Correct |
25 ms |
29652 KB |
Output is correct |
27 |
Correct |
23 ms |
29396 KB |
Output is correct |
28 |
Correct |
397 ms |
105820 KB |
Output is correct |
29 |
Correct |
632 ms |
136240 KB |
Output is correct |
30 |
Correct |
504 ms |
150656 KB |
Output is correct |
31 |
Correct |
518 ms |
150620 KB |
Output is correct |
32 |
Correct |
509 ms |
133380 KB |
Output is correct |
33 |
Correct |
514 ms |
151248 KB |
Output is correct |
34 |
Correct |
511 ms |
151276 KB |
Output is correct |
35 |
Correct |
210 ms |
74400 KB |
Output is correct |
36 |
Correct |
542 ms |
150496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
91868 KB |
Output is correct |
2 |
Correct |
136 ms |
91864 KB |
Output is correct |
3 |
Correct |
158 ms |
91528 KB |
Output is correct |
4 |
Correct |
154 ms |
96024 KB |
Output is correct |
5 |
Correct |
218 ms |
103536 KB |
Output is correct |
6 |
Correct |
202 ms |
103604 KB |
Output is correct |
7 |
Correct |
212 ms |
103548 KB |
Output is correct |
8 |
Correct |
228 ms |
103600 KB |
Output is correct |
9 |
Correct |
295 ms |
136496 KB |
Output is correct |
10 |
Correct |
151 ms |
82484 KB |
Output is correct |
11 |
Correct |
320 ms |
138748 KB |
Output is correct |
12 |
Correct |
14 ms |
26136 KB |
Output is correct |
13 |
Correct |
13 ms |
26092 KB |
Output is correct |
14 |
Correct |
14 ms |
26196 KB |
Output is correct |
15 |
Correct |
14 ms |
26108 KB |
Output is correct |
16 |
Correct |
16 ms |
26092 KB |
Output is correct |
17 |
Correct |
14 ms |
26068 KB |
Output is correct |
18 |
Correct |
117 ms |
91808 KB |
Output is correct |
19 |
Correct |
111 ms |
91792 KB |
Output is correct |
20 |
Correct |
124 ms |
91756 KB |
Output is correct |
21 |
Correct |
116 ms |
91832 KB |
Output is correct |
22 |
Correct |
273 ms |
133688 KB |
Output is correct |
23 |
Correct |
392 ms |
165196 KB |
Output is correct |
24 |
Correct |
381 ms |
166984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
113336 KB |
Output is correct |
2 |
Correct |
329 ms |
125884 KB |
Output is correct |
3 |
Correct |
124 ms |
91984 KB |
Output is correct |
4 |
Correct |
110 ms |
91764 KB |
Output is correct |
5 |
Correct |
802 ms |
196164 KB |
Output is correct |
6 |
Correct |
559 ms |
218184 KB |
Output is correct |
7 |
Correct |
13 ms |
26068 KB |
Output is correct |
8 |
Correct |
448 ms |
136436 KB |
Output is correct |
9 |
Correct |
587 ms |
153812 KB |
Output is correct |
10 |
Correct |
292 ms |
113200 KB |
Output is correct |
11 |
Correct |
332 ms |
125908 KB |
Output is correct |
12 |
Correct |
14 ms |
26032 KB |
Output is correct |
13 |
Correct |
14 ms |
26068 KB |
Output is correct |
14 |
Correct |
14 ms |
26068 KB |
Output is correct |
15 |
Correct |
13 ms |
26068 KB |
Output is correct |
16 |
Correct |
112 ms |
91852 KB |
Output is correct |
17 |
Correct |
112 ms |
91852 KB |
Output is correct |
18 |
Correct |
269 ms |
113388 KB |
Output is correct |
19 |
Correct |
323 ms |
125916 KB |
Output is correct |
20 |
Correct |
277 ms |
113388 KB |
Output is correct |
21 |
Correct |
306 ms |
120376 KB |
Output is correct |
22 |
Correct |
307 ms |
113472 KB |
Output is correct |
23 |
Correct |
337 ms |
123048 KB |
Output is correct |
24 |
Correct |
126 ms |
91868 KB |
Output is correct |
25 |
Correct |
136 ms |
91864 KB |
Output is correct |
26 |
Correct |
158 ms |
91528 KB |
Output is correct |
27 |
Correct |
154 ms |
96024 KB |
Output is correct |
28 |
Correct |
218 ms |
103536 KB |
Output is correct |
29 |
Correct |
202 ms |
103604 KB |
Output is correct |
30 |
Correct |
212 ms |
103548 KB |
Output is correct |
31 |
Correct |
228 ms |
103600 KB |
Output is correct |
32 |
Correct |
15 ms |
26104 KB |
Output is correct |
33 |
Correct |
13 ms |
26104 KB |
Output is correct |
34 |
Correct |
14 ms |
26068 KB |
Output is correct |
35 |
Correct |
15 ms |
26076 KB |
Output is correct |
36 |
Correct |
14 ms |
26020 KB |
Output is correct |
37 |
Correct |
14 ms |
26068 KB |
Output is correct |
38 |
Correct |
14 ms |
26068 KB |
Output is correct |
39 |
Correct |
14 ms |
26056 KB |
Output is correct |
40 |
Correct |
16 ms |
26324 KB |
Output is correct |
41 |
Correct |
20 ms |
27092 KB |
Output is correct |
42 |
Correct |
15 ms |
26452 KB |
Output is correct |
43 |
Correct |
19 ms |
26724 KB |
Output is correct |
44 |
Correct |
15 ms |
26196 KB |
Output is correct |
45 |
Correct |
16 ms |
26616 KB |
Output is correct |
46 |
Correct |
15 ms |
26452 KB |
Output is correct |
47 |
Correct |
17 ms |
26852 KB |
Output is correct |
48 |
Correct |
87 ms |
42356 KB |
Output is correct |
49 |
Correct |
91 ms |
42916 KB |
Output is correct |
50 |
Correct |
82 ms |
42636 KB |
Output is correct |
51 |
Correct |
82 ms |
42684 KB |
Output is correct |
52 |
Correct |
83 ms |
42580 KB |
Output is correct |
53 |
Correct |
154 ms |
58812 KB |
Output is correct |
54 |
Correct |
26 ms |
30036 KB |
Output is correct |
55 |
Correct |
63 ms |
38008 KB |
Output is correct |
56 |
Correct |
15 ms |
26708 KB |
Output is correct |
57 |
Correct |
25 ms |
29652 KB |
Output is correct |
58 |
Correct |
23 ms |
29396 KB |
Output is correct |
59 |
Correct |
397 ms |
105820 KB |
Output is correct |
60 |
Correct |
632 ms |
136240 KB |
Output is correct |
61 |
Correct |
504 ms |
150656 KB |
Output is correct |
62 |
Correct |
518 ms |
150620 KB |
Output is correct |
63 |
Correct |
509 ms |
133380 KB |
Output is correct |
64 |
Correct |
514 ms |
151248 KB |
Output is correct |
65 |
Correct |
511 ms |
151276 KB |
Output is correct |
66 |
Correct |
210 ms |
74400 KB |
Output is correct |
67 |
Correct |
542 ms |
150496 KB |
Output is correct |
68 |
Correct |
295 ms |
136496 KB |
Output is correct |
69 |
Correct |
151 ms |
82484 KB |
Output is correct |
70 |
Correct |
320 ms |
138748 KB |
Output is correct |
71 |
Correct |
14 ms |
26136 KB |
Output is correct |
72 |
Correct |
13 ms |
26092 KB |
Output is correct |
73 |
Correct |
14 ms |
26196 KB |
Output is correct |
74 |
Correct |
14 ms |
26108 KB |
Output is correct |
75 |
Correct |
16 ms |
26092 KB |
Output is correct |
76 |
Correct |
14 ms |
26068 KB |
Output is correct |
77 |
Correct |
117 ms |
91808 KB |
Output is correct |
78 |
Correct |
111 ms |
91792 KB |
Output is correct |
79 |
Correct |
124 ms |
91756 KB |
Output is correct |
80 |
Correct |
116 ms |
91832 KB |
Output is correct |
81 |
Correct |
273 ms |
133688 KB |
Output is correct |
82 |
Correct |
392 ms |
165196 KB |
Output is correct |
83 |
Correct |
381 ms |
166984 KB |
Output is correct |
84 |
Correct |
884 ms |
203520 KB |
Output is correct |
85 |
Correct |
849 ms |
209816 KB |
Output is correct |
86 |
Correct |
556 ms |
210416 KB |
Output is correct |
87 |
Correct |
564 ms |
218184 KB |
Output is correct |
88 |
Correct |
13 ms |
26068 KB |
Output is correct |
89 |
Correct |
620 ms |
218332 KB |
Output is correct |
90 |
Correct |
582 ms |
207820 KB |
Output is correct |
91 |
Correct |
562 ms |
197460 KB |
Output is correct |