Title Description
A set of positive integers, respectively represented by the height of the column of stacked cube. If a value of the height x, denoted by x positive cubic block after another (see below, 0 <= x <= 5000). Finds all possible stagnant water (the blue part of the figure), the total area they may water statistics (the figure is calculated cross sectional area of a cube location for a unit area).
FIG: column height variation is 0,102,120,020
The blue part of the water area, a total of 6 per unit area of water.
Input Format
Two rows, a first row n, expressed the number n (3 <= n <= 10000). N number of second continuous lines represented by the height of the cube after another in sequence, to 0 to ensure end to end.
Output Format
A number of possible water area.
Sample input and output
10
0 1 0 2 1 2 0 0 2 0
6
[] ideas solving
a simulation problem
to find a maximum value in the minimum value is determined on both sides, remove the current height of the bricks can be obtained a high water
[code]
1 #include <cstdio>
2 #include <iostream>
3 #include <algorithm>
4 using namespace std;
5 int n,a[10005],ans,l[10005],r[10005];
6 inline int Max(int a,int b){
7 return a>b?a:b;
8 }
9 inline int Min(int a,int b){
10 return a<b?a:b;
11 }
12 int main(){
13 //freopen("1011.in","r",stdin);
14 //freopen("1011.out","w",stdout);
15 scanf("%d",&n);
16 for(register int i=1;i<=n;i++){
17 scanf("%d",&a[i]);
18 l[i]=Max(l[i-1],a[i]);
19 }
20 for(register int i=n;i>=1;i--)
21 r[i]=Max(r[i+1],a[i]);
22 for(register int i=1;i<=n;i++){
23 if(Min(l[i],r[i])<a[i])continue;
24 else ans+=min(l[i],r[i])-a[i];
25 }
26 printf("%d\n",ans);
27 return 0;
28 }