#include "fish.h"
#include <bits/stdc++.h>
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;
const long long inf=1e16;
long long max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
set<int> tw;
for(int i=0;i<m;i++){
if(y[i]==0){
tw.insert(x[i]);
}
}
vector<vector<pair<int,long long> > > v(n);
for(int i=0;i<n;i++){
v[i].push_back({-2,0});
if(tw.find(i)!=tw.end()){
continue;
}
x.push_back(i);
y.push_back(0);
w.push_back(0);
}
m=x.size();
vector<vector<int> > lis(n);
vector<int> s(n),vs(n);
for(int i=0;i<m;i++){
v[x[i]].push_back({y[i],w[i]});
lis[x[i]].push_back(y[i]-1);
lis[x[i]].push_back(y[i]);
if(x[i]-1>=0){
lis[x[i]-1].push_back(y[i]);
}
if(x[i]+1<n){
lis[x[i]+1].push_back(y[i]);
}
}
auto org=lis;
for(int i=0;i<n;i++){
sort(v[i].begin(),v[i].end());
vs[i]=v[i].size();
for(int j=1;j<vs[i];j++){
v[i][j].sc+=v[i][j-1].sc;
}
v[i].push_back({1e9,v[i][vs[i]-1].sc});
sort(lis[i].begin(),lis[i].end());
lis[i].erase(unique(lis[i].begin(),lis[i].end()),lis[i].end());
s[i]=lis[i].size();
}
auto get=[&](int i,int a){
return lower_bound(lis[i].begin(),lis[i].end(),a)-lis[i].begin();
};
auto get2=[&](int i,int a){
return upper_bound(v[i].begin(),v[i].end(),pair<int,long long>(a,inf))-v[i].begin()-1;
};
auto get3=[&](int i,int a){
return upper_bound(v[i].begin(),v[i].end(),pair<int,long long>(a,-1))-v[i].begin();
};
vector<vector<long long> > dp[3];
dp[0].resize(n);
dp[1].resize(n);
dp[2].resize(n);
// 0: i-1 >= i, 1: i-1 < i, 2: i = 0, j = the chosen blocks in col i-1
for(int i=0;i<n;i++){
dp[0][i].resize(s[i]);
dp[1][i].resize(s[i]);
if(i){
dp[2][i].resize(s[i-1]);
}
else{
dp[2][i].resize(1);
}
}
for(int i=0;i<s[0];i++){
dp[1][0][i]=-inf;
}
for(int i=1;i<n;i++){
//dp[0][i][j] = max(dp[0][i-1][j]+cnt[i]);
// 0 -> 0
// 1 -> 0
int l=0;
int r=s[i-1]-1;
long long mx=-inf;
for(int j=s[i]-1;j>0;j--){
while(r>=0 && lis[i-1][r]>=lis[i][j]){
mx=max(mx,max(dp[0][i-1][r],dp[1][i-1][r])+v[i][get2(i,lis[i-1][r])].sc);
r--;
}
dp[0][i][j]=max(dp[0][i][j],mx-v[i][get2(i,lis[i][j])].sc);
}
// 1 -> 1
l=0;
mx=-inf;
for(int j=1;j<s[i];j++){
while(l<s[i-1] && lis[i-1][l]<lis[i][j]){
mx=max(mx,dp[1][i-1][l]-v[i-1][get2(i-1,lis[i-1][l])].sc);
l++;
}
dp[1][i][j]=max(dp[1][i][j],mx+v[i-1][get2(i-1,lis[i][j])].sc);
}
// 2 -> 1
if(i-2>=0){
l=0;
mx=-inf;
for(int j=1;j<s[i];j++){
while(l<s[i-2] && lis[i-2][l]<lis[i][j]){
int z=get2(i-1,lis[i-2][l]);
mx=max(mx,dp[2][i-1][l]-v[i-1][z].sc);
l++;
}
int z=get2(i-1,lis[i][j]);
dp[1][i][j]=max(dp[1][i][j],mx+v[i-1][z].sc);
}
r=s[i-2]-1;
mx=-inf;
for(int j=s[i]-1;j>0;j--){
while(r>=0 && lis[i-2][r]>=lis[i][j]){
mx=max(mx,dp[2][i-1][r]);
r--;
}
dp[1][i][j]=max(dp[1][i][j],mx);
}
}
else{
for(int j=1;j<s[i];j++){
dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][0]+v[i-1][get2(i-1,lis[i][j])].sc);
}
}
// 0 -> 2
// 1 -> 2
for(int j=0;j<s[i-1];j++){
dp[2][i][j]=max(dp[0][i-1][j],dp[1][i-1][j])+v[i][get2(i,lis[i-1][j])].sc;
}
// 2 -> 2
if(i>=2){
for(int j=0;j<s[i-2];j++){
dp[2][i][0]=max(dp[2][i][0],dp[2][i-1][j]);
}
}
else{
dp[2][i][0]=max(dp[2][i][0],0ll);
}
}
/*
for(int i=0;i<3;i++){
for(int j=0;j<n;j++){
for(auto h:dp[i][j]){
cout << h << " ";
}
cout << '\n';
}
cout << '\n';
}
*/
long long ans=0;
for(int i=0;i<2;i++){
for(int j=0;j<s[n-1];j++){
ans=max(ans,dp[i][n-1][j]);
}
}
for(int i=0;i<s[n-2];i++){
ans=max(ans,dp[2][n-1][i]);
}
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:52:10: warning: variable 'get' set but not used [-Wunused-but-set-variable]
52 | auto get=[&](int i,int a){
| ^~~
fish.cpp:58:10: warning: variable 'get3' set but not used [-Wunused-but-set-variable]
58 | auto get3=[&](int i,int a){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
45040 KB |
Output is correct |
2 |
Correct |
138 ms |
50816 KB |
Output is correct |
3 |
Correct |
63 ms |
39768 KB |
Output is correct |
4 |
Correct |
68 ms |
39736 KB |
Output is correct |
5 |
Correct |
363 ms |
84656 KB |
Output is correct |
6 |
Correct |
403 ms |
93512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
189 ms |
52908 KB |
Output is correct |
3 |
Correct |
228 ms |
60080 KB |
Output is correct |
4 |
Correct |
110 ms |
45104 KB |
Output is correct |
5 |
Correct |
127 ms |
50852 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 |
64 ms |
39848 KB |
Output is correct |
11 |
Correct |
62 ms |
39836 KB |
Output is correct |
12 |
Correct |
135 ms |
48412 KB |
Output is correct |
13 |
Correct |
160 ms |
54732 KB |
Output is correct |
14 |
Correct |
125 ms |
47072 KB |
Output is correct |
15 |
Correct |
137 ms |
50368 KB |
Output is correct |
16 |
Correct |
129 ms |
47200 KB |
Output is correct |
17 |
Correct |
144 ms |
52764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
39736 KB |
Output is correct |
2 |
Correct |
64 ms |
39744 KB |
Output is correct |
3 |
Correct |
99 ms |
39368 KB |
Output is correct |
4 |
Correct |
92 ms |
42844 KB |
Output is correct |
5 |
Correct |
154 ms |
47136 KB |
Output is correct |
6 |
Correct |
151 ms |
47164 KB |
Output is correct |
7 |
Correct |
159 ms |
47280 KB |
Output is correct |
8 |
Correct |
161 ms |
47196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
428 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
38 ms |
5348 KB |
Output is correct |
18 |
Correct |
36 ms |
6360 KB |
Output is correct |
19 |
Correct |
26 ms |
6068 KB |
Output is correct |
20 |
Correct |
26 ms |
5920 KB |
Output is correct |
21 |
Correct |
27 ms |
5964 KB |
Output is correct |
22 |
Correct |
67 ms |
11400 KB |
Output is correct |
23 |
Correct |
10 ms |
2132 KB |
Output is correct |
24 |
Correct |
32 ms |
4948 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
8 ms |
1832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
428 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
38 ms |
5348 KB |
Output is correct |
18 |
Correct |
36 ms |
6360 KB |
Output is correct |
19 |
Correct |
26 ms |
6068 KB |
Output is correct |
20 |
Correct |
26 ms |
5920 KB |
Output is correct |
21 |
Correct |
27 ms |
5964 KB |
Output is correct |
22 |
Correct |
67 ms |
11400 KB |
Output is correct |
23 |
Correct |
10 ms |
2132 KB |
Output is correct |
24 |
Correct |
32 ms |
4948 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
8 ms |
1832 KB |
Output is correct |
27 |
Correct |
4 ms |
1748 KB |
Output is correct |
28 |
Correct |
185 ms |
26292 KB |
Output is correct |
29 |
Correct |
274 ms |
37840 KB |
Output is correct |
30 |
Correct |
379 ms |
51896 KB |
Output is correct |
31 |
Correct |
397 ms |
51540 KB |
Output is correct |
32 |
Correct |
232 ms |
35592 KB |
Output is correct |
33 |
Correct |
411 ms |
52880 KB |
Output is correct |
34 |
Correct |
406 ms |
52864 KB |
Output is correct |
35 |
Correct |
100 ms |
18660 KB |
Output is correct |
36 |
Correct |
406 ms |
49764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
39736 KB |
Output is correct |
2 |
Correct |
64 ms |
39744 KB |
Output is correct |
3 |
Correct |
99 ms |
39368 KB |
Output is correct |
4 |
Correct |
92 ms |
42844 KB |
Output is correct |
5 |
Correct |
154 ms |
47136 KB |
Output is correct |
6 |
Correct |
151 ms |
47164 KB |
Output is correct |
7 |
Correct |
159 ms |
47280 KB |
Output is correct |
8 |
Correct |
161 ms |
47196 KB |
Output is correct |
9 |
Correct |
145 ms |
51636 KB |
Output is correct |
10 |
Correct |
101 ms |
27784 KB |
Output is correct |
11 |
Correct |
229 ms |
55244 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 |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
64 ms |
39812 KB |
Output is correct |
19 |
Correct |
65 ms |
39824 KB |
Output is correct |
20 |
Correct |
64 ms |
39812 KB |
Output is correct |
21 |
Correct |
63 ms |
39736 KB |
Output is correct |
22 |
Correct |
197 ms |
59140 KB |
Output is correct |
23 |
Correct |
284 ms |
70808 KB |
Output is correct |
24 |
Correct |
256 ms |
72648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
45040 KB |
Output is correct |
2 |
Correct |
138 ms |
50816 KB |
Output is correct |
3 |
Correct |
63 ms |
39768 KB |
Output is correct |
4 |
Correct |
68 ms |
39736 KB |
Output is correct |
5 |
Correct |
363 ms |
84656 KB |
Output is correct |
6 |
Correct |
403 ms |
93512 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
189 ms |
52908 KB |
Output is correct |
9 |
Correct |
228 ms |
60080 KB |
Output is correct |
10 |
Correct |
110 ms |
45104 KB |
Output is correct |
11 |
Correct |
127 ms |
50852 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 |
64 ms |
39848 KB |
Output is correct |
17 |
Correct |
62 ms |
39836 KB |
Output is correct |
18 |
Correct |
135 ms |
48412 KB |
Output is correct |
19 |
Correct |
160 ms |
54732 KB |
Output is correct |
20 |
Correct |
125 ms |
47072 KB |
Output is correct |
21 |
Correct |
137 ms |
50368 KB |
Output is correct |
22 |
Correct |
129 ms |
47200 KB |
Output is correct |
23 |
Correct |
144 ms |
52764 KB |
Output is correct |
24 |
Correct |
68 ms |
39736 KB |
Output is correct |
25 |
Correct |
64 ms |
39744 KB |
Output is correct |
26 |
Correct |
99 ms |
39368 KB |
Output is correct |
27 |
Correct |
92 ms |
42844 KB |
Output is correct |
28 |
Correct |
154 ms |
47136 KB |
Output is correct |
29 |
Correct |
151 ms |
47164 KB |
Output is correct |
30 |
Correct |
159 ms |
47280 KB |
Output is correct |
31 |
Correct |
161 ms |
47196 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 |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
428 KB |
Output is correct |
47 |
Correct |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
38 ms |
5348 KB |
Output is correct |
49 |
Correct |
36 ms |
6360 KB |
Output is correct |
50 |
Correct |
26 ms |
6068 KB |
Output is correct |
51 |
Correct |
26 ms |
5920 KB |
Output is correct |
52 |
Correct |
27 ms |
5964 KB |
Output is correct |
53 |
Correct |
67 ms |
11400 KB |
Output is correct |
54 |
Correct |
10 ms |
2132 KB |
Output is correct |
55 |
Correct |
32 ms |
4948 KB |
Output is correct |
56 |
Correct |
1 ms |
596 KB |
Output is correct |
57 |
Correct |
8 ms |
1832 KB |
Output is correct |
58 |
Correct |
4 ms |
1748 KB |
Output is correct |
59 |
Correct |
185 ms |
26292 KB |
Output is correct |
60 |
Correct |
274 ms |
37840 KB |
Output is correct |
61 |
Correct |
379 ms |
51896 KB |
Output is correct |
62 |
Correct |
397 ms |
51540 KB |
Output is correct |
63 |
Correct |
232 ms |
35592 KB |
Output is correct |
64 |
Correct |
411 ms |
52880 KB |
Output is correct |
65 |
Correct |
406 ms |
52864 KB |
Output is correct |
66 |
Correct |
100 ms |
18660 KB |
Output is correct |
67 |
Correct |
406 ms |
49764 KB |
Output is correct |
68 |
Correct |
145 ms |
51636 KB |
Output is correct |
69 |
Correct |
101 ms |
27784 KB |
Output is correct |
70 |
Correct |
229 ms |
55244 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 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 |
1 ms |
300 KB |
Output is correct |
77 |
Correct |
64 ms |
39812 KB |
Output is correct |
78 |
Correct |
65 ms |
39824 KB |
Output is correct |
79 |
Correct |
64 ms |
39812 KB |
Output is correct |
80 |
Correct |
63 ms |
39736 KB |
Output is correct |
81 |
Correct |
197 ms |
59140 KB |
Output is correct |
82 |
Correct |
284 ms |
70808 KB |
Output is correct |
83 |
Correct |
256 ms |
72648 KB |
Output is correct |
84 |
Correct |
453 ms |
88640 KB |
Output is correct |
85 |
Correct |
461 ms |
90764 KB |
Output is correct |
86 |
Correct |
420 ms |
94500 KB |
Output is correct |
87 |
Correct |
452 ms |
98576 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
467 ms |
98672 KB |
Output is correct |
90 |
Correct |
326 ms |
86412 KB |
Output is correct |
91 |
Correct |
265 ms |
78360 KB |
Output is correct |