nyoj magical girl

Magical Girl Time Limit: 1000 ms | Memory Limit: 65535 KB Difficulty: 3 Description Some time ago, Xu Yuanxuan's Juxian Madoka really caught fire. When Hei Changzhi (Xiaoyan) climbed the stairs to fight the Witch Night, she encountered a problem and wanted to ask for your help. Because Witch Night was suspended in mid-air, she had to climb the stairs, and the ruins had a total of n floors, and each floor had different heights, which caused Xiaoyan to climb each floor at a different time. But of course, Xiaoyan knows time magic, and can fly through one or two layers in an instant [that is, not time-consuming]. But every time she teleports, she has to climb up at least one more layer (replenishing magic power at this time) to use teleport again. It takes Xiaoyan 1 second to climb each unit of height. It is urgent to eliminate Witch Night, so Xiaoyan wants to find you to help her find a shortest time plan to reach the top of the building. Enter multiple sets of data for this question, ending with the end of file input. The first line of each set of data contains a number N (1 <= N <= 10000), which represents the number of floors. Next N lines, each line has a number H (1 <= H <= 100), representing the height of this layer. Output For each set of data, output a line, a number S, representing the minimum time required to reach the roof.

sample input

5

3

5

1

8

4

Sample output 1 d

p[i][0] means no magic

dp[i][1] means use magic

Use magic at least once every three moves to save time.

#include

#include

#include

using namespace std;

const int height=10005;

int dp[height][2],h[height];

intmain()

{

int n;

while(cin>>n)

{

for(int i=1;i<=n;++i)

cin>>h[i];

memset(dp,0,sizeof(dp));

dp[1][0]=h[1];//Because every time you use magic, you can only use it when you climb up one layer, so the first layer cannot use magic, and the dp value of magic is 0

dp[2][0]=h[2];//

for(int i=3;i<=n;++i)

{

dp[i][0]=min(dp[i-1][0],dp[i-1][1])+h[i];//This layer does not use magic, the previous layer can be used You can also not use magic, which is not directly related to the first two layers.

dp[i][1]=min(dp[i-1][0],dp[i-2][0]);//The premise of using magic in this layer is that the previous layer or the first two layers do not use magic

}

cout<<min(dp[n][0],dp[n][1])<<endl;

}

return 0;

}

Guess you like

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