Elevator Stopping Plan
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1896 | Accepted: 631 |
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)*4seconds 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)*10seconds (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)*20seconds 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=12second, then it will stop 10 seconds, then it will arrive 10 th floor at 3*4+10+6*4=46second. 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=32second 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.
There are H floors in GaoKe Hall. It takes 4 seconds for the elevator to raise one floor. It means:It costs (n-1)*4seconds 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)*10seconds (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)*20seconds 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=12second, then it will stop 10 seconds, then it will arrive 10 th floor at 3*4+10+6*4=46second. 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=32second 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.
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
Sample Input
3 4 5 10 1 2 0
Sample Output
46 4
题意:有一个电梯,有n个人要上不同的目标层数,每个人可以选择搭电梯或走路,走路上一层或下一层都耗费20s,楼梯上一层要用4s,每次停下来持续10s,最后一个目标层数的停靠时间不计。要求设计一个电梯停靠计划,使得最后一个人到达他的目标层数所用时间最小,输出这个最小时间。
分析:数据范围太大,DP会超时。看了牛人的博客,知道了要用贪心。这里用二分加快答案的查找,给定一个时间t,判断是否在时间t内所有人都能到达自己的目标层数,解空间t的范围为low=0,high=20*(max_fl-1),这里max_fl表示电梯要到达的最高的层数。
AC代码:
#include <cstring> #include <string> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <vector> #include <cstdlib> #include <iostream> using namespace std; bool vis[30005]; int max_fl; bool judge(int t) { int i,j; int num=0; //电梯已停靠次数 i=t/20+2; //在t时间内,t/20+2层以下的人可以走楼梯 while(i<=max_fl) //每次把i作为最后一个人的目标楼层 { while(i<=max_fl&&!vis[i]) //将i增加至某个人要去的目标楼层 i++; if(10*num+4*(i-1)>t) return false; //最后一个人到达目标楼层所用时间超过t,则这个t不可取,返回 j=(t-10*num+20*i+4)/24; //设在j层停,此时j>i,因为把i作为最后一个人的目标楼层,则满足等式t-10*num-4(j-1)-20*(j-i)=0 i=(t-10*num+16*j+4)/20+1; //这时i为满足等式t-10*num-4(j-1)-20(i-j)=0的下一个楼层的位置,此时i>j num++; //停靠次数+1 } return true; } int main() { int n,fl; while(cin>>n,n) { max_fl=0; memset(vis,false,sizeof(vis)); while(n--) { cin>>fl; vis[fl]=true; max_fl=max(max_fl,fl); } int low=0,high=20*(max_fl-1); int mid; while(low<=high) { mid=(low+high)>>1; if(judge(mid)) high=mid-1; else low=mid+1; } cout<<low<<endl; } return 0; }