Question link: Click the open link
# Include <stdio. h> # include <iostream> # include <string. h> # include <set> # include <vector> # include <map> # include <math. h> # include <queue> # include <string> # include <stdlib. h >#include <algorithm> using namespace STD; # define n 1000005 # define ll _ int64int N; int A [n], B [N]; int SX [N], sy [N]; int main () {int I, U, V; while (~ Scanf ("% d", & N) {int Maxx = 0; for (I = 0; I <n; I ++) {scanf ("% d ", & A [I]); if (a [I]> A [Maxx]) Maxx = I;} int Dep = 0; for (I = Maxx; I <N; I ++) B [++ Dep] = A [I]; for (I = 0; I <Maxx; I ++) B [++ Dep] = A [I]; ll ans = 0; int Top = 0; for (I = 1; I <= N; I ++) {While (top & B [I]> SX [Top-1]) ans + = Sy [-- top]; If (! Top) {SX [Top] = B [I]; Sy [top ++] = 1; continue;} If (SX [Top-1] = B [I]) {ans + = Sy [Top-1]; Sy [Top-1] ++; If (top> 1) ans ++;} else {ans ++; SX [Top] = B [I]; Sy [top ++] = 1 ;}} while (top> 2) ans + = Sy [-- top]; if (Top = 2) {top --; If (SY [Top-1]> 1) ans + = Sy [Top] ;}cout <ans <Endl ;} return 0 ;}