poj1744

1.链接地址

https://vjudge.net/problem/POJ-1744

2.问题描述

ZSoft Corp. is a software company in GaoKe Hall. And the workers in the hall are very hard-working. But the elevator in that hall always drives them crazy. Why? Because there is only one elevator in GaoKe Hall, while there are hundreds of companies in it. Every morning, people must waste a lot of time waiting for the elevator.Hal, a smart guy in ZSoft, wants to change this situation. He wants to find a way to make the elevator work more effectively. But its not an easy job. 
There are H floors in GaoKe Hall. It takes 4 seconds for the elevator to raise one floor. It means:It costs (n-1)*4seconds if the elevator goes from the 1 st floor to the nth floor without stop. And the elevator stops 10 second once. So, if the elevator stops at each floor, it will cost (n-1)*4+(n-2)*10seconds (It is not necessary to calculate the stopping time at nth floor). In another way, it takes 20 seconds for the workers to go up or down one floor. It takes (n-1)*20seconds for them to walk from the 1 st floor to the nth floor. Obviously, it is not a good idea. So some people choose to use the elevator to get a floor which is the nearest to their office. 
After thinking over for a long time, Hal finally found a way to improve this situation. He told the elevator man his idea: First, the elevator man asks the people which floors they want to go. He will then design a stopping plan which minimize the time the last person need to arrive the floor where his office locates. For example, if the elevator is required to stop at the 4 th , 5 th and 10 th floor, the stopping plan would be: the elevator stops at 4 th and 10 th floor. Because the elevator will arrive 4 th floor at 3*4=12second, then it will stop 10 seconds, then it will arrive 10 th floor at 3*4+10+6*4=46second. People who want to go 4 th floor will reach their office at 12 
second, people who want to go to 5 th floor will reach at 12+20=32second and people who want to go to 10 th floor will reach at 46 second. Therefore it takes 46 seconds for the last person to reach his office. It is a good deal for all people. 
Now, you are supposed to write a program to help the elevator man to design the stopping plan,which minimize the time the last person needs to arrive at his floor.

输入样例

3 4 5 10
1 2
0

输出样例

46
4

3.解题思路

所以说做算法题目一定要有数学头脑。。。

这是一道贪心题目,告诉你电梯会在哪几层停留,问你到达最后一层的人需要多少时间。

因为时间是有边界的,最短为0秒,最多为每一层都爬楼梯的时间,所以可以用二分法。

假设当前时间为t,则对于那些在t时间内爬楼梯可以到达的则可以不考虑(t时间内最多可以爬t/20+1)。需要从(t/20+2)开始考虑。从t/20+2层开始搜索乘客需要停靠的楼层,如果有乘客需要在第i层停靠,那么电梯最好应该停在哪一层才能使得想要去i层的乘客在t时间内到达i层呢?自然是停的楼层越高越好,假设停靠的楼层为j,那么有临界方程 10*cnt + (j-1) * 4 + (j-i)*20 =t,其中cnt为之前已经停得次数,解得j=(t-10*cnt+20*i+4)/24。

在电梯停在j层之时,有部分乘客可以下电梯步行到j之上的楼层,并且会在t时间内到达,这部分楼层不必考虑,设j之上的最高楼层为i,这时有临界方程(i-j)*20+10*n+(j-1)*4 = t,解得i=(t-10*cnt+16*j+4)/20,下回就从i=(t-10*cnt+16*j+4)/20+1开始考虑乘客需要停靠的楼层即可。
(参考https://blog.youkuaiyun.com/hqf1992/article/details/47700445

4.算法实现源代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 300010
int f[N],n;
int F;
bool check(int time)
{
    int cnt=0;
    for (int i=time/20+2;i<=F;i++)
    {
        while (!f[i] && i<F) 
        {
            i++;
        }
        if ((i-1)*4+cnt*10 >time) 
            return 0;
        int j=(20*i-10*cnt+4+time)/24;
        i=(16*j+time+4-10*cnt)/20 ;
        cnt++;
    }
    return 1;
}

int main()
{
    while (scanf("%d",&n) && n)
    {
        memset(f,0,sizeof f);
        for (int i=1;i<=n;i++) 
        {
            scanf("%d",&F);
            f[F]=1;
        }
        int l=0,r=F*20;
        while (l < r)
        {
            int mid=(l+r)/2;
            if (check(mid)) r=mid;
            else l=mid+1;
        }
        cout << r<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/KasenBob/p/11217471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值