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>