[openjudge] Elevator Stopping Plan

描述
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.
输入
The input consists of several test cases. Each test case is in a single line as the following:
n f1 f2 ... fn

It means, there are totally n floors at which the elevator need to stop, and n = 0 means no testcases
any more. f1 f2 ... fn are the floors at which the elevator is to be stopped (1<=n<=30000, 2<=f1< f2 ... fn<=30000). Every number is separated by a single space.
输出
For each testcase, output the time the last reading person needs in the a single line
样例输入
3 4 5 10
1 2
0
样例输出
46
4


分析:可以将这题转换为可行性问题——对于时间t内,是否能到达所有的楼层。

贪心法则:如果有乘客需要在第i层停靠,那么电梯最好应该停的楼层越高越好,假设停靠的楼层为j,(因为可以从j下到i)那么有临界方程 10*cnt + (j-1) * 4 + (j-i)*20 =t,其中cnt为之前已经停得次数,解得j=(t-10*cnt+20*i+4)/24。
然后你会发现你停在j层,这时乘客可以向上走,只要时间不超过t:这时有临界方程(i-j)*20+10*cnt+(j-1)*4 = t 。得i=(t-10*cnt+16*j+4)/20.

总的来说,电梯停在某一层,在时间t内控制的是包含这个停在的楼层的一段区域(以要到达的楼层为右边界)。

二分实现。

#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++)//!!从1楼可以直接走到到time/20+1楼
    {
        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)>>1;
            if (check(mid)) r=mid;
            else l=mid+1;
        }
        cout << r<<endl;
    }
    return 0;
}


posted @ 2016-11-13 18:55  Mr.doublerun  阅读(18)  评论(0)    收藏  举报