#include "fish.h"
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef map<ll, ll> mii;
typedef vector<mii> vmii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const ll INF = 3e5 * 1e9 + 100;
void updateIdx(ll y, ll &ny, vpii::iterator &itpref0, ll &pref0)
{
while (itpref0->first <= y)
{
pref0 = itpref0->second;
++itpref0;
}
ny = min(itpref0->first, ny);
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W)
{
vvpii grid(N + 4); // becomes prefix grid
for (int x = 0; x < sz(grid); ++x)
grid[x].push_back({0, 0});
for (int i = 0; i < M; ++i)
grid[X[i] + 3].push_back({Y[i] + 1, W[i]}); // Transformation in Y Richtung...
for (int x = 0; x < sz(grid); ++x)
sort(all(grid[x]));
for (int x = 0; x < sz(grid); ++x)
{
ll pref = 0;
for (int i = 0; i < sz(grid[x]); ++i)
{
grid[x][i].second += pref;
pref = grid[x][i].second;
}
grid[x].push_back({N + 1, pref});
}
vvpii dpg(sz(grid)); // growth stack thing
vvpii dp(sz(grid)); // normal all dp
vi dpm(sz(grid), 0); // maximum einer Spaltes
for (int i = 0; i < 3; ++i)
{
dp[i].push_back({0, 0});
dp[i].push_back({N + 1, 0});
dpg[i].push_back({0, 0});
dpg[i].push_back({N + 1, 0});
}
for (int x = 3; x < sz(grid) - 1; ++x) // so i choose the borders
{
ll c1 = -INF, c2 = -INF; // running variables for case 1 and 2 // TODO: INIT
ll ndp = 0, ndpg = 0;
dp[x].push_back({0, 0}), dpg[x].push_back({0, 0});
ll y = 1, ny;
ll pref0 = 0, pref1 = 0, pref2 = 0, dp0 = 0, dpg1 = 0;
auto itpref0 = grid[x - 1].begin(); // idx for pref x-1
auto itpref1 = grid[x].begin(); // idx for pref x
auto itpref2 = grid[x + 1].begin(); // idx for pref x+1
auto itdp0 = dp[x - 2].begin(); // idx for dp x-2
auto itdpg1 = dpg[x - 1].begin(); // idx for dpg x-1
while (y <= N)
{
// update indices
ny = INT_MAX;
updateIdx(y, ny, itpref0, pref0);
updateIdx(y, ny, itpref1, pref1);
updateIdx(y, ny, itpref2, pref2);
updateIdx(y, ny, itdp0, dp0);
updateIdx(y, ny, itdpg1, dpg1);
// Case 1 - last tower in x-1 and smaller y
c1 = max(c1, dpg1 - pref0 - pref1);
ndpg = max(ndpg, pref0 + pref2 + c1);
// Case 2 - last tower in x-2 and smaller y
c2 = max(c2, dp0 - pref0);
ndpg = max(ndpg, pref0 + pref2 + c2);
// Case 3 - last tower in x-1 and larger y (or if it is smaller, case 1 performs better)
// here the last tower might be larger than the current one
ndp = max(ndp, dpm[x - 1] - pref1 + pref2);
// Case 4 - Last Tower in x-2 and larger y (or if it is smaller, case 2 performs better)
ndpg = max(ndpg, dpm[x - 2] + pref2);
// Case 5 - Last Tower in x-3
ndpg = max(ndpg, dpm[x - 3] + pref0 + pref2);
// keep track for dpg & dpm
ndp = max(ndp, ndpg);
dpm[x] = max(dpm[x], ndp);
if (ndpg != dpg[x].back().second)
dpg[x].push_back({y, ndpg});
if (ndp != dp[x].back().second)
dp[x].push_back({y, ndp});
y = ny;
}
dp[x].push_back({N + 1, dp[x].back().second});
dpg[x].push_back({N + 1, dpg[x].back().second});
}
return *max_element(all(dpm));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
33192 KB |
Output is correct |
2 |
Correct |
89 ms |
37532 KB |
Output is correct |
3 |
Correct |
46 ms |
28364 KB |
Output is correct |
4 |
Correct |
46 ms |
28364 KB |
Output is correct |
5 |
Correct |
159 ms |
50620 KB |
Output is correct |
6 |
Correct |
213 ms |
62636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
126 ms |
36424 KB |
Output is correct |
3 |
Correct |
130 ms |
41512 KB |
Output is correct |
4 |
Correct |
79 ms |
34044 KB |
Output is correct |
5 |
Correct |
95 ms |
38800 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
46 ms |
28444 KB |
Output is correct |
11 |
Correct |
44 ms |
28456 KB |
Output is correct |
12 |
Correct |
74 ms |
32720 KB |
Output is correct |
13 |
Correct |
86 ms |
37280 KB |
Output is correct |
14 |
Correct |
73 ms |
32568 KB |
Output is correct |
15 |
Correct |
81 ms |
35952 KB |
Output is correct |
16 |
Correct |
78 ms |
34456 KB |
Output is correct |
17 |
Correct |
84 ms |
38332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
28444 KB |
Output is correct |
2 |
Correct |
35 ms |
22104 KB |
Output is correct |
3 |
Correct |
68 ms |
29484 KB |
Output is correct |
4 |
Correct |
64 ms |
31184 KB |
Output is correct |
5 |
Correct |
105 ms |
35844 KB |
Output is correct |
6 |
Correct |
93 ms |
35872 KB |
Output is correct |
7 |
Correct |
98 ms |
35792 KB |
Output is correct |
8 |
Correct |
97 ms |
35784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
2 ms |
440 KB |
Output is correct |
17 |
Correct |
17 ms |
4304 KB |
Output is correct |
18 |
Correct |
17 ms |
5092 KB |
Output is correct |
19 |
Correct |
16 ms |
4852 KB |
Output is correct |
20 |
Correct |
16 ms |
4808 KB |
Output is correct |
21 |
Correct |
17 ms |
4764 KB |
Output is correct |
22 |
Correct |
31 ms |
8772 KB |
Output is correct |
23 |
Correct |
4 ms |
1280 KB |
Output is correct |
24 |
Correct |
12 ms |
3384 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
4 ms |
1208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
2 ms |
440 KB |
Output is correct |
17 |
Correct |
17 ms |
4304 KB |
Output is correct |
18 |
Correct |
17 ms |
5092 KB |
Output is correct |
19 |
Correct |
16 ms |
4852 KB |
Output is correct |
20 |
Correct |
16 ms |
4808 KB |
Output is correct |
21 |
Correct |
17 ms |
4764 KB |
Output is correct |
22 |
Correct |
31 ms |
8772 KB |
Output is correct |
23 |
Correct |
4 ms |
1280 KB |
Output is correct |
24 |
Correct |
12 ms |
3384 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
4 ms |
1208 KB |
Output is correct |
27 |
Correct |
3 ms |
1492 KB |
Output is correct |
28 |
Correct |
75 ms |
19216 KB |
Output is correct |
29 |
Correct |
110 ms |
26608 KB |
Output is correct |
30 |
Correct |
119 ms |
28404 KB |
Output is correct |
31 |
Correct |
121 ms |
28524 KB |
Output is correct |
32 |
Correct |
101 ms |
26608 KB |
Output is correct |
33 |
Correct |
121 ms |
28784 KB |
Output is correct |
34 |
Correct |
121 ms |
28868 KB |
Output is correct |
35 |
Correct |
47 ms |
11852 KB |
Output is correct |
36 |
Correct |
132 ms |
29420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
28444 KB |
Output is correct |
2 |
Correct |
35 ms |
22104 KB |
Output is correct |
3 |
Correct |
68 ms |
29484 KB |
Output is correct |
4 |
Correct |
64 ms |
31184 KB |
Output is correct |
5 |
Correct |
105 ms |
35844 KB |
Output is correct |
6 |
Correct |
93 ms |
35872 KB |
Output is correct |
7 |
Correct |
98 ms |
35792 KB |
Output is correct |
8 |
Correct |
97 ms |
35784 KB |
Output is correct |
9 |
Correct |
105 ms |
41928 KB |
Output is correct |
10 |
Correct |
65 ms |
19396 KB |
Output is correct |
11 |
Correct |
144 ms |
37944 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
44 ms |
28412 KB |
Output is correct |
19 |
Correct |
45 ms |
28404 KB |
Output is correct |
20 |
Correct |
38 ms |
22196 KB |
Output is correct |
21 |
Correct |
36 ms |
22152 KB |
Output is correct |
22 |
Correct |
117 ms |
38996 KB |
Output is correct |
23 |
Correct |
180 ms |
48156 KB |
Output is correct |
24 |
Correct |
172 ms |
48480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
33192 KB |
Output is correct |
2 |
Correct |
89 ms |
37532 KB |
Output is correct |
3 |
Correct |
46 ms |
28364 KB |
Output is correct |
4 |
Correct |
46 ms |
28364 KB |
Output is correct |
5 |
Correct |
159 ms |
50620 KB |
Output is correct |
6 |
Correct |
213 ms |
62636 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
126 ms |
36424 KB |
Output is correct |
9 |
Correct |
130 ms |
41512 KB |
Output is correct |
10 |
Correct |
79 ms |
34044 KB |
Output is correct |
11 |
Correct |
95 ms |
38800 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
46 ms |
28444 KB |
Output is correct |
17 |
Correct |
44 ms |
28456 KB |
Output is correct |
18 |
Correct |
74 ms |
32720 KB |
Output is correct |
19 |
Correct |
86 ms |
37280 KB |
Output is correct |
20 |
Correct |
73 ms |
32568 KB |
Output is correct |
21 |
Correct |
81 ms |
35952 KB |
Output is correct |
22 |
Correct |
78 ms |
34456 KB |
Output is correct |
23 |
Correct |
84 ms |
38332 KB |
Output is correct |
24 |
Correct |
45 ms |
28444 KB |
Output is correct |
25 |
Correct |
35 ms |
22104 KB |
Output is correct |
26 |
Correct |
68 ms |
29484 KB |
Output is correct |
27 |
Correct |
64 ms |
31184 KB |
Output is correct |
28 |
Correct |
105 ms |
35844 KB |
Output is correct |
29 |
Correct |
93 ms |
35872 KB |
Output is correct |
30 |
Correct |
98 ms |
35792 KB |
Output is correct |
31 |
Correct |
97 ms |
35784 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
304 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 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 |
1 ms |
436 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
440 KB |
Output is correct |
46 |
Correct |
1 ms |
304 KB |
Output is correct |
47 |
Correct |
2 ms |
440 KB |
Output is correct |
48 |
Correct |
17 ms |
4304 KB |
Output is correct |
49 |
Correct |
17 ms |
5092 KB |
Output is correct |
50 |
Correct |
16 ms |
4852 KB |
Output is correct |
51 |
Correct |
16 ms |
4808 KB |
Output is correct |
52 |
Correct |
17 ms |
4764 KB |
Output is correct |
53 |
Correct |
31 ms |
8772 KB |
Output is correct |
54 |
Correct |
4 ms |
1280 KB |
Output is correct |
55 |
Correct |
12 ms |
3384 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
4 ms |
1208 KB |
Output is correct |
58 |
Correct |
3 ms |
1492 KB |
Output is correct |
59 |
Correct |
75 ms |
19216 KB |
Output is correct |
60 |
Correct |
110 ms |
26608 KB |
Output is correct |
61 |
Correct |
119 ms |
28404 KB |
Output is correct |
62 |
Correct |
121 ms |
28524 KB |
Output is correct |
63 |
Correct |
101 ms |
26608 KB |
Output is correct |
64 |
Correct |
121 ms |
28784 KB |
Output is correct |
65 |
Correct |
121 ms |
28868 KB |
Output is correct |
66 |
Correct |
47 ms |
11852 KB |
Output is correct |
67 |
Correct |
132 ms |
29420 KB |
Output is correct |
68 |
Correct |
105 ms |
41928 KB |
Output is correct |
69 |
Correct |
65 ms |
19396 KB |
Output is correct |
70 |
Correct |
144 ms |
37944 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
44 ms |
28412 KB |
Output is correct |
78 |
Correct |
45 ms |
28404 KB |
Output is correct |
79 |
Correct |
38 ms |
22196 KB |
Output is correct |
80 |
Correct |
36 ms |
22152 KB |
Output is correct |
81 |
Correct |
117 ms |
38996 KB |
Output is correct |
82 |
Correct |
180 ms |
48156 KB |
Output is correct |
83 |
Correct |
172 ms |
48480 KB |
Output is correct |
84 |
Correct |
163 ms |
55904 KB |
Output is correct |
85 |
Correct |
176 ms |
57340 KB |
Output is correct |
86 |
Correct |
224 ms |
61624 KB |
Output is correct |
87 |
Correct |
229 ms |
64584 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
228 ms |
64684 KB |
Output is correct |
90 |
Correct |
190 ms |
60904 KB |
Output is correct |
91 |
Correct |
175 ms |
59268 KB |
Output is correct |