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

Source: Internet
Author: User
Tags array length

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

Related Article

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.