E. By Elevator or Stairs?.Codeforces Round #595 (Div. 3)

Foreword

There is one that one, this is the easiest I've ever done an E title

The meaning of problems

I tell you there is a building, and then you find the shortest time from the first floor of each building. Among them, upstairs there are two ways 1. 2. take the elevator to take the stairs. Stairs can go directly to the elevator requires a waiting time. Two ways between the data latency is not given the time required for the layers of the stairs.

practice

It is easy to think of dp, and is the most basic dp (estimated div3 will dare question the algorithms)

State dp [i] [0] indicates the i-layer down stairs minimum time required. dp [i] [1] indicates the i-layer to the minimum time required when the elevator.

Transfer equation

dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i-1][0];
dp[i][1]=min(dp[i-1][0]+a[i-1][1]+m,dp[i-1][1]+a[i-1][1]);

Code

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[200005][2];
int dp[200005][2];
int main(){
    cin>>n>>m;
    for(int i=1;i<=n-1;i++){
        cin>>a[i][0];
    }
    for(int i=1;i<=n-1;i++){
        cin>>a[i][1];
    }
    dp[2][0]=a[1][0];
    dp[2][1]=a[1][1]+m;
    for(int i=3;i<=n;i++){
        dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i-1][0];
        dp[i][1]=min(dp[i-1][0]+a[i-1][1]+m,dp[i-1][1]+a[i-1][1]);
    }
    for(int i=1;i<=n;i++){
        cout<<min(dp[i][0],dp[i][1])<<" ";
    }
} 

 

Guess you like

Origin www.cnblogs.com/LH2000/p/12594670.html