XTU 1206Dormitory's Elevator(dp)

Dormitory's Elevator

Accepted : 21 Submit : 191
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


Source

daizhenyang 
首先要明确尽可能的电梯多停,画一下图,就能明白最开始停的位置为1或2,后面要停的楼i 可能是从第i-2或i-3层。
#include<stdio.h>
int main()
{
    int t,tt=0,n,m,k[100005],floot;
    __int64 A,B,dp[100005];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%I64d%I64d",&n,&m,&A,&B);
        for(int i=0;i<=n;i++)
            k[i]=0;

        while(m--)
        {
            scanf("%d",&floot);k[floot]++;
        }
        __int64 a,b,c,d,e;
        dp[1]=0; dp[2]=0;
        for(int i=3; i<=n;i++)
        {
            b=(1<<30);
            if(B<=A)
                a=B*k[i-1]+dp[i-2];
            else
                a=A*k[i-1]+dp[i-2];
            if(i-3>0)
            {
                b=B*(k[i-2]+k[i-1]*2)+dp[i-3];
                c=B*k[i-2]+A*k[i-1]+dp[i-3];
                d=A*k[i-2]*2+B*k[i-1]*2+dp[i-3];
                e=A*(k[i-2]*2+k[i-1])+dp[i-3];
                if(b>c)b=c; if(b>d)b=d; if(b>e)b=e;
            }
            if(a>b)a=b;
            dp[i]=a;
        }
        printf("Case %d: %I64d\n",++tt,dp[n]);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值