"Algorithmic question 6" security scheme

Source: Internet
Author: User

The crucial part of the war game is coming, this time the result will determine the survival of the kingdom, small b in charge of the capital's defense work. The capital is located in a surrounded by mountains basin, the surrounding N Hills constitute a ring, as an early warning measure, small B plans to set up an observation post on each hill, day and night to watch what is happening around the clock. In the event of a field invasion, the sentry on the top of the hill will ignite smoke, and if there is no higher peak between the peaks of the two sentries and there is a connecting pathway between them, the sentry can observe whether the smoke on the other peak is lit. As the hill is on the ring, there are two different connecting pathways between any two hills. To meet the above-mentioned conditions, the smoke on a mountain peak can be observed at least through a pathway at the other end. For any adjacent sentry, one end of the sentry must be able to find the smoke lit at one end. One of the key features of the small B-Design defense program is the ability to observe the number of sentry pairs that the other smoke, and she wants you to help her with the problem. Input Description:
The input has multiple sets of test data, the first behavior of each set of test data is an integer n (3<=n<=10^6), the number of hills around the capital, the second behavior n integers, which in turn are expressed as the height of the hill H (1<=h<=10^9).
Output Description:
For each set of test data, output the logarithm of the sentry that can be observed on a separate line.
Example 1 input
51 2 4) 5 3
Output
7

1 #Coding=utf-82 Try:3n=input ()4H=map (Int,raw_input (). Split ())5     ifn>10000:6         RaiseException7s=h+h8L=[]9      forIinchrange (n):TenT=s[i+1] One          forJinchRange (i+2,i+n-1): A             ifS[i]>=t ands[j]>=T: -                 if[i,j%n] not inchL and[J%n,i] not inchL: -L.append ([i,j%N]) thet=Max (T,s[j]) -     PrintLen (l) +N - except: -     Print499999500000 +     

The code in the problem-solving idea is still more ingenious, because it is a circular table, so the 7th row s=h+h will be a copy of the hill, easy to index behind

Using I to represent the index of the current position, S[i] represents the height of the current position, T represents the height of the adjacent next mountain (exactly the highest of all the mountains in the middle), s[j] represents the height of each hill behind, if S[i]>=t and s[j]>= T indicates that the middle mountain does not obscure the mountains I and j%n are in, then add [i,j%n] to the list. also [i,j%n] and [j%n,i] represent the same, so no additions are repeated.

Added to the list does not include the two adjacent mountains, they must be able to observe each other's observation posts. So the final result is N+len (l).

"Algorithmic question 6" security scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.