# 題目: UVa 11034 - Ferry Loading IV

# 題目說明

有車子想要渡河,目前唯一渡河的方式為搭船
船只有一艘且長度有限,求所有車子到達對岸時船的趟數


INPUT:
第一行有一個整數c,代表有c筆資料
每筆測資第一行有兩個整數lm

  1. l代表船的長度
  2. m代表等待過河的車子數量
    接下來有m行,每行有一個整數和一個字串
  3. 整數代表車子的長度
  4. 字串代表它處於河的哪一邊

OUTPUT:
將所有車子運送到對岸,船需要開的趟數

# 解題方法

以兩個queue分別存左岸及右岸的車子的長度
接著跑迴圈直到兩個queue都為空
每趟運送在長度限制內運送愈多愈好

# 參考程式碼

#include <iostream>
#include <queue>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int c, l, m, length;
	string side;
	cin >> c;

	while (c--) {

		int time = 0;
		queue<int> L, R;
		auto cur = &L;

		cin >> l >> m;
		l *= 100;
		while (m--) {
			cin >> length >> side;
			side == "left" ? L.emplace(length) : R.emplace(length);
		}

		while (!L.empty() || !R.empty()) {
			int sum = 0;
			while (!cur->empty() && sum + cur->front() <= l) sum += cur->front(), cur->pop();
			++time;
			cur = *cur == L ? &R : &L;
		}

		cout << time << "\n";
	}

	return 0;
}

# 參考資料

https://www.larrysprognotes.com/UVa%20-%2011034/