POJ - 3282:Ferry Loading IV(队列)

原题链接
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
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

题目描述

河的两岸分别停放着汽车,需要通过船将左边的汽车运到右岸,右边的汽车运到左岸,每辆车有长度以及停放在河岸(左、右)边,船的长度为l,求船需要多少趟才能运完汽车(船刚开始在左岸)
思路1:将左右边的汽车存入到队列当中,分别计算船将左右边汽车运完需要的次数,然后处理两边次数即可(注意:船刚开始在左岸)
思路2:也可以将两个队列放在一个循环内处理,这样就不用分别计算左右次数,可以统一计算船运的次数
:思路1
代码如下

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>

using namespace std;

int main()
{
   
    int t;
    scanf("%d",&t);
    while(t--)
    {
   
        int i,l,m,len;//len车的长度
        scanf("%d %d"<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值