您的位置:首页 > 运维架构

POJ 1744 Elevator Stopping Plan 以及对二分搜索的思考

2016-04-21 15:08 627 查看
最近+以前做过一些二分加搜索的题目,现在可以总结总结,同时也借一个经典一点的题目参照一下。

我发现比较简单的二分搜索题目都是普通递归或者枚举不方便或者会TLE的题目,有时搜索也会这样不好处理,那么这时,我们可以想一想。

如果答案在数据算出的最大情况和最小情况之间

每个答案可以通过数据检验

检验方式有一定规律的话

那么可以试试二分搜索,这还只是简单的情况。

也许会有题目需要你在过程中二分搜索,

也会有那种外面一个二分搜索,里面一个二分搜索,

当然我也只是想想的话

总之,二分很重要,毕竟它能快速的搜索出满足某种条件的值或者对象。

由于我还不是很老道,做的题也不多,也只能思考到这了。

下面贴题

POJ 1744 Elevator Stopping Plan

Time Limit:1000MS

Memory Limit:30000KB 64bit IO

Format:%I64d & %I64u

Description:

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.

INPUT :

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.

OUTPUT :

For each testcase, output the time the last reading person needs in the a single line

题意:

题意就是有一些人要分别要到f[i]楼去,电梯每上升一层要4秒,人自己走一层要20秒,电梯在某层停会停10秒,问电梯怎么运转可以使所有人都到他们的楼层所花费的时间最短。

很明显答案在最低楼层*4秒到最高座层*20秒之间,

并且这个估算的时间可以用贪心来检验,就是贪心最高楼层,让电梯尽可能停的次数少。

下面是代码:

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[30001];
int n;

bool check(int time) // 贪心判断这个时间是否可以
{
int now = 1 , t = 0 ,j; //t代表的是电梯升和停的时间 ,now代表电梯要升到的楼层
bool flag = false;
for(int i=0;i<n;i++){
if((a[i]-now)*20+t>time){
t += (flag)? 10 :0; //电梯停了一次
if((a[i]-now)*4+t>time) return false; //如果电梯直接升还不行,说明这个时间肯定不满足
for(j = a[i];(j-now)*4+(j-a[i])*20 + t<=time;j++); //找一个要停的点
j--;
t += (j-now)*4;
now = j;
flag = true;
}
}
return true;
}

int main(){
while(scanf("%d",&n),n){
for(int i=0;i<n;i++) scanf("%d",&a[i]);
int minn,maxx;
//sort(a,a+n); 没必要排序
minn = (a[0]-1)*4 - 1;
maxx = (a[n-1]-1)*20;
while(maxx-minn>1){
int mid = (maxx+minn)>>1;
if(check(mid)) maxx = mid;
else minn = mid;
}
printf("%d\n",maxx);

}
return 0;
}


最后这个代码我是看过别人自己才写出来的,总有有一天我也能自己写出来,如果有冒犯请见谅。

PS:不能发表情我表示很无奈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: