#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]; //0-decreasing, 1-increasing
multiset<ll, greater<ll>> pre1_high, pre1_low, pre2_high, pre2_low;
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.clear();
pre2_low.clear();
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.insert(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.insert(dp[idx2][0]-sum_right[idx2]);
pre2_low.insert(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]);
}
if (pre1_low.size()) {
dp[idx][1]=max(dp[idx][1],*(pre1_low.begin())+sum_left[idx]+sum_right[idx]);
}
if (pre2_high.size()) {
dp[idx][0]=max(dp[idx][0],*(pre2_high.begin())+sum_right[idx]);
dp[idx][1]=max(dp[idx][1],*(pre2_high.begin())+sum_right[idx]);
}
if (pre2_low.size()) {
dp[idx][0]=max(dp[idx][0],*(pre2_low.begin())+sum_left[idx]+sum_right[idx]);
dp[idx][1]=max(dp[idx][1],*(pre2_low.begin())+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 |
47 ms |
13284 KB |
Output is correct |
2 |
Correct |
58 ms |
15560 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
499 ms |
64272 KB |
Output is correct |
6 |
Correct |
416 ms |
58424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3432 KB |
Output is correct |
2 |
Correct |
308 ms |
42656 KB |
Output is correct |
3 |
Correct |
407 ms |
51240 KB |
Output is correct |
4 |
Correct |
48 ms |
13432 KB |
Output is correct |
5 |
Correct |
63 ms |
15568 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3432 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 |
4196 KB |
Output is correct |
12 |
Correct |
133 ms |
25256 KB |
Output is correct |
13 |
Correct |
170 ms |
30316 KB |
Output is correct |
14 |
Correct |
130 ms |
22928 KB |
Output is correct |
15 |
Correct |
169 ms |
25180 KB |
Output is correct |
16 |
Correct |
127 ms |
22976 KB |
Output is correct |
17 |
Correct |
147 ms |
25224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
68 ms |
13884 KB |
Output is correct |
4 |
Correct |
43 ms |
10772 KB |
Output is correct |
5 |
Correct |
123 ms |
22264 KB |
Output is correct |
6 |
Correct |
133 ms |
21592 KB |
Output is correct |
7 |
Correct |
121 ms |
22216 KB |
Output is correct |
8 |
Correct |
117 ms |
22268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3428 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 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 |
8 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 |
3 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 |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3428 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 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 |
8 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 |
3 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
7 ms |
3924 KB |
Output is correct |
17 |
Correct |
114 ms |
11164 KB |
Output is correct |
18 |
Correct |
120 ms |
11644 KB |
Output is correct |
19 |
Correct |
98 ms |
11528 KB |
Output is correct |
20 |
Correct |
98 ms |
11552 KB |
Output is correct |
21 |
Correct |
99 ms |
11444 KB |
Output is correct |
22 |
Correct |
221 ms |
19624 KB |
Output is correct |
23 |
Correct |
23 ms |
4956 KB |
Output is correct |
24 |
Correct |
78 ms |
8544 KB |
Output is correct |
25 |
Correct |
4 ms |
3716 KB |
Output is correct |
26 |
Correct |
20 ms |
4936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3428 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 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 |
8 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 |
3 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
7 ms |
3924 KB |
Output is correct |
17 |
Correct |
114 ms |
11164 KB |
Output is correct |
18 |
Correct |
120 ms |
11644 KB |
Output is correct |
19 |
Correct |
98 ms |
11528 KB |
Output is correct |
20 |
Correct |
98 ms |
11552 KB |
Output is correct |
21 |
Correct |
99 ms |
11444 KB |
Output is correct |
22 |
Correct |
221 ms |
19624 KB |
Output is correct |
23 |
Correct |
23 ms |
4956 KB |
Output is correct |
24 |
Correct |
78 ms |
8544 KB |
Output is correct |
25 |
Correct |
4 ms |
3716 KB |
Output is correct |
26 |
Correct |
20 ms |
4936 KB |
Output is correct |
27 |
Correct |
5 ms |
3956 KB |
Output is correct |
28 |
Correct |
582 ms |
41824 KB |
Output is correct |
29 |
Correct |
821 ms |
56400 KB |
Output is correct |
30 |
Correct |
746 ms |
55892 KB |
Output is correct |
31 |
Correct |
778 ms |
55608 KB |
Output is correct |
32 |
Correct |
722 ms |
57124 KB |
Output is correct |
33 |
Correct |
771 ms |
55812 KB |
Output is correct |
34 |
Correct |
768 ms |
55656 KB |
Output is correct |
35 |
Correct |
276 ms |
23956 KB |
Output is correct |
36 |
Correct |
809 ms |
57460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
68 ms |
13884 KB |
Output is correct |
4 |
Correct |
43 ms |
10772 KB |
Output is correct |
5 |
Correct |
123 ms |
22264 KB |
Output is correct |
6 |
Correct |
133 ms |
21592 KB |
Output is correct |
7 |
Correct |
121 ms |
22216 KB |
Output is correct |
8 |
Correct |
117 ms |
22268 KB |
Output is correct |
9 |
Correct |
115 ms |
22068 KB |
Output is correct |
10 |
Correct |
134 ms |
21412 KB |
Output is correct |
11 |
Correct |
285 ms |
39556 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 |
3432 KB |
Output is correct |
17 |
Correct |
2 ms |
3428 KB |
Output is correct |
18 |
Correct |
3 ms |
4180 KB |
Output is correct |
19 |
Correct |
3 ms |
4180 KB |
Output is correct |
20 |
Correct |
3 ms |
4200 KB |
Output is correct |
21 |
Correct |
3 ms |
4196 KB |
Output is correct |
22 |
Correct |
154 ms |
22372 KB |
Output is correct |
23 |
Correct |
343 ms |
39500 KB |
Output is correct |
24 |
Correct |
302 ms |
40044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
13284 KB |
Output is correct |
2 |
Correct |
58 ms |
15560 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
499 ms |
64272 KB |
Output is correct |
6 |
Correct |
416 ms |
58424 KB |
Output is correct |
7 |
Correct |
2 ms |
3432 KB |
Output is correct |
8 |
Correct |
308 ms |
42656 KB |
Output is correct |
9 |
Correct |
407 ms |
51240 KB |
Output is correct |
10 |
Correct |
48 ms |
13432 KB |
Output is correct |
11 |
Correct |
63 ms |
15568 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3432 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 |
4196 KB |
Output is correct |
18 |
Correct |
133 ms |
25256 KB |
Output is correct |
19 |
Correct |
170 ms |
30316 KB |
Output is correct |
20 |
Correct |
130 ms |
22928 KB |
Output is correct |
21 |
Correct |
169 ms |
25180 KB |
Output is correct |
22 |
Correct |
127 ms |
22976 KB |
Output is correct |
23 |
Correct |
147 ms |
25224 KB |
Output is correct |
24 |
Correct |
4 ms |
4180 KB |
Output is correct |
25 |
Correct |
3 ms |
4180 KB |
Output is correct |
26 |
Correct |
68 ms |
13884 KB |
Output is correct |
27 |
Correct |
43 ms |
10772 KB |
Output is correct |
28 |
Correct |
123 ms |
22264 KB |
Output is correct |
29 |
Correct |
133 ms |
21592 KB |
Output is correct |
30 |
Correct |
121 ms |
22216 KB |
Output is correct |
31 |
Correct |
117 ms |
22268 KB |
Output is correct |
32 |
Correct |
2 ms |
3412 KB |
Output is correct |
33 |
Correct |
2 ms |
3428 KB |
Output is correct |
34 |
Correct |
2 ms |
3412 KB |
Output is correct |
35 |
Correct |
2 ms |
3412 KB |
Output is correct |
36 |
Correct |
2 ms |
3412 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 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 |
8 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 |
3 ms |
3412 KB |
Output is correct |
45 |
Correct |
3 ms |
3540 KB |
Output is correct |
46 |
Correct |
2 ms |
3412 KB |
Output is correct |
47 |
Correct |
7 ms |
3924 KB |
Output is correct |
48 |
Correct |
114 ms |
11164 KB |
Output is correct |
49 |
Correct |
120 ms |
11644 KB |
Output is correct |
50 |
Correct |
98 ms |
11528 KB |
Output is correct |
51 |
Correct |
98 ms |
11552 KB |
Output is correct |
52 |
Correct |
99 ms |
11444 KB |
Output is correct |
53 |
Correct |
221 ms |
19624 KB |
Output is correct |
54 |
Correct |
23 ms |
4956 KB |
Output is correct |
55 |
Correct |
78 ms |
8544 KB |
Output is correct |
56 |
Correct |
4 ms |
3716 KB |
Output is correct |
57 |
Correct |
20 ms |
4936 KB |
Output is correct |
58 |
Correct |
5 ms |
3956 KB |
Output is correct |
59 |
Correct |
582 ms |
41824 KB |
Output is correct |
60 |
Correct |
821 ms |
56400 KB |
Output is correct |
61 |
Correct |
746 ms |
55892 KB |
Output is correct |
62 |
Correct |
778 ms |
55608 KB |
Output is correct |
63 |
Correct |
722 ms |
57124 KB |
Output is correct |
64 |
Correct |
771 ms |
55812 KB |
Output is correct |
65 |
Correct |
768 ms |
55656 KB |
Output is correct |
66 |
Correct |
276 ms |
23956 KB |
Output is correct |
67 |
Correct |
809 ms |
57460 KB |
Output is correct |
68 |
Correct |
115 ms |
22068 KB |
Output is correct |
69 |
Correct |
134 ms |
21412 KB |
Output is correct |
70 |
Correct |
285 ms |
39556 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 |
3432 KB |
Output is correct |
76 |
Correct |
2 ms |
3428 KB |
Output is correct |
77 |
Correct |
3 ms |
4180 KB |
Output is correct |
78 |
Correct |
3 ms |
4180 KB |
Output is correct |
79 |
Correct |
3 ms |
4200 KB |
Output is correct |
80 |
Correct |
3 ms |
4196 KB |
Output is correct |
81 |
Correct |
154 ms |
22372 KB |
Output is correct |
82 |
Correct |
343 ms |
39500 KB |
Output is correct |
83 |
Correct |
302 ms |
40044 KB |
Output is correct |
84 |
Execution timed out |
1044 ms |
63036 KB |
Time limit exceeded |
85 |
Halted |
0 ms |
0 KB |
- |