POJ 1744 Elevator Stopping Plan(贪心)

题目链接:http://poj.org/problem?id=1744

题意:n个人从一栋楼的1层要向上达到自己要达到的某一层。电梯上一层需要4秒,人走一层需要20秒,电梯从停下到开启需要10秒。问最后一个到达自己目的地的人需要的时间最少是多少?

思路:二分答案,电梯每次尽可能地向上走。。

 

View Code
 1 #include <stdio.h>

 2 

 3 

 4 const int INF=1000000000;

 5 int a[30005],n;

 6 

 7 

 8 int OK(int t)

 9 {

10     int curpos=0,prepos=1,cost=0,flag=0,i;

11     for(i=0;i<n;i++) if((a[i]-prepos)*20+cost>t)

12     {

13         if(flag) cost+=10;

14         else flag=1;

15         if((a[i]-prepos)*4+cost>t) return 0;

16         curpos=a[i];

17         while((curpos-prepos)*4+(curpos-a[i])*20+cost<=t)

18             curpos++;

19         curpos--;

20         cost+=(curpos-prepos)*4;

21         prepos=curpos;

22     }

23     return 1;

24 }

25 

26 int main()

27 {

28     while(scanf("%d",&n),n)

29     {

30         int i,low=0,high=INF,mid;

31         for(i=0;i<n;i++) scanf("%d",&a[i]);

32         while(low<=high)

33         {

34             mid=(low+high)>>1;

35             if(OK(mid)) high=mid-1;

36             else low=mid+1;

37         }

38         if(OK(high)) printf("%d\n",high);

39         else printf("%d\n",low);

40     }

41     return 0;

42 }

 

 

 

 

你可能感兴趣的:(ping)