#include "towers.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
using ll = int;
const ll big = 1e9+10;
ll n;
vector<ll> h;
//ll mx;
vector<ll> ps;
vector<pair<ll,ll>> rgs;
struct obj{
ll v,c;
bool operator<(const obj &o) const{
return v<o.v;
}
bool operator>(const obj &o) const{
return v>o.v;
}
};
void init(int N, std::vector<int> H) {
n = N;
h = H;
//for(ll i = 0;i<n;i++) if((i==0||(h[i-1]<h[i]))&&(i==n-1||h[i]>h[i+1])) mx = i;
//for(ll i = 1;i<n-1;i++){
// if (h[i-1]<h[i]&&h[i]>h[i+1]){
// ps.push_back(i);
// }
//}
//for(auto [u,v] : ps) cerr << "[" << u << "," << v << "]" << " ";cerr << "\n";
}
bool ps_inited = false;
void init_ps(ll d){
ps_inited = true;
vector<ll> l(n,0),r(n,0);
vector<ll> ld(n,big),rd(n,big);
{
vector<pair<ll,ll>> v;
v.push_back({h[0],0});
for(ll i = 1;i<n;i++){
auto it = lower_bound(v.begin(),v.end(),make_pair(h[i]-d+1,0));
if (it!=v.begin()) ld[i] = i-(*--it).second;
while(v.size()&&v.back().first>h[i]) v.pop_back();
v.push_back({h[i],i});
}
}
{
vector<pair<ll,ll>> v;
v.push_back({h[n-1],n-1});
for(ll i = n-2;i>=0;i--){
auto it = lower_bound(v.begin(),v.end(),make_pair(h[i]-d+1,0));
if (it!=v.begin()) rd[i] = (*--it).second-i;
while(v.size()&&v.back().first>h[i]) v.pop_back();
v.push_back({h[i],i});
}
}
{
vector<pair<ll,ll>> v;
v.push_back({h[0],h[0]});
for(ll i = 1;i<n;i++){
ll mi = h[i];
while(v.size()&&v.back().first<h[i]){
mi = min(mi,v.back().second);
v.pop_back();
}
if (mi<=h[i]-d) l[i] = 1;
v.push_back({h[i],mi});
}
}
{
vector<pair<ll,ll>> v;
v.push_back({h[n-1],h[n-1]});
for(ll i = n-2;i>=0;i--){
ll mi = h[i];
while(v.size()&&v.back().first<h[i]){
mi = min(mi,v.back().second);
v.pop_back();
}
if (mi<=h[i]-d) r[i] = 1;
v.push_back({h[i],mi});
}
}
for(ll i = 0;i<n;i++) if(l[i]&&r[i]) {
ps.push_back(i);
rgs.push_back({i-ld[i],i+rd[i]});
//cerr << i-ld[i] << " " << i << " " << i+rd[i] << "\n";
}
}
int max_towers(int L, int R, int D) {
if(!ps_inited) init_ps(D);
ll l = L, r = R, d = D;
ll ans = 1;
//auto it0 = lower_bound(ps.begin(),ps.end(),r);
//ll rf = it0-ps.begin();
//auto it1 = lower_bound(ps.begin(),ps.end(),l+1);
//ll lf = it1-ps.begin();
//cerr << lf << " " << rf << "\n";
//ans = max(0,rf-lf)+1;
auto it0 = lower_bound(rgs.begin(),rgs.end(),make_pair(r,0));
//if (it0!=rgs.begin()) --it0;
ll rf = it0-rgs.begin();
if (it0!=rgs.begin()){
if ((*--it0).second>r) --rf;
}
auto it1 = lower_bound(rgs.begin(),rgs.end(),make_pair(l,0));
ll lf = it1-rgs.begin();
ans = max(0,rf-lf)+1;
return ans;
/*vector<obj> a,b;
a.push_back({0,0});
b.push_back({big,0});
for(ll i = l;i<=r;i++){
ll cur = 0;
auto it = lower_bound(b.begin(),b.end(),obj({h[i]-1,0}),greater<obj>());
--it;
cur = (*it).c;
ans = max(ans,cur+1);
obj oa = {h[i],cur+1};
if (a.size()<=oa.c) a.push_back(oa);
else a[oa.c] = min(a[oa.c],oa);
auto it0 = lower_bound(a.begin(),a.end(),obj({h[i]-d+1,0}));
--it0;
obj ob = {h[i]-d,(*it0).c};
if (b.size()<=ob.c) b.push_back(ob);
else b[ob.c] = max(b[ob.c],ob);
ans = max(ans,cur+1);
//cerr << "a:";for(auto [u,v] : a) cerr << "(" << u <<"," << v << ") ";cerr << "\n";
//cerr << "b:";for(auto [u,v] : b) cerr << "(" << u <<"," << v << ") ";cerr << "\n";
}*/
return ans;
}
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:94:20: warning: unused variable 'd' [-Wunused-variable]
94 | ll l = L, r = R, d = D;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
308 ms |
2536 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
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 |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 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 |
0 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 |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
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 |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 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 |
0 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 |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 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 |
15 ms |
2392 KB |
Output is correct |
37 |
Correct |
15 ms |
2768 KB |
Output is correct |
38 |
Correct |
17 ms |
3280 KB |
Output is correct |
39 |
Correct |
14 ms |
2768 KB |
Output is correct |
40 |
Correct |
15 ms |
3792 KB |
Output is correct |
41 |
Correct |
15 ms |
3280 KB |
Output is correct |
42 |
Correct |
14 ms |
3792 KB |
Output is correct |
43 |
Correct |
18 ms |
4540 KB |
Output is correct |
44 |
Correct |
17 ms |
4540 KB |
Output is correct |
45 |
Correct |
17 ms |
4060 KB |
Output is correct |
46 |
Correct |
25 ms |
3564 KB |
Output is correct |
47 |
Correct |
21 ms |
3536 KB |
Output is correct |
48 |
Correct |
20 ms |
3768 KB |
Output is correct |
49 |
Correct |
14 ms |
3792 KB |
Output is correct |
50 |
Correct |
15 ms |
4516 KB |
Output is correct |
51 |
Correct |
14 ms |
4556 KB |
Output is correct |
52 |
Correct |
15 ms |
3536 KB |
Output is correct |
53 |
Correct |
20 ms |
3792 KB |
Output is correct |
54 |
Correct |
15 ms |
3796 KB |
Output is correct |
55 |
Correct |
15 ms |
4512 KB |
Output is correct |
56 |
Correct |
15 ms |
4168 KB |
Output is correct |
57 |
Correct |
15 ms |
2780 KB |
Output is correct |
58 |
Correct |
24 ms |
3016 KB |
Output is correct |
59 |
Correct |
24 ms |
3280 KB |
Output is correct |
60 |
Correct |
14 ms |
3792 KB |
Output is correct |
61 |
Correct |
22 ms |
3764 KB |
Output is correct |
62 |
Correct |
21 ms |
3252 KB |
Output is correct |
63 |
Correct |
15 ms |
3816 KB |
Output is correct |
64 |
Correct |
17 ms |
4540 KB |
Output is correct |
65 |
Correct |
21 ms |
4540 KB |
Output is correct |
66 |
Correct |
23 ms |
3556 KB |
Output is correct |
67 |
Correct |
16 ms |
4332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
531 ms |
3516 KB |
Output is correct |
2 |
Correct |
607 ms |
3536 KB |
Output is correct |
3 |
Correct |
544 ms |
3792 KB |
Output is correct |
4 |
Correct |
700 ms |
3756 KB |
Output is correct |
5 |
Correct |
679 ms |
3804 KB |
Output is correct |
6 |
Correct |
714 ms |
3792 KB |
Output is correct |
7 |
Correct |
577 ms |
3760 KB |
Output is correct |
8 |
Correct |
689 ms |
4408 KB |
Output is correct |
9 |
Correct |
652 ms |
4540 KB |
Output is correct |
10 |
Correct |
698 ms |
4556 KB |
Output is correct |
11 |
Correct |
584 ms |
4556 KB |
Output is correct |
12 |
Correct |
646 ms |
4548 KB |
Output is correct |
13 |
Correct |
589 ms |
4516 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 |
340 KB |
Output is correct |
17 |
Correct |
19 ms |
3760 KB |
Output is correct |
18 |
Correct |
23 ms |
3792 KB |
Output is correct |
19 |
Correct |
13 ms |
3792 KB |
Output is correct |
20 |
Correct |
13 ms |
4352 KB |
Output is correct |
21 |
Correct |
14 ms |
4556 KB |
Output is correct |
22 |
Correct |
15 ms |
3528 KB |
Output is correct |
23 |
Correct |
20 ms |
3792 KB |
Output is correct |
24 |
Correct |
16 ms |
3792 KB |
Output is correct |
25 |
Correct |
18 ms |
4540 KB |
Output is correct |
26 |
Correct |
19 ms |
4168 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 |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
182 ms |
1112 KB |
2nd lines differ - on the 1st token, expected: '7063', found: '7197' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
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 |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 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 |
0 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 |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 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 |
15 ms |
2392 KB |
Output is correct |
37 |
Correct |
15 ms |
2768 KB |
Output is correct |
38 |
Correct |
17 ms |
3280 KB |
Output is correct |
39 |
Correct |
14 ms |
2768 KB |
Output is correct |
40 |
Correct |
15 ms |
3792 KB |
Output is correct |
41 |
Correct |
15 ms |
3280 KB |
Output is correct |
42 |
Correct |
14 ms |
3792 KB |
Output is correct |
43 |
Correct |
18 ms |
4540 KB |
Output is correct |
44 |
Correct |
17 ms |
4540 KB |
Output is correct |
45 |
Correct |
17 ms |
4060 KB |
Output is correct |
46 |
Correct |
25 ms |
3564 KB |
Output is correct |
47 |
Correct |
21 ms |
3536 KB |
Output is correct |
48 |
Correct |
20 ms |
3768 KB |
Output is correct |
49 |
Correct |
14 ms |
3792 KB |
Output is correct |
50 |
Correct |
15 ms |
4516 KB |
Output is correct |
51 |
Correct |
14 ms |
4556 KB |
Output is correct |
52 |
Correct |
15 ms |
3536 KB |
Output is correct |
53 |
Correct |
20 ms |
3792 KB |
Output is correct |
54 |
Correct |
15 ms |
3796 KB |
Output is correct |
55 |
Correct |
15 ms |
4512 KB |
Output is correct |
56 |
Correct |
15 ms |
4168 KB |
Output is correct |
57 |
Correct |
15 ms |
2780 KB |
Output is correct |
58 |
Correct |
24 ms |
3016 KB |
Output is correct |
59 |
Correct |
24 ms |
3280 KB |
Output is correct |
60 |
Correct |
14 ms |
3792 KB |
Output is correct |
61 |
Correct |
22 ms |
3764 KB |
Output is correct |
62 |
Correct |
21 ms |
3252 KB |
Output is correct |
63 |
Correct |
15 ms |
3816 KB |
Output is correct |
64 |
Correct |
17 ms |
4540 KB |
Output is correct |
65 |
Correct |
21 ms |
4540 KB |
Output is correct |
66 |
Correct |
23 ms |
3556 KB |
Output is correct |
67 |
Correct |
16 ms |
4332 KB |
Output is correct |
68 |
Correct |
531 ms |
3516 KB |
Output is correct |
69 |
Correct |
607 ms |
3536 KB |
Output is correct |
70 |
Correct |
544 ms |
3792 KB |
Output is correct |
71 |
Correct |
700 ms |
3756 KB |
Output is correct |
72 |
Correct |
679 ms |
3804 KB |
Output is correct |
73 |
Correct |
714 ms |
3792 KB |
Output is correct |
74 |
Correct |
577 ms |
3760 KB |
Output is correct |
75 |
Correct |
689 ms |
4408 KB |
Output is correct |
76 |
Correct |
652 ms |
4540 KB |
Output is correct |
77 |
Correct |
698 ms |
4556 KB |
Output is correct |
78 |
Correct |
584 ms |
4556 KB |
Output is correct |
79 |
Correct |
646 ms |
4548 KB |
Output is correct |
80 |
Correct |
589 ms |
4516 KB |
Output is correct |
81 |
Correct |
1 ms |
344 KB |
Output is correct |
82 |
Correct |
1 ms |
344 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
19 ms |
3760 KB |
Output is correct |
85 |
Correct |
23 ms |
3792 KB |
Output is correct |
86 |
Correct |
13 ms |
3792 KB |
Output is correct |
87 |
Correct |
13 ms |
4352 KB |
Output is correct |
88 |
Correct |
14 ms |
4556 KB |
Output is correct |
89 |
Correct |
15 ms |
3528 KB |
Output is correct |
90 |
Correct |
20 ms |
3792 KB |
Output is correct |
91 |
Correct |
16 ms |
3792 KB |
Output is correct |
92 |
Correct |
18 ms |
4540 KB |
Output is correct |
93 |
Correct |
19 ms |
4168 KB |
Output is correct |
94 |
Correct |
1 ms |
344 KB |
Output is correct |
95 |
Correct |
1 ms |
344 KB |
Output is correct |
96 |
Correct |
1 ms |
344 KB |
Output is correct |
97 |
Correct |
1 ms |
344 KB |
Output is correct |
98 |
Correct |
1 ms |
344 KB |
Output is correct |
99 |
Correct |
0 ms |
344 KB |
Output is correct |
100 |
Correct |
1 ms |
344 KB |
Output is correct |
101 |
Correct |
0 ms |
344 KB |
Output is correct |
102 |
Correct |
0 ms |
344 KB |
Output is correct |
103 |
Correct |
1 ms |
344 KB |
Output is correct |
104 |
Correct |
575 ms |
3068 KB |
Output is correct |
105 |
Correct |
644 ms |
3280 KB |
Output is correct |
106 |
Correct |
727 ms |
3024 KB |
Output is correct |
107 |
Correct |
595 ms |
3280 KB |
Output is correct |
108 |
Correct |
670 ms |
2768 KB |
Output is correct |
109 |
Correct |
711 ms |
3280 KB |
Output is correct |
110 |
Correct |
671 ms |
3792 KB |
Output is correct |
111 |
Correct |
675 ms |
4540 KB |
Output is correct |
112 |
Correct |
641 ms |
4520 KB |
Output is correct |
113 |
Correct |
638 ms |
4364 KB |
Output is correct |
114 |
Correct |
630 ms |
4280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
308 ms |
2536 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |