E. Bindian signalizing
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/problemset/problem/5/E
Descriptioneveryone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital is surrounded by N hills, forming a circle. On each hill there is a Watchman, who watched the neighbourhood day and night.
In case of any danger the watchman could make a fire on the hill. One watchman could see the signal of another watchman, if on the circle arc connecting the one hills there is no hill hig Her than any of the. As for any, hills there is, different circle arcs connecting them, the signal was seen if the above mentioned Condi tion was satisfied on at least one of the arcs. For example, for any, and neighbouring Watchmen it is true that the signal of one would be seen by the other.
An important characteristics of this watch system is the amount of pairs of watchmen able to see each other ' s signals. You is to find this amount by the given heights of the hills.
Input
The first line of the input data contains a integer number n (3≤n≤106), n-the amount of hills around the capital. The second line contains n numbers-heights of the hills in clockwise order. All height numbers is integer and lie between 1 and 109.
Output
Print the required amount of pairs.
Sample Input
5
1 2 4) 5 3
Sample Output
7
HINT
Test instructions
Give you a ring of mountains, if there is no higher mountain than this mountain between the two mountains, then the two mountains can see each other, and then ask you how many mountains in the end can see each other
Exercises
First we turn the ring into a chain, and then use a and look at the set of ideas, processing each mountain on the left of the first taller than this mountain is which, the right first is higher than this mountain is which, this area and the mountain height of the mountains have how many
And then run and check the set.
Code
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 1000010#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF;Const intinf=0x3f3f3f3f;/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;} while (x>0) ch[++num]=x%10,x/=10; while (Num) Putchar (ch[num--]+48); Puts ("");}*/inline ll read () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;} while(x>0) ch[++num]=x%Ten, x/=Ten; while(Num) Putchar (ch[num--]+ -); Puts ("");}//**************************************************************************************intA[MAXN];intB[MAXN];intL[MAXN];intR[MAXN];intK[MAXN];intMain () {intn=read (); intp=-1; for(intI=0; i<n;i++) {A[i]=read (); if(p==-1) P=i; Else if(a[i]>A[p]) {P=i; } } for(intI=0; i<n;i++) B[i]=a[(i+p+n)%N]; B[n]=A[p]; for(intI=1; i<n;i++) {L[i]=i-1; while(l[i]>0&&b[i]>=B[l[i]]) L[i]=L[l[i]]; } K[n]=0; for(inti=n-1; i>=0; i--) {R[i]=i+1; while(r[i]<n&&b[i]>B[r[i]]) R[i]=R[r[i]]; if(r[i]<n&&b[i]==B[r[i]]) {K[i]=k[r[i]]+1; R[i]=R[r[i]]; }} ll ans=0; for(intI=1; i<n;i++) {ans+=K[i]; Ans+=1; if(l[i]!=0|| r[i]!=N) Ans++; } cout<<ans<<Endl;}
Codeforces Beta Round #5 E. Bindian signalizing and collection