#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], dp[MAX_N], dpLower[MAX_N], spX[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 = 1; x <= n; x++ ) {
sort( ys[x].begin(), ys[x].end() );
ys[x].resize( unique( ys[x].begin(), ys[x].end() ) - ys[x].begin() );
}
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++ ) {
for ( int i = 0; i < ys[x].size(); i++ ) {
int crtY = ys[x][i];
dp[x][crtY] = dpLower[x][crtY] = -INF;
if ( x >= 1 ) {
auto lower = upper_bound( ys[x - 1].begin(), ys[x - 1].end(), crtY );
auto upper = lower_bound( ys[x - 1].begin(), ys[x - 1].end(), crtY );
if ( lower != ys[x - 1].begin() ) {
lower--;
int prevY = *lower;
dpLower[x][crtY] = max( dpLower[x][crtY], maxPref1[x - 1][prevY] + spX[x - 1][crtY] );
dp[x][crtY] = max( dp[x][crtY], maxPref1[x - 1][prevY] + spX[x - 1][crtY] );
}
if ( upper != ys[x - 1].end() ) {
int prevY = *upper;
dp[x][crtY] = max( dp[x][crtY], maxSuf2[x - 1][prevY] - spX[x][crtY] );
}
}
if ( x >= 2 ) {
auto lower = upper_bound( ys[x - 2].begin(), ys[x - 2].end(), crtY );
auto upper = lower_bound( ys[x - 2].begin(), ys[x - 2].end(), crtY );
if ( lower != ys[x - 2].begin() ) {
lower--;
int prevY = *lower;
dpLower[x][crtY] = max( dpLower[x][crtY], maxPref3[x - 2][prevY] + spX[x - 1][crtY] );
dp[x][crtY] = max( dp[x][crtY], maxPref3[x - 2][prevY] + spX[x - 1][crtY] );
}
if ( upper != ys[x - 2].end() ) {
int prevY = *upper;
dpLower[x][crtY] = max( dpLower[x][crtY], maxSuf2[x - 2][prevY] );
dp[x][crtY] = max( dp[x][crtY], maxSuf2[x - 2][prevY] );
}
}
maxPref1[x][crtY] = dpLower[x][crtY] - spX[x][crtY];
maxSuf2[x][crtY] = dp[x][crtY] + spX[x + 1][crtY];
maxPref3[x][crtY] = dpLower[x][crtY];
}
for ( int i = 1; i < ys[x].size(); i++ ) {
maxPref1[x][ys[x][i]] = max( maxPref1[x][ys[x][i]], maxPref1[x][ys[x][i - 1]] );
maxPref3[x][ys[x][i]] = max( maxPref3[x][ys[x][i]], maxPref3[x][ys[x][i - 1]] );
}
for ( int i = ys[x].size() - 2; i >= 0; i-- )
maxSuf2[x][ys[x][i]] = max( maxSuf2[x][ys[x][i]], maxSuf2[x][ys[x][i + 1]] );
}
/*for ( int x = 0; x <= n + 1; x++ ) {
for ( int i = 0; i < ys[x].size(); i++ )
printf( "(%d %lld) ", ys[x][i], dp[x][i] );
printf( "\n" );
}*/
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:32:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for ( int i = 0; i < ys[x].size(); i++ )
| ~~^~~~~~~~~~~~~~
fish.cpp:34:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for ( int i = 0; i < ys[x - 1].size(); i++ )
| ~~^~~~~~~~~~~~~~~~~~
fish.cpp:36:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for ( int i = 0; i < ys[x + 1].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 = 1; i < yss.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 = 0; i < ys[x].size(); i++ ) {
| ~~^~~~~~~~~~~~~~
fish.cpp:86:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for ( int i = 1; i < ys[x].size(); i++ ) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
239128 KB |
Output is correct |
2 |
Correct |
319 ms |
262652 KB |
Output is correct |
3 |
Correct |
164 ms |
210256 KB |
Output is correct |
4 |
Correct |
165 ms |
210068 KB |
Output is correct |
5 |
Execution timed out |
1057 ms |
495200 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
122456 KB |
Output is correct |
2 |
Correct |
510 ms |
288948 KB |
Output is correct |
3 |
Correct |
604 ms |
324048 KB |
Output is correct |
4 |
Correct |
272 ms |
239132 KB |
Output is correct |
5 |
Correct |
327 ms |
262832 KB |
Output is correct |
6 |
Correct |
27 ms |
122532 KB |
Output is correct |
7 |
Correct |
29 ms |
122460 KB |
Output is correct |
8 |
Correct |
27 ms |
122460 KB |
Output is correct |
9 |
Correct |
29 ms |
122460 KB |
Output is correct |
10 |
Correct |
168 ms |
210200 KB |
Output is correct |
11 |
Correct |
163 ms |
210176 KB |
Output is correct |
12 |
Correct |
425 ms |
264720 KB |
Output is correct |
13 |
Correct |
441 ms |
298404 KB |
Output is correct |
14 |
Correct |
344 ms |
251812 KB |
Output is correct |
15 |
Correct |
388 ms |
263724 KB |
Output is correct |
16 |
Correct |
349 ms |
251980 KB |
Output is correct |
17 |
Correct |
406 ms |
275348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
175 ms |
210204 KB |
Output is correct |
2 |
Correct |
162 ms |
210008 KB |
Output is correct |
3 |
Correct |
269 ms |
221984 KB |
Output is correct |
4 |
Correct |
264 ms |
225908 KB |
Output is correct |
5 |
Correct |
379 ms |
245332 KB |
Output is correct |
6 |
Correct |
390 ms |
245324 KB |
Output is correct |
7 |
Correct |
392 ms |
245332 KB |
Output is correct |
8 |
Correct |
382 ms |
245292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
122460 KB |
Output is correct |
2 |
Correct |
28 ms |
122460 KB |
Output is correct |
3 |
Correct |
27 ms |
122528 KB |
Output is correct |
4 |
Correct |
27 ms |
122460 KB |
Output is correct |
5 |
Correct |
28 ms |
122456 KB |
Output is correct |
6 |
Correct |
31 ms |
122460 KB |
Output is correct |
7 |
Correct |
27 ms |
122712 KB |
Output is correct |
8 |
Correct |
28 ms |
122460 KB |
Output is correct |
9 |
Correct |
28 ms |
122832 KB |
Output is correct |
10 |
Correct |
31 ms |
123228 KB |
Output is correct |
11 |
Correct |
29 ms |
122716 KB |
Output is correct |
12 |
Correct |
30 ms |
123224 KB |
Output is correct |
13 |
Correct |
28 ms |
122460 KB |
Output is correct |
14 |
Correct |
30 ms |
122972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
122460 KB |
Output is correct |
2 |
Correct |
28 ms |
122460 KB |
Output is correct |
3 |
Correct |
27 ms |
122528 KB |
Output is correct |
4 |
Correct |
27 ms |
122460 KB |
Output is correct |
5 |
Correct |
28 ms |
122456 KB |
Output is correct |
6 |
Correct |
31 ms |
122460 KB |
Output is correct |
7 |
Correct |
27 ms |
122712 KB |
Output is correct |
8 |
Correct |
28 ms |
122460 KB |
Output is correct |
9 |
Correct |
28 ms |
122832 KB |
Output is correct |
10 |
Correct |
31 ms |
123228 KB |
Output is correct |
11 |
Correct |
29 ms |
122716 KB |
Output is correct |
12 |
Correct |
30 ms |
123224 KB |
Output is correct |
13 |
Correct |
28 ms |
122460 KB |
Output is correct |
14 |
Correct |
30 ms |
122972 KB |
Output is correct |
15 |
Correct |
29 ms |
122972 KB |
Output is correct |
16 |
Correct |
32 ms |
123600 KB |
Output is correct |
17 |
Correct |
104 ms |
143316 KB |
Output is correct |
18 |
Correct |
97 ms |
140624 KB |
Output is correct |
19 |
Correct |
105 ms |
142072 KB |
Output is correct |
20 |
Correct |
94 ms |
138696 KB |
Output is correct |
21 |
Correct |
87 ms |
138396 KB |
Output is correct |
22 |
Correct |
153 ms |
154568 KB |
Output is correct |
23 |
Correct |
58 ms |
129964 KB |
Output is correct |
24 |
Correct |
100 ms |
141528 KB |
Output is correct |
25 |
Correct |
30 ms |
123480 KB |
Output is correct |
26 |
Correct |
51 ms |
128324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
122460 KB |
Output is correct |
2 |
Correct |
28 ms |
122460 KB |
Output is correct |
3 |
Correct |
27 ms |
122528 KB |
Output is correct |
4 |
Correct |
27 ms |
122460 KB |
Output is correct |
5 |
Correct |
28 ms |
122456 KB |
Output is correct |
6 |
Correct |
31 ms |
122460 KB |
Output is correct |
7 |
Correct |
27 ms |
122712 KB |
Output is correct |
8 |
Correct |
28 ms |
122460 KB |
Output is correct |
9 |
Correct |
28 ms |
122832 KB |
Output is correct |
10 |
Correct |
31 ms |
123228 KB |
Output is correct |
11 |
Correct |
29 ms |
122716 KB |
Output is correct |
12 |
Correct |
30 ms |
123224 KB |
Output is correct |
13 |
Correct |
28 ms |
122460 KB |
Output is correct |
14 |
Correct |
30 ms |
122972 KB |
Output is correct |
15 |
Correct |
29 ms |
122972 KB |
Output is correct |
16 |
Correct |
32 ms |
123600 KB |
Output is correct |
17 |
Correct |
104 ms |
143316 KB |
Output is correct |
18 |
Correct |
97 ms |
140624 KB |
Output is correct |
19 |
Correct |
105 ms |
142072 KB |
Output is correct |
20 |
Correct |
94 ms |
138696 KB |
Output is correct |
21 |
Correct |
87 ms |
138396 KB |
Output is correct |
22 |
Correct |
153 ms |
154568 KB |
Output is correct |
23 |
Correct |
58 ms |
129964 KB |
Output is correct |
24 |
Correct |
100 ms |
141528 KB |
Output is correct |
25 |
Correct |
30 ms |
123480 KB |
Output is correct |
26 |
Correct |
51 ms |
128324 KB |
Output is correct |
27 |
Correct |
39 ms |
127316 KB |
Output is correct |
28 |
Correct |
430 ms |
211548 KB |
Output is correct |
29 |
Correct |
703 ms |
299344 KB |
Output is correct |
30 |
Execution timed out |
1040 ms |
365392 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
175 ms |
210204 KB |
Output is correct |
2 |
Correct |
162 ms |
210008 KB |
Output is correct |
3 |
Correct |
269 ms |
221984 KB |
Output is correct |
4 |
Correct |
264 ms |
225908 KB |
Output is correct |
5 |
Correct |
379 ms |
245332 KB |
Output is correct |
6 |
Correct |
390 ms |
245324 KB |
Output is correct |
7 |
Correct |
392 ms |
245332 KB |
Output is correct |
8 |
Correct |
382 ms |
245292 KB |
Output is correct |
9 |
Correct |
510 ms |
286032 KB |
Output is correct |
10 |
Correct |
264 ms |
196948 KB |
Output is correct |
11 |
Correct |
540 ms |
271332 KB |
Output is correct |
12 |
Correct |
28 ms |
122456 KB |
Output is correct |
13 |
Correct |
28 ms |
122456 KB |
Output is correct |
14 |
Correct |
28 ms |
122460 KB |
Output is correct |
15 |
Correct |
27 ms |
122460 KB |
Output is correct |
16 |
Correct |
28 ms |
122476 KB |
Output is correct |
17 |
Correct |
29 ms |
122708 KB |
Output is correct |
18 |
Correct |
164 ms |
210200 KB |
Output is correct |
19 |
Correct |
166 ms |
210000 KB |
Output is correct |
20 |
Correct |
164 ms |
210004 KB |
Output is correct |
21 |
Correct |
171 ms |
209956 KB |
Output is correct |
22 |
Correct |
520 ms |
285964 KB |
Output is correct |
23 |
Correct |
663 ms |
313056 KB |
Output is correct |
24 |
Correct |
738 ms |
323892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
239128 KB |
Output is correct |
2 |
Correct |
319 ms |
262652 KB |
Output is correct |
3 |
Correct |
164 ms |
210256 KB |
Output is correct |
4 |
Correct |
165 ms |
210068 KB |
Output is correct |
5 |
Execution timed out |
1057 ms |
495200 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |