POJ 1744 Elevator Stopping Plan
男人8题:第8题
二分+贪心判断答案
Elevator Stopping Plan
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1839 | Accepted: 614 |
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 101 20
Sample Output
464
Source
LouTiancheng@POJ
教主啊
![](http://i.gtimg.cn/qzone/em/e102.gif)
1 #include <iostream> 2 3 using namespace std; 4 int a[30010]; 5 int n; 6 7 int vail(int time) 8 { 9 int pos,np,nt,fl=0; 10 pos=1;nt=0;np=0; 11 for(int i=0;i<n;i++) 12 { 13 if(nt+(a[i]-pos)*20>time) 14 { 15 if(fl) 16 { 17 nt+=10; 18 } 19 else fl=1;//每停下一次加上电梯等待时间 20 21 if(nt+(a[i]-pos)*4>time) return 0;//加上最短的时间都没发完成,TIME不可行 22 23 for(np=a[i];(np-pos)*4+20*(np-a[i])+nt<=time;np++) ;//电梯尽可能向上走,只要当前的i可以满足np~a[i]之间的都能到达。 24 25 np--; 26 nt+=(np-pos)*4; 27 28 pos=np; 29 } 30 } 31 return 1; 32 } 33 34 int main() 35 { 36 while(cin>>n&&n) 37 { 38 for(int i=0;i<n;i++) 39 { 40 cin>>a[i]; 41 } 42 int st=(a[n-1]-1)*4-1;//修正误差 43 int et=(a[n-1]-1)*20; 44 int mt; 45 while(et-st>1) 46 { 47 mt=(st+et)/2; 48 49 if(vail(mt)) 50 { 51 et=mt; 52 } 53 else 54 st=mt; 55 } 56 57 cout<<et<<endl; 58 } 59 60 61 return 0; 62 }