贪心算法之Elevator Stopping Plan

 

Elevator Stopping Plan

Description

ZSoft Corp. is asoftware company in GaoKe Hall. And the workers in the hall are veryhard-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 ofcompanies in it. Every morning, people must waste a lot of time waiting for theelevator. 
Hal, a smart guy in ZSoft, wants to change this situation. He wants to find away to make the elevator work more effectively. But it’s not an easy job. 
There are 31 floors in GaoKe Hall. It takes 4 seconds for the elevator to raiseone floor. It means: 
It costs (31-1)*4=120 seconds if the elevator goes from the 1st floor to the31st floor without stop. And the elevator stops 10 second once. So, if theelevator stops at each floor, it will cost 30*4+29*10 = 410 seconds (It is notnecessary to calculate the stopping time at 31st floor). In another way, ittakes 20 seconds for the workers to go up or down one floor. It takes 30*20 =600 seconds for them to walk from the 1st floor to the 31st floor. Obviously,it is not a good idea. So some people choose to use the elevator to get a floorwhich is the nearest to their office. 
After thinking over for a long time, Hal finally found a way to improve thissituation. He told the elevator man his idea: First, the elevator man asks thepeople which floors they want to go. He will then design a stopping plan whichminimize the time the last person need to arrive the floor where his officelocates. For example, if the elevator is required to stop at the 4th, 5th and10th floor,the stopping plan would be: the elevator stops at 4th and 10thfloor. Because the elevator will arrive 4th floor at 3*4 = 12 second, then itwill stop 10 seconds, then it will arrive 10th floor at 3*4+10+6*4 = 46 second.People who want to go 4th floor will reach their office at 12 second, peoplewho want to go to 5th floor will reach at 12+20 = 32 second and people who wantto go to 10th floor will reach at 46 second. Therefore it takes 46 seconds forthe 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 thestopping plan,which minimize the time the last person needs to arrive at hisfloor.

Input

The input consistsof several testcases. Each testcase 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 theelevator is to be stopped (n <= 30, 2 <= f1 < f2 ... fn <= 31).Every number is separated by a single space.

Output

For each testcase,output the time the last reading person needs in the first line and thestopping floors in the second line. Please note that there is a summary of thefloors at the head of the second line. There may be several solutions, anyappropriate one is accepted. No extra spaces are allowed.

Sample Input

3 4 5 10

1 2

0

Sample Output

46

2 4 10

4

1 2

 

 

题目意思:电梯上一层楼需要4s,在一层楼停留10s,人上一层楼需20s。现在需要设计一个算法,使得最后到达办公室的人所需的时间最短。

解题思路:

本题解题思路为贪心算法+最小二分法

在本题中,花费时间最短为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开始考虑乘客需要停靠的楼层即可。

 

根据上面的思想,得出的程序为:

#include<iostream>

using namespacestd;

 

#define maxfloor35           //最大楼层数

#defineelevatorTime 4     //电梯上升一层楼所需时间

#define stopTime10          //电梯在一层楼中停留时间

#define peopleTime20      //人走一层楼所需时间

 

intfloors[maxfloor];   //存储需要停的楼层,floors[i]=1表示第i层需要停留

intstop[maxfloor];             //存储电梯最终停留的楼层

int n;                                //需要停的楼层的总数

int topfloor;                   //停的楼层最高的楼层

int mid;                            //时间变量,最终为最短时间即输出时间

int cnt;                      //电梯实际停的次数

 

void result();                  //求结果函数

bool judge(inttmp);    //判断tmp时间够不够

int solve();               //最终解决方案

 

int main()

{

       cin>>n;

       while(n!=0)

       {

              result();

              cin>>n;

       }

       return 0;

}

void result()

{

       int t;

       for(int i=0;i<maxfloor;i++)

       {

              //清空楼层

              floors[i]=0;

       }

       for(int i=0;i<n;i++)

       {

              //由于输入是按照升序排列的,因此最后输入的楼层就是最高的

              cin>>t;

              floors[t]=1;

              topfloor=t;

       }

       judge(solve());

       cout<<mid<<endl;

       cout<<cnt;

       for (int i = 0; i < cnt; i++)

              cout<<""<<stop[i];

       cout<<endl;

}

bool judge(inttmp)

{

    cnt = 0;

    memset(stop, 0, sizeof(stop));

       //由于楼层是从1开始计算的,中间间隔 tmp / peopleTime,因此从tmp / peopleTime + 2开始查找

    for (int i = tmp / peopleTime + 2; i <=topfloor; i++)

       {

              //while循环用来找需要停下的楼层

              while (!floors[i] && i< topfloor) 

                     i++;

              //t表示运行到i时需要的时间

              int t = (i - 1) * elevatorTime +cnt * stopTime;

              if (tmp < t)

                     return false;

              //根据公式计算

              t = (tmp - stopTime * cnt +peopleTime * i + elevatorTime) / (peopleTime+elevatorTime);

              i = (tmp - stopTime * cnt +(peopleTime-elevatorTime) * t + elevatorTime) / peopleTime;

              stop[cnt++] = t;

       }

    return true;

}

 

int solve()

{

       //L 最短时间,R最长时间

    int L = 0, R = (stopTime+elevatorTime)*(topfloor - 1);

    while (L < R)

       {

              mid = (L + R + 1) / 2;

              if (mid == R)

                     break;

              if (judge(mid))

                     R = mid;

              else

                     L = mid;

    }

    return mid;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值