Openjudge Exercise Solution (c + +)-title 4074: Amount of water accumulated

Source: Internet
Author: User
Tags goto

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

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.