#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10, smalln = 3010;
vector < pair < int, int > > col[maxn];
vector < int > state[maxn];
unordered_set < int > state_set[maxn];
vector < vector < ll > > dp[maxn];
ll val[smalln][smalln];
ll pref[smalln][smalln];
ll get_sum(int col, int left, int right)
{
if (left > right)
return 0;
ll sum = pref[col][right];
if (left > 0)
sum = sum - pref[col][left - 1];
return sum;
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W)
{
bool all_even = true, small_x = true;
for (int i = 0; i < M; i ++)
{
if (X[i] % 2 != 0)
all_even = false;
if (X[i] > 1)
small_x = false;
}
for (int i = 0; i < M; i ++)
{
col[X[i]].push_back({Y[i], W[i]});
}
for (int i = 0; i < N; i ++)
sort(col[i].begin(), col[i].end());
for (int i = 0; i < M; i ++)
{
if (X[i] != 0)
state_set[X[i] - 1].insert(Y[i] + 1);
if (X[i] != N - 1)
state_set[X[i] + 1].insert(Y[i] + 1);
}
for (int i = 0; i < N; i ++)
{
state_set[i].insert(0);
state_set[i].insert(N);
}
for (int i = 0; i < N; i ++)
{
for (auto it : state_set[i])
state[i].push_back(it);
sort(state[i].begin(), state[i].end());
}
if (all_even)
{
ll sum = 0;
for (int i = 0; i < M; i ++)
sum = sum + W[i];
return sum;
}
else if (small_x)
{
ll zero = 0, one = 0;
for (int i = 0; i < M; i ++)
{
if (X[i] == 0)
zero += W[i];
else
one += W[i];
}
if (N == 2)
return max(zero, one);
int to = 0;
ll cur = 0, ans = 0;
for (int i = 0; i < col[0].size(); i ++)
{
if (cur + one > ans)
ans = cur + one;
cur += col[0][i].second;
while(to < col[1].size() && col[1][to].first <= col[0][i].first)
{
one -= col[1][to].second;
to ++;
}
}
if (cur + one > ans)
ans = cur + one;
return ans;
}
for (int i = 0; i < M; i ++)
val[X[i]][Y[i]] = W[i];
/**for (int i = 0; i < N; i ++, cout << endl)
for (int j = 0; j < N; j ++)
cout << val[i][j] << " ";*/
for (int i = 0; i < N; i ++)
{
pref[i][0] = val[i][0];
for (int j = 1; j <= N; j ++)
{
pref[i][j] = pref[i][j - 1] + val[i][j];
}
}
for (int i = 0; i < N; i ++)
{
dp[i].resize(state[i].size());
for (int j = 0; j < state[i].size(); j ++)
{
dp[i][j].resize(2);
}
}
for (int j = 0; j < state[1].size(); j ++)
dp[1][j][0] = dp[1][j][1] = 0;
for (int i = 1; i < N; i ++)
{
ll so_far = 0, to = 0;
for (int j = 0; j < state[i].size(); j ++)
{
if (j > 0)
dp[i][j][1] = dp[i][j - 1][1] + get_sum(i - 1, state[i][j - 1], state[i][j] - 1);
while(to < state[i - 1].size() && state[i - 1][to] <= state[i][j])
{
///cout << i << " " << j << " :: " << state[i - 1][to] << " " << state[i][j] << endl;
so_far = max(so_far, dp[i - 1][to][1] + get_sum(i - 1, state[i - 1][to], state[i][j] - 1));
to ++;
}
dp[i][j][1] = max(dp[i][j][1], so_far);
}
so_far = 0;
to = (int)(state[i - 1].size()) - 1;
for (int j = (int)(state[i].size()) - 1; j >= 0; j --)
{
if (j + 1 < state[i].size())
dp[i][j][0] = dp[i][j + 1][0] + get_sum(i, state[i][j], state[i][j + 1] - 1);
while(to >= 0 && state[i - 1][to] >= state[i][j])
{
///cout << i << " " << j << " ::: " << to << endl;
so_far = max(so_far, max(dp[i - 1][to][0], dp[i - 1][to][1]) + get_sum(i, state[i][j], state[i - 1][to] - 1));
to --;
}
dp[i][j][0] = max(dp[i][j][0], so_far);
}
if (i > 1)
{
so_far = 0;
to = 0;
for (int j = 0; j < state[i].size(); j ++)
{
while(to < state[i - 2].size() && state[i - 2][to] <= state[i][j])
{
so_far = max(so_far, max(dp[i - 2][to][0], dp[i - 2][to][1]));
to ++;
}
dp[i][j][1] = max(so_far + get_sum(i - 1, 0, state[i][j] - 1), dp[i][j][1]);
}
so_far = 0;
to = (int)(state[i - 2].size()) - 1;
for (int j = (int)(state[i].size()) - 1; j >= 0; j --)
{
while(to >= 0 && state[i - 2][to] >= state[i][j])
{
so_far = max(so_far, max(dp[i - 2][to][0], dp[i - 2][to][1]) + get_sum(i - 1, 0, state[i - 2][to] - 1));
to --;
}
dp[i][j][1] = max(so_far, dp[i][j][1]);
}
}
}
/**cout << "-----------" << endl;
for (int i = 0; i < N; i ++, cout << endl)
for (int j = 0; j < state[i].size(); j ++)
cout << dp[i][j][0] << " ";
cout << "-----------" << endl;
for (int i = 0; i < N; i ++, cout << endl)
for (int j = 0; j < state[i].size(); j ++)
cout << dp[i][j][1] << " ";*/
ll ans = 0;
for (int j = 0; j < state[N - 1].size(); j ++)
for (int t = 0; t < 2; t ++)
ans = max(ans, dp[N - 1][j][t]);
return ans;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:90:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0; i < col[0].size(); i ++)
| ~~^~~~~~~~~~~~~~~
fish.cpp:95:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | while(to < col[1].size() && col[1][to].first <= col[0][i].first)
| ~~~^~~~~~~~~~~~~~~
fish.cpp:127:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for (int j = 0; j < state[i].size(); j ++)
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:133:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for (int j = 0; j < state[1].size(); j ++)
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:139:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for (int j = 0; j < state[i].size(); j ++)
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:143:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
143 | while(to < state[i - 1].size() && state[i - 1][to] <= state[i][j])
| ~~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:156:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | if (j + 1 < state[i].size())
| ~~~~~~^~~~~~~~~~~~~~~~~
fish.cpp:173:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
173 | for (int j = 0; j < state[i].size(); j ++)
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:175:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
175 | while(to < state[i - 2].size() && state[i - 2][to] <= state[i][j])
| ~~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:205:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
205 | for (int j = 0; j < state[N - 1].size(); j ++)
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
37520 KB |
Output is correct |
2 |
Correct |
114 ms |
41584 KB |
Output is correct |
3 |
Correct |
52 ms |
33116 KB |
Output is correct |
4 |
Correct |
53 ms |
33100 KB |
Output is correct |
5 |
Correct |
441 ms |
73616 KB |
Output is correct |
6 |
Correct |
521 ms |
70120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
170 ms |
47268 KB |
Output is correct |
3 |
Correct |
214 ms |
54680 KB |
Output is correct |
4 |
Correct |
99 ms |
37592 KB |
Output is correct |
5 |
Correct |
114 ms |
41572 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12756 KB |
Output is correct |
8 |
Correct |
7 ms |
12756 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
53 ms |
33100 KB |
Output is correct |
11 |
Correct |
53 ms |
33148 KB |
Output is correct |
12 |
Correct |
113 ms |
41164 KB |
Output is correct |
13 |
Correct |
139 ms |
46472 KB |
Output is correct |
14 |
Correct |
115 ms |
39560 KB |
Output is correct |
15 |
Correct |
122 ms |
43196 KB |
Output is correct |
16 |
Correct |
106 ms |
39324 KB |
Output is correct |
17 |
Correct |
120 ms |
43088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
33100 KB |
Output is correct |
2 |
Runtime error |
78 ms |
67012 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12884 KB |
Output is correct |
3 |
Correct |
7 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12756 KB |
Output is correct |
5 |
Correct |
6 ms |
12756 KB |
Output is correct |
6 |
Correct |
7 ms |
12756 KB |
Output is correct |
7 |
Correct |
7 ms |
12756 KB |
Output is correct |
8 |
Correct |
7 ms |
12756 KB |
Output is correct |
9 |
Correct |
8 ms |
14292 KB |
Output is correct |
10 |
Correct |
10 ms |
16340 KB |
Output is correct |
11 |
Correct |
8 ms |
14308 KB |
Output is correct |
12 |
Correct |
9 ms |
16212 KB |
Output is correct |
13 |
Correct |
8 ms |
13524 KB |
Output is correct |
14 |
Correct |
10 ms |
16084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12884 KB |
Output is correct |
3 |
Correct |
7 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12756 KB |
Output is correct |
5 |
Correct |
6 ms |
12756 KB |
Output is correct |
6 |
Correct |
7 ms |
12756 KB |
Output is correct |
7 |
Correct |
7 ms |
12756 KB |
Output is correct |
8 |
Correct |
7 ms |
12756 KB |
Output is correct |
9 |
Correct |
8 ms |
14292 KB |
Output is correct |
10 |
Correct |
10 ms |
16340 KB |
Output is correct |
11 |
Correct |
8 ms |
14308 KB |
Output is correct |
12 |
Correct |
9 ms |
16212 KB |
Output is correct |
13 |
Correct |
8 ms |
13524 KB |
Output is correct |
14 |
Correct |
10 ms |
16084 KB |
Output is correct |
15 |
Correct |
9 ms |
16084 KB |
Output is correct |
16 |
Correct |
8 ms |
13652 KB |
Output is correct |
17 |
Correct |
45 ms |
23672 KB |
Output is correct |
18 |
Correct |
41 ms |
23008 KB |
Output is correct |
19 |
Correct |
41 ms |
23776 KB |
Output is correct |
20 |
Correct |
38 ms |
22836 KB |
Output is correct |
21 |
Correct |
39 ms |
22728 KB |
Output is correct |
22 |
Correct |
67 ms |
29648 KB |
Output is correct |
23 |
Correct |
17 ms |
18772 KB |
Output is correct |
24 |
Correct |
35 ms |
22904 KB |
Output is correct |
25 |
Correct |
10 ms |
16216 KB |
Output is correct |
26 |
Correct |
17 ms |
18132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12884 KB |
Output is correct |
3 |
Correct |
7 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12756 KB |
Output is correct |
5 |
Correct |
6 ms |
12756 KB |
Output is correct |
6 |
Correct |
7 ms |
12756 KB |
Output is correct |
7 |
Correct |
7 ms |
12756 KB |
Output is correct |
8 |
Correct |
7 ms |
12756 KB |
Output is correct |
9 |
Correct |
8 ms |
14292 KB |
Output is correct |
10 |
Correct |
10 ms |
16340 KB |
Output is correct |
11 |
Correct |
8 ms |
14308 KB |
Output is correct |
12 |
Correct |
9 ms |
16212 KB |
Output is correct |
13 |
Correct |
8 ms |
13524 KB |
Output is correct |
14 |
Correct |
10 ms |
16084 KB |
Output is correct |
15 |
Correct |
9 ms |
16084 KB |
Output is correct |
16 |
Correct |
8 ms |
13652 KB |
Output is correct |
17 |
Correct |
45 ms |
23672 KB |
Output is correct |
18 |
Correct |
41 ms |
23008 KB |
Output is correct |
19 |
Correct |
41 ms |
23776 KB |
Output is correct |
20 |
Correct |
38 ms |
22836 KB |
Output is correct |
21 |
Correct |
39 ms |
22728 KB |
Output is correct |
22 |
Correct |
67 ms |
29648 KB |
Output is correct |
23 |
Correct |
17 ms |
18772 KB |
Output is correct |
24 |
Correct |
35 ms |
22904 KB |
Output is correct |
25 |
Correct |
10 ms |
16216 KB |
Output is correct |
26 |
Correct |
17 ms |
18132 KB |
Output is correct |
27 |
Correct |
72 ms |
97224 KB |
Output is correct |
28 |
Correct |
196 ms |
59148 KB |
Output is correct |
29 |
Correct |
415 ms |
146776 KB |
Output is correct |
30 |
Correct |
558 ms |
200976 KB |
Output is correct |
31 |
Correct |
557 ms |
202472 KB |
Output is correct |
32 |
Correct |
281 ms |
68300 KB |
Output is correct |
33 |
Correct |
559 ms |
227080 KB |
Output is correct |
34 |
Correct |
556 ms |
226932 KB |
Output is correct |
35 |
Correct |
195 ms |
126024 KB |
Output is correct |
36 |
Correct |
476 ms |
167376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
33100 KB |
Output is correct |
2 |
Runtime error |
78 ms |
67012 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
37520 KB |
Output is correct |
2 |
Correct |
114 ms |
41584 KB |
Output is correct |
3 |
Correct |
52 ms |
33116 KB |
Output is correct |
4 |
Correct |
53 ms |
33100 KB |
Output is correct |
5 |
Correct |
441 ms |
73616 KB |
Output is correct |
6 |
Correct |
521 ms |
70120 KB |
Output is correct |
7 |
Correct |
7 ms |
12756 KB |
Output is correct |
8 |
Correct |
170 ms |
47268 KB |
Output is correct |
9 |
Correct |
214 ms |
54680 KB |
Output is correct |
10 |
Correct |
99 ms |
37592 KB |
Output is correct |
11 |
Correct |
114 ms |
41572 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
7 ms |
12756 KB |
Output is correct |
14 |
Correct |
7 ms |
12756 KB |
Output is correct |
15 |
Correct |
7 ms |
12748 KB |
Output is correct |
16 |
Correct |
53 ms |
33100 KB |
Output is correct |
17 |
Correct |
53 ms |
33148 KB |
Output is correct |
18 |
Correct |
113 ms |
41164 KB |
Output is correct |
19 |
Correct |
139 ms |
46472 KB |
Output is correct |
20 |
Correct |
115 ms |
39560 KB |
Output is correct |
21 |
Correct |
122 ms |
43196 KB |
Output is correct |
22 |
Correct |
106 ms |
39324 KB |
Output is correct |
23 |
Correct |
120 ms |
43088 KB |
Output is correct |
24 |
Correct |
55 ms |
33100 KB |
Output is correct |
25 |
Runtime error |
78 ms |
67012 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |