目录
题目
题目链接:1014 Waiting in Line (PAT (Advanced Level) Practice)
输入样例
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
输出样例
08:07
08:06
08:10
17:00
Sorry
提交结果截图
带详细注释的源代码
代码参考了博文——PTA 1014 Waiting in Line (30分) 解题思路及满分代码
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
#define open_time 8*60
#define close_time 17*60
struct Customer
{
int process_time;
int start_time;
int finish_time;
}customer[1001];
int main()
{
int N,//the num of windows, <=20
M,//max capacity of each line, <=10
K,//the num of customers, <=1000
Q;//the num of queries, <=1000
cin>>N>>M>>K>>Q;
queue<Customer>Window[20];
int queries[1001];
for(int i = 1; i <= K; i++)
cin>>customer[i].process_time;
for(int i = 0; i < Q; i++)
cin>>queries[i];
for(int i = 1; i <= K; i++)
{
int window_num;
if(i <= N*M)
{
window_num = (i-1)%N;//window num start from 0 while customer num start from 1
if(i <= N)//their start time = open time
customer[i].start_time = open_time;
else
{ //get the previous customer's infomation
Customer pre_customer = Window[window_num].back();
customer[i].start_time = pre_customer.finish_time;
}
}
else
{
window_num = 0;//window num start from 0
for(int j = 1; j < N; j++)//get the head customers's leaving time
if(Window[j].front().finish_time < Window[window_num].front().finish_time)
window_num = j;
Customer pre_customer = Window[window_num].back();get the head customer
Window[window_num].pop();
customer[i].start_time = pre_customer.finish_time;
}
customer[i].finish_time = customer[i].start_time + customer[i].process_time;
Window[window_num].push(customer[i]);
}
//solve the queries
for(int i = 0; i < Q; i++)
{
if(customer[queries[i]].start_time >= close_time)//if the bank is closed
cout<<"Sorry"<<endl;
else//output the finishing time
printf("%02d:%02d\n", customer[queries[i]].finish_time/60, customer[queries[i]].finish_time%60);
}
return 0;
}