#include "fish.h"
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
using ll = long long ;
#define sz(x) (int)(x).size()
#define xx first
#define yy second
//dp0[i][j] -> i. oszlopban j-ig elé építve piert max hogyha mindkettő szomszédosnál magasabb
//dp1[i][j] -> i. oszlopban j-ig elé építve piert max hogyha bal oldali számít
//dp2[i][j] -> i. oszlopban j-ig elé építve piert max hogyha jobb oldali számít
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
vector<vector<pair<int,ll>>> lst(N+2, vector<pair<int,ll>>());
for(int i=0;i<M;++i) {
lst[X[i]].push_back({Y[i], W[i]});
}
for(int i=0;i<=N+1;++i) lst[i].push_back({N+10,0});
for(auto& i:lst) sort(i.begin(), i.end());
for(auto& i:lst)
for(int j=1;j<sz(i);++j) i[j].yy+=i[j-1].yy;
auto get_bal=[&](int x, int l) -> ll {
auto itL=lower_bound(lst[x].begin(), lst[x].end(), make_pair(l, 0LL));
return (itL==lst[x].begin()?0:prev(itL)->yy);
};
auto get_jobb=[&](int x, int r) -> ll {
auto itR=lower_bound(lst[x].begin(), lst[x].end(), make_pair(r+1, 0LL));
return (itR==lst[x].begin()?0:prev(itR)->yy);
};
auto get_sum=[&](int x, int l, int r) -> ll {
if(l>r) return 0;
//~ ll sum=0;
//~ for(int i=0;i<sz(lst[x]);++i) {
//~ if(l<=lst[x][i].xx && lst[x][i].xx<=r) sum+=lst[x][i].yy;
//~ }
//~ cerr<<x<<","<<l<<","<<r<<" -> "<<sum<<"\n";
ll sum=get_jobb(x, r)-get_bal(x, l);
return sum;
};
vector<ll> dp0(lst[0].size());
vector<ll> dp1(lst[0].size());
vector<ll> dp2(lst[0].size());
for(int i=1;i<N;++i) {
vector<ll> ndp0(lst[i].size());
vector<ll> ndp1(lst[i].size());
vector<ll> ndp2(lst[i].size());
ll mxdp0=*max_element(dp0.begin(), dp0.end());
ll mxdp1=*max_element(dp1.begin(), dp1.end());
ll mxdp2=*max_element(dp2.begin(), dp2.end());
//dp0
int k=0;
ll mx=0;
for(int j=0;j<sz(lst[i]);++j) {
ndp0[j]=mxdp1;
for(;k<sz(lst[i-1]);++k) {
if(lst[i-1][k].xx>lst[i][j].xx-1) break ;
mx=max(mx, dp2[k]-get_bal(i-1, lst[i-1][k].xx));
}
ndp0[j]=max({ndp0[j], get_jobb(i-1, lst[i][j].xx-1)+mx});
}
//dp1
k=sz(lst[i-1])-1;
ll mx0=0, mx1=0;
for(int j=0;j<sz(lst[i]);++j) {
ndp1[j]=max(mxdp0, mxdp1);
for(;k>=0;--k) {
if(lst[i][j].xx>lst[i-1][k].xx-1) break ;
mx0=max(mx0, dp0[k]+get_jobb(i, lst[i-1][k].xx-1));
mx1=max(mx1, dp1[k]+get_jobb(i, lst[i-1][k].xx-1));
}
ndp1[j]=max({ndp1[j], max(mx0,mx1)-get_bal(i, lst[i][j].xx)});
}
//dp2
k=0;
mx=0;
for(int j=0;j<sz(lst[i]);++j) {
ndp2[j]=max(mxdp0, mxdp1);
for(;k<sz(lst[i-1]);++k) {
if(lst[i-1][k].xx>lst[i][j].xx-1) break ;
mx=max(mx, dp2[k]-get_bal(i-1, lst[i-1][k].xx));
}
ndp2[j]=max({ndp2[j], get_jobb(i-1, lst[i][j].xx-1)+mx});
}
//~ cerr<<i<<", 0: "; for(auto j:ndp0) cerr<<j<<" ";cerr<<"\n";
//~ cerr<<i<<", 1: "; for(auto j:ndp1) cerr<<j<<" ";cerr<<"\n";
//~ cerr<<i<<", 2: "; for(auto j:ndp2) cerr<<j<<" ";cerr<<"\n\n";
dp0.swap(ndp0);
dp1.swap(ndp1);
dp2.swap(ndp2);
}
return max({
*max_element(dp0.begin(), dp0.end()),
*max_element(dp1.begin(), dp1.end()),
*max_element(dp2.begin(), dp2.end())
});
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:63:12: warning: unused variable 'mxdp2' [-Wunused-variable]
63 | ll mxdp2=*max_element(dp2.begin(), dp2.end());
| ^~~~~
fish.cpp:41:10: warning: variable 'get_sum' set but not used [-Wunused-but-set-variable]
41 | auto get_sum=[&](int x, int l, int r) -> ll {
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
10308 KB |
Output is correct |
2 |
Correct |
64 ms |
11956 KB |
Output is correct |
3 |
Correct |
16 ms |
5716 KB |
Output is correct |
4 |
Correct |
17 ms |
5716 KB |
Output is correct |
5 |
Correct |
176 ms |
19616 KB |
Output is correct |
6 |
Correct |
160 ms |
19532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
113 ms |
15288 KB |
Output is correct |
3 |
Correct |
152 ms |
18316 KB |
Output is correct |
4 |
Correct |
57 ms |
10184 KB |
Output is correct |
5 |
Correct |
83 ms |
12080 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
16 ms |
5716 KB |
Output is correct |
11 |
Correct |
22 ms |
5716 KB |
Output is correct |
12 |
Correct |
63 ms |
10268 KB |
Output is correct |
13 |
Correct |
74 ms |
12052 KB |
Output is correct |
14 |
Correct |
68 ms |
10232 KB |
Output is correct |
15 |
Correct |
70 ms |
11448 KB |
Output is correct |
16 |
Correct |
55 ms |
10296 KB |
Output is correct |
17 |
Correct |
63 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
5716 KB |
Output is correct |
2 |
Correct |
16 ms |
5716 KB |
Output is correct |
3 |
Correct |
64 ms |
7608 KB |
Output is correct |
4 |
Correct |
35 ms |
7272 KB |
Output is correct |
5 |
Correct |
69 ms |
10608 KB |
Output is correct |
6 |
Correct |
69 ms |
10600 KB |
Output is correct |
7 |
Correct |
64 ms |
10600 KB |
Output is correct |
8 |
Correct |
65 ms |
10552 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
27 ms |
2252 KB |
Output is correct |
18 |
Correct |
26 ms |
2932 KB |
Output is correct |
19 |
Correct |
24 ms |
2784 KB |
Output is correct |
20 |
Correct |
22 ms |
2624 KB |
Output is correct |
21 |
Correct |
22 ms |
2752 KB |
Output is correct |
22 |
Correct |
49 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
724 KB |
Output is correct |
24 |
Correct |
16 ms |
1676 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
13 ms |
652 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
27 ms |
2252 KB |
Output is correct |
18 |
Correct |
26 ms |
2932 KB |
Output is correct |
19 |
Correct |
24 ms |
2784 KB |
Output is correct |
20 |
Correct |
22 ms |
2624 KB |
Output is correct |
21 |
Correct |
22 ms |
2752 KB |
Output is correct |
22 |
Correct |
49 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
724 KB |
Output is correct |
24 |
Correct |
16 ms |
1676 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
13 ms |
652 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
139 ms |
11016 KB |
Output is correct |
29 |
Correct |
229 ms |
14704 KB |
Output is correct |
30 |
Correct |
165 ms |
14028 KB |
Output is correct |
31 |
Correct |
182 ms |
14076 KB |
Output is correct |
32 |
Correct |
202 ms |
16048 KB |
Output is correct |
33 |
Correct |
201 ms |
14056 KB |
Output is correct |
34 |
Correct |
170 ms |
14048 KB |
Output is correct |
35 |
Correct |
65 ms |
6220 KB |
Output is correct |
36 |
Correct |
196 ms |
15952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
5716 KB |
Output is correct |
2 |
Correct |
16 ms |
5716 KB |
Output is correct |
3 |
Correct |
64 ms |
7608 KB |
Output is correct |
4 |
Correct |
35 ms |
7272 KB |
Output is correct |
5 |
Correct |
69 ms |
10608 KB |
Output is correct |
6 |
Correct |
69 ms |
10600 KB |
Output is correct |
7 |
Correct |
64 ms |
10600 KB |
Output is correct |
8 |
Correct |
65 ms |
10552 KB |
Output is correct |
9 |
Correct |
78 ms |
10600 KB |
Output is correct |
10 |
Correct |
58 ms |
8260 KB |
Output is correct |
11 |
Correct |
130 ms |
16404 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 |
1 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 |
21 ms |
5728 KB |
Output is correct |
19 |
Correct |
18 ms |
5692 KB |
Output is correct |
20 |
Correct |
23 ms |
5716 KB |
Output is correct |
21 |
Correct |
20 ms |
5716 KB |
Output is correct |
22 |
Correct |
74 ms |
10288 KB |
Output is correct |
23 |
Correct |
141 ms |
16396 KB |
Output is correct |
24 |
Correct |
158 ms |
16348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
10308 KB |
Output is correct |
2 |
Correct |
64 ms |
11956 KB |
Output is correct |
3 |
Correct |
16 ms |
5716 KB |
Output is correct |
4 |
Correct |
17 ms |
5716 KB |
Output is correct |
5 |
Correct |
176 ms |
19616 KB |
Output is correct |
6 |
Correct |
160 ms |
19532 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
113 ms |
15288 KB |
Output is correct |
9 |
Correct |
152 ms |
18316 KB |
Output is correct |
10 |
Correct |
57 ms |
10184 KB |
Output is correct |
11 |
Correct |
83 ms |
12080 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 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 |
16 ms |
5716 KB |
Output is correct |
17 |
Correct |
22 ms |
5716 KB |
Output is correct |
18 |
Correct |
63 ms |
10268 KB |
Output is correct |
19 |
Correct |
74 ms |
12052 KB |
Output is correct |
20 |
Correct |
68 ms |
10232 KB |
Output is correct |
21 |
Correct |
70 ms |
11448 KB |
Output is correct |
22 |
Correct |
55 ms |
10296 KB |
Output is correct |
23 |
Correct |
63 ms |
11352 KB |
Output is correct |
24 |
Correct |
16 ms |
5716 KB |
Output is correct |
25 |
Correct |
16 ms |
5716 KB |
Output is correct |
26 |
Correct |
64 ms |
7608 KB |
Output is correct |
27 |
Correct |
35 ms |
7272 KB |
Output is correct |
28 |
Correct |
69 ms |
10608 KB |
Output is correct |
29 |
Correct |
69 ms |
10600 KB |
Output is correct |
30 |
Correct |
64 ms |
10600 KB |
Output is correct |
31 |
Correct |
65 ms |
10552 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
2 ms |
340 KB |
Output is correct |
48 |
Correct |
27 ms |
2252 KB |
Output is correct |
49 |
Correct |
26 ms |
2932 KB |
Output is correct |
50 |
Correct |
24 ms |
2784 KB |
Output is correct |
51 |
Correct |
22 ms |
2624 KB |
Output is correct |
52 |
Correct |
22 ms |
2752 KB |
Output is correct |
53 |
Correct |
49 ms |
5068 KB |
Output is correct |
54 |
Correct |
5 ms |
724 KB |
Output is correct |
55 |
Correct |
16 ms |
1676 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
13 ms |
652 KB |
Output is correct |
58 |
Correct |
2 ms |
596 KB |
Output is correct |
59 |
Correct |
139 ms |
11016 KB |
Output is correct |
60 |
Correct |
229 ms |
14704 KB |
Output is correct |
61 |
Correct |
165 ms |
14028 KB |
Output is correct |
62 |
Correct |
182 ms |
14076 KB |
Output is correct |
63 |
Correct |
202 ms |
16048 KB |
Output is correct |
64 |
Correct |
201 ms |
14056 KB |
Output is correct |
65 |
Correct |
170 ms |
14048 KB |
Output is correct |
66 |
Correct |
65 ms |
6220 KB |
Output is correct |
67 |
Correct |
196 ms |
15952 KB |
Output is correct |
68 |
Correct |
78 ms |
10600 KB |
Output is correct |
69 |
Correct |
58 ms |
8260 KB |
Output is correct |
70 |
Correct |
130 ms |
16404 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
1 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 |
21 ms |
5728 KB |
Output is correct |
78 |
Correct |
18 ms |
5692 KB |
Output is correct |
79 |
Correct |
23 ms |
5716 KB |
Output is correct |
80 |
Correct |
20 ms |
5716 KB |
Output is correct |
81 |
Correct |
74 ms |
10288 KB |
Output is correct |
82 |
Correct |
141 ms |
16396 KB |
Output is correct |
83 |
Correct |
158 ms |
16348 KB |
Output is correct |
84 |
Correct |
231 ms |
18268 KB |
Output is correct |
85 |
Correct |
229 ms |
18852 KB |
Output is correct |
86 |
Correct |
195 ms |
19324 KB |
Output is correct |
87 |
Correct |
181 ms |
19736 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
188 ms |
19840 KB |
Output is correct |
90 |
Correct |
167 ms |
19300 KB |
Output is correct |
91 |
Correct |
170 ms |
19560 KB |
Output is correct |