Problem:
-
Total time limit: 1000ms memory limit: 65536kB
-
Describe
-
The uneven ground always accumulates when it rains. Assuming that the ground is one-dimensional, each piece is 1 wide and the height is a non-negative integer, 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 (assuming no evaporation, no infiltration).
-
Input
-
The first line is an integer m, indicating that there is an M-group sample, not exceeding 100.
Next m block, the first row of each block is a positive integer n, representing the total width of the ground (array length), no more than 20000.
The next line is n integers, separated by a space, to indicate the ground height.
-
Output
-
For each set of inputs, an integer is output to indicate the amount of water accumulated.
-
Sample input:
-
-
Sample 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 re-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 re-enter"<<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;}
Openjudge Exercise Solution (c + +)-title 4074: Amount of water accumulated