E. By Elevator or Stairs?
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
ai for all i from 1 to n−1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
bi for all i from 1 to n−1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c — time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x≠y) in two different ways:
If you are using the stairs, just sum up the corresponding values of ai. Formally, it will take ∑i=min(x,y)max(x,y)−1ai time units.
If you are using the elevator, just sum up c and the corresponding values of bi. Formally, it will take c+∑i=min(x,y)max(x,y)−1bi time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2≤n≤2⋅105,1≤c≤1000) — the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n−1 integers a1,a2,…,an−1 (1≤ai≤1000), where ai is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n−1 integers b1,b2,…,bn−1 (1≤bi≤1000), where bi is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t1,t2,…,tn, where ti is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
output
0 7 13 18 24 35 36 37 40 45
input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
output
0 2 4 7 8 11 13 14 16 17
解题思路:
DP题,题目意思是每层楼上楼可以坐电梯,可以走楼梯,时间不同,坐电梯的话开一次门需要m的时间,连续坐几层楼的话就还是m的时间。然后考虑第i层楼的时候,枚举两个状态,一个是走楼梯,走楼梯的话就是走到第i-1层的最短时间+上a[i]就行了,然后如果是坐电梯就要判断,如果第i-1层是坐电梯上去的,那么就不需要加m的时间,否则就需要加上m的时间。
AC代码:
#include <iostream>
const int N = 1e6;
using namespace std;
int a[N],b[N],c[N];
int ans[N][2];
int main()
{
int n,m;
cin>>n>>m;
for(int i = 0 ; i < n-1 ; i ++)
cin>>a[i];
for(int i = 0 ; i < n-1 ; i ++)
cin>>b[i];
c[0] = b[0];
for(int i = 1 ; i < n-1 ; i ++)
c[i] = c[i-1] + b[i];
ans[0][0] = 0;ans[0][1] = m;
for(int i = 1 ; i < n ; i ++)
{
ans[i][0] = min(ans[i-1][0],ans[i-1][1])+a[i-1];
ans[i][1] = min(ans[i-1][0] + b[i-1] + m ,ans[i-1][1]+b[i-1]);
}
for(int i = 0 ; i < n ; i ++)
cout<<min(ans[i][0],ans[i][1])<<" ";
return 0;
}