Dormitory's Elevator

Accepted : 70   Submit : 394
Time Limit : 1000 MS   Memory Limit : 65536 KB

Problem Description

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.

Input

First line contain an integer T, there are T(1≤T≤10) cases. For each case T, there are two lines. First line: The number of floors N(1≤N≤100000), and the number of students M(1≤M≤100000),A,B(1≤A,B≤100) Second line: M integers (2≤A[i]≤N), the student's desire floor.

Output

Output case number first, then the answer, the minimum of the total cost of student's walking cost.

Sample Input

1
3 2 1 1
2 3

Sample Output

Case 1: 1
解题思路:这个题最开始想开二维dp[i][j],一看数据就pass了。然后想了下,dp[i]表示电梯停到第i层楼时,学生到1-i层楼的最小化费。这样为了保证最小花费,肯定要尽可能多的使用电梯,那么dp[i]的状态首先肯定是可以从dp[i-2]转移过来的,如果仅仅只有dp[i-2],那么电梯就可能会停到2-4-6.....,中间停奇数层的可能就被忽略了。所以会有dp[i-3]的情况,保证了奇偶层都可能停,dp[i-4]就没有必要了,因为肯定会坐电梯上来的。这样在第i层,i-1层,i-2层之间进行状态转移。详见代码。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;const int maxn = 100005;
int n,m,A,B,dp[maxn],F[maxn],cnt[maxn];int MIN(int a,int b,int c,int d)
{if(a <= b && a <= c && a <= d) return a;if(b <= a && b <= c && b <= d) return b;if(c <= a && c <= b && c <= d) return c;if(d <= a && d <= b && d <= c) return d;
}int main()
{int t,cas = 1;scanf("%d",&t);while(t--){scanf("%d%d%d%d",&n,&m,&A,&B);memset(cnt,0,sizeof(cnt));memset(dp,0,sizeof(dp));for(int i = 1; i <= m; i++){scanf("%d",&F[i]);cnt[F[i]]++;}dp[1] = dp[2] = 0;dp[3] = min(A,B)*cnt[2];for(int i = 4; i <= n; i++){dp[i] = dp[i-2] + min(A,B)*cnt[i-1];int tmp1 = cnt[i-1]*A + cnt[i-2]*2*A; //从i楼下到i-1和i-2楼int tmp2 = cnt[i-1]*2*B + cnt[i-2]*B; //从i-3楼上到i-1和i-2楼int tmp3 = cnt[i-1]*A + cnt[i-2]*B; //i楼下到i-1楼,i-3楼上到i-2楼int tmp4 = cnt[i-2]*2*A + cnt[i-1]*2*B; //i楼下到i-2楼,i-3楼上到i-1楼dp[i] = min(dp[i],dp[i-3] + MIN(tmp1,tmp2,tmp3,tmp4));}int ans = dp[n];ans = min(ans,dp[n-1]+cnt[n]*B);ans = min(ans,dp[n-2]+cnt[n]*2*B + cnt[n-1]*B);printf("Case %d: %d\n",cas++,ans);}return 0;
}

XTU Dormitory's Elevator(动态规划)相关推荐

  1. XTUOJ 1206 Dormitory's Elevator

    Dormitory's Elevator Time Limit : 1000 MS   Memory Limit : 65536 KB Problem Description The new dorm ...

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

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

  3. 杭电oj题目题型分类(转)

    1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 Hero In Maze 广度搜索 ...

  4. HDOJ题目分类大全

    版权声明:本文为博主原创文章,欢迎转载,转载请注明本文链接! https://blog.csdn.net/qq_38238041/article/details/78178043 杭电里面有很多题目, ...

  5. HDU题目分类大全【大集合】

    基础题: 1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.  1032.1037.1040.1048.1056.105 ...

  6. 杭电OJ题目分类(转载)

    1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 Hero In Maze 广度搜索 ...

  7. hdoj杭电问题分类

    杭电上的题虽然多,但是一直苦于找不到问题分类,网页都是英文的,所以平时做题也没怎么看,今天仔细一看,问题分类竟然就在主页....做了那么久的题居然没发现,表示已经狗带..不要笑,不知道有没有像我一样傻 ...

  8. 转载:Hdu 题目分类

    原址点击 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056. ...

  9. HD OJ 题目分类

    动态规划:2037 2054 2084 hdu题目分类(转) http://blog.csdn.net/vsooda/article/details/7258791 1001 整数求和 水题 1002 ...

最新文章

  1. IBM大裁70% 员工,撕掉了国内大批伪AI企业最后一块遮羞布!
  2. no output in console for unittests in pycharm 2017
  3. 8个独立按键控制LED
  4. 第3周实践项目3 求集合并集
  5. 开发一个爆款 VS Code 插件这么简单!
  6. 键盘输入Scanner类方法属性使用
  7. python 中移去文件的只读属性
  8. Spring Boot + JPA + Oracle 自增长字段实现示例
  9. js三级分类菜单导航
  10. 使用Masonry让cell高度自适
  11. Java每隔5秒查找redis_Redis利用Pipeline加速查询速度的方法
  12. per_cpu机制的详解
  13. 读书笔记——《思维的乐趣matrix67数学笔记》
  14. .net reactor 学习系列(五)---.net reactor针对De4Dot脱壳工具的应对
  15. 元进Python(3)---如何实现IP子网掩码计算器功能?
  16. django微信公众号开发入门详细指南
  17. 公司邮箱怎么用微信收邮件?企业微信邮箱原来这么方便!
  18. 零伽壹浅谈:区块链技术在审计工作的应用
  19. Pandas熊猫框架
  20. Cadence 应用注意事项--转载

热门文章

  1. 寻人 | 一个有趣的职业可能性,与你分享
  2. 聊聊tomcat jdbc pool的默认参数及poolSweeper
  3. 解决:MVC对象转json包含\r \n
  4. Java窗口(JFrame)从零开始(8)——文本框+文本域+密码框
  5. Cisco路由器基础安全配置---特权模式和VTY线路密码
  6. 【转】Docker —— 从入门到实践
  7. Android 基础教程之-------Android ProgressBar的使用!
  8. Unity AOP 处理异常的方法
  9. Machine Monitoring System Document
  10. social science and IP