POJ 3282 Ferry Loading IV(模拟,队列)

题意   汽车通过渡船过河  渡船开始在左边   输入按车辆来的顺序输入河两岸的车   渡船每次运输的汽车的总长度不能超过渡船自己本身的长度  先来的车先走   求轮船至少跨河多少次才能将所有的车辆都运完

简单模拟  建两个队列  分别装左边的车  和右边的车   算出两边各至少需要运输多少次就行了

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int main()
{
    int cas, lcnt, rcnt, on,n,m,l;
    char s[10];
    scanf ("%d", &cas);
    while (cas--)
    {
        queue<int> 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 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

POJ 3282 Ferry Loading IV(模拟,队列),布布扣,bubuko.com

时间: 2025-01-06 21:13:32

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 cross

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 cross

POJ 2336 Ferry Loading II

POJ 2336 Ferry Loading II 贪心 || 伪dp Description:NULL ( 233333因为我懒 ) 哦..这个解题报告是我从lzy自带解题报告的代码里摘来的233 最早到达对岸的时间,取决于最后一辆车的被运送时间最优解是最后一辆车能够被尽早的运走if(a > dp[i - j]) dp[i] = min(dp[i], 2 * t + a);else dp[i] = min(dp[i], 2 * t + dp[i - j]); dp[i]表示第i个车到达对岸花的

POJ 3087 Shuffle&#39;m Up (模拟+map)

题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s1,最顶的c块牌归为s2,依此循环下去. 现在输入s1和s2的初始状态 以及 预想的最终状态s12.问s1 s2经过多少次洗牌之后,最终能达到状态s12,若永远不可能相同,则输出"-1". 解题思路:照着模拟就好了,只是判断是否永远不能达到状态s12需要用map,定义map<

php+mysql 模拟队列发送邮件

1.使用场景 主要解决PHP开发过程中,常见的发送多封邮件时页面卡死的问题,如果需要给网站所有用户发送一封系统通知邮件,假设网站有10000个注册用户,发送每封邮件需要0.1秒,直接发送导致页面耗时长卡死,因此就要用到队列的知识. 2.建表语句 create table users ( user_id int(5) not null auto_increment, user_email varchar(40) not null, user_password char(32) not null,

POJ Ikki&#39;s Story IV - Panda&#39;s Trick [2-SAT]

题意: 圆上n个点,m对点之间连边,连在园内或园外,所有边不相交是否可行 发现两对点连线都在内相交则都在外也相交,那么只有一个在内一个在外啦,转化为$2-SAT$问题 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int N=1005,M=5e5; t

poj-2336 Ferry Loading II(dp)

题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: 1985 Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a

POJ 3670 Eating Together 二分单调队列解法O(nlgn)和O(n)算法

本题就是一题LIS(最长递增子序列)的问题.本题要求求最长递增子序列和最长递减子序列. dp的解法是O(n*n),这个应该大家都知道,不过本题应该超时了. 因为有O(nlgn)的解法. 但是由于本题的数据特殊性,故此本题可以利用这个特殊性加速到O(n)的解法,其中的底层思想是counting sort分段的思想.就是如果你不会counting sort的话,就很难想出这种优化的算法了. O(nlgn)的单调队列解法,利用二分加速是有代表性的,无数据特殊的时候也可以使用,故此这里先给出这个算法代码

[ACM] poj 2823 Sliding Window(单调队列)

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 36212   Accepted: 10723 Case Time Limit: 5000MS Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left