#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, n) for (int i = 0; i < n; i++)
#define PB push_back
#define pii pair<int, int>
#define F first
#define S second
#define ALL(x) x.begin(), x.end()
const int INF = 1e9 + 7;
const int maxn = 1e5 + 10;
int n, m;
vector<pii> fish[maxn]; // {y, w}
vector<ll> include[maxn], includent[maxn];
ll max_weights(int N, int M, vector<int> x,
vector<int> y, vector<int> w){
n = N, m = M;
FOR(i, m) fish[x[i]].PB({y[i], w[i]});
fish[0].PB({n, 0}); sort(ALL(fish[0]));
include[0].assign(fish[0].size(), 0);
includent[0].assign(fish[0].size(), 0);
for (int k = 1; k < n; k++){
fish[k].PB({n, 0}); sort(ALL(fish[k]));
// case 0: k + 1 pier is >= k pier
// k + 1 pier determines fish on k
// k - 1 pier length does not matter
ll best = 0;
for (auto v : include[k - 1]) best = max(best, v);
include[k].assign(fish[k].size(), best);
includent[k].assign(fish[k].size(), best);
// case 1: k pier is >= k - 1 pier
// k pier determines which fish can be caught on k - 1
ll j = 0, sum = 0, mx = -INF;
FOR(i, fish[k].size()){
// building a pier of length y[fish[k][i]] - 1
while (j < fish[k - 1].size() && fish[k - 1][j].F < fish[k][i].F){
mx = max(mx, includent[k - 1][j] - sum);
sum += fish[k - 1][j].S; j++;
}
include[k][i] = max(include[k][i], mx + sum);
includent[k][i] = max(includent[k][i], mx + sum);
}
// case 2: k pier is <= k - 1 pier
// k - 1 pier determines which fish can be caught on k
j = fish[k - 1].size(), sum = 0, mx = -INF;
for (int i = fish[k].size() - 1; i >= 0; i--){
// building a pier of length y[fish[k][i]] - 1
while (j > 0 && fish[k - 1][j - 1].F > fish[k][i].F){
j--;
mx = max(mx, include[k - 1][j] - sum);
}
sum += fish[k][i].S;
include[k][i] = max(include[k][i], mx + sum);
}
// cout << "we are on x = " << k << endl;
// for (auto v : include[k]) cout << v << ' '; cout << endl;
// for (auto v : includent[k]) cout << v << ' '; cout << endl;
// cout << endl;
}
ll re = 0;
for (auto v : include[n - 1]) re = max(re, v);
return re;
}
// signed main(){
// cout << max_weights(5, 4, {0, 1, 4, 3},
// {2, 1, 4, 3}, {5, 2, 1, 3});
// }
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:5:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define FOR(i, n) for (int i = 0; i < n; i++)
......
37 | FOR(i, fish[k].size()){
| ~~~~~~~~~~~~~~~~~
fish.cpp:37:9: note: in expansion of macro 'FOR'
37 | FOR(i, fish[k].size()){
| ^~~
fish.cpp:39:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | while (j < fish[k - 1].size() && fish[k - 1][j].F < fish[k][i].F){
| ~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
19904 KB |
Output is correct |
2 |
Correct |
51 ms |
21596 KB |
Output is correct |
3 |
Correct |
18 ms |
16744 KB |
Output is correct |
4 |
Correct |
19 ms |
16688 KB |
Output is correct |
5 |
Correct |
139 ms |
31144 KB |
Output is correct |
6 |
Correct |
118 ms |
30552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7348 KB |
Output is correct |
2 |
Correct |
66 ms |
23780 KB |
Output is correct |
3 |
Correct |
100 ms |
26436 KB |
Output is correct |
4 |
Correct |
42 ms |
19776 KB |
Output is correct |
5 |
Correct |
48 ms |
21664 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7348 KB |
Output is correct |
8 |
Correct |
3 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
16 ms |
16724 KB |
Output is correct |
11 |
Correct |
15 ms |
16648 KB |
Output is correct |
12 |
Correct |
40 ms |
20936 KB |
Output is correct |
13 |
Correct |
48 ms |
23184 KB |
Output is correct |
14 |
Correct |
47 ms |
20928 KB |
Output is correct |
15 |
Correct |
43 ms |
22596 KB |
Output is correct |
16 |
Correct |
40 ms |
20928 KB |
Output is correct |
17 |
Correct |
45 ms |
22448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16684 KB |
Output is correct |
2 |
Correct |
17 ms |
16700 KB |
Output is correct |
3 |
Correct |
37 ms |
17956 KB |
Output is correct |
4 |
Correct |
28 ms |
18132 KB |
Output is correct |
5 |
Correct |
49 ms |
20680 KB |
Output is correct |
6 |
Correct |
47 ms |
20056 KB |
Output is correct |
7 |
Correct |
45 ms |
20532 KB |
Output is correct |
8 |
Correct |
55 ms |
20596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7332 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7304 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7348 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7352 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7284 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7332 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7304 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7348 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7352 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7284 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
3 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7484 KB |
Output is correct |
17 |
Correct |
18 ms |
10304 KB |
Output is correct |
18 |
Correct |
17 ms |
10572 KB |
Output is correct |
19 |
Correct |
17 ms |
10580 KB |
Output is correct |
20 |
Correct |
18 ms |
10592 KB |
Output is correct |
21 |
Correct |
17 ms |
10452 KB |
Output is correct |
22 |
Correct |
33 ms |
13620 KB |
Output is correct |
23 |
Correct |
7 ms |
7892 KB |
Output is correct |
24 |
Correct |
12 ms |
9280 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
6 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7332 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7304 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7348 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7352 KB |
Output is correct |
10 |
Correct |
5 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7284 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
3 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7484 KB |
Output is correct |
17 |
Correct |
18 ms |
10304 KB |
Output is correct |
18 |
Correct |
17 ms |
10572 KB |
Output is correct |
19 |
Correct |
17 ms |
10580 KB |
Output is correct |
20 |
Correct |
18 ms |
10592 KB |
Output is correct |
21 |
Correct |
17 ms |
10452 KB |
Output is correct |
22 |
Correct |
33 ms |
13620 KB |
Output is correct |
23 |
Correct |
7 ms |
7892 KB |
Output is correct |
24 |
Correct |
12 ms |
9280 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
6 ms |
7892 KB |
Output is correct |
27 |
Correct |
4 ms |
7636 KB |
Output is correct |
28 |
Correct |
69 ms |
22664 KB |
Output is correct |
29 |
Correct |
108 ms |
28396 KB |
Output is correct |
30 |
Correct |
96 ms |
27980 KB |
Output is correct |
31 |
Correct |
95 ms |
27976 KB |
Output is correct |
32 |
Correct |
90 ms |
28880 KB |
Output is correct |
33 |
Correct |
101 ms |
28000 KB |
Output is correct |
34 |
Correct |
110 ms |
28136 KB |
Output is correct |
35 |
Correct |
40 ms |
15352 KB |
Output is correct |
36 |
Correct |
96 ms |
28528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16684 KB |
Output is correct |
2 |
Correct |
17 ms |
16700 KB |
Output is correct |
3 |
Correct |
37 ms |
17956 KB |
Output is correct |
4 |
Correct |
28 ms |
18132 KB |
Output is correct |
5 |
Correct |
49 ms |
20680 KB |
Output is correct |
6 |
Correct |
47 ms |
20056 KB |
Output is correct |
7 |
Correct |
45 ms |
20532 KB |
Output is correct |
8 |
Correct |
55 ms |
20596 KB |
Output is correct |
9 |
Correct |
48 ms |
20400 KB |
Output is correct |
10 |
Correct |
45 ms |
16816 KB |
Output is correct |
11 |
Correct |
84 ms |
26456 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
3 ms |
7348 KB |
Output is correct |
14 |
Correct |
3 ms |
7348 KB |
Output is correct |
15 |
Correct |
3 ms |
7252 KB |
Output is correct |
16 |
Correct |
4 ms |
7348 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
15 ms |
16684 KB |
Output is correct |
19 |
Correct |
16 ms |
16632 KB |
Output is correct |
20 |
Correct |
17 ms |
16628 KB |
Output is correct |
21 |
Correct |
16 ms |
16748 KB |
Output is correct |
22 |
Correct |
54 ms |
21696 KB |
Output is correct |
23 |
Correct |
95 ms |
26384 KB |
Output is correct |
24 |
Correct |
90 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
19904 KB |
Output is correct |
2 |
Correct |
51 ms |
21596 KB |
Output is correct |
3 |
Correct |
18 ms |
16744 KB |
Output is correct |
4 |
Correct |
19 ms |
16688 KB |
Output is correct |
5 |
Correct |
139 ms |
31144 KB |
Output is correct |
6 |
Correct |
118 ms |
30552 KB |
Output is correct |
7 |
Correct |
4 ms |
7348 KB |
Output is correct |
8 |
Correct |
66 ms |
23780 KB |
Output is correct |
9 |
Correct |
100 ms |
26436 KB |
Output is correct |
10 |
Correct |
42 ms |
19776 KB |
Output is correct |
11 |
Correct |
48 ms |
21664 KB |
Output is correct |
12 |
Correct |
3 ms |
7252 KB |
Output is correct |
13 |
Correct |
3 ms |
7348 KB |
Output is correct |
14 |
Correct |
3 ms |
7252 KB |
Output is correct |
15 |
Correct |
3 ms |
7252 KB |
Output is correct |
16 |
Correct |
16 ms |
16724 KB |
Output is correct |
17 |
Correct |
15 ms |
16648 KB |
Output is correct |
18 |
Correct |
40 ms |
20936 KB |
Output is correct |
19 |
Correct |
48 ms |
23184 KB |
Output is correct |
20 |
Correct |
47 ms |
20928 KB |
Output is correct |
21 |
Correct |
43 ms |
22596 KB |
Output is correct |
22 |
Correct |
40 ms |
20928 KB |
Output is correct |
23 |
Correct |
45 ms |
22448 KB |
Output is correct |
24 |
Correct |
15 ms |
16684 KB |
Output is correct |
25 |
Correct |
17 ms |
16700 KB |
Output is correct |
26 |
Correct |
37 ms |
17956 KB |
Output is correct |
27 |
Correct |
28 ms |
18132 KB |
Output is correct |
28 |
Correct |
49 ms |
20680 KB |
Output is correct |
29 |
Correct |
47 ms |
20056 KB |
Output is correct |
30 |
Correct |
45 ms |
20532 KB |
Output is correct |
31 |
Correct |
55 ms |
20596 KB |
Output is correct |
32 |
Correct |
5 ms |
7332 KB |
Output is correct |
33 |
Correct |
4 ms |
7252 KB |
Output is correct |
34 |
Correct |
4 ms |
7304 KB |
Output is correct |
35 |
Correct |
3 ms |
7252 KB |
Output is correct |
36 |
Correct |
3 ms |
7348 KB |
Output is correct |
37 |
Correct |
3 ms |
7252 KB |
Output is correct |
38 |
Correct |
5 ms |
7252 KB |
Output is correct |
39 |
Correct |
4 ms |
7252 KB |
Output is correct |
40 |
Correct |
4 ms |
7352 KB |
Output is correct |
41 |
Correct |
5 ms |
7508 KB |
Output is correct |
42 |
Correct |
4 ms |
7284 KB |
Output is correct |
43 |
Correct |
4 ms |
7380 KB |
Output is correct |
44 |
Correct |
4 ms |
7252 KB |
Output is correct |
45 |
Correct |
4 ms |
7380 KB |
Output is correct |
46 |
Correct |
3 ms |
7380 KB |
Output is correct |
47 |
Correct |
4 ms |
7484 KB |
Output is correct |
48 |
Correct |
18 ms |
10304 KB |
Output is correct |
49 |
Correct |
17 ms |
10572 KB |
Output is correct |
50 |
Correct |
17 ms |
10580 KB |
Output is correct |
51 |
Correct |
18 ms |
10592 KB |
Output is correct |
52 |
Correct |
17 ms |
10452 KB |
Output is correct |
53 |
Correct |
33 ms |
13620 KB |
Output is correct |
54 |
Correct |
7 ms |
7892 KB |
Output is correct |
55 |
Correct |
12 ms |
9280 KB |
Output is correct |
56 |
Correct |
4 ms |
7380 KB |
Output is correct |
57 |
Correct |
6 ms |
7892 KB |
Output is correct |
58 |
Correct |
4 ms |
7636 KB |
Output is correct |
59 |
Correct |
69 ms |
22664 KB |
Output is correct |
60 |
Correct |
108 ms |
28396 KB |
Output is correct |
61 |
Correct |
96 ms |
27980 KB |
Output is correct |
62 |
Correct |
95 ms |
27976 KB |
Output is correct |
63 |
Correct |
90 ms |
28880 KB |
Output is correct |
64 |
Correct |
101 ms |
28000 KB |
Output is correct |
65 |
Correct |
110 ms |
28136 KB |
Output is correct |
66 |
Correct |
40 ms |
15352 KB |
Output is correct |
67 |
Correct |
96 ms |
28528 KB |
Output is correct |
68 |
Correct |
48 ms |
20400 KB |
Output is correct |
69 |
Correct |
45 ms |
16816 KB |
Output is correct |
70 |
Correct |
84 ms |
26456 KB |
Output is correct |
71 |
Correct |
4 ms |
7252 KB |
Output is correct |
72 |
Correct |
3 ms |
7348 KB |
Output is correct |
73 |
Correct |
3 ms |
7348 KB |
Output is correct |
74 |
Correct |
3 ms |
7252 KB |
Output is correct |
75 |
Correct |
4 ms |
7348 KB |
Output is correct |
76 |
Correct |
4 ms |
7252 KB |
Output is correct |
77 |
Correct |
15 ms |
16684 KB |
Output is correct |
78 |
Correct |
16 ms |
16632 KB |
Output is correct |
79 |
Correct |
17 ms |
16628 KB |
Output is correct |
80 |
Correct |
16 ms |
16748 KB |
Output is correct |
81 |
Correct |
54 ms |
21696 KB |
Output is correct |
82 |
Correct |
95 ms |
26384 KB |
Output is correct |
83 |
Correct |
90 ms |
26972 KB |
Output is correct |
84 |
Correct |
112 ms |
36304 KB |
Output is correct |
85 |
Correct |
115 ms |
36556 KB |
Output is correct |
86 |
Correct |
124 ms |
34256 KB |
Output is correct |
87 |
Correct |
128 ms |
35028 KB |
Output is correct |
88 |
Correct |
5 ms |
7252 KB |
Output is correct |
89 |
Correct |
121 ms |
35104 KB |
Output is correct |
90 |
Correct |
116 ms |
35676 KB |
Output is correct |
91 |
Correct |
110 ms |
36192 KB |
Output is correct |