#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, h[maxn];
void init(int N, std::vector<int> H)
{
n = N;
for (int i = 0; i < n; i ++)
h[i + 1] = H[i];
}
int par[maxn], rnk[maxn];
int find_leader(int v)
{
return (v == par[v]) ? v : par[v] = find_leader(par[v]);
}
void unite(int v, int u)
{
v = find_leader(v);
u = find_leader(u);
if (v == u)
return;
if (rnk[v] < rnk[u])
swap(v, u);
rnk[v] += rnk[u];
par[u] = v;
}
int dp[maxn], bef[maxn], aft[maxn];
int tree[4 * maxn];
void update(int root, int left, int right, int pos, int val)
{
if (left == right)
{
tree[root] = val;
return;
}
int mid = (left + right) / 2;
if (pos <= mid)
update(root * 2, left, mid, pos, val);
else
update(root * 2 + 1, mid + 1, right, pos, val);
tree[root] = max(tree[root * 2], tree[root * 2 + 1]);
}
int query(int root, int left, int right, int qleft, int qright)
{
if (left > qright || right < qleft)
return 0;
if (left >= qleft && right <= qright)
return tree[root];
int mid = (left + right) / 2;
return max(query(root * 2, left, mid, qleft, qright),
query(root * 2 + 1, mid + 1, right, qleft, qright));
}
vector < int > act[maxn];
int max_towers(int L, int R, int D)
{
L ++;
R ++;
for (int i = 0; i < n; i ++)
act[i].clear();
for (int i = 0; i < 4 * n; i ++)
tree[i] = 0;
h[0] = h[n + 1] = 2e9;
vector < int > st;
st.push_back(0);
for (int i = 1; i <= n; i ++)
{
int lf = 0, rf = st.size() - 1;
while(lf <= rf)
{
int mf = (lf + rf) / 2;
if (h[st[mf]] - D >= h[i])
lf = mf + 1;
else
rf = mf - 1;
}
bef[i] = st[rf];
while(!st.empty() && h[st.back()] <= h[i])
st.pop_back();
st.push_back(i);
}
st.clear();
st.push_back(n + 1);
for (int i = n; i > 0; i --)
{
int lf = 0, rf = st.size() - 1;
while(lf <= rf)
{
int mf = (lf + rf) / 2;
if (h[st[mf]] - D >= h[i])
lf = mf + 1;
else
rf = mf - 1;
}
aft[i] = st[rf];
while(!st.empty() && h[st.back()] <= h[i])
st.pop_back();
st.push_back(i);
}
int ans = 0;
for (int i = L; i <= R; i ++)
{
for (int pos : act[i])
{
update(1, 0, n - 1, pos, dp[pos]);
}
act[i].clear();
dp[i] = 1;
int left = L, right = bef[i];
if (left <= right)
{
int x = query(1, 0, n - 1, left, right) + 1;
dp[i] = x;
}
act[aft[i]].push_back(i);
ans = max(ans, dp[i]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4101 ms |
72908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2764 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
2 ms |
2640 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
2 ms |
2640 KB |
Output is correct |
19 |
Correct |
2 ms |
2640 KB |
Output is correct |
20 |
Correct |
3 ms |
2768 KB |
Output is correct |
21 |
Correct |
3 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
2 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
2 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2764 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
2 ms |
2640 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
2 ms |
2640 KB |
Output is correct |
19 |
Correct |
2 ms |
2640 KB |
Output is correct |
20 |
Correct |
3 ms |
2768 KB |
Output is correct |
21 |
Correct |
3 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
2 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
2 ms |
2640 KB |
Output is correct |
36 |
Correct |
19 ms |
4944 KB |
Output is correct |
37 |
Correct |
21 ms |
6088 KB |
Output is correct |
38 |
Correct |
22 ms |
5980 KB |
Output is correct |
39 |
Correct |
17 ms |
6052 KB |
Output is correct |
40 |
Correct |
19 ms |
5960 KB |
Output is correct |
41 |
Correct |
31 ms |
6992 KB |
Output is correct |
42 |
Correct |
23 ms |
5696 KB |
Output is correct |
43 |
Correct |
23 ms |
6988 KB |
Output is correct |
44 |
Correct |
25 ms |
6740 KB |
Output is correct |
45 |
Correct |
19 ms |
6592 KB |
Output is correct |
46 |
Correct |
17 ms |
6220 KB |
Output is correct |
47 |
Correct |
28 ms |
6464 KB |
Output is correct |
48 |
Correct |
32 ms |
6212 KB |
Output is correct |
49 |
Correct |
19 ms |
5824 KB |
Output is correct |
50 |
Correct |
20 ms |
6728 KB |
Output is correct |
51 |
Correct |
21 ms |
7492 KB |
Output is correct |
52 |
Correct |
53 ms |
7672 KB |
Output is correct |
53 |
Correct |
53 ms |
7724 KB |
Output is correct |
54 |
Correct |
54 ms |
7740 KB |
Output is correct |
55 |
Correct |
36 ms |
7380 KB |
Output is correct |
56 |
Correct |
40 ms |
9528 KB |
Output is correct |
57 |
Correct |
31 ms |
6728 KB |
Output is correct |
58 |
Correct |
45 ms |
7156 KB |
Output is correct |
59 |
Correct |
50 ms |
7364 KB |
Output is correct |
60 |
Correct |
51 ms |
7728 KB |
Output is correct |
61 |
Correct |
45 ms |
7616 KB |
Output is correct |
62 |
Correct |
38 ms |
7428 KB |
Output is correct |
63 |
Correct |
45 ms |
7708 KB |
Output is correct |
64 |
Correct |
25 ms |
7744 KB |
Output is correct |
65 |
Correct |
25 ms |
7368 KB |
Output is correct |
66 |
Correct |
32 ms |
7496 KB |
Output is correct |
67 |
Correct |
35 ms |
7384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4010 ms |
7880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4094 ms |
69724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2764 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
2 ms |
2640 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
2 ms |
2640 KB |
Output is correct |
19 |
Correct |
2 ms |
2640 KB |
Output is correct |
20 |
Correct |
3 ms |
2768 KB |
Output is correct |
21 |
Correct |
3 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
2 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2640 KB |
Output is correct |
34 |
Correct |
2 ms |
2640 KB |
Output is correct |
35 |
Correct |
2 ms |
2640 KB |
Output is correct |
36 |
Correct |
19 ms |
4944 KB |
Output is correct |
37 |
Correct |
21 ms |
6088 KB |
Output is correct |
38 |
Correct |
22 ms |
5980 KB |
Output is correct |
39 |
Correct |
17 ms |
6052 KB |
Output is correct |
40 |
Correct |
19 ms |
5960 KB |
Output is correct |
41 |
Correct |
31 ms |
6992 KB |
Output is correct |
42 |
Correct |
23 ms |
5696 KB |
Output is correct |
43 |
Correct |
23 ms |
6988 KB |
Output is correct |
44 |
Correct |
25 ms |
6740 KB |
Output is correct |
45 |
Correct |
19 ms |
6592 KB |
Output is correct |
46 |
Correct |
17 ms |
6220 KB |
Output is correct |
47 |
Correct |
28 ms |
6464 KB |
Output is correct |
48 |
Correct |
32 ms |
6212 KB |
Output is correct |
49 |
Correct |
19 ms |
5824 KB |
Output is correct |
50 |
Correct |
20 ms |
6728 KB |
Output is correct |
51 |
Correct |
21 ms |
7492 KB |
Output is correct |
52 |
Correct |
53 ms |
7672 KB |
Output is correct |
53 |
Correct |
53 ms |
7724 KB |
Output is correct |
54 |
Correct |
54 ms |
7740 KB |
Output is correct |
55 |
Correct |
36 ms |
7380 KB |
Output is correct |
56 |
Correct |
40 ms |
9528 KB |
Output is correct |
57 |
Correct |
31 ms |
6728 KB |
Output is correct |
58 |
Correct |
45 ms |
7156 KB |
Output is correct |
59 |
Correct |
50 ms |
7364 KB |
Output is correct |
60 |
Correct |
51 ms |
7728 KB |
Output is correct |
61 |
Correct |
45 ms |
7616 KB |
Output is correct |
62 |
Correct |
38 ms |
7428 KB |
Output is correct |
63 |
Correct |
45 ms |
7708 KB |
Output is correct |
64 |
Correct |
25 ms |
7744 KB |
Output is correct |
65 |
Correct |
25 ms |
7368 KB |
Output is correct |
66 |
Correct |
32 ms |
7496 KB |
Output is correct |
67 |
Correct |
35 ms |
7384 KB |
Output is correct |
68 |
Execution timed out |
4010 ms |
7880 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4101 ms |
72908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |