close

題目連結:   https://onlinejudge.org/external/110/11034.pdf

題目大意:  問最少需要運幾次,能夠將所有車送去對面。

思路:  用2個queue,分別存左邊右邊。輪流走。一開始走左邊,再來走右邊,每次走cnt++

走之前盡量塞車子

代碼:  

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t, l, m;
    cin >> t;
    while(t--){
        cin >> l >> m;
        l *= 100;
        queue<int> L, R;
        for(int i = 0; i < m; ++i){
            int x; string s;
            cin >> x >> s;
            if(s[0] == 'l') L.push(x);
            else R.push(x);
        }
        int res = 0;
        bool dir = 0;
        while(!L.empty() || !R.empty()){
            res++;
            int sum = 0;
            if(dir){
                while(!R.empty() && sum + R.front() <= l)
                    sum += R.front(), R.pop();
            }
            else{
                while(!L.empty() && sum + L.front() <= l)
                    sum += L.front(), L.pop();
            }
            dir ^= 1;
        }
        cout << res << endl;
    }
    return 0;
}

arrow
arrow
    創作者介紹
    創作者 尾玉 的頭像
    尾玉

    louisfghbvc的部落格

    尾玉 發表在 痞客邦 留言(0) 人氣()