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

题目地址:http://codeforces.com/contest/1249/problem/E

题意:有n层楼,上楼有楼梯和电梯两种方法,从 i 到 i+1 层有不同花费,用电梯有等电梯门开的时间,但上一次用的电梯就不用等这个时间。问去每层楼的最小花费。

思路:很基础的dp题,dp [ i ] [ 0 ] 表示一楼到 i楼的总时间,其中从 i - 1 楼到 i 楼用的是楼梯,dp [ i ] [ 1 ] 表示从一楼到 i 楼的总时间,其中 i - 1 到 i 楼用的电梯。

AC代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 const int N=2e5+5;
 8 ll n,c,a[N],b[N],dp[N][2];
 9 void sol(){
10
11     cin>>n>>c;
12     for(int i=2;i<=n;i++) cin>>a[i];
13     for(int i=2;i<=n;i++) cin>>b[i];
14
15     dp[1][0]=0;     //初始化,一楼直接是0时间,且不能有用过电梯这个状态
16     dp[1][1]=2e8+10;
17
18     for(int i=2;i<=n;i++){      //转移方程
19         dp[i][0]=min(dp[i-1][0]+a[i],dp[i-1][1]+a[i]);
20         dp[i][1]=min(dp[i-1][0]+b[i]+c,dp[i-1][1]+b[i]);    //前一层用过电梯就不用加c这个等门时间了
21     }
22     for(int i=1;i<=n;i++){  //取小的就OK了
23         ll ans=min(dp[i][0],dp[i][1]);
24         cout<<ans<<" ";
25     }
26 }
27 int main(){
28     sol();
29     return 0;
30 }

原文地址:https://www.cnblogs.com/xunzf0402/p/11734678.html

时间: 2024-07-30 14:10:52

Codeforces Round #595 (Div. 3) E. By Elevator or Stairs?的相关文章

Codeforces Round #595 (Div. 3)

比赛链接:传送门 Codeforces1249A. Yet Another Dividing into Teams(水题) 代码: #include <bits/stdc++.h> #define N 105 using namespace std; int a[N]; int main() { int q; cin >> q; while (q--) { int n; cin >> n; for (int i = 1; i <= n; i++) { cin &g

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

前言 有一说一,这是我做过最简单的一道E题 题意 告诉你有个大楼,然后让你求出从一楼到每一楼的最短时间.其中,上楼有两种方式1.走楼梯2.坐电梯.楼梯可以直接走,电梯需要一个等待时间.数据给出层与层之间不算等待时间的两种方式上楼所需的时间. 做法 很容易想到dp,而且是最基础的dp(估计div3也就敢这么出个算法题) 状态dp[i][0]表示走楼梯上到i层所需的最短时间.dp[i][1]表示坐电梯时上到i层所需的最短时间. 转移方程 dp[i][0]=min(dp[i-1][0],dp[i-1]

Codeforces Round #595 (Div. 3)B2 简单的dfs

原题 https://codeforces.com/contest/1249/problem/B2 这道题一开始给的数组相当于地图的路标,我们只需对每个没走过的点进行dfs即可 #include <bits/stdc++.h> using namespace std;const int maxn=2e5+20;int a[maxn],b[maxn],c[maxn];int dfs(int pos,int step){//传递坐标与步数 if(b[pos]==1){//再次遇到b[pos],返回

Codeforces Round #595 (Div. 3) 题解

前言 无 A 因为没有重复的数,我们只要将数据排序,比较两两之间有没有\(a_j - a_i == 1 (j > i)\) 的,有则输出 \(2\) , 无则输出 \(1\) 普及T1难度 Code #include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define N 107 using namespace st

题解Codeforces Round #595 (Div. 3)(CF1249)

开题1小时(雾)严重影响我的提交以及做题心情..我刚开题就发现有人阿克了.. 实际上这场div3真心简单良心很休闲. A:送分题,先排序,每次枚举一下这个数可以加到哪个集合里,加进去就行. 1 #include<stdio.h> 2 #include<algorithm> 3 #define it register int 4 #define il inline 5 using namespace std; 6 const int N=1000005; 7 int a[N],o[N

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd