#include "towers.h"
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
using namespace std;
struct SegTree{
int N;
vector<int>tree;
int merge(int a,int b){
return max(a,b);
}
void build(int n){
N=1;
while(N<n) N<<=1;
tree.assign(2*N,0);
}
void update(int v,int l,int r,int id,int x){
if(r-l==1){
tree[v]=x;
return;
}
int mid=(l+r)/2;
if(id<mid) update(v<<1,l,mid,id,x);
else update(v<<1|1,mid,r,id,x);
tree[v]=merge(tree[v<<1],tree[v<<1|1]);
}
void update(int id,int x){
return update(1,0,N,id,x);
}
int get(int v,int l,int r,int ql,int qr){
if(ql>=r || l>=qr) return 0;
if(l>=ql && qr>=r) return tree[v];
int mid=(l+r)/2;
return merge(get(v<<1,l,mid,ql,qr),get(v<<1|1,mid,r,ql,qr));
}
int get(int l,int r){
if(r<=l) return 0;
return get(1,0,N,l,r);
}
};
int n,k;
vector<int>h;
bool su1=true;
void init(int N, std::vector<int> H) {
n=N;
h=H;
k=0;
bool ok=true;
for(int i=1;i<n;i++){
if(h[i]>h[i-1] && ok) k=i;
else{
if(h[i]>h[i-1]) su1=false;
ok=false;
}
}
}
int max_towers(int L, int R, int D) {
if(su1){
if(L<k && R>k && h[k]-D>=h[L] && h[k]-D>=h[R]) return 2;
return 1;
}
vector<int>a,dp(R-L+1);
dp[0]=1;
for(int i=L;i<=R;i++){
a.push_back(h[i]);
}
// map<int,int>mp;
vector<int>b=a;
sort(b.begin(),b.end());
int c=b.size();
// for(int x:b) m[x]=c++;
SegTree S1,S2;
S1.build(c+2);
S2.build(c+2);
int ans=1,mx=0;
for(int i=0;i<a.size();i++){
int pos=lower_bound(b.begin(),b.end(),a[i]+D)-b.begin();
dp[i]=S2.get(pos,c+2);
dp[i]++;
ans=max(ans,dp[i]);
int pos2=lower_bound(b.begin(),b.end(),a[i])-b.begin();
S1.update(pos2,dp[i]);
int pos1=lower_bound(b.begin(),b.end(),a[i]-D+1)-b.begin();
S2.update(pos2,S1.get(0,pos1));
// cout<<dp[i]<<' ';
}
return ans;
}
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:95:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
towers.cpp:94:15: warning: unused variable 'mx' [-Wunused-variable]
94 | int ans=1,mx=0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
1112 KB |
Output is correct |
2 |
Correct |
641 ms |
1368 KB |
Output is correct |
3 |
Correct |
649 ms |
1368 KB |
Output is correct |
4 |
Correct |
639 ms |
1368 KB |
Output is correct |
5 |
Correct |
649 ms |
1368 KB |
Output is correct |
6 |
Correct |
597 ms |
1368 KB |
Output is correct |
7 |
Correct |
547 ms |
1368 KB |
Output is correct |
8 |
Correct |
0 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
436 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
436 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
20 ms |
1624 KB |
Output is correct |
37 |
Correct |
28 ms |
2776 KB |
Output is correct |
38 |
Correct |
16 ms |
1488 KB |
Output is correct |
39 |
Correct |
29 ms |
2000 KB |
Output is correct |
40 |
Correct |
18 ms |
1488 KB |
Output is correct |
41 |
Correct |
65 ms |
4304 KB |
Output is correct |
42 |
Correct |
16 ms |
1368 KB |
Output is correct |
43 |
Correct |
14 ms |
1600 KB |
Output is correct |
44 |
Correct |
8 ms |
1596 KB |
Output is correct |
45 |
Correct |
11 ms |
1368 KB |
Output is correct |
46 |
Correct |
8 ms |
1444 KB |
Output is correct |
47 |
Correct |
34 ms |
2768 KB |
Output is correct |
48 |
Correct |
23 ms |
2000 KB |
Output is correct |
49 |
Correct |
13 ms |
1368 KB |
Output is correct |
50 |
Correct |
9 ms |
1368 KB |
Output is correct |
51 |
Correct |
24 ms |
2740 KB |
Output is correct |
52 |
Correct |
95 ms |
4560 KB |
Output is correct |
53 |
Correct |
88 ms |
4560 KB |
Output is correct |
54 |
Correct |
88 ms |
4560 KB |
Output is correct |
55 |
Correct |
8 ms |
1368 KB |
Output is correct |
56 |
Correct |
49 ms |
4536 KB |
Output is correct |
57 |
Correct |
86 ms |
4308 KB |
Output is correct |
58 |
Correct |
84 ms |
4516 KB |
Output is correct |
59 |
Correct |
86 ms |
4560 KB |
Output is correct |
60 |
Correct |
103 ms |
4560 KB |
Output is correct |
61 |
Correct |
88 ms |
4556 KB |
Output is correct |
62 |
Correct |
80 ms |
4560 KB |
Output is correct |
63 |
Correct |
88 ms |
4572 KB |
Output is correct |
64 |
Correct |
8 ms |
1368 KB |
Output is correct |
65 |
Correct |
15 ms |
1600 KB |
Output is correct |
66 |
Correct |
46 ms |
4540 KB |
Output is correct |
67 |
Correct |
41 ms |
4560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4050 ms |
4808 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4091 ms |
1780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
436 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
20 ms |
1624 KB |
Output is correct |
37 |
Correct |
28 ms |
2776 KB |
Output is correct |
38 |
Correct |
16 ms |
1488 KB |
Output is correct |
39 |
Correct |
29 ms |
2000 KB |
Output is correct |
40 |
Correct |
18 ms |
1488 KB |
Output is correct |
41 |
Correct |
65 ms |
4304 KB |
Output is correct |
42 |
Correct |
16 ms |
1368 KB |
Output is correct |
43 |
Correct |
14 ms |
1600 KB |
Output is correct |
44 |
Correct |
8 ms |
1596 KB |
Output is correct |
45 |
Correct |
11 ms |
1368 KB |
Output is correct |
46 |
Correct |
8 ms |
1444 KB |
Output is correct |
47 |
Correct |
34 ms |
2768 KB |
Output is correct |
48 |
Correct |
23 ms |
2000 KB |
Output is correct |
49 |
Correct |
13 ms |
1368 KB |
Output is correct |
50 |
Correct |
9 ms |
1368 KB |
Output is correct |
51 |
Correct |
24 ms |
2740 KB |
Output is correct |
52 |
Correct |
95 ms |
4560 KB |
Output is correct |
53 |
Correct |
88 ms |
4560 KB |
Output is correct |
54 |
Correct |
88 ms |
4560 KB |
Output is correct |
55 |
Correct |
8 ms |
1368 KB |
Output is correct |
56 |
Correct |
49 ms |
4536 KB |
Output is correct |
57 |
Correct |
86 ms |
4308 KB |
Output is correct |
58 |
Correct |
84 ms |
4516 KB |
Output is correct |
59 |
Correct |
86 ms |
4560 KB |
Output is correct |
60 |
Correct |
103 ms |
4560 KB |
Output is correct |
61 |
Correct |
88 ms |
4556 KB |
Output is correct |
62 |
Correct |
80 ms |
4560 KB |
Output is correct |
63 |
Correct |
88 ms |
4572 KB |
Output is correct |
64 |
Correct |
8 ms |
1368 KB |
Output is correct |
65 |
Correct |
15 ms |
1600 KB |
Output is correct |
66 |
Correct |
46 ms |
4540 KB |
Output is correct |
67 |
Correct |
41 ms |
4560 KB |
Output is correct |
68 |
Execution timed out |
4050 ms |
4808 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
1112 KB |
Output is correct |
2 |
Correct |
641 ms |
1368 KB |
Output is correct |
3 |
Correct |
649 ms |
1368 KB |
Output is correct |
4 |
Correct |
639 ms |
1368 KB |
Output is correct |
5 |
Correct |
649 ms |
1368 KB |
Output is correct |
6 |
Correct |
597 ms |
1368 KB |
Output is correct |
7 |
Correct |
547 ms |
1368 KB |
Output is correct |
8 |
Correct |
0 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
356 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
436 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
2 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
2 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
1 ms |
344 KB |
Output is correct |
46 |
Correct |
20 ms |
1624 KB |
Output is correct |
47 |
Correct |
28 ms |
2776 KB |
Output is correct |
48 |
Correct |
16 ms |
1488 KB |
Output is correct |
49 |
Correct |
29 ms |
2000 KB |
Output is correct |
50 |
Correct |
18 ms |
1488 KB |
Output is correct |
51 |
Correct |
65 ms |
4304 KB |
Output is correct |
52 |
Correct |
16 ms |
1368 KB |
Output is correct |
53 |
Correct |
14 ms |
1600 KB |
Output is correct |
54 |
Correct |
8 ms |
1596 KB |
Output is correct |
55 |
Correct |
11 ms |
1368 KB |
Output is correct |
56 |
Correct |
8 ms |
1444 KB |
Output is correct |
57 |
Correct |
34 ms |
2768 KB |
Output is correct |
58 |
Correct |
23 ms |
2000 KB |
Output is correct |
59 |
Correct |
13 ms |
1368 KB |
Output is correct |
60 |
Correct |
9 ms |
1368 KB |
Output is correct |
61 |
Correct |
24 ms |
2740 KB |
Output is correct |
62 |
Correct |
95 ms |
4560 KB |
Output is correct |
63 |
Correct |
88 ms |
4560 KB |
Output is correct |
64 |
Correct |
88 ms |
4560 KB |
Output is correct |
65 |
Correct |
8 ms |
1368 KB |
Output is correct |
66 |
Correct |
49 ms |
4536 KB |
Output is correct |
67 |
Correct |
86 ms |
4308 KB |
Output is correct |
68 |
Correct |
84 ms |
4516 KB |
Output is correct |
69 |
Correct |
86 ms |
4560 KB |
Output is correct |
70 |
Correct |
103 ms |
4560 KB |
Output is correct |
71 |
Correct |
88 ms |
4556 KB |
Output is correct |
72 |
Correct |
80 ms |
4560 KB |
Output is correct |
73 |
Correct |
88 ms |
4572 KB |
Output is correct |
74 |
Correct |
8 ms |
1368 KB |
Output is correct |
75 |
Correct |
15 ms |
1600 KB |
Output is correct |
76 |
Correct |
46 ms |
4540 KB |
Output is correct |
77 |
Correct |
41 ms |
4560 KB |
Output is correct |
78 |
Execution timed out |
4050 ms |
4808 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |