nyoj 诡异的电梯【Ⅰ】 1070 (DP) 好题
2015-11-27 21:02
323 查看
诡异的电梯【Ⅰ】
时间限制:1000 ms | 内存限制:65535 KB难度:3
描述
新的宿舍楼有 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个数字表示每个学生想要停的楼层。
输出输出看样例。样例输入
1
3 2 1 1
2 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.思路:
用dp【i】表示从1层上到第 i 层花费的最小的体力。
因为不能在相邻的楼层停留,所以可以从dp【i-2】转移,但这样不是最优的还要从dp【i-3】转移,因为这样的话就可以到达所有的楼层。我们只要在所有的之间dp最优即可。
其他要注意的一个条件是,从dp【i-3】转移时,中间两层的人有四种选择:
1:都上去或都下来
2:上面的上去一层,下面的下来一层
3:上面的下来两层,下面的上去两层,(当时没有考虑到这个,要细心啊)
那么代码就很好写了、
#include<stdio.h> #include<string.h> #include<algorithm> #define N 1000010 #define INF 0x3f3f3f3f using namespace std; int a ; int dp ; int main() { int t,T=1; int n,m,s,x,y; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&n,&m,&s,&x); memset(a,0,sizeof(a)); int mm=min(s,x); for(int i=0;i<m;i++) { scanf("%d",&y); a[y]++; } for(int i=3;i<=n;i++) dp[i]=INF; dp[0]=dp[1]=dp[2]=0; for(int i=3;i<=n;i++) { dp[i]=min(dp[i],dp[i-2]+a[i-1]*mm); if(i>=4) { int tmp=INF; tmp=min(a[i-2]*x*2+a[i-1]*x,a[i-2]*s+a[i-1]*s*2); tmp=min(tmp,a[i-2]*s+a[i-1]*x); tmp=min(tmp,a[i-2]*2*x+a[i-1]*s*2); dp[i]=min(dp[i-3]+tmp,dp[i]); } } printf("Case %d: %d\n",T++,dp ); } return 0; }
#include<stdio.h> #include<string.h> #include<algorithm> #define N 1000010 #define INF 0x3f3f3f3f using namespace std; int a ; int dp ; int main() { int t,T=1; int n,m,s,x,y; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&n,&m,&x,&s); memset(a,0,sizeof(a)); int mm=min(s,x); for(int i=0;i<m;i++) { scanf("%d",&y); a[y]++; } for(int i=3;i<=n;i++) dp[i]=INF; dp[0]=dp[1]=dp[2]=0; for(int i=3;i<=n;i++) { dp[i]=min(dp[i],dp[i-2]+a[i-1]*mm); if(i>=4) { int tmp=INF; tmp=min(a[i-2]*s*2+a[i-1]*s,a[i-1]*x*2+a[i-2]*x); tmp=min(tmp,a[i-1]*s+a[i-2]*x); tmp=min(tmp,a[i-1]*2*x+a[i-2]*s*2); dp[i]=min(dp[i-3]+tmp,dp[i]); } } printf("Case %d: %d\n",T++,dp ); } return 0; }
相关文章推荐
- Linux压缩解压缩文章总结
- My way on Linux - 知识梳理计划
- 拉丁矩阵问题
- hdu 3466 Proud Merchants
- C语言端口扫描
- codeforces 412A Poster
- CodeForces 546C Soldier and Cards (队列)
- ViewPager
- objective - C 下字符串的处理方法
- Oracle例外处理
- PPT动画的若干问题
- 一些有用的HTML5 pattern属性
- TIMESTAMP类型插入到VARCHAR2后转成DATE类型和指定格式字符串
- 智能家居喂鱼系统(五)-树莓派C++库
- 菜鸟学Java----基本类型及其封装类
- 关于switch语句中省略break语句的问题
- c++实现简单的链表
- Java——多人聊天室(基于TCP的网络编程——Socket)
- 局势在恶化
- 分析Android的进程通信机制