Problem Solving - Locking the Demon Tower again (Luogu: P2800)

Lock the demon tower again

Topic description

But Xiao A was very strange. He kept going around the demon lock tower, but he just wouldn't go in, so Xiao D asked him: "What are you doing? Why don't you go up?" Xiao A said: "I'm thinking about how to get from "Climb up outside the Demon Lock Tower" (reverse...) The construction of the Demon Lock Tower is very special. The tower has n floors in total, but the heights are not the same, which causes the time for Little A to climb each floor is different. Little A will use fairy He can jump up one or two floors every time he uses it, but after each jump, little A will use up his spiritual power and must climb at least one floor before jumping again (you can think that little A needs to jump twice a The first floor to rest), Xiao A wants to climb to the top of the tower in the shortest time, but he can't find a plan with the shortest time, so please help him find a plan with the shortest time to let him climb to the top of the tower, Xiao A only cares about time, So you just tell him what the shortest time is. You can finally jump outside the tower and exceed the height of the tower.

Input and output format

Input format:

 The first line contains a number n (n<=1000000), which represents the number of layers of the tower.

The number of n in the second line (<=100) represents the height of each layer from bottom to top.

 

Output format:

A number representing the shortest time.

Problem solving ideas:

This question is a classic dp, and the difficulty lies in how to list the state transition equation.

First, let's take the original problem (minimum time to climb to the Nth floor) Break it down into several sub-problems.

I'm having a little difficulty decomposing the subproblems. The problem limit cannot be leaped continuously, so how to define the state is the key to solving the problem

We define f[i] to represent the maximum value climbed when reaching the i-th layer (that is, the i-th layer to the i+1-th layer must be climbed)

So we can easily find its subproblems:

1. Ripples i layers, (f [i] = f [i-1] + a [i])

2. 飞 跃 跃 跃 top i layer, (f [i] = min (f [i-2] + a [i], f [i-3] + a [i]))

Pay attention to the boundary problem (must be calculated to the n+1th layer)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325262609&siteId=291194637