XTU Dormitory's Elevator(动态规划)
来源:互联网 发布:淘宝后台登录 编辑:程序博客网 时间:2024/06/09 19:55
Dormitory's Elevator
Accepted : 70 Submit : 394Time Limit : 1000 MS Memory Limit : 65536 KBProblem 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
13 2 1 12 3
Sample Output
Case 1: 1
#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(动态规划)
- XTU 1206 Dormitory's Elevator
- XTU 1206 Dormitory's Elevator
- XTU 1206Dormitory's Elevator(dp)
- POJ2392 Space Elevator 动态规划
- poj 2392 Space Elevator(动态规划)
- poj 动态规划DP - 2392 Space Elevator
- 暑假- 动态规划 I-(U - Space Elevator)
- 动态规划 problem S
- POJ2392 Space Elevator ——动态规划初步
- 【动态规划】Loser's Spring
- HashMap v.s. 动态规划
- XTU 1185 Bob's Problem
- XTU-1185 Bob's Problem
- XTU 1247 Robb's Problem
- XTU 1244 Estrella's Chocolate
- XTU 1245 Lisa’s Puzzle
- [动态规划]Pku2411--Mondriaan's Dream
- Java删除文件夹和文件
- 编程规范 --- 代码编辑、编译、审查
- Xcode7.3工具解析App崩溃日志(.crash文件)
- Mariadb
- 【算法学习】快包算法
- XTU Dormitory's Elevator(动态规划)
- c++ string split分割字符串
- Qt设置窗体的透明度: setWindowOpacity()
- php实习(四)
- interface和abstract的区别
- 编程规范 --- 代码测试、维护
- 箭无虚发
- linux中手动安装的软件的快捷方式的创建
- 开机SetupWizard界面时区显示异常