North Lake Deep Hole | Feel the charm of algorithm

5. North Lake Deep Hole

Grades 10 opening time Monday, September 7, 2020 09:00
discount 0.8 Discount time Tuesday, September 15, 2020 09:00
Late submission allowed no Closing time Saturday, October 10, 2020 23:00

Description

Ten years ago, Beihu was only a deep pit, and the water storage work was not completed. In order to ensure the smooth progress of water storage, we need to make a rough estimate of the water storage capacity of the North Lake.

In order to simplify the calculation, we assume that the ground of Beihu Lake is one-dimensional, and each piece has a width of 1 and a height of a non-negative integer. Then an array can be used to express a piece of ground.

For example, the array [0 1 0 2 1 0 1 3 2 1 2 1] can be used to represent the ground in the following figure:

The green part in the picture represents the ground part, the blue part represents the water storage part, and the water storage capacity is 6.

Input

There are multiple groups of sample input.

Enter the integer T in the first line to indicate that there are T groups of test cases.

Next, for each set of use cases, enter a positive integer n to represent the ground width, and then n numbers to represent the ground height.

Output

For each use case, output a number on a line to indicate the total amount of water storage.

  Test input Expected output time limit Memory limit Extra process
Test case 1  
  1. 2↵
  2. 12↵
  3. 0 1 0 2 1 0 1 3 2 1 2 1↵
  4. 5↵
  5. 5 2 3 2 4↵
  1. 6↵
  2. 5↵
1 second 64M 0


This question is a thinking question, you can solve it if you think of a method.

1. Thinking analysis

The overall idea: analyze the storage capacity of each width, and then add them together, which is the total storage capacity we require.

So the point is here, how to obtain the storage capacity of each width?

Through observation, we can see that for a certain amount of water from its final position a smaller value at the top left and right sides of the decision (where the left and right sides, and there is not necessarily neighboring Kazakhstan, as long as the left, on the right can)

For example, in the picture above: the water volume at position 4 is determined by numbers 1 and 11, and the water volume at position 8 is determined by numbers 7 and 11...


2. Algorithm design

       The idea is out, the next part is to write the algorithm. We should first calculate two arrays: left and right. Among them, left[ i] represents the highest point value at 0 ~ i, that is, the highest point on the left side of i; right[ i] represents the highest point value at i ~ n-1, that is, the highest point on the right side of i.

       Then the water storage capacity at each location can be expressed as: min(left[ i ], right[ i ])-a[ i]

Finally, pay attention to:

  1. We define that the left, right, and h arrays are outside the main function, that is, defined as global variables . Because the stack memory in the main function is less than the global memory, if some large memory applications are defined in the main function, it is easy to burst the stack, that is, burst memory. When you submit it, tle will appear (in fact, it should be mle in essence). 
  2. Since each set of test cases is entered in a loop, you must remember to re-initialize them in the loop, otherwise the calculation will continue on the basis of the previous loop each time.

Go to the code~ 

#include<stdio.h>

long long int left[100000] = {0}, right[100000] = {0}, h[100000] = {0};

int main() {
    int n, T;
    for (scanf("%d", &T); T; T--) {

        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            scanf("%lld", &h[i]);

        /* 分别计算left和right数组 */
        
        left[0] = h[0];  //边缘情况单独考虑
        right[n - 1] = h[n - 1];   //边缘情况单独考虑

        for (int i = 1; i < n - 1; i++)
            if (h[i] < left[i - 1])
                left[i] = left[i - 1];
            else
                left[i] = h[i];
        for (int i = n - 2; i > 0; i--)
            if (h[i] < right[i + 1])
                right[i] = right[i + 1];
            else
                right[i] = h[i];
            
        //计算总的储水量
        long long int total = 0;  //一定记得赋初值0,不然下一组求值的时候会在上一组答案的基础上加
        for (int i = 1; i < n - 1; i++)
            if (left[i] > right[i])
                total += right[i] - h[i];
            else
                total += left[i] - h[i];

        printf("%lld\n", total);
    }
}


Welcome to pay attention to the personal public account "  Chicken Wing Programming" , here is a serious and well-behaved code farmer.

---- Be the most well-behaved blogger and the most solid programmer ----

Aim to write each article carefully, and usually summarize the notes into push updates~

Insert picture description here

 

Guess you like

Origin blog.csdn.net/weixin_43787043/article/details/108456824