POJ 3282 Ferry Loading IV(類比,隊列),poj3282

來源:互聯網
上載者:User

POJ 3282 Ferry Loading IV(類比,隊列),poj3282

題意   汽車通過渡船過河  渡船開始在左邊   輸入按車輛來的順序輸入河兩岸的車   渡船每次運輸的汽車的總長度不能超過渡船自己本身的長度  先來的車先走   求輪船至少跨河多少次才能將所有的車輛都運完

簡單類比  建兩個隊列  分別裝左邊的車  和右邊的車   算出兩邊各至少需要運輸多少次就行了

#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

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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.