#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5 + 2;
const long long INF = 1e18;
unordered_map<int, long long> costCell[MAX_N], spX[MAX_N];
vector<long long> dp[MAX_N], dpLower[MAX_N], maxPref1[MAX_N], maxSuf2[MAX_N], maxPref3[MAX_N];
vector <int> ys[MAX_N];
long long max_weights( int n, int m, vector <int> X, vector <int> Y, vector <int> W ) {
for ( int i = 0; i < m; i++ ) {
X[i]++;
Y[i]++;
costCell[X[i]][Y[i]] = W[i];
ys[X[i] - 1].push_back( Y[i] );
ys[X[i] + 1].push_back( Y[i] );
}
ys[0].clear();
ys[n + 1].clear();
for ( int x = 0; x <= n + 1; x++ )
ys[x].push_back( 0 );
for ( int x = 0; x <= n + 1; x++ ) {
sort( ys[x].begin(), ys[x].end() );
ys[x].resize( unique( ys[x].begin(), ys[x].end() ) - ys[x].begin() );
dp[x].resize( ys[x].size() );
dpLower[x].resize( ys[x].size() );
maxPref1[x].resize( ys[x].size() );
maxSuf2[x].resize( ys[x].size() );
maxPref3[x].resize( ys[x].size() );
}
for ( int x = 1; x <= n; x++ ) {
vector<int> yss;
for ( int i = 0; i < ys[x].size(); i++ )
yss.push_back( ys[x][i] );
for ( int i = 0; i < ys[x - 1].size(); i++ )
yss.push_back( ys[x - 1][i] );
for ( int i = 0; i < ys[x + 1].size(); i++ )
yss.push_back( ys[x + 1][i] );
sort( yss.begin(), yss.end() );
yss.resize( unique( yss.begin(), yss.end() ) - yss.begin() );
for ( int i = 1; i < yss.size(); i++ )
spX[x][yss[i]] = spX[x][yss[i - 1]] + costCell[x][yss[i]];
}
dp[0][0] = dpLower[0][0] = 0;
for ( int x = 1; x <= n + 1; x++ ) {
int lower1 = 0, upper1 = 0, lower2 = 0, upper2 = 0;
for ( int i = 0; i < ys[x].size(); i++ ) {
int crtY = ys[x][i];
dp[x][i] = dpLower[x][i] = -INF;
if ( x >= 1 ) {
while ( lower1 + 1 < ys[x - 1].size() && ys[x - 1][lower1 + 1] <= crtY )
lower1++;
while ( upper1 < ys[x - 1].size() && ys[x - 1][upper1] < crtY )
upper1++;
if ( lower1 >= 0 ) {
dpLower[x][i] = max( dpLower[x][i], maxPref1[x - 1][lower1] + spX[x - 1][crtY] );
dp[x][i] = max( dp[x][i], maxPref1[x - 1][lower1] + spX[x - 1][crtY] );
}
if ( upper1 < ys[x - 1].size() )
dp[x][i] = max( dp[x][i], maxSuf2[x - 1][upper1] - spX[x][crtY] );
}
if ( x >= 2 ) {
while ( lower2 + 1 < ys[x - 2].size() && ys[x - 2][lower2 + 1] <= crtY )
lower2++;
while ( upper2 < ys[x - 2].size() && ys[x - 2][upper2] < crtY )
upper2++;
if ( lower2 >= 0 ) {
dpLower[x][i] = max( dpLower[x][i], maxPref3[x - 2][lower2] + spX[x - 1][crtY] );
dp[x][i] = max( dp[x][i], maxPref3[x - 2][lower2] + spX[x - 1][crtY] );
}
if ( upper2 < ys[x - 2].size() ) {
dpLower[x][i] = max( dpLower[x][i], maxSuf2[x - 2][upper2] );
dp[x][i] = max( dp[x][i], maxSuf2[x - 2][upper2] );
}
}
maxPref1[x][i] = dpLower[x][i] - spX[x][crtY];
maxSuf2[x][i] = dp[x][i] + spX[x + 1][crtY];
maxPref3[x][i] = dpLower[x][i];
}
for ( int i = 1; i < ys[x].size(); i++ ) {
maxPref1[x][i] = max( maxPref1[x][i], maxPref1[x][i - 1] );
maxPref3[x][i] = max( maxPref3[x][i], maxPref3[x][i - 1] );
}
for ( int i = ys[x].size() - 2; i >= 0; i-- )
maxSuf2[x][i] = max( maxSuf2[x][i], maxSuf2[x][i + 1] );
}
return dp[n + 1][0];
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:38:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for ( int i = 0; i < ys[x].size(); i++ )
| ~~^~~~~~~~~~~~~~
fish.cpp:40:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for ( int i = 0; i < ys[x - 1].size(); i++ )
| ~~^~~~~~~~~~~~~~~~~~
fish.cpp:42:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for ( int i = 0; i < ys[x + 1].size(); i++ )
| ~~^~~~~~~~~~~~~~~~~~
fish.cpp:46:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for ( int i = 1; i < yss.size(); i++ )
| ~~^~~~~~~~~~~~
fish.cpp:53:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for ( int i = 0; i < ys[x].size(); i++ ) {
| ~~^~~~~~~~~~~~~~
fish.cpp:58:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while ( lower1 + 1 < ys[x - 1].size() && ys[x - 1][lower1 + 1] <= crtY )
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:60:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | while ( upper1 < ys[x - 1].size() && ys[x - 1][upper1] < crtY )
| ~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:67:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if ( upper1 < ys[x - 1].size() )
| ~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:72:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | while ( lower2 + 1 < ys[x - 2].size() && ys[x - 2][lower2 + 1] <= crtY )
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:74:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while ( upper2 < ys[x - 2].size() && ys[x - 2][upper2] < crtY )
| ~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:81:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | if ( upper2 < ys[x - 2].size() ) {
| ~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:92:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for ( int i = 1; i < ys[x].size(); i++ ) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
130000 KB |
Output is correct |
2 |
Correct |
180 ms |
142392 KB |
Output is correct |
3 |
Correct |
82 ms |
108372 KB |
Output is correct |
4 |
Correct |
83 ms |
108364 KB |
Output is correct |
5 |
Correct |
620 ms |
279104 KB |
Output is correct |
6 |
Correct |
772 ms |
263264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
75392 KB |
Output is correct |
2 |
Correct |
254 ms |
154020 KB |
Output is correct |
3 |
Correct |
308 ms |
166836 KB |
Output is correct |
4 |
Correct |
145 ms |
129984 KB |
Output is correct |
5 |
Correct |
171 ms |
142400 KB |
Output is correct |
6 |
Correct |
17 ms |
75356 KB |
Output is correct |
7 |
Correct |
17 ms |
75564 KB |
Output is correct |
8 |
Correct |
16 ms |
75356 KB |
Output is correct |
9 |
Correct |
18 ms |
75588 KB |
Output is correct |
10 |
Correct |
80 ms |
108356 KB |
Output is correct |
11 |
Correct |
82 ms |
108224 KB |
Output is correct |
12 |
Correct |
195 ms |
142920 KB |
Output is correct |
13 |
Correct |
219 ms |
159640 KB |
Output is correct |
14 |
Correct |
171 ms |
136392 KB |
Output is correct |
15 |
Correct |
179 ms |
135980 KB |
Output is correct |
16 |
Correct |
168 ms |
136532 KB |
Output is correct |
17 |
Correct |
196 ms |
147684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
108336 KB |
Output is correct |
2 |
Correct |
83 ms |
108416 KB |
Output is correct |
3 |
Correct |
155 ms |
116468 KB |
Output is correct |
4 |
Correct |
131 ms |
117812 KB |
Output is correct |
5 |
Correct |
250 ms |
127944 KB |
Output is correct |
6 |
Correct |
226 ms |
127952 KB |
Output is correct |
7 |
Correct |
225 ms |
127904 KB |
Output is correct |
8 |
Correct |
227 ms |
128020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
75352 KB |
Output is correct |
2 |
Correct |
16 ms |
75548 KB |
Output is correct |
3 |
Correct |
17 ms |
75396 KB |
Output is correct |
4 |
Correct |
17 ms |
75588 KB |
Output is correct |
5 |
Correct |
17 ms |
75356 KB |
Output is correct |
6 |
Correct |
17 ms |
75588 KB |
Output is correct |
7 |
Correct |
17 ms |
75356 KB |
Output is correct |
8 |
Correct |
16 ms |
75608 KB |
Output is correct |
9 |
Correct |
17 ms |
75648 KB |
Output is correct |
10 |
Correct |
18 ms |
75868 KB |
Output is correct |
11 |
Correct |
17 ms |
75624 KB |
Output is correct |
12 |
Correct |
18 ms |
75868 KB |
Output is correct |
13 |
Correct |
17 ms |
75612 KB |
Output is correct |
14 |
Correct |
18 ms |
75864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
75352 KB |
Output is correct |
2 |
Correct |
16 ms |
75548 KB |
Output is correct |
3 |
Correct |
17 ms |
75396 KB |
Output is correct |
4 |
Correct |
17 ms |
75588 KB |
Output is correct |
5 |
Correct |
17 ms |
75356 KB |
Output is correct |
6 |
Correct |
17 ms |
75588 KB |
Output is correct |
7 |
Correct |
17 ms |
75356 KB |
Output is correct |
8 |
Correct |
16 ms |
75608 KB |
Output is correct |
9 |
Correct |
17 ms |
75648 KB |
Output is correct |
10 |
Correct |
18 ms |
75868 KB |
Output is correct |
11 |
Correct |
17 ms |
75624 KB |
Output is correct |
12 |
Correct |
18 ms |
75868 KB |
Output is correct |
13 |
Correct |
17 ms |
75612 KB |
Output is correct |
14 |
Correct |
18 ms |
75864 KB |
Output is correct |
15 |
Correct |
17 ms |
75608 KB |
Output is correct |
16 |
Correct |
19 ms |
75864 KB |
Output is correct |
17 |
Correct |
54 ms |
85332 KB |
Output is correct |
18 |
Correct |
54 ms |
84048 KB |
Output is correct |
19 |
Correct |
51 ms |
85072 KB |
Output is correct |
20 |
Correct |
46 ms |
83292 KB |
Output is correct |
21 |
Correct |
46 ms |
83036 KB |
Output is correct |
22 |
Correct |
80 ms |
90560 KB |
Output is correct |
23 |
Correct |
30 ms |
79704 KB |
Output is correct |
24 |
Correct |
49 ms |
85588 KB |
Output is correct |
25 |
Correct |
18 ms |
75904 KB |
Output is correct |
26 |
Correct |
28 ms |
78652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
75352 KB |
Output is correct |
2 |
Correct |
16 ms |
75548 KB |
Output is correct |
3 |
Correct |
17 ms |
75396 KB |
Output is correct |
4 |
Correct |
17 ms |
75588 KB |
Output is correct |
5 |
Correct |
17 ms |
75356 KB |
Output is correct |
6 |
Correct |
17 ms |
75588 KB |
Output is correct |
7 |
Correct |
17 ms |
75356 KB |
Output is correct |
8 |
Correct |
16 ms |
75608 KB |
Output is correct |
9 |
Correct |
17 ms |
75648 KB |
Output is correct |
10 |
Correct |
18 ms |
75868 KB |
Output is correct |
11 |
Correct |
17 ms |
75624 KB |
Output is correct |
12 |
Correct |
18 ms |
75868 KB |
Output is correct |
13 |
Correct |
17 ms |
75612 KB |
Output is correct |
14 |
Correct |
18 ms |
75864 KB |
Output is correct |
15 |
Correct |
17 ms |
75608 KB |
Output is correct |
16 |
Correct |
19 ms |
75864 KB |
Output is correct |
17 |
Correct |
54 ms |
85332 KB |
Output is correct |
18 |
Correct |
54 ms |
84048 KB |
Output is correct |
19 |
Correct |
51 ms |
85072 KB |
Output is correct |
20 |
Correct |
46 ms |
83292 KB |
Output is correct |
21 |
Correct |
46 ms |
83036 KB |
Output is correct |
22 |
Correct |
80 ms |
90560 KB |
Output is correct |
23 |
Correct |
30 ms |
79704 KB |
Output is correct |
24 |
Correct |
49 ms |
85588 KB |
Output is correct |
25 |
Correct |
18 ms |
75904 KB |
Output is correct |
26 |
Correct |
28 ms |
78652 KB |
Output is correct |
27 |
Correct |
23 ms |
77660 KB |
Output is correct |
28 |
Correct |
195 ms |
117784 KB |
Output is correct |
29 |
Correct |
362 ms |
162548 KB |
Output is correct |
30 |
Correct |
587 ms |
219216 KB |
Output is correct |
31 |
Correct |
537 ms |
216800 KB |
Output is correct |
32 |
Correct |
245 ms |
127100 KB |
Output is correct |
33 |
Correct |
577 ms |
222804 KB |
Output is correct |
34 |
Correct |
563 ms |
222712 KB |
Output is correct |
35 |
Correct |
158 ms |
114772 KB |
Output is correct |
36 |
Correct |
492 ms |
191760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
108336 KB |
Output is correct |
2 |
Correct |
83 ms |
108416 KB |
Output is correct |
3 |
Correct |
155 ms |
116468 KB |
Output is correct |
4 |
Correct |
131 ms |
117812 KB |
Output is correct |
5 |
Correct |
250 ms |
127944 KB |
Output is correct |
6 |
Correct |
226 ms |
127952 KB |
Output is correct |
7 |
Correct |
225 ms |
127904 KB |
Output is correct |
8 |
Correct |
227 ms |
128020 KB |
Output is correct |
9 |
Correct |
285 ms |
152916 KB |
Output is correct |
10 |
Correct |
144 ms |
106968 KB |
Output is correct |
11 |
Correct |
305 ms |
138324 KB |
Output is correct |
12 |
Correct |
16 ms |
75352 KB |
Output is correct |
13 |
Correct |
18 ms |
75356 KB |
Output is correct |
14 |
Correct |
16 ms |
75592 KB |
Output is correct |
15 |
Correct |
18 ms |
75352 KB |
Output is correct |
16 |
Correct |
17 ms |
75356 KB |
Output is correct |
17 |
Correct |
17 ms |
75356 KB |
Output is correct |
18 |
Correct |
89 ms |
108280 KB |
Output is correct |
19 |
Correct |
81 ms |
108368 KB |
Output is correct |
20 |
Correct |
83 ms |
108372 KB |
Output is correct |
21 |
Correct |
83 ms |
108292 KB |
Output is correct |
22 |
Correct |
301 ms |
155596 KB |
Output is correct |
23 |
Correct |
358 ms |
164448 KB |
Output is correct |
24 |
Correct |
390 ms |
171092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
130000 KB |
Output is correct |
2 |
Correct |
180 ms |
142392 KB |
Output is correct |
3 |
Correct |
82 ms |
108372 KB |
Output is correct |
4 |
Correct |
83 ms |
108364 KB |
Output is correct |
5 |
Correct |
620 ms |
279104 KB |
Output is correct |
6 |
Correct |
772 ms |
263264 KB |
Output is correct |
7 |
Correct |
17 ms |
75392 KB |
Output is correct |
8 |
Correct |
254 ms |
154020 KB |
Output is correct |
9 |
Correct |
308 ms |
166836 KB |
Output is correct |
10 |
Correct |
145 ms |
129984 KB |
Output is correct |
11 |
Correct |
171 ms |
142400 KB |
Output is correct |
12 |
Correct |
17 ms |
75356 KB |
Output is correct |
13 |
Correct |
17 ms |
75564 KB |
Output is correct |
14 |
Correct |
16 ms |
75356 KB |
Output is correct |
15 |
Correct |
18 ms |
75588 KB |
Output is correct |
16 |
Correct |
80 ms |
108356 KB |
Output is correct |
17 |
Correct |
82 ms |
108224 KB |
Output is correct |
18 |
Correct |
195 ms |
142920 KB |
Output is correct |
19 |
Correct |
219 ms |
159640 KB |
Output is correct |
20 |
Correct |
171 ms |
136392 KB |
Output is correct |
21 |
Correct |
179 ms |
135980 KB |
Output is correct |
22 |
Correct |
168 ms |
136532 KB |
Output is correct |
23 |
Correct |
196 ms |
147684 KB |
Output is correct |
24 |
Correct |
80 ms |
108336 KB |
Output is correct |
25 |
Correct |
83 ms |
108416 KB |
Output is correct |
26 |
Correct |
155 ms |
116468 KB |
Output is correct |
27 |
Correct |
131 ms |
117812 KB |
Output is correct |
28 |
Correct |
250 ms |
127944 KB |
Output is correct |
29 |
Correct |
226 ms |
127952 KB |
Output is correct |
30 |
Correct |
225 ms |
127904 KB |
Output is correct |
31 |
Correct |
227 ms |
128020 KB |
Output is correct |
32 |
Correct |
17 ms |
75352 KB |
Output is correct |
33 |
Correct |
16 ms |
75548 KB |
Output is correct |
34 |
Correct |
17 ms |
75396 KB |
Output is correct |
35 |
Correct |
17 ms |
75588 KB |
Output is correct |
36 |
Correct |
17 ms |
75356 KB |
Output is correct |
37 |
Correct |
17 ms |
75588 KB |
Output is correct |
38 |
Correct |
17 ms |
75356 KB |
Output is correct |
39 |
Correct |
16 ms |
75608 KB |
Output is correct |
40 |
Correct |
17 ms |
75648 KB |
Output is correct |
41 |
Correct |
18 ms |
75868 KB |
Output is correct |
42 |
Correct |
17 ms |
75624 KB |
Output is correct |
43 |
Correct |
18 ms |
75868 KB |
Output is correct |
44 |
Correct |
17 ms |
75612 KB |
Output is correct |
45 |
Correct |
18 ms |
75864 KB |
Output is correct |
46 |
Correct |
17 ms |
75608 KB |
Output is correct |
47 |
Correct |
19 ms |
75864 KB |
Output is correct |
48 |
Correct |
54 ms |
85332 KB |
Output is correct |
49 |
Correct |
54 ms |
84048 KB |
Output is correct |
50 |
Correct |
51 ms |
85072 KB |
Output is correct |
51 |
Correct |
46 ms |
83292 KB |
Output is correct |
52 |
Correct |
46 ms |
83036 KB |
Output is correct |
53 |
Correct |
80 ms |
90560 KB |
Output is correct |
54 |
Correct |
30 ms |
79704 KB |
Output is correct |
55 |
Correct |
49 ms |
85588 KB |
Output is correct |
56 |
Correct |
18 ms |
75904 KB |
Output is correct |
57 |
Correct |
28 ms |
78652 KB |
Output is correct |
58 |
Correct |
23 ms |
77660 KB |
Output is correct |
59 |
Correct |
195 ms |
117784 KB |
Output is correct |
60 |
Correct |
362 ms |
162548 KB |
Output is correct |
61 |
Correct |
587 ms |
219216 KB |
Output is correct |
62 |
Correct |
537 ms |
216800 KB |
Output is correct |
63 |
Correct |
245 ms |
127100 KB |
Output is correct |
64 |
Correct |
577 ms |
222804 KB |
Output is correct |
65 |
Correct |
563 ms |
222712 KB |
Output is correct |
66 |
Correct |
158 ms |
114772 KB |
Output is correct |
67 |
Correct |
492 ms |
191760 KB |
Output is correct |
68 |
Correct |
285 ms |
152916 KB |
Output is correct |
69 |
Correct |
144 ms |
106968 KB |
Output is correct |
70 |
Correct |
305 ms |
138324 KB |
Output is correct |
71 |
Correct |
16 ms |
75352 KB |
Output is correct |
72 |
Correct |
18 ms |
75356 KB |
Output is correct |
73 |
Correct |
16 ms |
75592 KB |
Output is correct |
74 |
Correct |
18 ms |
75352 KB |
Output is correct |
75 |
Correct |
17 ms |
75356 KB |
Output is correct |
76 |
Correct |
17 ms |
75356 KB |
Output is correct |
77 |
Correct |
89 ms |
108280 KB |
Output is correct |
78 |
Correct |
81 ms |
108368 KB |
Output is correct |
79 |
Correct |
83 ms |
108372 KB |
Output is correct |
80 |
Correct |
83 ms |
108292 KB |
Output is correct |
81 |
Correct |
301 ms |
155596 KB |
Output is correct |
82 |
Correct |
358 ms |
164448 KB |
Output is correct |
83 |
Correct |
390 ms |
171092 KB |
Output is correct |
84 |
Correct |
626 ms |
247984 KB |
Output is correct |
85 |
Correct |
637 ms |
245260 KB |
Output is correct |
86 |
Correct |
796 ms |
262100 KB |
Output is correct |
87 |
Correct |
826 ms |
264020 KB |
Output is correct |
88 |
Correct |
17 ms |
75356 KB |
Output is correct |
89 |
Correct |
823 ms |
264056 KB |
Output is correct |
90 |
Correct |
625 ms |
211536 KB |
Output is correct |
91 |
Correct |
383 ms |
170708 KB |
Output is correct |