[Algorithm question 6] Defense scheme

The crucial part of the war game is coming. This time the outcome will decide the life and death of the kingdom. Little B is responsible for the defense of the capital. The capital is located in a basin surrounded by mountains, and the surrounding n hills form a ring. As an early warning measure, Little B plans to set up an observation post on each hill to keep an eye on the surrounding situation day and night. In the event of a foreign invasion event, the sentry on the top of the mountain will light the beacon smoke. If there is no higher mountain between the two sentry peaks and there is a connecting path between the two, the sentry can observe whether the beacon smoke on the other mountain is not. ignite. Since the hill is on the ring, there are two different connecting paths between any two hills. Under the above conditions of not being blocked, the beacon smoke lit by the sentry post on a mountain can be observed at the other end through at least one passage. For any adjacent sentry, the sentry at one end must be able to find the beacon smoke lit at one end. An important feature of this defense scheme designed by Xiao B is the ability to observe the number of sentry pairs of the opponent's beacon smoke. She hopes that you can help her solve this problem.

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.
Example 1

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).

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324403222&siteId=291194637