OpenJudge exercises (C ++) -- question 4074: Water Accumulation

Source: Internet
Author: User

OpenJudge exercises (C ++) -- question 4074: Water Accumulation
Question:

Total time limit: 1000 ms memory limit: 65536kB
Description

The uneven ground always accumulates water when it rains. Assume that the ground is one-dimensional, each piece of width is 1, and the height is a non-negative integer, you can use an array to express a piece of ground. For example, [,] can be used to represent the ground:

After the rain, the ground will accumulate water, and the blue area is the area of water. Now I will give you an array to indicate the amount of water on the ground after rain (if not evaporated or infiltrated ).

InputThe first line is an integer m, indicating that there are m samples, no more than 100.
Next m blocks, the first line of each block is a positive integer n, indicating the total width of the ground (array length), no more than 20000.
The next line contains n integers separated by spaces, indicating the ground height. OutputFor each input group, an integer is output to indicate the accumulated amount of water. Sample input:
 
    Sample output: 
    
6
2
Solution:
# Include <iostream> using namespace std; int main () {int m, n, I, j, c, B, z, l, x = 0; int a [20000]; h: cout <"Enter the number of style groups m (m <= 100)" <endl; cin = ""> m; if (m <0 | m> 100) {cout <"input error. Enter" <endl; goto = "" h; = ""} = "" for (I = "0; I <m; I ++) e: =" "=" "cin =" "> n; if (n> 20000 | n <0) {cout <"input error. Enter" <endl; goto = "" j = "0; j <n; j ++) "cin =" "> a [j];} B = 0; j = 0; w: B = j; for (j = B + 1; j <n; j & #43; & #43;) z = "B; z"> = 0; z --) {if (a [z]> a [j]) {for (c = j + 1; c <n; c & #43; & #43;)> a [j]) {if (a [c]> a [z]) {l = a [z];} else {l = a [c];} x = x + (l-a [j]); goto w ;}}}}cout <"water accumulation is" <x <endl; return = "" pre = ""> <p> </x <endl;> </n; c & #43; & #43;)> </n; j & #43; & #43;)> </endl;> </iostream>

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.