#include <bits/stdc++.h>
#define INF 1e18
using namespace std;
vector<int> H;
vector<int> seg;
int N;
void build(int v, int l, int r)
{
for (int i = 0; i < N; i++) seg[N+i] = H[i];
for (int i = N-1; i >= 1; i--) seg[i] = max(seg[2*i], seg[2*i+1]);
}
int max_query(int v, int l, int r, int q_l, int q_r)
{
if (l > q_r || r < q_l) return 0;
if (l >= q_l && r <= q_r) return seg[v];
int mid = (l + r) / 2;
return max(max_query(2 * v, l, mid, q_l, q_r), max_query(2 * v + 1, mid+1, r, q_l, q_r));
}
bool check(pair<int, int> f, pair<int, int> s, int d)
{
return (max(f.first, s.first) <= (max_query(0, 0, N-1, min(f.second, s.second), max(f.second, s.second))-d));
}
void init(int n, vector<int> h)
{
H = h;
N = n;
seg.resize(2*N);
build(0, 0, N-1);
}
int max_towers(int L, int R, int D)
{
vector<pair<int, int>> r;
for (int i = L; i <= R; i++) r.push_back({H[i], i});
sort(r.begin(), r.end());
int ans = 1;
vector<bool> taken(N);
pair<int, int> last = r[0];
taken[r[0].second] = true;
for (int i = 1; i < r.size(); i++)
{
if ((r[i].second == 0 || !taken[r[i].second-1]) && (r[i].second == N-1 || !taken[r[i].second+1]) && check(last, r[i], D))
{
taken[r[i].second] = true;
ans++;
}
}
return ans;
}
// int main()
// {
// init(7, {10, 20, 60, 40, 50, 30, 70});
// cout << max_towers(1, 5, 10) << endl; // Ans:- 3
// cout << max_towers(2, 2, 100) << endl; // Ans:- 1
// cout << max_towers(0, 6, 17) << endl; // Ans:- 2
// return 0;
// }
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 1; i < r.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4011 ms |
2724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '13', found: '22' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '13', found: '22' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4003 ms |
4136 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4003 ms |
2316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '13', found: '22' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4011 ms |
2724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |