#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++ ) {
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][crtY] = dpLower[x][crtY] = -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 ) {
int prevY = ys[x - 1][lower1];
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 ( upper1 < ys[x - 1].size() ) {
int prevY = ys[x - 1][upper1];
dp[x][crtY] = max( dp[x][crtY], maxSuf2[x - 1][prevY] - 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 ) {
int prevY = ys[x - 2][lower2];
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 ( upper2 < ys[x - 2].size() ) {
int prevY = ys[x - 2][upper2];
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:47:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for ( int i = 0; i < ys[x].size(); i++ ) {
| ~~^~~~~~~~~~~~~~
fish.cpp:52:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while ( lower1 + 1 < ys[x - 1].size() && ys[x - 1][lower1 + 1] <= crtY )
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:54:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | while ( upper1 < ys[x - 1].size() && ys[x - 1][upper1] < crtY )
| ~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:62:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if ( upper1 < ys[x - 1].size() ) {
| ~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:69:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | while ( lower2 + 1 < ys[x - 2].size() && ys[x - 2][lower2 + 1] <= crtY )
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:71:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | while ( upper2 < ys[x - 2].size() && ys[x - 2][upper2] < crtY )
| ~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:79:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | if ( upper2 < ys[x - 2].size() ) {
| ~~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:91:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for ( int i = 1; i < ys[x].size(); i++ ) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
239128 KB |
Output is correct |
2 |
Correct |
400 ms |
262668 KB |
Output is correct |
3 |
Correct |
233 ms |
210208 KB |
Output is correct |
4 |
Correct |
239 ms |
210340 KB |
Output is correct |
5 |
Execution timed out |
1053 ms |
495428 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
122532 KB |
Output is correct |
2 |
Correct |
546 ms |
288896 KB |
Output is correct |
3 |
Correct |
655 ms |
323984 KB |
Output is correct |
4 |
Correct |
342 ms |
239096 KB |
Output is correct |
5 |
Correct |
388 ms |
262584 KB |
Output is correct |
6 |
Correct |
28 ms |
122448 KB |
Output is correct |
7 |
Correct |
27 ms |
122456 KB |
Output is correct |
8 |
Correct |
28 ms |
122536 KB |
Output is correct |
9 |
Correct |
28 ms |
122460 KB |
Output is correct |
10 |
Correct |
236 ms |
210004 KB |
Output is correct |
11 |
Correct |
237 ms |
210212 KB |
Output is correct |
12 |
Correct |
451 ms |
264724 KB |
Output is correct |
13 |
Correct |
502 ms |
298372 KB |
Output is correct |
14 |
Correct |
404 ms |
251768 KB |
Output is correct |
15 |
Correct |
448 ms |
263720 KB |
Output is correct |
16 |
Correct |
421 ms |
251964 KB |
Output is correct |
17 |
Correct |
460 ms |
275180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
210204 KB |
Output is correct |
2 |
Correct |
235 ms |
209976 KB |
Output is correct |
3 |
Correct |
348 ms |
221976 KB |
Output is correct |
4 |
Correct |
326 ms |
225852 KB |
Output is correct |
5 |
Correct |
476 ms |
245344 KB |
Output is correct |
6 |
Correct |
462 ms |
245324 KB |
Output is correct |
7 |
Correct |
483 ms |
245360 KB |
Output is correct |
8 |
Correct |
460 ms |
245328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
122456 KB |
Output is correct |
2 |
Correct |
28 ms |
122456 KB |
Output is correct |
3 |
Correct |
28 ms |
122460 KB |
Output is correct |
4 |
Correct |
29 ms |
122428 KB |
Output is correct |
5 |
Correct |
28 ms |
122456 KB |
Output is correct |
6 |
Correct |
28 ms |
122460 KB |
Output is correct |
7 |
Correct |
28 ms |
122524 KB |
Output is correct |
8 |
Correct |
28 ms |
122532 KB |
Output is correct |
9 |
Correct |
28 ms |
122780 KB |
Output is correct |
10 |
Correct |
31 ms |
123472 KB |
Output is correct |
11 |
Correct |
30 ms |
122716 KB |
Output is correct |
12 |
Correct |
30 ms |
123340 KB |
Output is correct |
13 |
Correct |
28 ms |
122460 KB |
Output is correct |
14 |
Correct |
30 ms |
122968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
122456 KB |
Output is correct |
2 |
Correct |
28 ms |
122456 KB |
Output is correct |
3 |
Correct |
28 ms |
122460 KB |
Output is correct |
4 |
Correct |
29 ms |
122428 KB |
Output is correct |
5 |
Correct |
28 ms |
122456 KB |
Output is correct |
6 |
Correct |
28 ms |
122460 KB |
Output is correct |
7 |
Correct |
28 ms |
122524 KB |
Output is correct |
8 |
Correct |
28 ms |
122532 KB |
Output is correct |
9 |
Correct |
28 ms |
122780 KB |
Output is correct |
10 |
Correct |
31 ms |
123472 KB |
Output is correct |
11 |
Correct |
30 ms |
122716 KB |
Output is correct |
12 |
Correct |
30 ms |
123340 KB |
Output is correct |
13 |
Correct |
28 ms |
122460 KB |
Output is correct |
14 |
Correct |
30 ms |
122968 KB |
Output is correct |
15 |
Correct |
29 ms |
122972 KB |
Output is correct |
16 |
Correct |
32 ms |
123584 KB |
Output is correct |
17 |
Correct |
96 ms |
143220 KB |
Output is correct |
18 |
Correct |
93 ms |
140624 KB |
Output is correct |
19 |
Correct |
97 ms |
141904 KB |
Output is correct |
20 |
Correct |
89 ms |
138580 KB |
Output is correct |
21 |
Correct |
85 ms |
138272 KB |
Output is correct |
22 |
Correct |
145 ms |
154556 KB |
Output is correct |
23 |
Correct |
55 ms |
129616 KB |
Output is correct |
24 |
Correct |
96 ms |
141532 KB |
Output is correct |
25 |
Correct |
31 ms |
123732 KB |
Output is correct |
26 |
Correct |
53 ms |
128340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
122456 KB |
Output is correct |
2 |
Correct |
28 ms |
122456 KB |
Output is correct |
3 |
Correct |
28 ms |
122460 KB |
Output is correct |
4 |
Correct |
29 ms |
122428 KB |
Output is correct |
5 |
Correct |
28 ms |
122456 KB |
Output is correct |
6 |
Correct |
28 ms |
122460 KB |
Output is correct |
7 |
Correct |
28 ms |
122524 KB |
Output is correct |
8 |
Correct |
28 ms |
122532 KB |
Output is correct |
9 |
Correct |
28 ms |
122780 KB |
Output is correct |
10 |
Correct |
31 ms |
123472 KB |
Output is correct |
11 |
Correct |
30 ms |
122716 KB |
Output is correct |
12 |
Correct |
30 ms |
123340 KB |
Output is correct |
13 |
Correct |
28 ms |
122460 KB |
Output is correct |
14 |
Correct |
30 ms |
122968 KB |
Output is correct |
15 |
Correct |
29 ms |
122972 KB |
Output is correct |
16 |
Correct |
32 ms |
123584 KB |
Output is correct |
17 |
Correct |
96 ms |
143220 KB |
Output is correct |
18 |
Correct |
93 ms |
140624 KB |
Output is correct |
19 |
Correct |
97 ms |
141904 KB |
Output is correct |
20 |
Correct |
89 ms |
138580 KB |
Output is correct |
21 |
Correct |
85 ms |
138272 KB |
Output is correct |
22 |
Correct |
145 ms |
154556 KB |
Output is correct |
23 |
Correct |
55 ms |
129616 KB |
Output is correct |
24 |
Correct |
96 ms |
141532 KB |
Output is correct |
25 |
Correct |
31 ms |
123732 KB |
Output is correct |
26 |
Correct |
53 ms |
128340 KB |
Output is correct |
27 |
Correct |
51 ms |
127212 KB |
Output is correct |
28 |
Correct |
375 ms |
211536 KB |
Output is correct |
29 |
Correct |
647 ms |
299348 KB |
Output is correct |
30 |
Execution timed out |
1037 ms |
365016 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
210204 KB |
Output is correct |
2 |
Correct |
235 ms |
209976 KB |
Output is correct |
3 |
Correct |
348 ms |
221976 KB |
Output is correct |
4 |
Correct |
326 ms |
225852 KB |
Output is correct |
5 |
Correct |
476 ms |
245344 KB |
Output is correct |
6 |
Correct |
462 ms |
245324 KB |
Output is correct |
7 |
Correct |
483 ms |
245360 KB |
Output is correct |
8 |
Correct |
460 ms |
245328 KB |
Output is correct |
9 |
Correct |
595 ms |
285984 KB |
Output is correct |
10 |
Correct |
330 ms |
197108 KB |
Output is correct |
11 |
Correct |
617 ms |
271440 KB |
Output is correct |
12 |
Correct |
32 ms |
122452 KB |
Output is correct |
13 |
Correct |
27 ms |
122460 KB |
Output is correct |
14 |
Correct |
29 ms |
122448 KB |
Output is correct |
15 |
Correct |
28 ms |
122460 KB |
Output is correct |
16 |
Correct |
28 ms |
122460 KB |
Output is correct |
17 |
Correct |
30 ms |
122460 KB |
Output is correct |
18 |
Correct |
234 ms |
210004 KB |
Output is correct |
19 |
Correct |
235 ms |
210128 KB |
Output is correct |
20 |
Correct |
241 ms |
210424 KB |
Output is correct |
21 |
Correct |
233 ms |
210088 KB |
Output is correct |
22 |
Correct |
599 ms |
286036 KB |
Output is correct |
23 |
Correct |
757 ms |
313140 KB |
Output is correct |
24 |
Correct |
787 ms |
323768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
239128 KB |
Output is correct |
2 |
Correct |
400 ms |
262668 KB |
Output is correct |
3 |
Correct |
233 ms |
210208 KB |
Output is correct |
4 |
Correct |
239 ms |
210340 KB |
Output is correct |
5 |
Execution timed out |
1053 ms |
495428 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |