24h購物| | PChome| 登入
2013-03-18 11:37:57| 人氣1,313| 回應0 | 上一篇 | 下一篇

[UVA][Queue] 11034 - Ferry Loading IV

推薦 0 收藏 0 轉貼0 訂閱站台

Problem A: 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.

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").

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

Output for sample input

3
3
5
6

Piotr Rudnicki

#include <stdio.h>
#include <queue>
using namespace std;

int main() {
    int t, n, m;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &n, &m);
        n = n*100;
        queue<int> L, R;
        int l;
        char s[50];
        while(m--) {
            scanf("%d %s", &l, s);
            if(s[0] == 'l')
                L.push(l);
            else
                R.push(l);
        }
        int flag = 0, res = 0;
        while(!L.empty() || !R.empty()) {
            res++;
            int sum = 0;
            if(flag) { // right
                while(!R.empty() && sum + R.front() <= n)
                    sum += R.front(), R.pop();
            } else {
                while(!L.empty() && sum + L.front() <= n)
                    sum += L.front(), L.pop();
            }
            flag = 1-flag;
        }
        printf("%d\n", res);
    }
    return 0;
}

台長: Morris
人氣(1,313) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][字串處理] 464 - Sentence/Phrase Generator
此分類上一篇:[UVA][Queue] 10901 - Ferry Loading III

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文