#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, h[maxn], pref[maxn], peak_pos;
void init(int N, std::vector<int> H)
{
n = N;
for (int i = 0; i < n; i ++)
h[i + 1] = H[i];
for (int i = 2; i <= n; i ++)
{
pref[i] = pref[i - 1];
if (h[i] > h[i - 1] && h[i] > h[i + 1])
pref[i] ++, peak_pos = 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 ++;
if (pref[n] == 1)
{
if (peak_pos <= L || peak_pos >= R)
return 0;
if (h[L] <= h[peak_pos] - D && h[R] <= h[peak_pos] - D)
return 1;
return 0;
}
if (D == 1)
{
int peaks = max(0, pref[R - 1] - pref[L]);
return peaks + 1;
}
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 |
Incorrect |
368 ms |
3536 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2768 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 |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2640 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2768 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 |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2640 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
386 ms |
4168 KB |
Output is correct |
2 |
Correct |
844 ms |
4200 KB |
Output is correct |
3 |
Correct |
873 ms |
4204 KB |
Output is correct |
4 |
Correct |
775 ms |
4180 KB |
Output is correct |
5 |
Correct |
809 ms |
4168 KB |
Output is correct |
6 |
Correct |
935 ms |
4184 KB |
Output is correct |
7 |
Correct |
739 ms |
4196 KB |
Output is correct |
8 |
Incorrect |
849 ms |
4168 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4035 ms |
69900 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2768 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 |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2640 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
368 ms |
3536 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |