Solution of 11034 - Ferry Loading IV

Problem Description
source:https://uva.onlinejudge.org/external/110/11034.html

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, m. m 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

1 1 3 20 left 81 left 99 left

Sample Output  

5


Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
long int transfer,l;
queue<long int> leftq;
queue<long int> rightq;
void LeftCheck();
void RightCheck();


int main()
{
    long int i,j,t,m,carlen;
    string side;
    while(scanf("%ld",&t)==1)
    {
        for(i=1;i<=t;i++)
        {
            transfer=0;
            scanf("%ld%ld",&l,&m);
            for(j=1;j<=m;j++)
            {
                cin>>carlen>>side;
                if(side.compare("left")==0)
                    leftq.push(carlen);
                else
                    rightq.push(carlen);
            }
            while(!leftq.empty() || !rightq.empty())
            {
                if(!leftq.empty() || !rightq.empty())
                    LeftCheck();
                if(!leftq.empty() || !rightq.empty())
                    RightCheck();
            }
            printf("%ld\n",transfer);
        }
    }
    return 0;
}

void LeftCheck()
{
    int Ltotal=0;
    transfer+=1;
    if(leftq.empty())
        return;
    else
    {
        while(Ltotal+ leftq.front()<=l*100 && !leftq.empty())
        {
            Ltotal+=leftq.front();
            leftq.pop();
        }
    }
}

void RightCheck()
{
    int Rtotal=0;
    transfer+=1;
    if(rightq.empty())
        return;
    else
    {
        while(Rtotal+rightq.front()<=l*100 && !rightq.empty())
        {
           Rtotal+=rightq.front();
           rightq.pop();
        }
    }
}

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience