#include "fish.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
vector<pii> fish[100001]; //row,value
vector<pii> pos; //find find -> x,y
ll sum_in[600010],sum_left[600010],sum_right[600010],dp[600010][2],mmax[100001],pre1_low,pre2_low; //0-decreasing, 1-increasing
multiset<ll, greater<ll>> pre1_high,pre2_high; //high
int start_col[100001],end_col[100001];
ll max_weights(int n, int m, vector<int> X, vector<int> Y, vector<int> W) {
memset(start_col,-1,sizeof(start_col));
memset(end_col,-1,sizeof(end_col));
for (int i=0; i<m; ++i) {
fish[X[i]].push_back(pii(Y[i],W[i]));
if (X[i]) pos.push_back(pii(X[i]-1,Y[i]));
if (X[i]<n-1) pos.push_back(pii(X[i]+1,Y[i]));
}
for (int i=0; i<n; ++i) {
sort(fish[i].begin(),fish[i].end());
for (int j=1; j<fish[i].size(); ++j) fish[i][j].second=fish[i][j].second+fish[i][j-1].second;
}
sort(pos.begin(),pos.end());
int k=pos.size(),pre_col=-1;
for (int i=0; i<k; ++i) {
if (pos[i].first!=pre_col) {
start_col[pos[i].first]=i;
pre_col=pos[i].first;
}
end_col[pos[i].first]=i;
}
for (int i=0; i<k; ++i) {
int x=pos[i].first, y=pos[i].second;
//sum_in
auto it=upper_bound(fish[x].begin(),fish[x].end(),pii(y,LLONG_MAX));
if (it==fish[x].begin()) sum_in[i]=0;
else sum_in[i]=(*(--it)).second;
//sum_left
if (x) {
it=upper_bound(fish[x-1].begin(),fish[x-1].end(),pii(y,LLONG_MAX));
if (it==fish[x-1].begin()) sum_left[i]=0;
else sum_left[i]=(*(--it)).second;
} else sum_left[i]=0;
//sum_right
if (x<n-1) {
it=upper_bound(fish[x+1].begin(),fish[x+1].end(),pii(y,LLONG_MAX));
if (it==fish[x+1].begin()) sum_right[i]=0;
else sum_right[i]=(*(--it)).second;
} else sum_right[i]=0;
}
for (int i=0; i<n; ++i) {
if (start_col[i]!=-1) {
//reset
pre1_high.clear();
pre2_high.clear();
pre1_low=0;
pre2_low=0;
int idx1,idx2;
if (i>0 && start_col[i-1]!=-1) {
for (int j=start_col[i-1]; j<=end_col[i-1]; ++j) {
pre1_high.insert(dp[j][0]);
pre1_high.insert(dp[j][1]);
}
idx1=start_col[i-1];
} else idx1=INT_MAX;
if (i>1 && start_col[i-2]!=-1) {
for (int j=start_col[i-2]; j<=end_col[i-2]; ++j) {
pre2_high.insert(dp[j][0]);
pre2_high.insert(dp[j][1]);
}
idx2=start_col[i-2];
} else idx2=INT_MAX;
for (int idx=start_col[i]; idx<=end_col[i]; ++idx) {
//find dp[idx][0], dp[idx][1]
if (i>=3) {
dp[idx][0]=sum_left[idx]+sum_right[idx]+mmax[i-3];
dp[idx][1]=sum_left[idx]+sum_right[idx]+mmax[i-3];
} else {
dp[idx][0]=sum_left[idx]+sum_right[idx];
dp[idx][1]=sum_left[idx]+sum_right[idx];
}
while (idx1<=end_col[i-1] && pos[idx1].second<=pos[idx].second) { //go to lower
pre1_high.erase(pre1_high.find(dp[idx1][0]));
pre1_high.erase(pre1_high.find(dp[idx1][1]));
pre1_low=max(pre1_low,dp[idx1][1]-sum_in[idx1]-sum_right[idx1]);
++idx1;
}
while (idx2<=end_col[i-2] && pos[idx2].second<=pos[idx].second) { //go to lower
pre2_high.erase(pre2_high.find(dp[idx2][0]));
pre2_high.erase(pre2_high.find(dp[idx2][1]));
pre2_low=max({pre2_low,dp[idx2][0]-sum_right[idx2],dp[idx2][1]-sum_right[idx2]});
++idx2;
}
if (pre1_high.size()) {
dp[idx][0]=max(dp[idx][0],*(pre1_high.begin())-sum_in[idx]+sum_right[idx]);
}
dp[idx][1]=max(dp[idx][1],pre1_low+sum_left[idx]+sum_right[idx]);
if (pre2_high.size()) {
ll h=*(pre2_high.begin());
dp[idx][0]=max(dp[idx][0],h+sum_right[idx]);
dp[idx][1]=max(dp[idx][1],h+sum_right[idx]);
}
dp[idx][0]=max(dp[idx][0],pre2_low+sum_left[idx]+sum_right[idx]);
dp[idx][1]=max(dp[idx][1],pre2_low+sum_left[idx]+sum_right[idx]);
mmax[i]=max({mmax[i],dp[idx][0],dp[idx][1]});
}
}
mmax[i]=max(mmax[i],mmax[i-1]);
}
return mmax[n-1];
}
/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
Compilation message
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:27:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int j=1; j<fish[i].size(); ++j) fish[i][j].second=fish[i][j].second+fish[i][j-1].second;
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
13280 KB |
Output is correct |
2 |
Correct |
58 ms |
15480 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4196 KB |
Output is correct |
5 |
Correct |
366 ms |
60076 KB |
Output is correct |
6 |
Correct |
333 ms |
53800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
233 ms |
41808 KB |
Output is correct |
3 |
Correct |
292 ms |
49728 KB |
Output is correct |
4 |
Correct |
48 ms |
13288 KB |
Output is correct |
5 |
Correct |
59 ms |
15492 KB |
Output is correct |
6 |
Correct |
2 ms |
3392 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
3 ms |
4180 KB |
Output is correct |
11 |
Correct |
3 ms |
4180 KB |
Output is correct |
12 |
Correct |
103 ms |
25192 KB |
Output is correct |
13 |
Correct |
135 ms |
30244 KB |
Output is correct |
14 |
Correct |
110 ms |
22956 KB |
Output is correct |
15 |
Correct |
147 ms |
25156 KB |
Output is correct |
16 |
Correct |
105 ms |
23052 KB |
Output is correct |
17 |
Correct |
119 ms |
25368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
57 ms |
13892 KB |
Output is correct |
4 |
Correct |
38 ms |
10780 KB |
Output is correct |
5 |
Correct |
100 ms |
22240 KB |
Output is correct |
6 |
Correct |
98 ms |
21608 KB |
Output is correct |
7 |
Correct |
102 ms |
22244 KB |
Output is correct |
8 |
Correct |
125 ms |
22188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3428 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3432 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3456 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
5 ms |
3924 KB |
Output is correct |
11 |
Correct |
4 ms |
3540 KB |
Output is correct |
12 |
Correct |
4 ms |
3668 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3428 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3432 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3456 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
5 ms |
3924 KB |
Output is correct |
11 |
Correct |
4 ms |
3540 KB |
Output is correct |
12 |
Correct |
4 ms |
3668 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3432 KB |
Output is correct |
16 |
Correct |
6 ms |
3828 KB |
Output is correct |
17 |
Correct |
87 ms |
11132 KB |
Output is correct |
18 |
Correct |
93 ms |
11700 KB |
Output is correct |
19 |
Correct |
77 ms |
11600 KB |
Output is correct |
20 |
Correct |
79 ms |
11536 KB |
Output is correct |
21 |
Correct |
81 ms |
11496 KB |
Output is correct |
22 |
Correct |
172 ms |
19656 KB |
Output is correct |
23 |
Correct |
19 ms |
4952 KB |
Output is correct |
24 |
Correct |
58 ms |
8588 KB |
Output is correct |
25 |
Correct |
4 ms |
3696 KB |
Output is correct |
26 |
Correct |
17 ms |
4912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3428 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3432 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3456 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
5 ms |
3924 KB |
Output is correct |
11 |
Correct |
4 ms |
3540 KB |
Output is correct |
12 |
Correct |
4 ms |
3668 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3432 KB |
Output is correct |
16 |
Correct |
6 ms |
3828 KB |
Output is correct |
17 |
Correct |
87 ms |
11132 KB |
Output is correct |
18 |
Correct |
93 ms |
11700 KB |
Output is correct |
19 |
Correct |
77 ms |
11600 KB |
Output is correct |
20 |
Correct |
79 ms |
11536 KB |
Output is correct |
21 |
Correct |
81 ms |
11496 KB |
Output is correct |
22 |
Correct |
172 ms |
19656 KB |
Output is correct |
23 |
Correct |
19 ms |
4952 KB |
Output is correct |
24 |
Correct |
58 ms |
8588 KB |
Output is correct |
25 |
Correct |
4 ms |
3696 KB |
Output is correct |
26 |
Correct |
17 ms |
4912 KB |
Output is correct |
27 |
Correct |
6 ms |
4052 KB |
Output is correct |
28 |
Correct |
448 ms |
39968 KB |
Output is correct |
29 |
Correct |
612 ms |
52776 KB |
Output is correct |
30 |
Correct |
582 ms |
51976 KB |
Output is correct |
31 |
Correct |
598 ms |
52052 KB |
Output is correct |
32 |
Correct |
559 ms |
54124 KB |
Output is correct |
33 |
Correct |
584 ms |
52196 KB |
Output is correct |
34 |
Correct |
587 ms |
52004 KB |
Output is correct |
35 |
Correct |
227 ms |
23968 KB |
Output is correct |
36 |
Correct |
617 ms |
53968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
57 ms |
13892 KB |
Output is correct |
4 |
Correct |
38 ms |
10780 KB |
Output is correct |
5 |
Correct |
100 ms |
22240 KB |
Output is correct |
6 |
Correct |
98 ms |
21608 KB |
Output is correct |
7 |
Correct |
102 ms |
22244 KB |
Output is correct |
8 |
Correct |
125 ms |
22188 KB |
Output is correct |
9 |
Correct |
96 ms |
21980 KB |
Output is correct |
10 |
Correct |
107 ms |
21464 KB |
Output is correct |
11 |
Correct |
234 ms |
38056 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3424 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
4180 KB |
Output is correct |
19 |
Correct |
3 ms |
4196 KB |
Output is correct |
20 |
Correct |
3 ms |
4180 KB |
Output is correct |
21 |
Correct |
3 ms |
4180 KB |
Output is correct |
22 |
Correct |
134 ms |
22332 KB |
Output is correct |
23 |
Correct |
286 ms |
38000 KB |
Output is correct |
24 |
Correct |
263 ms |
38044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
13280 KB |
Output is correct |
2 |
Correct |
58 ms |
15480 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4196 KB |
Output is correct |
5 |
Correct |
366 ms |
60076 KB |
Output is correct |
6 |
Correct |
333 ms |
53800 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
233 ms |
41808 KB |
Output is correct |
9 |
Correct |
292 ms |
49728 KB |
Output is correct |
10 |
Correct |
48 ms |
13288 KB |
Output is correct |
11 |
Correct |
59 ms |
15492 KB |
Output is correct |
12 |
Correct |
2 ms |
3392 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
3 ms |
4180 KB |
Output is correct |
17 |
Correct |
3 ms |
4180 KB |
Output is correct |
18 |
Correct |
103 ms |
25192 KB |
Output is correct |
19 |
Correct |
135 ms |
30244 KB |
Output is correct |
20 |
Correct |
110 ms |
22956 KB |
Output is correct |
21 |
Correct |
147 ms |
25156 KB |
Output is correct |
22 |
Correct |
105 ms |
23052 KB |
Output is correct |
23 |
Correct |
119 ms |
25368 KB |
Output is correct |
24 |
Correct |
3 ms |
4180 KB |
Output is correct |
25 |
Correct |
3 ms |
4180 KB |
Output is correct |
26 |
Correct |
57 ms |
13892 KB |
Output is correct |
27 |
Correct |
38 ms |
10780 KB |
Output is correct |
28 |
Correct |
100 ms |
22240 KB |
Output is correct |
29 |
Correct |
98 ms |
21608 KB |
Output is correct |
30 |
Correct |
102 ms |
22244 KB |
Output is correct |
31 |
Correct |
125 ms |
22188 KB |
Output is correct |
32 |
Correct |
2 ms |
3428 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
3412 KB |
Output is correct |
35 |
Correct |
2 ms |
3432 KB |
Output is correct |
36 |
Correct |
2 ms |
3412 KB |
Output is correct |
37 |
Correct |
3 ms |
3456 KB |
Output is correct |
38 |
Correct |
2 ms |
3412 KB |
Output is correct |
39 |
Correct |
2 ms |
3412 KB |
Output is correct |
40 |
Correct |
3 ms |
3540 KB |
Output is correct |
41 |
Correct |
5 ms |
3924 KB |
Output is correct |
42 |
Correct |
4 ms |
3540 KB |
Output is correct |
43 |
Correct |
4 ms |
3668 KB |
Output is correct |
44 |
Correct |
2 ms |
3412 KB |
Output is correct |
45 |
Correct |
3 ms |
3540 KB |
Output is correct |
46 |
Correct |
3 ms |
3432 KB |
Output is correct |
47 |
Correct |
6 ms |
3828 KB |
Output is correct |
48 |
Correct |
87 ms |
11132 KB |
Output is correct |
49 |
Correct |
93 ms |
11700 KB |
Output is correct |
50 |
Correct |
77 ms |
11600 KB |
Output is correct |
51 |
Correct |
79 ms |
11536 KB |
Output is correct |
52 |
Correct |
81 ms |
11496 KB |
Output is correct |
53 |
Correct |
172 ms |
19656 KB |
Output is correct |
54 |
Correct |
19 ms |
4952 KB |
Output is correct |
55 |
Correct |
58 ms |
8588 KB |
Output is correct |
56 |
Correct |
4 ms |
3696 KB |
Output is correct |
57 |
Correct |
17 ms |
4912 KB |
Output is correct |
58 |
Correct |
6 ms |
4052 KB |
Output is correct |
59 |
Correct |
448 ms |
39968 KB |
Output is correct |
60 |
Correct |
612 ms |
52776 KB |
Output is correct |
61 |
Correct |
582 ms |
51976 KB |
Output is correct |
62 |
Correct |
598 ms |
52052 KB |
Output is correct |
63 |
Correct |
559 ms |
54124 KB |
Output is correct |
64 |
Correct |
584 ms |
52196 KB |
Output is correct |
65 |
Correct |
587 ms |
52004 KB |
Output is correct |
66 |
Correct |
227 ms |
23968 KB |
Output is correct |
67 |
Correct |
617 ms |
53968 KB |
Output is correct |
68 |
Correct |
96 ms |
21980 KB |
Output is correct |
69 |
Correct |
107 ms |
21464 KB |
Output is correct |
70 |
Correct |
234 ms |
38056 KB |
Output is correct |
71 |
Correct |
2 ms |
3412 KB |
Output is correct |
72 |
Correct |
2 ms |
3412 KB |
Output is correct |
73 |
Correct |
2 ms |
3412 KB |
Output is correct |
74 |
Correct |
2 ms |
3412 KB |
Output is correct |
75 |
Correct |
2 ms |
3424 KB |
Output is correct |
76 |
Correct |
2 ms |
3412 KB |
Output is correct |
77 |
Correct |
3 ms |
4180 KB |
Output is correct |
78 |
Correct |
3 ms |
4196 KB |
Output is correct |
79 |
Correct |
3 ms |
4180 KB |
Output is correct |
80 |
Correct |
3 ms |
4180 KB |
Output is correct |
81 |
Correct |
134 ms |
22332 KB |
Output is correct |
82 |
Correct |
286 ms |
38000 KB |
Output is correct |
83 |
Correct |
263 ms |
38044 KB |
Output is correct |
84 |
Correct |
684 ms |
59024 KB |
Output is correct |
85 |
Correct |
773 ms |
63136 KB |
Output is correct |
86 |
Correct |
486 ms |
57660 KB |
Output is correct |
87 |
Correct |
483 ms |
58280 KB |
Output is correct |
88 |
Correct |
2 ms |
3412 KB |
Output is correct |
89 |
Correct |
485 ms |
58220 KB |
Output is correct |
90 |
Correct |
538 ms |
57380 KB |
Output is correct |
91 |
Correct |
489 ms |
57140 KB |
Output is correct |