#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, mii::iterator &itpref0, ll &pref0)
{
if (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)
{
vmii grid(N + 4); // becomes prefix grid
for (int x = 0; x < sz(grid); ++x)
grid[x][0] = 0;
for (int i = 0; i < M; ++i)
grid[X[i] + 3][Y[i]] = W[i];
for (int x = 0; x < sz(grid); ++x)
{
ll pref = 0;
for (auto it = grid[x].begin(); it != grid[x].end(); ++it)
{
pref += it->second;
it->second = pref;
}
grid[x][N] = pref;
}
vmii dpg(sz(grid)); // growth stack thing
vmii dp(sz(grid)); // normal all dp
vi dpm(sz(grid), 0); // maximum einer Spaltes
for (int i = 0; i < 3; ++i)
dpg[i][0] = dpg[i][N] = dp[i][0] = dp[i][N] = 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 y = 0, 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);
dpg[x][y] = max(dpg[x][y], pref0 + pref2 + c1);
// Case 2 - last tower in x-2 and smaller y
c2 = max(c2, dp0 - pref0);
dpg[x][y] = max(dpg[x][y], 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
dp[x][y] = max(dp[x][y], dpm[x - 1] - pref1 + pref2);
// Case 4 - Last Tower in x-2 and larger y (or if it is smaller, case 2 performs better)
dpg[x][y] = max(dpg[x][y], dpm[x - 2] + pref2);
// Case 5 - Last Tower in x-3
dpg[x][y] = max(dpg[x][y], dpm[x - 3] + pref0 + pref2);
// keep track for dpg & dpm
dp[x][y] = max(dp[x][y], dpg[x][y]);
dpm[x] = max(dpm[x], dp[x][y]);
y = ny;
}
dp[x][N] = max(dp[x][N], dp[x].rbegin()->second);
dpg[x][N] = max(dpg[x][N], dpg[x].rbegin()->second);
}
return *max_element(all(dpm));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1079 ms |
106344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1083 ms |
107676 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
52632 KB |
Output is correct |
2 |
Correct |
63 ms |
52640 KB |
Output is correct |
3 |
Correct |
78 ms |
49684 KB |
Output is correct |
4 |
Correct |
75 ms |
54204 KB |
Output is correct |
5 |
Correct |
96 ms |
56600 KB |
Output is correct |
6 |
Correct |
91 ms |
55952 KB |
Output is correct |
7 |
Correct |
101 ms |
56632 KB |
Output is correct |
8 |
Correct |
97 ms |
56652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 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 |
0 ms |
300 KB |
Output is correct |
6 |
Correct |
1 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 |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
820 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 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 |
0 ms |
300 KB |
Output is correct |
6 |
Correct |
1 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 |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
820 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
20 ms |
6020 KB |
Output is correct |
16 |
Correct |
3 ms |
1108 KB |
Output is correct |
17 |
Correct |
71 ms |
15540 KB |
Output is correct |
18 |
Correct |
66 ms |
14028 KB |
Output is correct |
19 |
Correct |
78 ms |
16052 KB |
Output is correct |
20 |
Correct |
68 ms |
14804 KB |
Output is correct |
21 |
Correct |
51 ms |
10640 KB |
Output is correct |
22 |
Correct |
118 ms |
20824 KB |
Output is correct |
23 |
Correct |
47 ms |
12184 KB |
Output is correct |
24 |
Correct |
62 ms |
14668 KB |
Output is correct |
25 |
Correct |
3 ms |
1212 KB |
Output is correct |
26 |
Correct |
23 ms |
6544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 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 |
0 ms |
300 KB |
Output is correct |
6 |
Correct |
1 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 |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
820 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
20 ms |
6020 KB |
Output is correct |
16 |
Correct |
3 ms |
1108 KB |
Output is correct |
17 |
Correct |
71 ms |
15540 KB |
Output is correct |
18 |
Correct |
66 ms |
14028 KB |
Output is correct |
19 |
Correct |
78 ms |
16052 KB |
Output is correct |
20 |
Correct |
68 ms |
14804 KB |
Output is correct |
21 |
Correct |
51 ms |
10640 KB |
Output is correct |
22 |
Correct |
118 ms |
20824 KB |
Output is correct |
23 |
Correct |
47 ms |
12184 KB |
Output is correct |
24 |
Correct |
62 ms |
14668 KB |
Output is correct |
25 |
Correct |
3 ms |
1212 KB |
Output is correct |
26 |
Correct |
23 ms |
6544 KB |
Output is correct |
27 |
Execution timed out |
1096 ms |
240332 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
52632 KB |
Output is correct |
2 |
Correct |
63 ms |
52640 KB |
Output is correct |
3 |
Correct |
78 ms |
49684 KB |
Output is correct |
4 |
Correct |
75 ms |
54204 KB |
Output is correct |
5 |
Correct |
96 ms |
56600 KB |
Output is correct |
6 |
Correct |
91 ms |
55952 KB |
Output is correct |
7 |
Correct |
101 ms |
56632 KB |
Output is correct |
8 |
Correct |
97 ms |
56652 KB |
Output is correct |
9 |
Execution timed out |
1083 ms |
258400 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1079 ms |
106344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |