NYIST 1070 诡异的电梯【Ⅰ】
诡异的电梯【Ⅰ】
时间限制: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.
来源
翻译【2014湘潭邀请赛】
上传者
ACM_钟诗俊
解题:动态规划。dp[i]表示在i层停下,现在假设在i层停下,那么上一次停的曾可以是i-2或者是i-3
假设是i-2。那么去i-1的人可以做到i再下一层,或者直接在i-2的时候走上一层,min(up,down)*a[i-1]
假设在i-3层停的,那么情况是,i-2的人可以做到i下去两层,或者从i-3走上一层,同理i-1的人。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 100100; 18 int a[maxn],dp[maxn],up,down,n,m; 19 int main() { 20 int t,tmp,cs = 1; 21 scanf("%d",&t); 22 while(t--){ 23 scanf("%d %d %d %d",&n,&m,&down,&up); 24 memset(dp,0x3f,sizeof(dp)); 25 memset(a,0,sizeof(a)); 26 for(int i = 0; i < m; ++i){ 27 scanf("%d",&tmp); 28 a[tmp]++; 29 } 30 dp[0] = dp[1] = dp[2] = 0; 31 for(int i = 3; i <= n; ++i){ 32 dp[i] = min(dp[i],dp[i-2]+min(up,down)*a[i-1]); 33 int x = min(a[i-1]*down,a[i-1]*up*2); 34 int y = min(a[i-2]*up,a[i-2]*down*2); 35 dp[i] = min(dp[i],dp[i-3]+x+y); 36 } 37 printf("Case %d: %d\n",cs++,dp[n]); 38 } 39 return 0; 40 }