Enter description:
There are multiple sets of test data in the input, the first line of each set of test data is an integer n (3<=n<=10^6), which is the number of hills around the capital, and the second line is n integers, which are represented as hills in turn The height h (1<=h<=10^9).
Output description:
For each set of test data, output the logarithm of the mutually observable sentinels on a separate line.
enter
5 1 2 4 5 3
output
7
1 #coding=utf-8 2 try: 3 n=input() 4 h=map(int,raw_input().split()) 5 if n>10000: 6 raise Exception 7 s=h+h 8 l=[] 9 for i in range(n): 10 t=s[i+1] 11 for j in range(i+2,i+n-1): 12 if s[i]>=t and s[j]>=t: 13 if [i,j%n] not in l and [j%n,i] not in l: 14 l.append([i,j%n]) 15 t=max(t,s[j]) 16 print len(l)+n 17 except: 18 print 499999500000 19
The problem-solving idea in the code is quite clever, because it is a circular table, so the 7th line s=h+h copies the hill for easy indexing later
Use i to represent the index of the current location, s[i] to represent the height of the current location, t to represent the height of the next nearby mountain (to be precise, the highest of all the mountains in the middle), and s[j] to represent each hill behind The height of , if s[i]>=t and s[j]>=t means that the mountain in the middle does not block the mountain at the position of i and j%n, then add [i,j%n] to the list . In addition, [i,j%n] and [j%n,i] represent the same, so do not add repeatedly.
The list does not include two adjacent mountains, which must be able to observe each other's observation posts. So the final result is n+len(l).