POJ 3282

题意:船载车过河,车有长度,船也有长度,给出每辆车来的顺序以及到达的岸,问最少渡几次。

题解:贪心,能装多少装多少,每次都尽可能多装。

View Code
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 #include<list>

 5 using namespace std;

 6 list<int> ql,qr;

 7 int main()

 8 {

 9     int T;

10     for(scanf("%d",&T);T;T--)

11     {

12         int len,n,x,y,cnt1,cnt2;

13         char s[10];

14         scanf("%d%d",&len,&n);

15         len*=100;

16         ql.clear();

17         qr.clear();

18         while(n--)

19         {

20             scanf("%d%s",&x,&s);

21             if(s[0]=='l')

22                 ql.push_back(x);

23             else

24                 qr.push_back(x);

25         }

26         cnt1=cnt2=0;

27         while(!ql.empty())

28         {

29             x=len;

30             y=ql.front();

31             while(x-y>0&&!ql.empty())

32             {

33                 ql.pop_front();

34                 x-=y;

35                 y=ql.front();

36             }

37             cnt1++;

38         }

39         while(!qr.empty())

40         {

41             x=len;

42             y=qr.front();

43             while(x-y>0&&!qr.empty())

44             {

45                 qr.pop_front();

46                 x-=y;

47                 y=qr.front();

48             }

49             cnt2++;

50         }

51         if(cnt1==cnt2)

52             cnt1+=cnt2;

53         else if(cnt1>cnt2)

54             cnt1=cnt1*2-1;

55         else

56             cnt1=cnt2*2;

57         printf("%d\n",cnt1);

58     }

59     return 0;

60 }

你可能感兴趣的:(poj)