#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]);
if (l == r) seg[v] = H[l];
else
{
int mid = (l + r) / 2;
build(2 * v, l, mid);
build(2 * v + 1, mid+1, r);
seg[v] = max(seg[2*v], seg[2*v+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 (min(f.second, s.second)+1 < max(f.second, s.second)) && (max(f.first, s.first) <= (max_query(0, 0, N-1, min(f.second, s.second)+1, max(f.second, s.second)-1)-d));
}
void init(int n, vector<int> h)
{
H = h;
N = n;
seg.resize(4*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());
pair<int, int> last = r[0];
int ans = 1;
for (int i = 1; i < r.size(); i++)
{
if (check(last, r[i], D))
{
ans++;
last = r[i];
}
}
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:57: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]
57 | for (int i = 1; i < r.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4030 ms |
3532 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: '40' |
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: '40' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4011 ms |
5060 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4017 ms |
2004 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: '40' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4030 ms |
3532 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |