#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
template<typename S, typename T>
void xmax(S&a, T const&b){
if(b>a) a = b;
}
struct Fish{
int y;
ll w;
ll left = 0, middle = 0, right = 0;
};
struct DP{
vector<ll> up, down;
ll zero_with = 0, zero_without = 0;
ll next_zero_with = 0, next_zero_without = 0;
};
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
vector<vector<Fish> > ev(N+2);
for(int i=0; i<M; ++i){
ev[X[i]+1].push_back(Fish{Y[i], W[i]});
ev[X[i]+1].push_back(Fish{Y[i]+1, 0});
}
for(auto &e : ev) {
e.push_back(Fish{-1, 0});
e.push_back(Fish{N+1, 0});
sort(e.begin(), e.end(), [&](auto const&a, auto const&b){ return a.y < b.y; });
e.erase(unique(e.begin(), e.end(), [&](auto &a, auto &b){
if(a.y == b.y){
a.w = b.w = a.w+b.w;
return true;
}
return false;
}), e.end());
for(int i=1; i<(int)e.size(); ++i){
e[i].middle = e[i-1].middle + e[i-1].w;
}
}
for(int x=0; x<=N; ++x){
auto &L = ev[x], &R = ev[x+1];
int i = (int)L.size()-1, j = (int)R.size()-1;
while(i >= 0 && j >= 0){
auto &l = L[i], &r = R[j];
if(l.y < r.y){
r.left = l.middle;
--j;
} else if(l.y > r.y){
l.right = r.middle;
--i;
} else {
l.right = r.middle;
r.left = l.middle;
--i; --j;
}
}
}
DP prev;
prev.up.assign(2, 0);
prev.down.assign(2, 0);
for(int x=1; x<=N+1; ++x){
auto &you = ev[x-1];
auto &me = ev[x];
/* compute up: For each height
* - find best pred
* - add left
* - subtract me (due to next up)
* - check transform into down
*
* compute down: For each height
* - find best pred (or cur.up)
* - add right
* - subtract me (due to prev down)
* - check transform to zero.
*
*/
DP cur;
cur.up.assign(me.size(), 0);
cur.down.assign(me.size(), 0);
/// compute up
{
int i = (int)you.size()-1, j = (int)me.size()-1;
while(i >= 0 && j >= 0){
if(you[i].y > me[j].y) --i;
else {
cur.up[j] = prev.up[i];
--j;
}
}
}
// add left - middle
// also consider the zero start
for(int i=0; i<(int)me.size(); ++i){
xmax(cur.up[i], prev.zero_without);
cur.up[i] += me[i].left;
xmax(cur.up[i], prev.zero_with);
cur.up[i] -= me[i].middle;
}
/// update zeros
cur.zero_with = prev.next_zero_with;
cur.zero_without = prev.next_zero_without;
/// compute down
{
// find best pred
int i = 0, j = 0;
while(i < (int)you.size() && j < (int)me.size()){
if(you[i].y < me[j].y) ++i;
else {
cur.down[j] = max(prev.down[i], cur.up[j] + 2*me[j].middle);
++j;
}
}
}
// add right - middle
// consider creating zeros
for(int i = (int)me.size()-1; i > 0; --i){
cur.down[i] -= me[i].middle;
xmax(cur.next_zero_without, cur.down[i]);
cur.down[i] += me[i].right;
xmax(cur.next_zero_with, cur.down[i]);
}
/*cerr << "at: " << x << "\n";
for(int i=0; i<(int)me.size(); ++i){
auto &e = me[i];
cerr << e.y << " " << e.w << " : " << e.left << " / " << e.middle << " / " << e.right << " : ";
cerr << cur.up[i] << " | " << cur.down[i] << "\n";
}*/
/// turn values into prefix / suffic maxima
// move up
for(int i=1; i<(int)me.size(); ++i){
xmax(cur.up[i], cur.up[i-1]);
}
// move down
for(int i = (int)me.size()-1; i > 0; --i){
xmax(cur.down[i-1], cur.down[i]);
}
prev = move(cur);
}
return max(prev.zero_with, prev.up[0]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
20364 KB |
Output is correct |
2 |
Correct |
78 ms |
23692 KB |
Output is correct |
3 |
Correct |
23 ms |
11988 KB |
Output is correct |
4 |
Correct |
30 ms |
11988 KB |
Output is correct |
5 |
Correct |
222 ms |
44008 KB |
Output is correct |
6 |
Correct |
227 ms |
52544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
109 ms |
30144 KB |
Output is correct |
3 |
Correct |
134 ms |
35444 KB |
Output is correct |
4 |
Correct |
64 ms |
20356 KB |
Output is correct |
5 |
Correct |
79 ms |
23844 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
23 ms |
11988 KB |
Output is correct |
11 |
Correct |
23 ms |
11988 KB |
Output is correct |
12 |
Correct |
64 ms |
20408 KB |
Output is correct |
13 |
Correct |
77 ms |
23708 KB |
Output is correct |
14 |
Correct |
66 ms |
20548 KB |
Output is correct |
15 |
Correct |
74 ms |
22764 KB |
Output is correct |
16 |
Correct |
64 ms |
20428 KB |
Output is correct |
17 |
Correct |
76 ms |
22704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
11988 KB |
Output is correct |
2 |
Correct |
23 ms |
11988 KB |
Output is correct |
3 |
Correct |
50 ms |
16860 KB |
Output is correct |
4 |
Correct |
48 ms |
15696 KB |
Output is correct |
5 |
Correct |
82 ms |
24012 KB |
Output is correct |
6 |
Correct |
75 ms |
24032 KB |
Output is correct |
7 |
Correct |
81 ms |
24000 KB |
Output is correct |
8 |
Correct |
82 ms |
24012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
21 ms |
5588 KB |
Output is correct |
18 |
Correct |
20 ms |
7152 KB |
Output is correct |
19 |
Correct |
19 ms |
7304 KB |
Output is correct |
20 |
Correct |
21 ms |
7124 KB |
Output is correct |
21 |
Correct |
21 ms |
7236 KB |
Output is correct |
22 |
Correct |
40 ms |
13652 KB |
Output is correct |
23 |
Correct |
4 ms |
1620 KB |
Output is correct |
24 |
Correct |
14 ms |
4308 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
21 ms |
5588 KB |
Output is correct |
18 |
Correct |
20 ms |
7152 KB |
Output is correct |
19 |
Correct |
19 ms |
7304 KB |
Output is correct |
20 |
Correct |
21 ms |
7124 KB |
Output is correct |
21 |
Correct |
21 ms |
7236 KB |
Output is correct |
22 |
Correct |
40 ms |
13652 KB |
Output is correct |
23 |
Correct |
4 ms |
1620 KB |
Output is correct |
24 |
Correct |
14 ms |
4308 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1492 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
99 ms |
31120 KB |
Output is correct |
29 |
Correct |
144 ms |
36556 KB |
Output is correct |
30 |
Correct |
161 ms |
40300 KB |
Output is correct |
31 |
Correct |
159 ms |
40176 KB |
Output is correct |
32 |
Correct |
142 ms |
44920 KB |
Output is correct |
33 |
Correct |
158 ms |
40164 KB |
Output is correct |
34 |
Correct |
157 ms |
40160 KB |
Output is correct |
35 |
Correct |
59 ms |
18432 KB |
Output is correct |
36 |
Correct |
152 ms |
43232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
11988 KB |
Output is correct |
2 |
Correct |
23 ms |
11988 KB |
Output is correct |
3 |
Correct |
50 ms |
16860 KB |
Output is correct |
4 |
Correct |
48 ms |
15696 KB |
Output is correct |
5 |
Correct |
82 ms |
24012 KB |
Output is correct |
6 |
Correct |
75 ms |
24032 KB |
Output is correct |
7 |
Correct |
81 ms |
24000 KB |
Output is correct |
8 |
Correct |
82 ms |
24012 KB |
Output is correct |
9 |
Correct |
91 ms |
24068 KB |
Output is correct |
10 |
Correct |
70 ms |
21824 KB |
Output is correct |
11 |
Correct |
156 ms |
43492 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
23 ms |
11968 KB |
Output is correct |
19 |
Correct |
23 ms |
11976 KB |
Output is correct |
20 |
Correct |
23 ms |
12016 KB |
Output is correct |
21 |
Correct |
23 ms |
11988 KB |
Output is correct |
22 |
Correct |
90 ms |
25048 KB |
Output is correct |
23 |
Correct |
151 ms |
43484 KB |
Output is correct |
24 |
Correct |
158 ms |
43508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
20364 KB |
Output is correct |
2 |
Correct |
78 ms |
23692 KB |
Output is correct |
3 |
Correct |
23 ms |
11988 KB |
Output is correct |
4 |
Correct |
30 ms |
11988 KB |
Output is correct |
5 |
Correct |
222 ms |
44008 KB |
Output is correct |
6 |
Correct |
227 ms |
52544 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
109 ms |
30144 KB |
Output is correct |
9 |
Correct |
134 ms |
35444 KB |
Output is correct |
10 |
Correct |
64 ms |
20356 KB |
Output is correct |
11 |
Correct |
79 ms |
23844 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
23 ms |
11988 KB |
Output is correct |
17 |
Correct |
23 ms |
11988 KB |
Output is correct |
18 |
Correct |
64 ms |
20408 KB |
Output is correct |
19 |
Correct |
77 ms |
23708 KB |
Output is correct |
20 |
Correct |
66 ms |
20548 KB |
Output is correct |
21 |
Correct |
74 ms |
22764 KB |
Output is correct |
22 |
Correct |
64 ms |
20428 KB |
Output is correct |
23 |
Correct |
76 ms |
22704 KB |
Output is correct |
24 |
Correct |
26 ms |
11988 KB |
Output is correct |
25 |
Correct |
23 ms |
11988 KB |
Output is correct |
26 |
Correct |
50 ms |
16860 KB |
Output is correct |
27 |
Correct |
48 ms |
15696 KB |
Output is correct |
28 |
Correct |
82 ms |
24012 KB |
Output is correct |
29 |
Correct |
75 ms |
24032 KB |
Output is correct |
30 |
Correct |
81 ms |
24000 KB |
Output is correct |
31 |
Correct |
82 ms |
24012 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
21 ms |
5588 KB |
Output is correct |
49 |
Correct |
20 ms |
7152 KB |
Output is correct |
50 |
Correct |
19 ms |
7304 KB |
Output is correct |
51 |
Correct |
21 ms |
7124 KB |
Output is correct |
52 |
Correct |
21 ms |
7236 KB |
Output is correct |
53 |
Correct |
40 ms |
13652 KB |
Output is correct |
54 |
Correct |
4 ms |
1620 KB |
Output is correct |
55 |
Correct |
14 ms |
4308 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
5 ms |
1492 KB |
Output is correct |
58 |
Correct |
2 ms |
980 KB |
Output is correct |
59 |
Correct |
99 ms |
31120 KB |
Output is correct |
60 |
Correct |
144 ms |
36556 KB |
Output is correct |
61 |
Correct |
161 ms |
40300 KB |
Output is correct |
62 |
Correct |
159 ms |
40176 KB |
Output is correct |
63 |
Correct |
142 ms |
44920 KB |
Output is correct |
64 |
Correct |
158 ms |
40164 KB |
Output is correct |
65 |
Correct |
157 ms |
40160 KB |
Output is correct |
66 |
Correct |
59 ms |
18432 KB |
Output is correct |
67 |
Correct |
152 ms |
43232 KB |
Output is correct |
68 |
Correct |
91 ms |
24068 KB |
Output is correct |
69 |
Correct |
70 ms |
21824 KB |
Output is correct |
70 |
Correct |
156 ms |
43492 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
23 ms |
11968 KB |
Output is correct |
78 |
Correct |
23 ms |
11976 KB |
Output is correct |
79 |
Correct |
23 ms |
12016 KB |
Output is correct |
80 |
Correct |
23 ms |
11988 KB |
Output is correct |
81 |
Correct |
90 ms |
25048 KB |
Output is correct |
82 |
Correct |
151 ms |
43484 KB |
Output is correct |
83 |
Correct |
158 ms |
43508 KB |
Output is correct |
84 |
Correct |
197 ms |
42712 KB |
Output is correct |
85 |
Correct |
197 ms |
43952 KB |
Output is correct |
86 |
Correct |
218 ms |
52300 KB |
Output is correct |
87 |
Correct |
232 ms |
53132 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
240 ms |
53188 KB |
Output is correct |
90 |
Correct |
207 ms |
51944 KB |
Output is correct |
91 |
Correct |
184 ms |
52836 KB |
Output is correct |