#include "towers.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T1, typename T2>
using indexed_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using indexed_set = indexed_map<T, null_type>;
#define loop(x, i) for (int i = 0; i < (x); i++)
#define loop1(x, i) for (int i = 1; i <= (x); i++)
#define rev(x, i) for (int i = (int)(x) - 1; i >= 0; i--)
#define itloop(x) for (auto it = begin(x); x != end(x); it++)
#define itrev(x) for (auto it = rbegin(x); x != rend(x); it++)
#define INF ((int64_t)(4e18 + 1))
#define INF32 ((int32_t)(2e9 + 1))
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define removeIn(x, l) l.erase(find(ALL(l), x))
#define pb push_back
#define sz(x) (int)(x).size()
#define F first
#define S second
#define var const auto &
#define foreach(l) for (var e : l)
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vii> vvii;
typedef vector<viii> vviii;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;
typedef vector<si> vsi;
typedef vector<sii> vsii;
typedef vector<vsi> vvsi;
typedef vector<string> vstr;
typedef vector<vector<string>> vvstr;
typedef vector<bool> vb;
typedef vector<vb> vvb;
vi h;
vii minTree, maxTree;
int n;
int treeN = 1;
void init(int N, vi H)
{
h = H;
n = N;
while (treeN < N)
treeN *= 2;
minTree = vii(treeN * 2, {1e9, 1e9});
maxTree = vii(treeN * 2, {0, -1e9});
loop(n, i)
{
minTree[treeN + i] = {H[i], i};
maxTree[treeN + i] = {H[i], -i};
}
rev(treeN, i)
{
minTree[i] = min(minTree[2 * i + 1], minTree[2 * i]);
maxTree[i] = max(maxTree[2 * i + 1], maxTree[2 * i]);
}
}
ii combine(ii a, ii b, bool minOp)
{
if (minOp)
return min(a, b);
return max(a, b);
}
// v, ix
ii query(int i, int l, int r, int ql, int qr, bool minOp)
{
if (r < ql || qr < l)
return {1e9 * minOp, 1e9 * minOp};
int m = l + (r - l) / 2;
if (ql <= l && r <= qr)
{
if (minOp)
return minTree[i];
return maxTree[i];
}
return combine(query(2 * i, l, m, ql, qr, minOp),
query(2 * i + 1, m + 1, r, ql, qr, minOp), minOp);
}
int max_towers(int L, int R, int D)
{
vi dp(R + 1, 1);
vi maxDP(R + 1, 1);
for (int i = R - 2; i >= L; i--)
{
int lo = i + 2, hi = R;
bool suc = 0;
int lastIx = -1;
while (hi >= lo)
{
int m = lo + (hi - lo) / 2;
if (i + 1 > m - 1)
{
lo = m + 1;
continue;
}
auto [mn, mnIx] = query(1, 0, treeN - 1, i + 2, m, 1);
auto [mx, mxIx] = query(1, 0, treeN - 1, i + 1, mnIx - 1, 0);
mxIx *= -1;
if (mx - max(mn, h[i]) >= D)
{
lastIx = mnIx;
suc = 1;
if (lo == hi)
break;
hi = m;
}
else
{
lo = m + 1;
}
}
if (suc)
{
dp[i] = maxDP[lastIx] + 1;
}
maxDP[i] = max(dp[i], maxDP[i + 1]);
}
return *max_element(ALL(dp));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4054 ms |
3368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
524 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
420 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
3 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
2 ms |
472 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
8 ms |
520 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
5 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
524 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
524 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
420 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
3 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
2 ms |
472 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
8 ms |
520 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
5 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
524 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
36 |
Correct |
97 ms |
3160 KB |
Output is correct |
37 |
Correct |
223 ms |
5840 KB |
Output is correct |
38 |
Correct |
86 ms |
5840 KB |
Output is correct |
39 |
Correct |
191 ms |
5720 KB |
Output is correct |
40 |
Correct |
70 ms |
5720 KB |
Output is correct |
41 |
Correct |
537 ms |
6096 KB |
Output is correct |
42 |
Correct |
20 ms |
5720 KB |
Output is correct |
43 |
Correct |
262 ms |
6028 KB |
Output is correct |
44 |
Correct |
80 ms |
5720 KB |
Output is correct |
45 |
Correct |
18 ms |
5720 KB |
Output is correct |
46 |
Correct |
22 ms |
5656 KB |
Output is correct |
47 |
Correct |
196 ms |
5720 KB |
Output is correct |
48 |
Correct |
114 ms |
5840 KB |
Output is correct |
49 |
Correct |
44 ms |
5720 KB |
Output is correct |
50 |
Correct |
129 ms |
5720 KB |
Output is correct |
51 |
Correct |
197 ms |
5840 KB |
Output is correct |
52 |
Correct |
635 ms |
6344 KB |
Output is correct |
53 |
Correct |
602 ms |
6344 KB |
Output is correct |
54 |
Correct |
606 ms |
6096 KB |
Output is correct |
55 |
Correct |
761 ms |
6096 KB |
Output is correct |
56 |
Correct |
622 ms |
6096 KB |
Output is correct |
57 |
Correct |
641 ms |
6044 KB |
Output is correct |
58 |
Correct |
682 ms |
6300 KB |
Output is correct |
59 |
Correct |
740 ms |
6100 KB |
Output is correct |
60 |
Correct |
610 ms |
6096 KB |
Output is correct |
61 |
Correct |
665 ms |
6088 KB |
Output is correct |
62 |
Correct |
671 ms |
6096 KB |
Output is correct |
63 |
Correct |
690 ms |
6096 KB |
Output is correct |
64 |
Correct |
642 ms |
6088 KB |
Output is correct |
65 |
Correct |
786 ms |
5912 KB |
Output is correct |
66 |
Correct |
741 ms |
6100 KB |
Output is correct |
67 |
Correct |
791 ms |
6108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4094 ms |
6068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4069 ms |
1920 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
524 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
420 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
3 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
2 ms |
472 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
8 ms |
520 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
5 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
524 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
36 |
Correct |
97 ms |
3160 KB |
Output is correct |
37 |
Correct |
223 ms |
5840 KB |
Output is correct |
38 |
Correct |
86 ms |
5840 KB |
Output is correct |
39 |
Correct |
191 ms |
5720 KB |
Output is correct |
40 |
Correct |
70 ms |
5720 KB |
Output is correct |
41 |
Correct |
537 ms |
6096 KB |
Output is correct |
42 |
Correct |
20 ms |
5720 KB |
Output is correct |
43 |
Correct |
262 ms |
6028 KB |
Output is correct |
44 |
Correct |
80 ms |
5720 KB |
Output is correct |
45 |
Correct |
18 ms |
5720 KB |
Output is correct |
46 |
Correct |
22 ms |
5656 KB |
Output is correct |
47 |
Correct |
196 ms |
5720 KB |
Output is correct |
48 |
Correct |
114 ms |
5840 KB |
Output is correct |
49 |
Correct |
44 ms |
5720 KB |
Output is correct |
50 |
Correct |
129 ms |
5720 KB |
Output is correct |
51 |
Correct |
197 ms |
5840 KB |
Output is correct |
52 |
Correct |
635 ms |
6344 KB |
Output is correct |
53 |
Correct |
602 ms |
6344 KB |
Output is correct |
54 |
Correct |
606 ms |
6096 KB |
Output is correct |
55 |
Correct |
761 ms |
6096 KB |
Output is correct |
56 |
Correct |
622 ms |
6096 KB |
Output is correct |
57 |
Correct |
641 ms |
6044 KB |
Output is correct |
58 |
Correct |
682 ms |
6300 KB |
Output is correct |
59 |
Correct |
740 ms |
6100 KB |
Output is correct |
60 |
Correct |
610 ms |
6096 KB |
Output is correct |
61 |
Correct |
665 ms |
6088 KB |
Output is correct |
62 |
Correct |
671 ms |
6096 KB |
Output is correct |
63 |
Correct |
690 ms |
6096 KB |
Output is correct |
64 |
Correct |
642 ms |
6088 KB |
Output is correct |
65 |
Correct |
786 ms |
5912 KB |
Output is correct |
66 |
Correct |
741 ms |
6100 KB |
Output is correct |
67 |
Correct |
791 ms |
6108 KB |
Output is correct |
68 |
Execution timed out |
4094 ms |
6068 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4054 ms |
3368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |