#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const int INF = 1e9+7;
int N, A[MAXN+10], B[MAXN+10], P[MAXN+10];
struct SEG
{
int tree[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
tree[node]=A[tl];
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node]=min(tree[node*2], tree[node*2+1]);
}
int query(int node, int tl, int tr, int l, int r)
{
if(r<tl || tr<l) return INF;
if(l<=tl && tr<=r) return tree[node];
int mid=tl+tr>>1;
return min(query(node*2, tl, mid, l, r), query(node*2+1, mid+1, tr, l, r));
}
}seg;
void init(int _N, vector<int> _H)
{
N=_N;
for(int i=1; i<=N; i++) A[i]=_H[i-1];
seg.init(1, 1, N);
vector<int> V;
V.push_back(1);
for(int i=2; i<=N; i++)
{
if(V.size()%2)
{
if(A[V.back()]<A[i]) V.push_back(i);
else V.back()=i;
}
else
{
if(A[V.back()]>A[i]) V.push_back(i);
else V.back()=i;
}
}
if(V.size()%2==0) V.pop_back();
set<int> S;
set<pii> S2;
for(int i=0; i<V.size(); i++)
{
S.insert(V[i]);
if(i%2==0) B[V[i]]=1;
else B[V[i]]=2;
}
for(int i=0; i+1<V.size(); i++) S2.insert({abs(A[V[i+1]]-A[V[i]]), V[i]});
while(!S2.empty())
{
auto [val, p] = *S2.begin(); S2.erase(S2.begin());
auto it=S.find(p);
int l=-1, r=-1;
if(it!=S.begin())
{
int t1=*prev(it), t2=*it;
S2.erase({abs(A[t1]-A[t2]), t1});
l=t1;
}
if(next(next(it))!=S.end())
{
int t1=*next(it), t2=*next(next(it));
S2.erase({abs(A[t1]-A[t2]), t1});
r=t2;
}
if(l!=-1 && r!=-1) S2.insert({abs(A[r]-A[l]), l});
P[*next(it)]=val; P[*it]=val;
S.erase(*next(it));
S.erase(it);
}
}
int max_towers(int L, int R, int D)
{
L++; R++;
vector<int> V;
for(int i=L; i<=R; i++) if(B[i] && P[i]>=D) V.push_back(i);
if(V.empty()) return 1;
int ans=V.size();
if(B[V[0]]==2 && seg.query(1, 1, N, L, V[0]-1)<=A[V[0]]-D) ans++;
if(B[V.back()]==2 && seg.query(1, 1, N, V.back()+1, R)<=A[V.back()]-D) ans++;
return ans/2+1;
}
Compilation message
towers.cpp: In member function 'void SEG::init(int, int, int)':
towers.cpp:24:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | int mid=tl+tr>>1;
| ~~^~~
towers.cpp: In member function 'int SEG::query(int, int, int, int, int)':
towers.cpp:33:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int mid=tl+tr>>1;
| ~~^~~
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:63:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i=0; i<V.size(); i++)
| ~^~~~~~~~~
towers.cpp:69:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i=0; i+1<V.size(); i++) S2.insert({abs(A[V[i+1]]-A[V[i]]), V[i]});
| ~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2610 ms |
1456 KB |
2nd lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '14' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '14' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4027 ms |
9628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4021 ms |
2476 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '14' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2610 ms |
1456 KB |
2nd lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |