Beihu digging | Feel the charm of algorithms

4. North Lake digging

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

Eleven years ago, Beihu used to be a flat piece of land. The construction team of Beihu planned to dig it out of a deep pit and inject water to create an artificial 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.

In the beginning, the North Lake was a flat land, and the height of each piece was about h. The following figure shows h=3the situation, represented by an array as [3,3,3,3,3,3,3,3,3,3,3,3].

The construction team wanted to dig it into a bumpy shape according to the drawing. As shown in the figure below, the array is represented as [0,1,0,2,1,0,1,3,2,1,2,1].

The construction team has an excavator, and the excavator can dig a block of soil in a continuous section every day. The contractor, Xiao Zhang, wants to complete the task as soon as possible. Please tell him how many days it will take to dig the flat ground into the situation shown in the drawing?

Input

Enter two integers on the first line n (1 \ leq n \ leq 100000), h (1 \ leq h \ leq 1000000). Represents the total width of North Lake and the height of each initial block.

nAn integer in the next line a_i (0 \leq a_i \leq h )represents the height of each position on the drawing.

Output

An integer means that Beihu can be dug in at least a few days.

Notes

The 9-day excavation interval is [1,7], [1,3], [5,7], [1,1], [3,3], [6,6], [9,12], [ 10,10], [12,12].

 

  Test input Expected output time limit Memory limit Extra process
Test case 1 Display as text
  1. 12 3↵
  2. 0 1 0 2 1 0 1 3 2 1 2 1↵
Display as text
  1. 9↵
1 second 64M 0

       This question of violence and dichotomy will be tle (timeout), then we need a smarter way to reduce the time complexity of our code:

       For the use case given in the question, the part we need to dig out is as follows:

       For the convenience of thinking, we will consider the above picture upside down , don't worry, the results of such consideration will definitely be the same. The height of each place that needs to be dug can be calculated and stored in an array : [3, 2, 3, 1, 2, 3, 2, 0, 1, 2, 1, 2].

       We initially set the number of construction days to the height of the first block to be excavated. As shown in the figure above, days = 3.

       Then, we start with the second block and consider in turn from left to right:

  • If the current height to be excavated is lower than the previous one , then the construction time of this one can be completed before (the excavator can dig out one block of soil in a continuous interval every day ), we do not need to add the current one Time .
  • If the current height to be digged is higher than the previous day , then we need to add time , days += the height difference between the two.

       For example, in the above use case, our algorithm process is shown in the figure:

The method is very simple, but this way of thinking is hard to think of. The code is not difficult, just go to the code:

Oh yes, remember to use long long int as the data type, otherwise it may be due to number overflow...

#include <stdio.h>

#define LEN 100005

long long h[LEN];  //每一处需要挖去的高度

int main() {
    long long wid, height;
    scanf("%lld %lld", &wid, &height);

    for(long long i = 0; i < wid; i++) {
        long long cur;
        scanf("%lld", &cur);  //输入最终要求的高度
        h[i] = height - cur;  //计算每一处需要挖去的高度
    }

    long long ans = h[0];  //初始天数
    for(long long i = 1; i < wid; i++) {
        if(h[i] > h[i - 1]) 
            ans += h[i] - h[i - 1];  //加时
    }

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


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/108481712