#include "towers.h"
#include <vector>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
namespace {
int N, K;
int H[100005];
}
void init(int N, std::vector<int> H) {
::N = N;
::K = -1;
for (int i = 0; i < N; i++) {
::H[i] = H[i];
}
}
int dp[100005];
struct maxseg
{
vector<int> tree;
maxseg(int n)
{
int sz = 1 << ((int)ceil(log2(n))+1);
tree.resize(sz);
}
int init(int s, int e, int node, vector<int> &vc)
{
if (s == e) return tree[node] = vc[s];
else return tree[node] = max(init(s, (s+e)/2, 2*node, vc), init((s+e)/2+1, e, 2*node+1, vc));
}
void upd(int s, int e, int node, int id, int val)
{
if (e < id || id < s) return;
if (s == e) {
tree[node] = val;
return;
}
upd(s, (s+e)/2, 2*node, id, val);
upd((s+e)/2+1, e, 2*node+1, id, val);
tree[node] = max(tree[2*node], tree[2*node+1]);
}
int query(int s, int e, int node, int l, int r)
{
if (e < l || r < s) return 0;
if (l <= s && e <= r) return tree[node];
return max(query(s, (s+e)/2, 2*node, l, r), query((s+e)/2+1, e, 2*node+1, l, r));
}
};
int max_towers(int L, int R, int D) {
int M = R - L + 1;
vector<int> A(M);
for (int i = L; i <= R; i++) A[i - L] = H[i];
maxseg seg(M), seg2(M);
seg.init(0, M-1, 1, A);
dp[0] = 1;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push(make_pair(A[0], 0));
for (int i = 1; i < M; i++) {
//키가 A[i] - D 이하인 애들을 모두 해방시키자
while (!pq.empty() && pq.top().first <= A[i] - D) {
seg2.upd(0, M-1, 1, pq.top().second, dp[pq.top().second]);
pq.pop();
}
int l = 0, r = i - 1; //max(k ... i) >= A[i] + D인 최대 k 찾기
while (l < r) {
int m = l + r + 1 >> 1;
if (seg.query(0, M-1, 1, m, i) >= A[i] + D) {
l = m;
}
else r = m - 1;
}
if (seg.query(0, M-1, 1, l, i) < A[i] + D) l = -1;
dp[i] = seg2.query(0, M-1, 1, 0, l) + 1;
pq.push(make_pair(A[i], i));
}
int ans = 0;
for (int i = 0; i < M; i++) ans = max(ans, dp[i]);
return ans;
//0부터 M-1까지 A에서 문제 풀기
}
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:70:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | int m = l + r + 1 >> 1;
| ~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4069 ms |
3088 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
4 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
284 KB |
Output is correct |
4 |
Correct |
3 ms |
336 KB |
Output is correct |
5 |
Correct |
4 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
208 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
3 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
4 ms |
336 KB |
Output is correct |
21 |
Correct |
4 ms |
336 KB |
Output is correct |
22 |
Correct |
6 ms |
336 KB |
Output is correct |
23 |
Correct |
4 ms |
336 KB |
Output is correct |
24 |
Correct |
3 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
3 ms |
336 KB |
Output is correct |
27 |
Correct |
4 ms |
336 KB |
Output is correct |
28 |
Correct |
4 ms |
336 KB |
Output is correct |
29 |
Correct |
3 ms |
336 KB |
Output is correct |
30 |
Correct |
5 ms |
396 KB |
Output is correct |
31 |
Correct |
3 ms |
336 KB |
Output is correct |
32 |
Correct |
4 ms |
336 KB |
Output is correct |
33 |
Correct |
4 ms |
336 KB |
Output is correct |
34 |
Correct |
5 ms |
336 KB |
Output is correct |
35 |
Correct |
4 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
4 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
284 KB |
Output is correct |
4 |
Correct |
3 ms |
336 KB |
Output is correct |
5 |
Correct |
4 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
208 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
3 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
4 ms |
336 KB |
Output is correct |
21 |
Correct |
4 ms |
336 KB |
Output is correct |
22 |
Correct |
6 ms |
336 KB |
Output is correct |
23 |
Correct |
4 ms |
336 KB |
Output is correct |
24 |
Correct |
3 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
3 ms |
336 KB |
Output is correct |
27 |
Correct |
4 ms |
336 KB |
Output is correct |
28 |
Correct |
4 ms |
336 KB |
Output is correct |
29 |
Correct |
3 ms |
336 KB |
Output is correct |
30 |
Correct |
5 ms |
396 KB |
Output is correct |
31 |
Correct |
3 ms |
336 KB |
Output is correct |
32 |
Correct |
4 ms |
336 KB |
Output is correct |
33 |
Correct |
4 ms |
336 KB |
Output is correct |
34 |
Correct |
5 ms |
336 KB |
Output is correct |
35 |
Correct |
4 ms |
336 KB |
Output is correct |
36 |
Correct |
55 ms |
1472 KB |
Output is correct |
37 |
Correct |
100 ms |
2868 KB |
Output is correct |
38 |
Correct |
67 ms |
1484 KB |
Output is correct |
39 |
Correct |
74 ms |
2224 KB |
Output is correct |
40 |
Correct |
36 ms |
1472 KB |
Output is correct |
41 |
Correct |
233 ms |
4624 KB |
Output is correct |
42 |
Correct |
15 ms |
1448 KB |
Output is correct |
43 |
Correct |
179 ms |
2904 KB |
Output is correct |
44 |
Correct |
36 ms |
1640 KB |
Output is correct |
45 |
Correct |
16 ms |
1448 KB |
Output is correct |
46 |
Correct |
24 ms |
1456 KB |
Output is correct |
47 |
Correct |
100 ms |
2348 KB |
Output is correct |
48 |
Correct |
64 ms |
1704 KB |
Output is correct |
49 |
Correct |
26 ms |
1344 KB |
Output is correct |
50 |
Correct |
57 ms |
2076 KB |
Output is correct |
51 |
Correct |
114 ms |
2404 KB |
Output is correct |
52 |
Correct |
278 ms |
3916 KB |
Output is correct |
53 |
Correct |
297 ms |
3912 KB |
Output is correct |
54 |
Correct |
298 ms |
3880 KB |
Output is correct |
55 |
Correct |
285 ms |
5424 KB |
Output is correct |
56 |
Correct |
313 ms |
3908 KB |
Output is correct |
57 |
Correct |
306 ms |
4812 KB |
Output is correct |
58 |
Correct |
314 ms |
4696 KB |
Output is correct |
59 |
Correct |
292 ms |
4256 KB |
Output is correct |
60 |
Correct |
307 ms |
3912 KB |
Output is correct |
61 |
Correct |
286 ms |
4420 KB |
Output is correct |
62 |
Correct |
290 ms |
4736 KB |
Output is correct |
63 |
Correct |
280 ms |
4232 KB |
Output is correct |
64 |
Correct |
250 ms |
4988 KB |
Output is correct |
65 |
Correct |
265 ms |
5236 KB |
Output is correct |
66 |
Correct |
247 ms |
4344 KB |
Output is correct |
67 |
Correct |
302 ms |
5268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4089 ms |
3952 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4078 ms |
1664 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
4 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
284 KB |
Output is correct |
4 |
Correct |
3 ms |
336 KB |
Output is correct |
5 |
Correct |
4 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
208 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
3 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
4 ms |
336 KB |
Output is correct |
21 |
Correct |
4 ms |
336 KB |
Output is correct |
22 |
Correct |
6 ms |
336 KB |
Output is correct |
23 |
Correct |
4 ms |
336 KB |
Output is correct |
24 |
Correct |
3 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
3 ms |
336 KB |
Output is correct |
27 |
Correct |
4 ms |
336 KB |
Output is correct |
28 |
Correct |
4 ms |
336 KB |
Output is correct |
29 |
Correct |
3 ms |
336 KB |
Output is correct |
30 |
Correct |
5 ms |
396 KB |
Output is correct |
31 |
Correct |
3 ms |
336 KB |
Output is correct |
32 |
Correct |
4 ms |
336 KB |
Output is correct |
33 |
Correct |
4 ms |
336 KB |
Output is correct |
34 |
Correct |
5 ms |
336 KB |
Output is correct |
35 |
Correct |
4 ms |
336 KB |
Output is correct |
36 |
Correct |
55 ms |
1472 KB |
Output is correct |
37 |
Correct |
100 ms |
2868 KB |
Output is correct |
38 |
Correct |
67 ms |
1484 KB |
Output is correct |
39 |
Correct |
74 ms |
2224 KB |
Output is correct |
40 |
Correct |
36 ms |
1472 KB |
Output is correct |
41 |
Correct |
233 ms |
4624 KB |
Output is correct |
42 |
Correct |
15 ms |
1448 KB |
Output is correct |
43 |
Correct |
179 ms |
2904 KB |
Output is correct |
44 |
Correct |
36 ms |
1640 KB |
Output is correct |
45 |
Correct |
16 ms |
1448 KB |
Output is correct |
46 |
Correct |
24 ms |
1456 KB |
Output is correct |
47 |
Correct |
100 ms |
2348 KB |
Output is correct |
48 |
Correct |
64 ms |
1704 KB |
Output is correct |
49 |
Correct |
26 ms |
1344 KB |
Output is correct |
50 |
Correct |
57 ms |
2076 KB |
Output is correct |
51 |
Correct |
114 ms |
2404 KB |
Output is correct |
52 |
Correct |
278 ms |
3916 KB |
Output is correct |
53 |
Correct |
297 ms |
3912 KB |
Output is correct |
54 |
Correct |
298 ms |
3880 KB |
Output is correct |
55 |
Correct |
285 ms |
5424 KB |
Output is correct |
56 |
Correct |
313 ms |
3908 KB |
Output is correct |
57 |
Correct |
306 ms |
4812 KB |
Output is correct |
58 |
Correct |
314 ms |
4696 KB |
Output is correct |
59 |
Correct |
292 ms |
4256 KB |
Output is correct |
60 |
Correct |
307 ms |
3912 KB |
Output is correct |
61 |
Correct |
286 ms |
4420 KB |
Output is correct |
62 |
Correct |
290 ms |
4736 KB |
Output is correct |
63 |
Correct |
280 ms |
4232 KB |
Output is correct |
64 |
Correct |
250 ms |
4988 KB |
Output is correct |
65 |
Correct |
265 ms |
5236 KB |
Output is correct |
66 |
Correct |
247 ms |
4344 KB |
Output is correct |
67 |
Correct |
302 ms |
5268 KB |
Output is correct |
68 |
Execution timed out |
4089 ms |
3952 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4069 ms |
3088 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |