PAT (Advanced Level) 1014 Waiting in Line

题意

模拟题,读者还是自行理解题意比较好。写完可以来参考一下我的代码。

思路

deque模拟队列。注意,等待时间超过17:00则sorry,是等待时间!

代码

#include 
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n, m, k, q;
	cin >> n >> m >> k >> q;
	vector<int> need(k);
	for (int &e : need) cin >> e;
	vector<int> backup_need = need;
	vector<deque<int>> dek(n);
	for (int i = 0; i < min(n * m, k); ++i) {
		dek[i % n].push_back(i);
	}
	deque<int> wait;
	for (int i = min(n * m, k); i < k; ++i)
		wait.push_back(i);
	vector<int> end(k, -1);
	int now = 480;
	while (true) {
		int minn = INT_MAX;
		for (int i = 0; i < n; ++i)
			if (!dek[i].empty()) minn = min(minn, need[dek[i].front()]);
		if (minn == INT_MAX) break;
		now += minn;
		for (int i = 0; i < n; ++i) {
			if (dek[i].empty()) continue;
			need[dek[i].front()] -= minn;
			if (need[dek[i].front()] == 0) {
				end[dek[i].front()] = now;
				dek[i].pop_front();
			}
		}
		for (int i = 0; i < n; ++i) 
			if (dek[i].size() < m && !wait.empty()) {
				dek[i].push_back(wait.front());
				wait.pop_front();
			}
	}
	while (q--) {
		int id;
		cin >> id;
		--id;
		if (end[id] - backup_need[id] >= 1020) {
			cout << "Sorry\n";
			continue;
		}
		cout << setw(2) << setfill('0') << end[id] / 60 << ':';
		cout << setw(2) << setfill('0') << end[id] % 60 << '\n';
	}
	return 0;
} 

HINT

不定时更新更多题解,详见 git ! ! !

你可能感兴趣的:(PAT (Advanced Level) 1014 Waiting in Line)