Sicily-water "Looking for the law"

Source: Internet
Author: User

Description

Mobile Information Engineering College is going to leave the Pearl, KY in Zhuhai know a lot of girls, naturally very reluctant to the pearl. So, he wanted to leave some gifts for these girls, the recent rain in the Pearl, the five elements of the lack of water, he thought of leaving the most precious medium-pearl rainwater as a souvenir. Now he wants to collect as much as possible a little more of the medium-bead rain, assuming that the topography of the pearl is abstracted into a two-dimensional elevation map, the number of regions is N, each area has a terrain width of 1, height with a h, then you can help KY calculates the maximum amount of water (area ) of the Pearl rain he can receive in such a terrain?

For example, the height of each area in the altitude map is [0,1,0,2,1,0,1,3,2,1,2,1], so he can receive a maximum of 6 unit area of medium-bead rainwater.

Input

The input contains samples of several sets of test specimens.

Each set of sample samples contains two lines, the first behavior is an integer n (1<=n<=10^6), the number of terrain areas, the second row has N integers, representing the height of each region H (1 <= H <= 100), separated by a space.

N is the end of the 0 o'clock input.

Output

Each set of data outputs an integer representing the maximum amount of water (area) that KY can receive.

Sample Input
120 1 0 2 1 0 1 3 2 1 2 10
Sample Output
6

Analysis:

1) start by initializing the left boundary of the water zone cur=0, then ans=0,sum=0, then start traversing 1 ~ n-1
2) when traversing to A[i], if a[i]<a[cur],sum + = A[i]

3) When traversing to A[i], if a[i]≥a[cur]:

2.1) If cur-i-1=0, this indicates that cur can not be the left edge of the water zone, I is, the left boundary of the water zone is then set to I.

2.2) If the cur-i-1>0, indicating that has traversed a water zone (after the overall change in the first drop after the rise), here you can add the water in the flooded zone: ans + = (cur-i-1) *a[cur]-sum, and will sum=0, update the left edge to I, i.e.: cur=i

There is also a problem, that is, maybe A[cur] is the global maximum, its right is no bigger than him, then the solution is natural:

Scan from right to left again, scan interval for [cur, n-1]

#include <cstdio>#include<iostream>using namespacestd;inta[1000005];intMain () {intN;  while(SCANF ("%d", &n)! = EOF &&N) { for(intI=0; i<n; i++) {scanf ("%d", &A[i]); }                intCur =0, sum=0, ans=0;  for(inti=cur+1; i<n; i++) {            if(A[i] >=A[cur]) {                if(i-cur-1) {ans+ = (i-cur-1) *a[cur]-sum; } cur=i; Sum=0; } Else{sum+=A[i]; }        }        intLB =cur; Cur= N1, sum=0;  for(inti=cur-1; i>=lb; i--) {            if(A[i] >=A[cur]) {                if(cur-i-1) {ans+ = (cur-i-1) *a[cur]-sum; } cur=i; Sum=0; } Else{sum+=A[i]; }} printf ("%d\n", ans); }        return 0;}

Sicily-water "Looking for the law"

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.