nyoj 1070 诡异的电梯【Ⅰ】 动态规划
来源:互联网 发布:模具分析软件 编辑:程序博客网 时间:2024/06/10 02:43
- 描述
新的宿舍楼有 N(1≤N≤100000) 层 and M(1≤M≤100000)个学生. 在新的宿舍楼里, 为了节约学生的时间也为了鼓励学生锻炼身体, 所以规定该宿舍楼里的电梯在相邻的两层之间是不会连续停下(即,如果在第2层停下就不能在第3层停下。).所以,如果有学生在相邻的两层之间要停下, 则其中的一部分学生必须选择走楼梯来代替。规定:一个人走下一层楼梯的花费为A,走上一层楼梯的花费为B。(1≤A,B≤100)现在请你设计一个算法来计算出所有学生走楼梯花费的最小费用总和。 所有的学生一开始都在第一层,电梯不能往下走,在第二层的时候电梯可以停止。
- 输入
- 输入有几组数据T。T(1≤T≤10)
每组数据有N (1≤N≤100000),M(1≤M≤100000),A,B(1≤A,B≤100)。
接下来有M个数字表示每个学生想要停的楼层。 - 输出
- 输出看样例。
- 样例输入
13 2 1 12 3
- 样例输出
Case 1: 1
- 提示
- 原题:
The new dormitory has N(1≤N≤100000) floors and M(1≤M≤100000)students. In the new dormitory, in order to save student's time as well as encourage student exercise, the elevator in dormitory will not stop in adjacent floor. So if there are people want to get off the elevator in adjacent floor, one of them must walk one stair instead. Suppose a people go down 1 floor costs A energy, go up 1 floor costs B energy(1≤A,B≤100). Please arrange where the elevator stop to minimize the total cost of student's walking cost.All students and elevator are at floor 1 initially, and the elevator can not godown and can stop at floor 2.
OUTPUT:
Output case number first, then the answer, the minimum of the total cost of student's walking cost. - 来源
- 翻译【2014湘潭邀请赛】
- 上传者
- ACM_钟诗俊
ps:本题只需一个一维dp即可.
用dp【i】表示从1层上到第 i 层花费的最小的体力。
因为不能在相邻的楼层停留,所以可以从dp【i-2】转移,但这样不是最优的还要从dp【i-3】转移,因为这样的话就可以到达所有的楼层。我们只要在所有的之间dp最优即可。
其他要注意的一个条件是,从dp【i-3】转移时,中间两层的人有四种选择:
1:都上去
2:上面的上去一层,下面的下来一层
3:上面的下来两层,下面的上去两层,
4:都下来
代码如下:
#include<stdio.h>#include<string.h>const int inf=100000020;int s[100020],dp[100020];int min1(int a,int b){ return a<b?a:b;}int main(){ int t,n,m,a,b,z; int f=0; scanf("%d",&t); while(t--) { memset(s,0,sizeof(s)); f++; scanf("%d %d %d %d",&n,&m,&a,&b); for(int i=1; i<=m; i++) { scanf("%d",&z); s[z]++; } memset(dp,inf,sizeof(dp)); dp[0]=dp[1]=dp[2]=0; int minn=min1(a,b),min2; for(int i=3;i<=n;i++) { dp[i]=min1(dp[i-2]+s[i-1]*minn,dp[i]); //printf("%d\n",dp[i]); if(i>=4) { min2=min1(s[i-1]*b+s[i-2]*2*b,s[i-1]*2*a+s[i-2]*a); min2=min1(min2,s[i-1]*b+s[i-2]*a); min2=min1(min2,s[i-2]*2*b+s[i-1]*2*a); dp[i]=min1(dp[i],min2+dp[i-3]); } } printf("Case %d: %d\n",f,dp[n]); }}
0 0
- NYOJ-诡异的电梯【Ⅰ】-(动态规划)
- nyoj 1070 诡异的电梯【Ⅰ】 动态规划
- nyoj 1070 诡异的电梯【Ⅰ】【dp】
- NYOJ 1070 诡异的电梯【Ⅰ】 DP
- NYOJ 1070 诡异的电梯【Ⅰ】
- 南阳理工acm 1070诡异的电梯【Ⅰ】(动态规划)
- NYOJ 题目1070 诡异的电梯【Ⅰ】(dp)
- nyoj 诡异的电梯【Ⅰ】 1070 (DP) 好题
- NYOJ-1070-诡异的电梯[Ⅰ]
- nyoj 1070 诡异的电梯 简单dp
- 诡异的电梯【Ⅰ】
- 诡异的电梯【Ⅰ】
- 诡异的电梯【Ⅰ】
- nyoj1070诡异的电梯【Ⅰ】【dp】
- 数塔-诡异的电梯
- nyist 1070 诡异的电梯【Ⅰ】(比较好的DP)
- 一、动态规划(2)奇怪的电梯
- NYOJ 1070 诡异的楼梯(1)
- Android之AsyncTask异步任务——防止内存泄露
- Codeforces 629 D Finals in arithmetic(最大上升子序列和,O(nlogn)、线段树/树状数组)
- 在WPF的TreeView中实现右键选定
- UVa 11478 Halum BellmanFord判负权环
- 欢迎使用CSDN-markdown编辑器
- nyoj 1070 诡异的电梯【Ⅰ】 动态规划
- 说起Android的Binder,我们都能说些什么?
- 新空间RecycleView复习
- ViewPager嵌套Fragment实现item实时更新的两种方式
- 字符串反转(reverse函数)
- 给UICollectionView添加表头
- win32-hookdll,隐藏辅助进程
- POJ - 3069 Saruman's Army 萨鲁曼的大军 贪心 重庆一中高2018级竞赛班第三次测试 2016.7.24 Problem 2
- 复合数据类型