题意

n层楼,a[i] (0<i<n)表示从 i 楼到 i + 1 楼走楼梯的时间,b[i] (0<i<n)表示从 i 楼到 i + 1 楼乘电梯的时间,其中每一次乘电梯需要等待 k 时间,楼梯和电梯一次均可上从 x 楼上升到 y 楼 ( y != x ),即一次可以通过楼梯或电梯上升任意层数 。求从1楼到 1 ~ n 层楼所需要的最短时间

题目

You are planning to buy an apartment in a nn-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:

aiai for all ii from 1 to n−1 be the time required to go from the ii-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
bibi for all ii from 11 to n−1n−1 be the time required to go from the ii-th floor to the (i+1)-th one (and from the(i+1)-th to the ii-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 xx to any floor yy (x≠y) in two different ways:

If you are using the stairs, just sum up the corresponding values of aiai. Formally, it will take ∑min(x,y)max(x,y)−1ai\sum_{min(x,y)}^{max(x,y)−1}aimin(x,y)∑max(x,y)−1​ai time units.
If you are using the elevator, just sum up cc and the corresponding values of bibi. Formally, it will take c+∑min(x,y)max(x,y)−1bi\sum_{min(x,y)}^{max(x,y)−1}bimin(x,y)∑max(x,y)−1​bi time units.
You can perform as many moves as you want (possibly zero).

ai

So your task is for each ii to determine the minimum total time it takes to reach the ii-th floor from the 1-st (bottom) floor.

Input

The first line of the input contains two integers n and c (2≤n≤2⋅10510^{5}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 aiai is the time required to go from the ii-th floor to the (i+1)-th one (and from the(i+1)-th to the ii-th as well) using the stairs.

The third line of the input contains n−1n−1 integers b1,b2,…,bn−1 (1≤bi≤1000), where bibi is the time required to go from the ii-th floor to the (i+1)-th one (and from the (i+1)-th to the ii-th as well) using the elevator.

Output

Print nn integers t1,t2,…,tn where titi is the minimum total time to reach the ii-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

官方题解

This is easy dynamic programming problem. It is easy to understand that we don’t need to go down at all (otherwise your solution will be Dijkstra’s algorithm, not dynamic programming). Let dpi,0be the minimum required time to reach the floor ii if we not in the elevator right now and dpi,1 be the minimum required time to reach the floor ii if we in the elevator right now.

Initially, all values dp are +∞, except dp1,0=0 and dp1,1=c.

Transitions are pretty easy:

dpi+1,0=min(dpi+1,0,dpi,0+ai) (we was not in the elevator and going to the next floor using stairs);
dpi+1,0=min(dpi+1,0,dpi,1+ai) (we was in the elevator and going to the next floor using stairs);
dpi+1,1=min(dpi+1,1,dpi,0+bi+c) (we was not in the elevator and going to the next floor using elevator);
dpi+1,1=min(dpi+1,1,dpi,1+bi) (we was in the elevator and going to the next floor using elevator).
The answer for the ii-th floor is min(dpi,0,dpi,1).

Time complexity: O(n).

百度翻译

这是一个简单的动态规划问题。很容易理解,我们根本不需要往下走(否则你的解决方案将是Dijkstra的算法,而不是动态编程)如果我们现在不在电梯里,dpi,0是到达二层的最小要求时间;如果我们现在在电梯里,dpi,1是到达二层的最小要求时间。
最初,除dp1,0=0和dp1,1=c外,所有值dp均为+∞。
转换非常容易:
dpi+1,0=min(dpi+1,0,dpi,0+ai)(我们当时不在电梯里,正在下一层楼梯上);
dpi+1,0=min(dpi+1,0,dpi,1+ai)(我们当时在电梯里,正在下一层楼梯上);
dpi+1,1=min(dpi+1,1,dpi,0+bi+c)(我们不在电梯里,乘电梯去下一层);
dpi+1,1=min(dpi+1,1,dpi,1+bi)(我们在电梯里,乘电梯去了下一层)。
第二层的答案是min(dpi,0,dpi,1)。
时间复杂度:O(n)。

思路

dp题:二维数组dp[i][j],表示通过 j 的方法( j = 0 表示楼梯,j = 1表示电梯)第 i 层所需的最少时间。
可以看出有四种状态,
楼梯——》楼梯
楼梯——》电梯
电梯——》电梯
电梯——》楼梯
所以得出状态转移方程
dp[i+1][0]=min(dp[i][1]+a[i],dp[i][0]+a[i]);
dp[i+1][1]=min(dp[i][1]+b[i],dp[i][0]+b[i]+c);
简单是真简单(理解题意,推出转移方程),难是真的难(总会想偏,或是没有思路)菜的抠脚​​​​​​

AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2*1e5+10;
const int inf=0x3f3f3f3f;
int a[N],b[N],dp[N][2];
int n,c;
int main()
{while(~scanf("%d%d",&n,&c)){for(int i=1; i<n; i++)scanf("%d",&a[i]);for(int i=1; i<n; i++)scanf("%d",&b[i]);memset(dp,inf,sizeof(dp));dp[1][0]=0,dp[1][1]=c; ///dp[i][0]:走楼梯到达第i层,dp[i][1]:做电梯到达第i层for(int i=1; i<n; i++){dp[i+1][0]=min(dp[i][1]+a[i],dp[i][0]+a[i]);dp[i+1][1]=min(dp[i][1]+b[i],dp[i][0]+b[i]+c);}for(int i=1; i<=n; i++)printf("%d ",min(dp[i][0],dp[i][1]));printf("\n");}return 0;
}

By Elevator or Stairs? CodeForces - 1249E(动态规划)相关推荐

  1. E. By Elevator or Stairs?

    链接:https://codeforces.com/contest/1249/problem/E You are planning to buy an apartment in a nn-floor ...

  2. E:By Elevator or Stairs? CF595 DP最短路

    题目链接 比赛的时候一看,这不是最短路吗,然后敲了一个最短路. 然后比赛完发现大家基本都写的dp,我真是个憨憨,dp3行 最短路就建个简单的图,dp就是从上一维转化过来就是了 优秀的dp: //#pr ...

  3. Long Path CodeForces - 407B(动态规划+思维+公式推导)

    题意: 起点为1,终点为n+1,对应第i个各点,如果我奇数次到达i点,那么下一步走到a[i]的位子,如果是偶数次到达,那么下一步走到i+1的位子. 问从1走到n+1一共需要走多少步?结果对1e9+7取 ...

  4. LeetCode 746. Min Cost Climbing Stairs--动态规划--Java,C++,Python解法

    题目地址:Min Cost Climbing Stairs - LeetCode LeetCode 动态规划(Dynamic programming)系列题目:LeetCode 动态规划(Dynami ...

  5. codeforces gym-101741 Elevator 动态规划、单调队列

    题目 这里写链接内容 题解 注意:题目给出是按照时间给出的顺序. 我们考虑第iii个人要上的楼高h[i]" role="presentation" style=" ...

  6. Codeforces Round #394 (Div. 2) A. Dasha and Stairs 水题

    A. Dasha and Stairs 题目连接: http://codeforces.com/contest/761/problem/A Description On her way to prog ...

  7. 动态规划训练10 [Coloring Brackets CodeForces - 149D]

    西安交大 软件53 蔡少斐 整理 Coloring Brackets CodeForces - 149D 题目大意: 给定合法的括号序列,让你给括弧上色,并且上色时一定要满足3个要求: (1)每个括号 ...

  8. 【Codeforces】925A Stairs and Elevators【贪心】

    [Codeforces]925A Stairs and Elevators [题目大意] 在一个n*m的矩阵里,有clcl个楼梯和cece个电梯,电梯和楼梯可以到任意一层,给出clcl个楼梯的位置和c ...

  9. codeforces 792CDivide by Three(两种方法:模拟、动态规划

    传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...

最新文章

  1. Servlet - HTTP超文本传输协议
  2. 机器人编程语言python-机器人行业,10大流行编程语言对比
  3. java 查询表 并返回数据_ajax与java前后台传值及数据表查询解决一个bug的问题
  4. arrayfunction[LeetCode]Convert Sorted Array to Binary Search Tree
  5. 跟着鸟哥学Linux系列笔记1
  6. 配置表CRMC_SORG_R3ORG在SPRO里的配置路径
  7. primefaces_PrimeFaces在GlassFish 3.1.2.2上推动大气
  8. 为什么是先更新数据库再删除缓存,而不是更新缓存?
  9. Linux系统:centos7下搭建ElasticSearch中间件,常用接口演示
  10. table 的 id 属性不被 document.getElementById支持
  11. MDK 编译错误和警告 使用时遇到的小问题
  12. 【智驾深谈】想拿自动驾驶融资,先过VC这16问
  13. 腾讯计算机安全实验室,TRP-AI反病毒引擎创新:腾讯安全最新成果入围顶级学术会议...
  14. 计算机网络base,计算机网络中的术语100Base-TX/FX指的是什么?
  15. css内边距外边距和边框
  16. Java全文搜索怎么弄的_全文搜索 简介
  17. (1.5.1.3)编程之美:一摞烙饼的排序
  18. 微服务架构之公共模块式中创建API接口统一返回结果ApiResult
  19. 【STM32】标准库与HAL库对照学习教程十三--软件IIC控制AT24C02
  20. linux内核添加spi驱动,Linux内核驱动之spi子系统spi协议.docx

热门文章

  1. linux c之wait和waitpid函数的用法和总结
  2. 一个基础的 HTML 文档有哪些标签?(3)
  3. 如何升级浏览器_前谷歌员工爆料:谷歌工程师们是如何合谋“杀死”IE6浏览器的...
  4. php运行条件,PHP配置环境要求 php运行的先决条件
  5. 耶鲁大学计算机科学录取,2020年耶鲁大学排名TFE Times美国最佳计算机科学硕士专业排名第18...
  6. 假如人类长出翅膀,会变成这种怪样子
  7. 重磅公开!36个高考数学破题大招
  8. 这个爱喝酒的酒鬼可真是让人操碎了心
  9. 外国人最常说的100个“中国词”出炉,第一个你绝对想不到…
  10. 服务器文件每天备份重新命名,定时备份服务器文件至本地电脑