Lock the tower again, and lock the tower again

Source: Internet
Author: User

Lock the tower again, and lock the tower again
Background

D bought the desired things at X star. On the way to the next destination, he was bored. He turned his head and looked at Xiao A, and found that Xiao A was playing <Xian Jian>

Description

But Mr. A is very strange. He has been turning around the lock tower, but he just won't go in. So Mr. D asked him, "What are you doing? Why not ?" Mr. A said, "I was wondering how to climb out of the lock tower." (inverted ...) The construction of the lock demon tower is very special. The tower has n layers in total, but the height is different, which makes the time for A to climb each layer different. xiao A will use the fairy technique. Every time he uses it, he can jump up one or two layers. However, after each hop, Xiao A will use up the flexibility, you must climb at least one layer to jump again (you can think that A needs to Jump twice to rest), A wants to climb to the top of the tower in the shortest time, however, he cannot find the solution with the shortest time, so please help him find A solution with the shortest time so that he can climb to the top of the tower. Mr. A only cares about the time, so you just need to tell him the shortest time. you can finally jump out of the tower to exceed the height of the tower.

Input/Output Format

Input Format:

 

The number n (n <= 1000000) in the first row indicates the number of floors of the tower.

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

 

Output Format:

 

A number, indicating the shortest time.

Input and Output example:

Input:

5
3 5 1 8 4

Output:

1

Code (dp ):

#include<iostream>using namespace std;int h[1000000];long long dp[1000000][2];int main(){    int n;    cin>>n;    for(int i=1;i<=n;i++)    cin>>h[i];    dp[2][1]=0;    dp[1][1]=0;    dp[1][0]=h[1];    dp[2][0]=min(dp[1][1]+h[2],dp[1][0]+h[2]);    for(int i=3;i<=n;i++)    {        dp[i][0]=min(dp[i-1][1]+h[i],dp[i-1][0]+h[i]);        dp[i][1]=min(dp[i-1][0],dp[i-2][0]);    }    dp[n+1][1]=dp[n-1][0];    cout<<min(min(dp[n][1],dp[n][0]),dp[n+1][1]);}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.