#include <iostream>
#include <vector>
#include <stack>
using namespace std;
stack<int>s;
int tab[1000010];
/*int binsearch(int x){
int kon=v.size()-1;
int poc=0;
while(kon-poc>5){
int mid=(poc+kon)/2;
if(v[mid]>=x){
poc=mid;
}else{
kon=mid;
}
}
for(int i=poc;i<=kon;i++){
if(v[i]>x){
return i;
}
}
}*/
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
long long wynik=0;
pair<int,int>maxi={0,-1};
for(int i=0;i<n;i++){
cin>>tab[i];
if(tab[i]>maxi.first){
maxi={tab[i],i};
}
}
s.push(maxi.first);
for(int i=maxi.second-1;i>=0;i--){
while(tab[i]>s.top()){
s.pop();
}
if(tab[i]<s.top()){
s.push(tab[i]);
}
wynik=max(wynik,(long long)s.size());
}
for(int i=n-1;i>maxi.second;i--){
while(tab[i]>s.top()){
s.pop();
}
if(tab[i]<s.top()){
s.push(tab[i]);
}
wynik=max(wynik,(long long)s.size());
}
cout<<wynik;
return 0;
}
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> #include <vector> #include <stack> using namespace std; stack<int>s; int tab[1000010]; /*int binsearch(int x){ int kon=v.size()-1; int poc=0; while(kon-poc>5){ int mid=(poc+kon)/2; if(v[mid]>=x){ poc=mid; }else{ kon=mid; } } for(int i=poc;i<=kon;i++){ if(v[i]>x){ return i; } } }*/ int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; long long wynik=0; pair<int,int>maxi={0,-1}; for(int i=0;i<n;i++){ cin>>tab[i]; if(tab[i]>maxi.first){ maxi={tab[i],i}; } } s.push(maxi.first); for(int i=maxi.second-1;i>=0;i--){ while(tab[i]>s.top()){ s.pop(); } if(tab[i]<s.top()){ s.push(tab[i]); } wynik=max(wynik,(long long)s.size()); } for(int i=n-1;i>maxi.second;i--){ while(tab[i]>s.top()){ s.pop(); } if(tab[i]<s.top()){ s.push(tab[i]); } wynik=max(wynik,(long long)s.size()); } cout<<wynik; return 0; } |
English