Problem:
-
Total time limit: 1000ms memory limit: 65536kB
-
Descriptive narrative
-
The uneven ground always accumulates when it rains. If the ground is one-dimensional. Each piece has a width of 1 and a height of non-negative integers. Then you can use an array to express a piece of ground.
For example [0,1,0,2,1,0,1,3,2,1,2,1] can be used to represent the ground:
After the rain, the ground will be stagnant, the blue area is stagnant water area. Now give you an array to represent the ground, to find out how much water the ground has after the rain (if it does not evaporate, do not penetrate).
-
Input
-
The first line is an integer m. Indicates that there is an M-group sample, not exceeding 100.
Next m block. The first line of each block is a positive integer n. Represents the total ground width (array length). No more than 20000.
The next line is n integers. Separated by a space, indicates the ground height.
-
Output
-
For each set of inputs, an integer is output to indicate the amount of water accumulated.
-
Example input:
-
-
Example output:
-
6
-
2
Solution:
#include<iostream>using namespaceStd;int Main(){intM,N,I,J,C,B,Z,L,X=0;intA[20000];H:cout<<"Please enter the number of style groups M (m<=100)"<<Endl;Cin>>M;if(M<0||M> -){cout<<"input error. Please enter "<<Endl;GotoH;} for(I=0;I<M;I++){E:cout<<"Please enter array length n (n<=20000)"<<Endl;Cin>>N;if(N>20000||N<0){cout<<"Input error, please enter again"<<Endl;GotoE;} for(J=0;J<N;J++){Cin>>A[J];}B=0;J=0;W:B=J; for(J=B+1;J<N;J++){ for(Z=B;Z>=0;Z--){if(A[Z]>A[J]){ for(C=J+1;C<N;C++){if(A[C]>A[J]){if(A[C]>A[Z]){L=A[Z];}Else{L=A[C];}X=X+(L-A[J]);GotoW;}}}}}cout<<"The amount of water accumulated"<<X<<Endl;}return 0;}
Recommended article: those years. Several applications to do
Openjudge Exercise Solution (c + +)-title 4074: Amount of water accumulated