#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int N = 2e6+1;
int inc[N],A[N],n,ansv[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int max_ans = 0,pref_max = 0;
cin >> n;
A[0] = N;
for(int i = 1;i <= n;i++){
cin >> A[i];
if(A[i] > pref_max){
max_ans++;
pref_max = A[i];
}
}
for(int i = n + 1;i <= 2*n;i++)A[i] = A[i-n];
stack<int>st;
st.push(0);
for(int i = 1;i <= 2*n;i++){
while(A[st.top()] < A[i])st.pop();
ansv[i] = st.top();
if(i <= n)inc[st.top()]++;
st.push(i);
}
//for(int i = 1;i <= n;i++)cout << inc[i] << " ";
int current_ans = max_ans;
for(int i = 1;i <= n;i++){
current_ans--;
current_ans += inc[i];
if(ansv[i+n] <= i)current_ans++;
else inc[ansv[i+n]]++;
max_ans = max(max_ans,current_ans);
}
cout << max_ans;
}
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 | #include<bits/stdc++.h> #define ll long long #define pb push_back #define pii pair<int,int> using namespace std; const int N = 2e6+1; int inc[N],A[N],n,ansv[N]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int max_ans = 0,pref_max = 0; cin >> n; A[0] = N; for(int i = 1;i <= n;i++){ cin >> A[i]; if(A[i] > pref_max){ max_ans++; pref_max = A[i]; } } for(int i = n + 1;i <= 2*n;i++)A[i] = A[i-n]; stack<int>st; st.push(0); for(int i = 1;i <= 2*n;i++){ while(A[st.top()] < A[i])st.pop(); ansv[i] = st.top(); if(i <= n)inc[st.top()]++; st.push(i); } //for(int i = 1;i <= n;i++)cout << inc[i] << " "; int current_ans = max_ans; for(int i = 1;i <= n;i++){ current_ans--; current_ans += inc[i]; if(ansv[i+n] <= i)current_ans++; else inc[ansv[i+n]]++; max_ans = max(max_ans,current_ans); } cout << max_ans; } |
English