//Sylwia Sapkowska
#include <bits/stdc++.h>
#include "fish.h"
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
typedef long long ll;
typedef pair<int, int> T;
const ll oo = 1e18;
void ckmax(ll &a, ll b){
a = max(a, b);
}
ll max_weights(int n, int m, vector<int>x, vector<int>y, vector<int>w){
vector<vector<T>>where(n+2);
vector<vector<ll>>valid(n+1), pref(n+1);
for (int i = 0; i<m; i++){
where[x[i]+1].emplace_back(y[i]+1, w[i]);
}
valid[0].emplace_back(0);
where[0].emplace_back(0, 0);
pref[0].emplace_back(0);
for (int i = 1; i<=n; i++){
valid[i].emplace_back(0);
for (auto [j, val]: where[i]) valid[i].emplace_back(j-1);
for (auto [j, val]: where[i-1]) valid[i].emplace_back(j);
for (auto [j, val]: where[i+1]) valid[i].emplace_back(j);
sort(valid[i].begin(), valid[i].end());
where[i].emplace_back(0, 0);
sort(where[i].begin(), where[i].end());
pref[i].resize((int)where[i].size());
for (int j = 1; j<(int)where[i].size(); j++) pref[i][j] = pref[i][j-1] + (ll)where[i][j].second;
valid[i].erase(unique(valid[i].begin(), valid[i].end()), valid[i].end());
debug(i, where[i], pref[i], valid[i]);
}
vector dp(1, vector<ll>(3, -oo));
dp[0][0] = 0;
dp[0][1] = 0;
auto less = [&](int i, int h){//ostatni <= h w where[i]
int l = 0, r = (int)where[i].size()-1;
int ans = 0;
while (r >= l){
int m = (l+r)/2;
if (where[i][m].first <= h){
ans = m;
l = m+1;
} else r = m-1;
}
return ans;
};
for (int i = 1; i<=n; i++){
int s = (int)valid[i].size();
int t = (int)valid[i-1].size();
vector new_dp(s, vector<ll>(3, -oo));
debug(i);
//h = 0
//1) poprzednia miala dodatnie pole
for (int j = 1; j<t; j++){
int h = valid[i-1][j];
ckmax(new_dp[0][2], max(dp[j][1], dp[j][0]) + pref[i][less(i, h)]); //<= h
}
//2) poprzednia byla zerem
new_dp[0][0] = max(dp[0][2], dp[0][0]);
//h > 0
//1) poprzednia byla pusta
for (int j = 1; j<s; j++){
int h = valid[i][j];
new_dp[j][0] = new_dp[j][1] = max(dp[0][2], dp[0][0] + pref[i-1][less(i-1, h)]);
}
//2) poprzednia cos miala miau
ll mx = -oo;
int ptr = -1;
for (int j = 1; j<s; j++){
int h = valid[i][j];
while (ptr + 1 < (int)valid[i-1].size() && valid[i-1][ptr+1] <= h){
ptr++;
mx = max(mx, dp[ptr][0] - pref[i-1][less(i-1, valid[i-1][ptr])]);
}
int tt = less(i-1, h);
ckmax(new_dp[j][0], mx+pref[i-1][tt]);
ckmax(new_dp[j][1], mx+pref[i-1][tt]);
}
mx = -oo; ptr = t;
for (int j = s-1; j>=1; j--){
int h = valid[i][j];
while (ptr >= 1 && valid[i-1][ptr-1] >= h){
ptr--;
mx = max(mx, dp[ptr][1] + pref[i][less(i, valid[i-1][ptr])]);
}
ckmax(new_dp[j][1], mx-pref[i][less(i, h)]);
}
for (int j = 0; j<s; j++){
int h = valid[i][j];
debug(h, new_dp[j]);
}
swap(dp, new_dp);
}
debug(dp);
ll ans = 0;
for (int j = 0; j<(int)dp.size(); j++){
for (int rep = 0; rep < 3; rep++){
ans = max(ans, dp[j][rep]);
}
}
return ans;
}
Compilation message
fish.cpp: In function 'll max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:115:17: warning: unused variable 'h' [-Wunused-variable]
115 | int h = valid[i][j];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
28356 KB |
Output is correct |
2 |
Correct |
96 ms |
33236 KB |
Output is correct |
3 |
Correct |
29 ms |
16728 KB |
Output is correct |
4 |
Correct |
28 ms |
16640 KB |
Output is correct |
5 |
Correct |
301 ms |
45392 KB |
Output is correct |
6 |
Correct |
228 ms |
38736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
147 ms |
34376 KB |
Output is correct |
3 |
Correct |
199 ms |
43176 KB |
Output is correct |
4 |
Correct |
80 ms |
29896 KB |
Output is correct |
5 |
Correct |
96 ms |
34964 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
27 ms |
16876 KB |
Output is correct |
11 |
Correct |
27 ms |
16644 KB |
Output is correct |
12 |
Correct |
98 ms |
30548 KB |
Output is correct |
13 |
Correct |
135 ms |
35664 KB |
Output is correct |
14 |
Correct |
91 ms |
30088 KB |
Output is correct |
15 |
Correct |
94 ms |
29124 KB |
Output is correct |
16 |
Correct |
95 ms |
30156 KB |
Output is correct |
17 |
Correct |
107 ms |
33480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
16732 KB |
Output is correct |
2 |
Correct |
27 ms |
16732 KB |
Output is correct |
3 |
Correct |
62 ms |
18832 KB |
Output is correct |
4 |
Correct |
44 ms |
19284 KB |
Output is correct |
5 |
Correct |
76 ms |
23872 KB |
Output is correct |
6 |
Correct |
67 ms |
23888 KB |
Output is correct |
7 |
Correct |
73 ms |
23872 KB |
Output is correct |
8 |
Correct |
84 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
516 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
516 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
33 ms |
3676 KB |
Output is correct |
18 |
Correct |
28 ms |
4188 KB |
Output is correct |
19 |
Correct |
27 ms |
3932 KB |
Output is correct |
20 |
Correct |
32 ms |
4312 KB |
Output is correct |
21 |
Correct |
24 ms |
3916 KB |
Output is correct |
22 |
Correct |
54 ms |
7672 KB |
Output is correct |
23 |
Correct |
8 ms |
1116 KB |
Output is correct |
24 |
Correct |
27 ms |
3020 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
516 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
33 ms |
3676 KB |
Output is correct |
18 |
Correct |
28 ms |
4188 KB |
Output is correct |
19 |
Correct |
27 ms |
3932 KB |
Output is correct |
20 |
Correct |
32 ms |
4312 KB |
Output is correct |
21 |
Correct |
24 ms |
3916 KB |
Output is correct |
22 |
Correct |
54 ms |
7672 KB |
Output is correct |
23 |
Correct |
8 ms |
1116 KB |
Output is correct |
24 |
Correct |
27 ms |
3020 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
1116 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Correct |
164 ms |
17564 KB |
Output is correct |
29 |
Correct |
235 ms |
23124 KB |
Output is correct |
30 |
Correct |
259 ms |
25796 KB |
Output is correct |
31 |
Correct |
275 ms |
25972 KB |
Output is correct |
32 |
Correct |
202 ms |
25172 KB |
Output is correct |
33 |
Correct |
270 ms |
26128 KB |
Output is correct |
34 |
Correct |
260 ms |
25996 KB |
Output is correct |
35 |
Correct |
79 ms |
9788 KB |
Output is correct |
36 |
Correct |
293 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
16732 KB |
Output is correct |
2 |
Correct |
27 ms |
16732 KB |
Output is correct |
3 |
Correct |
62 ms |
18832 KB |
Output is correct |
4 |
Correct |
44 ms |
19284 KB |
Output is correct |
5 |
Correct |
76 ms |
23872 KB |
Output is correct |
6 |
Correct |
67 ms |
23888 KB |
Output is correct |
7 |
Correct |
73 ms |
23872 KB |
Output is correct |
8 |
Correct |
84 ms |
23888 KB |
Output is correct |
9 |
Correct |
78 ms |
23892 KB |
Output is correct |
10 |
Correct |
50 ms |
13908 KB |
Output is correct |
11 |
Correct |
103 ms |
27732 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
26 ms |
16760 KB |
Output is correct |
19 |
Correct |
28 ms |
16732 KB |
Output is correct |
20 |
Correct |
27 ms |
16728 KB |
Output is correct |
21 |
Correct |
27 ms |
16732 KB |
Output is correct |
22 |
Correct |
96 ms |
23084 KB |
Output is correct |
23 |
Correct |
114 ms |
27744 KB |
Output is correct |
24 |
Correct |
114 ms |
27836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
28356 KB |
Output is correct |
2 |
Correct |
96 ms |
33236 KB |
Output is correct |
3 |
Correct |
29 ms |
16728 KB |
Output is correct |
4 |
Correct |
28 ms |
16640 KB |
Output is correct |
5 |
Correct |
301 ms |
45392 KB |
Output is correct |
6 |
Correct |
228 ms |
38736 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
147 ms |
34376 KB |
Output is correct |
9 |
Correct |
199 ms |
43176 KB |
Output is correct |
10 |
Correct |
80 ms |
29896 KB |
Output is correct |
11 |
Correct |
96 ms |
34964 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
27 ms |
16876 KB |
Output is correct |
17 |
Correct |
27 ms |
16644 KB |
Output is correct |
18 |
Correct |
98 ms |
30548 KB |
Output is correct |
19 |
Correct |
135 ms |
35664 KB |
Output is correct |
20 |
Correct |
91 ms |
30088 KB |
Output is correct |
21 |
Correct |
94 ms |
29124 KB |
Output is correct |
22 |
Correct |
95 ms |
30156 KB |
Output is correct |
23 |
Correct |
107 ms |
33480 KB |
Output is correct |
24 |
Correct |
26 ms |
16732 KB |
Output is correct |
25 |
Correct |
27 ms |
16732 KB |
Output is correct |
26 |
Correct |
62 ms |
18832 KB |
Output is correct |
27 |
Correct |
44 ms |
19284 KB |
Output is correct |
28 |
Correct |
76 ms |
23872 KB |
Output is correct |
29 |
Correct |
67 ms |
23888 KB |
Output is correct |
30 |
Correct |
73 ms |
23872 KB |
Output is correct |
31 |
Correct |
84 ms |
23888 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
516 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
33 ms |
3676 KB |
Output is correct |
49 |
Correct |
28 ms |
4188 KB |
Output is correct |
50 |
Correct |
27 ms |
3932 KB |
Output is correct |
51 |
Correct |
32 ms |
4312 KB |
Output is correct |
52 |
Correct |
24 ms |
3916 KB |
Output is correct |
53 |
Correct |
54 ms |
7672 KB |
Output is correct |
54 |
Correct |
8 ms |
1116 KB |
Output is correct |
55 |
Correct |
27 ms |
3020 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
7 ms |
1116 KB |
Output is correct |
58 |
Correct |
2 ms |
1116 KB |
Output is correct |
59 |
Correct |
164 ms |
17564 KB |
Output is correct |
60 |
Correct |
235 ms |
23124 KB |
Output is correct |
61 |
Correct |
259 ms |
25796 KB |
Output is correct |
62 |
Correct |
275 ms |
25972 KB |
Output is correct |
63 |
Correct |
202 ms |
25172 KB |
Output is correct |
64 |
Correct |
270 ms |
26128 KB |
Output is correct |
65 |
Correct |
260 ms |
25996 KB |
Output is correct |
66 |
Correct |
79 ms |
9788 KB |
Output is correct |
67 |
Correct |
293 ms |
23892 KB |
Output is correct |
68 |
Correct |
78 ms |
23892 KB |
Output is correct |
69 |
Correct |
50 ms |
13908 KB |
Output is correct |
70 |
Correct |
103 ms |
27732 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
26 ms |
16760 KB |
Output is correct |
78 |
Correct |
28 ms |
16732 KB |
Output is correct |
79 |
Correct |
27 ms |
16728 KB |
Output is correct |
80 |
Correct |
27 ms |
16732 KB |
Output is correct |
81 |
Correct |
96 ms |
23084 KB |
Output is correct |
82 |
Correct |
114 ms |
27744 KB |
Output is correct |
83 |
Correct |
114 ms |
27836 KB |
Output is correct |
84 |
Correct |
342 ms |
45936 KB |
Output is correct |
85 |
Correct |
355 ms |
47284 KB |
Output is correct |
86 |
Correct |
227 ms |
42084 KB |
Output is correct |
87 |
Correct |
232 ms |
44144 KB |
Output is correct |
88 |
Correct |
0 ms |
344 KB |
Output is correct |
89 |
Correct |
230 ms |
44000 KB |
Output is correct |
90 |
Correct |
212 ms |
44368 KB |
Output is correct |
91 |
Correct |
198 ms |
44504 KB |
Output is correct |