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
| #include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; #define int ll #define REP(i,e,s) for(register int i=e; i<=s; i++) #define DREP(i,e,s) for(register int i=e; i>=s; i--) #define ll long long #define DE(...) fprintf(stderr,__VA_ARGS__) #define DEBUG(a) DE("DEBUG: %d\n",a) #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout) int read() { int x=0,f=1,ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; }
const int MAXN=2000000+10;
int h[MAXN],a[MAXN],c[MAXN],l[MAXN],r[MAXN];
signed main() {
int t=1; while(t--) { memset(c,0,sizeof(c)); memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); int n=read(); REP(i,1,n) h[i]=read(); int p=1; REP(i,1,n) if(h[i]>=h[p]) p=i; REP(i,0,n) a[i]=h[(i+p)%n==0?n:(i+p)%n]; DREP(i,n-1,0) { r[i]=i+1; while(r[i]<n&&a[i]>a[r[i]]) r[i]=r[r[i]]; if(r[i]<n&&a[i]==a[r[i]]) c[i]=c[r[i]]+1,r[i]=r[r[i]]; }
int ans=0;
REP(i,0,n-1) { ans+=c[i]; if(a[i]!=a[0]) { ans+=2;l[i]=i-1; while(l[i]>0&&a[i]>=a[l[i]]) l[i]=l[l[i]]; if(l[i]==0&&r[i]==n) ans--; } } printf("%lld\n",ans); } return 0; }
|