PAT真题练习(甲级)1014 Waiting in Line (30 分)
Sample Input:
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
Sample Output:
08:07
08:06
08:10
17:00
Sorry
#include<iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
struct Customer {
// 存储相较于8:00am过去的分钟数,便于比较计算
int begin;
// 服务结束的时间
int end;
// 服务消耗时间
int cost_time;
};
void PrintTime(int t) {
if (t < 10) {
cout << 0 << t;
}
else {
cout << t;
}
}
int main() {
vector<Customer> customers; // 存储客户信息
vector<deque<int>> windows; // 存储窗口排队信息
int ctime = 0;// 存储当前时间
int N, M, K, Q;
cin >> N >> M >> K >> Q;
windows.resize(N);
customers.resize(K);
for (auto i = 0; i < K;i++) {
cin >> customers[i].cost_time;
}
int cid = 0;// 表示当前轮到的顾客
int next_time = 540; //存储下一个顾客办理完成的时间
// 对队列进行初始化
for (auto k = 0; k < M;k++) {
for (auto i = 0; i < N;i++) {
customers[cid].begin = windows[i].size() > 0 ? customers[windows[i].back()].end : ctime;
customers[cid].end = customers[cid].begin + customers[cid].cost_time;
windows[i].push_back(cid);
cid++;
// 若所有顾客排队完成,则跳出循环
if (cid >= K) break;
}
}
// 以时间为参数,进入循环判断
while (true) {
next_time = -1;
for (auto i = 0; i < N;i++) {
if (windows[i].size() > 0 && customers[windows[i].front()].end <= ctime) {
// 到时间则pop出队列,同时push新的顾客进入队列
windows[i].pop_front();
if (cid < K) {
customers[cid].begin = windows[i].size() > 0 ? customers[windows[i].back()].end : ctime;
customers[cid].end = customers[cid].begin + customers[cid].cost_time;
windows[i].push_back(cid);
cid++;
}
}
if (windows[i].empty()) continue;
if (next_time == -1) next_time = customers[windows[i].front()].end;
else next_time = min(next_time, customers[windows[i].front()].end);
}
// 若所有队列均为空,则表示所有顾客的服务时间均计算完毕
// 此时,跳出循环
if (next_time == -1) break;
ctime = next_time;
}
//读取查询对象,并输出结果
int query_id;
for (auto i = 0;i < Q;i++) {
cin >> query_id;
auto end_time = customers[query_id - 1].end;
// 这里注意有个坑,是服务开始时间
// “ those customers who cannot be served before 17:00, you must output Sorry instead.”
if (customers[query_id - 1].begin >= 540) cout << "Sorry" << endl;
else {
PrintTime(end_time / 60 + 8);
cout << ":";
PrintTime(end_time % 60);
cout << endl;
}
}
}