#include <bits/stdc++.h>
using namespace std;
using ind = long long;
using cind = const ind;
#define FOR(i,l,r) for(int i = (l); i <= (r); i++)
#define FORD(i,l,r) for(int i = (l); i >= (r); i--)
ind v[1'000'001];
ind next1[1'000'001];
vector<ind> children[1'000'001];
stack<ind> s;
void step(ind i){
while(true){
if(s.size()==0) break;
if(v[s.top()] >= v[i]) break;
if(next1[s.top()]==0){ next1[s.top()] = i;
children[i].push_back(s.top());
}
s.pop();
}
s.push(i);
}
ind dfs(ind k){
ind wyn=0;
for(auto c : children[k]) wyn = max(wyn,dfs(c));
return wyn + 1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ind N;
cin >> N;
FOR(i,1,N) cin >> v[i];
FOR(i,1,N) step(i);
FOR(i,1,N) step(i);
ind wyn=0;
FOR(i,1,N) if(next1[i]==0) wyn = max(wyn,dfs(i));
cout << wyn << endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <bits/stdc++.h> using namespace std; using ind = long long; using cind = const ind; #define FOR(i,l,r) for(int i = (l); i <= (r); i++) #define FORD(i,l,r) for(int i = (l); i >= (r); i--) ind v[1'000'001]; ind next1[1'000'001]; vector<ind> children[1'000'001]; stack<ind> s; void step(ind i){ while(true){ if(s.size()==0) break; if(v[s.top()] >= v[i]) break; if(next1[s.top()]==0){ next1[s.top()] = i; children[i].push_back(s.top()); } s.pop(); } s.push(i); } ind dfs(ind k){ ind wyn=0; for(auto c : children[k]) wyn = max(wyn,dfs(c)); return wyn + 1; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ind N; cin >> N; FOR(i,1,N) cin >> v[i]; FOR(i,1,N) step(i); FOR(i,1,N) step(i); ind wyn=0; FOR(i,1,N) if(next1[i]==0) wyn = max(wyn,dfs(i)); cout << wyn << endl; } |
English