POJ 3282 Ferry Loading IV (simulation, queue)

Source: Internet
Author: User

POJ 3282 Ferry Loading IV (simulation, queue)

A car crosses the river through a ferry. The ferry starts to enter the vehicle order on the left. The total length of the car to be transported on both sides of the river cannot exceed the length of the ferry itself. how many times can a ship cross the river at least run all vehicles?

Simply simulate the creation of two queues to install the left car and the right car to calculate the minimum transportation required on both sides.

#include
 
  #include
  
   #include
   
    using namespace std;int main(){    int cas, lcnt, rcnt, on,n,m,l;    char s[10];    scanf ("%d", &cas);    while (cas--)    {        queue
    
      le, ri;        scanf ("%d%d", &n, &m);        n *= 100;        while (m--)        {            scanf ("%d%s", &l, s);            if (s[0] == 'l') le.push (l);            else ri.push (l);        }        lcnt = on = 0;        while (!le.empty())        {            while (!le.empty() && on + le.front() < n)                on += le.front(), le.pop();            ++lcnt, on = 0;        }        rcnt = on = 0;        while (!ri.empty())        {            while (!ri.empty() && on + ri.front() < n)                on += ri.front(), ri.pop();            ++rcnt, on = 0;        }        if (lcnt > rcnt) printf ("%d\n", 2 * lcnt - 1);        else printf ("%d\n",2 * rcnt);    }    return 0;}
    
   
  
 

Ferry Loading IV

Description

Before bridges were common, ferries were used to transport cars into SS 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 isL-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 containsC, The number of test cases. Each test case beginsL, m.MLines 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

420 4380 left720 left1340 right1040 left15 4 380 left720 left1340 right1040 left15 4 380 left720 left1340 left1040 left15 4 380 right720 right1340 right1040 right

Sample Output

3356


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.