首页 > 代码库 > POJ 3282 Ferry Loading IV(模拟)

POJ 3282 Ferry Loading IV(模拟)

Description

Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river‘s current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.

There is an l-meter-long ferry that crosses the river. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long as it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up cars that are waiting to cross as long as they fit on its deck. The cars are loaded in the order of their arrival; ferry‘s deck accommodates only one lane of cars. The ferry is initially on the left bank where it broke and it took quite some time to fix it. In the meantime, lines of cars formed on both banks that await to cross the river.

Input

The first line of input contains c, the number of test cases. Each test case begins with l, mm lines follow describing the cars that arrive in this order to be transported. Each line gives the length of a car (in centimeters), and the bank at which the car arrives ("left" or "right").

Output

For each test case, output one line giving the number of times the ferry has to cross the river in order to serve all waiting cars.

Sample Input

4
20 4
380 left
720 left
1340 right
1040 left
15 4 
380 left
720 left
1340 right
1040 left
15 4 
380 left
720 left
1340 left
1040 left
15 4 
380 right
720 right
1340 right
1040 right

Sample Output

3
3
5
6

Source




坑爹的题,题目读半天;


题意:船刚开始在左边,刚开始给出船的长度,单位是米,后来给出左右货物的来到顺序和货物的长度,单位居然是厘米 !!!,吧左边货物运到右边,右边的运到左边,还有题目说货物必须先来的先运,我居然以为必须严格的先后顺序,例如船  100  3   50 left  50  rigt  50 left  我以为必须把右边的那个运完再运左边第二个50,搞得错4发,其实不是的 可以左边两个一起(和小于船的长度),再运右边⊙﹏⊙b汗,后来还有10分钟的时候终于a了,哎语文和英语不行啊。。。。。。




#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;

#define N 100005

int ll[N],rr[N];  //分别存左边和右边

int main()
{
    int t,i,n,len;

    scanf("%d",&t);

    while(t--)
    {


        scanf("%d%d",&len,&n);
        len*=100;//米转化成厘米

         if(n==0)
        {
            printf("0\n");
            continue;
        }

        char c[10];
        int x;

         int xx,yy;
         xx=yy=0;  


      int ans=0;
        for(i=0;i<n;i++)
            {
                scanf("%d%s",&x,c);
                if(c[0]=='l')  //左边
                    ll[xx++]=x;
                else
                    rr[yy++]=x; //右边
            }


        int flag=0;

        int temp=0;
         int j;

          i=j=0;

        for(i=j=0;i<xx||j<yy;)  //左边和右边都运完
        {

            temp=0;
            ans++;  //去右边

             if(i==xx&&j==yy)
                {
                    ans--;
                    break;
                 }

            while(i<xx&&temp+ll[i]<=len)//这里是上面去右边之前运的左边的货物
               {
                   temp+=ll[i];
                   i++;
               }


            temp=0;

             ans++;  //回到左边

              if(i==xx&&j==yy)   //这里很重要,当把货物运到右边时全部运完了,这里就不用到左边去了
                ans--;

            while(j<yy&&temp+rr[j]<=len) //同上
               {
                   temp+=rr[j];
                   j++;
               }


        }

        printf("%d\n",ans);

    }
    return 0;
}