#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
const int N = 1e5 + 100;
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w){
vector<ll> fishes[N];
vector<vector<int>> states(n + 1, {0});
for(int i = 0; i < m; ++i){
++y[i];
for(int k = max(0, x[i] - 1); k <= min(n - 1, x[i] + 1); ++k){
states[k].pb(y[i]);
}
}
for(int i = 0; i <= n; ++i){
sort(states[i].begin(), states[i].end());
states[i].erase(unique(states[i].begin(), states[i].end()), states[i].end());
fishes[i].resize(states[i].size());
}
for(int i = 0; i < m; ++i){
int j = x[i];
int Y = lower_bound(states[j].begin(), states[j].end(), y[i]) - states[j].begin();
fishes[j][Y] += w[i];
}
vector<vector<ll>> dp_up(n + 1), dp_down(n + 1);
for(int i = 0; i <= n; ++i){
int sz = states[i].size();
dp_up[i].resize(sz, 0);
dp_down[i].resize(sz, 0);
if(i > 0){
ll best = dp_down[i - 1][0], sum = 0;
for(int j = 0, left = 0; j < sz; ++j){
while(left + 1 < states[i - 1].size() && states[i - 1][left + 1] <= states[i][j]){
++left;
best += fishes[i - 1][left];
best = max(best, dp_down[i - 1][left]);
}
dp_down[i][j] = max(dp_down[i][j], best);
}
best = 0;
sum = accumulate(fishes[i].begin(), fishes[i].end(), 0ll);
for(int j = sz - 1, left = states[i - 1].size(); j >= 0; --j){
while(left > 0 && states[i - 1][left - 1] >= states[i][j]){
--left;
best = max(best, max(dp_up[i - 1][left], dp_down[i - 1][left]) + sum);
}
dp_up[i][j] = max(dp_up[i][j], best - sum);
sum -= fishes[i][j];
}
}
if(i > 1){
ll best = max(dp_down[i - 2][0], dp_up[i - 2][0]), sum = 0;
for(int j = 0, left = 0, leftt = 0; j < sz; ++j){
while(leftt + 1 < states[i - 2].size() && states[i - 2][leftt + 1] <= states[i][j]){
++leftt;
while(left + 1 < states[i - 1].size() && states[i - 1][left + 1] <= states[i][j]){
++left;
sum += fishes[i - 1][left];
}
best = max(best, max(dp_down[i - 2][leftt], dp_up[i - 2][leftt]) + sum);
}
dp_down[i][j] = max(dp_down[i][j], best);
}
best = 0;
sum = accumulate(fishes[i - 1].begin(), fishes[i - 1].end(), 0ll);
for(int j = sz - 1, left = states[i - 1].size(), leftt = states[i - 2].size(); j >= 0; --j){
while(leftt - 1 > 0 && states[i - 2][leftt - 1] >= states[i][j]){
--leftt;
while(left - 1 > 0 && states[i - 1][left - 1] >= states[i - 2][leftt]){
--left;
sum -= fishes[i - 1][left];
}
best = max(best, max(dp_down[i - 2][leftt], dp_up[i - 2][leftt]) + sum);
}
dp_down[i][j] = max(dp_down[i][j], best);
}
}
}
return max(dp_up[n][0], dp_down[n][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:36:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while(left + 1 < states[i - 1].size() && states[i - 1][left + 1] <= states[i][j]){
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
fish.cpp:57:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | while(leftt + 1 < states[i - 2].size() && states[i - 2][leftt + 1] <= states[i][j]){
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
fish.cpp:59:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | while(left + 1 < states[i - 1].size() && states[i - 1][left + 1] <= states[i][j]){
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
26616 KB |
Output is correct |
2 |
Correct |
85 ms |
30252 KB |
Output is correct |
3 |
Correct |
32 ms |
22100 KB |
Output is correct |
4 |
Correct |
29 ms |
22116 KB |
Output is correct |
5 |
Correct |
236 ms |
54376 KB |
Output is correct |
6 |
Correct |
289 ms |
51236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
131 ms |
32000 KB |
Output is correct |
3 |
Correct |
152 ms |
36164 KB |
Output is correct |
4 |
Correct |
72 ms |
26608 KB |
Output is correct |
5 |
Correct |
87 ms |
30296 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
31 ms |
22092 KB |
Output is correct |
11 |
Correct |
29 ms |
22228 KB |
Output is correct |
12 |
Correct |
79 ms |
28912 KB |
Output is correct |
13 |
Correct |
93 ms |
32988 KB |
Output is correct |
14 |
Correct |
75 ms |
27836 KB |
Output is correct |
15 |
Correct |
83 ms |
28728 KB |
Output is correct |
16 |
Correct |
79 ms |
27892 KB |
Output is correct |
17 |
Correct |
84 ms |
30652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
22172 KB |
Output is correct |
2 |
Correct |
29 ms |
22220 KB |
Output is correct |
3 |
Incorrect |
55 ms |
21612 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '16359027219341' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2604 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Incorrect |
2 ms |
2644 KB |
1st lines differ - on the 1st token, expected: '216624184325', found: '191674602563' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2604 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Incorrect |
2 ms |
2644 KB |
1st lines differ - on the 1st token, expected: '216624184325', found: '191674602563' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2604 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Incorrect |
2 ms |
2644 KB |
1st lines differ - on the 1st token, expected: '216624184325', found: '191674602563' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
22172 KB |
Output is correct |
2 |
Correct |
29 ms |
22220 KB |
Output is correct |
3 |
Incorrect |
55 ms |
21612 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '16359027219341' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
26616 KB |
Output is correct |
2 |
Correct |
85 ms |
30252 KB |
Output is correct |
3 |
Correct |
32 ms |
22100 KB |
Output is correct |
4 |
Correct |
29 ms |
22116 KB |
Output is correct |
5 |
Correct |
236 ms |
54376 KB |
Output is correct |
6 |
Correct |
289 ms |
51236 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
131 ms |
32000 KB |
Output is correct |
9 |
Correct |
152 ms |
36164 KB |
Output is correct |
10 |
Correct |
72 ms |
26608 KB |
Output is correct |
11 |
Correct |
87 ms |
30296 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
31 ms |
22092 KB |
Output is correct |
17 |
Correct |
29 ms |
22228 KB |
Output is correct |
18 |
Correct |
79 ms |
28912 KB |
Output is correct |
19 |
Correct |
93 ms |
32988 KB |
Output is correct |
20 |
Correct |
75 ms |
27836 KB |
Output is correct |
21 |
Correct |
83 ms |
28728 KB |
Output is correct |
22 |
Correct |
79 ms |
27892 KB |
Output is correct |
23 |
Correct |
84 ms |
30652 KB |
Output is correct |
24 |
Correct |
29 ms |
22172 KB |
Output is correct |
25 |
Correct |
29 ms |
22220 KB |
Output is correct |
26 |
Incorrect |
55 ms |
21612 KB |
1st lines differ - on the 1st token, expected: '21261825233649', found: '16359027219341' |
27 |
Halted |
0 ms |
0 KB |
- |