PAT甲级 1014
题目 Waiting in Line
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:
The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line.
Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
Customeri will take Ti minutes to have his/her transaction processed.
The first N customers are assumed to be served at 8:00am.
Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.
For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is served at window1 while customer2 is served at window2 . Customer3 will wait in front of window1 and customer4 will wait in front of window2 . Customer5 will wait behind the yellow line.
At 08:01, customer1 is done and customer5 enters the line in front of window1 since hat line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (≤20, number of windows), M (≤10, the maximum capacity of each line inside the yellow line), K (≤1000, number of customers), and Q (≤1000, number of customer queries).
The next line contains K positive integers, which are the processing time of the K customers.
The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.
Output Specification:
For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output Sorry instead.
解析
有n个窗口,m为黄线内允许排队最大数量,k是左右用户的所需要时间,q是最后要求输出的用户的咨询结束时间,如果不能咨询则输出sorry
用map作为每个窗口,map值以vector(队列)来记录,队列里包含想要的数据
#include<bits/stdc++.h>
#define INF 1<<29
using namespace std;
struct cu {
int index; // 用户的序号
int remain_time; //剩余时间,刚开始是该用户需要处理业务的时间
};
int n, m, k, q;
string res[1005]; //结果
int times[1005];
int queries[1005];
map<int, vector<cu>> mp;
void pat1014() {
cin >> n >> m >> k >> q;
for (int i = 1; i <= k; ++i) {
cin >> times[i];
}
for (int i = 1; i <= q; ++i) {
cin >> queries[i];
}
int init_capacity = 0;
int t = 1; //填满mp时的位置,之后往mp放入用户只要+1
if (n * m > k) {
init_capacity = k / n + 1;
} else {
init_capacity = m;
}
//初始化mp队列将mp填满(n * m < k),或则填满k各用户(n * m > k)
for (int i = 1; i <= init_capacity; ++i) {
if (n * m > k && i == init_capacity) {
for (int j = 1; j <= k % n; ++j) {
cu c = {
t, times[t]
};
mp[j].push_back(c);
++t;
}
} else {
for (int j = 1; j <= n; ++j) {
cu c = {
t, times[t]
};
mp[j].push_back(c);
++t;
}
}
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 0; j < m; ++j) {
// cout << mp[i][j].index << " ";
// }
// cout << endl;
// }
bool f = true; //mp 是否全为空
int hour = 8, minute = 0;
while (f) {
//获取mp中所有窗口第一个用户(0)中那个需要花费时间最少的用户
int min = 10000;
int min_index = 0;
for (int i = 1; i <= n; ++i) {
if (mp[i].size() != 0) {
if (mp[i][0].remain_time < min) {
min = mp[i][0].remain_time;
min_index = i;
}
}
}
//换算时间,并格式化输入到res对应的序号中
minute += min;
if (minute / 60 > 0) {
hour += (minute / 60);
minute = minute % 60;
}
char s[6];
sprintf(s, "%02d:%02d", hour, minute);
res[mp[min_index][0].index] = s;
// cout << "min_index:" << min_index << endl;
// cout << "index:" << mp[min_index][0].index << endl;
// cout << "hour:" << hour << ",minute:" << minute << endl;
//将mp中所有窗口第一个用户中所需时间减掉那个最少花费的用户的办理时间
for (int i = 0; i <= n; ++i) {
if (mp[i].size() != 0) {
mp[i][0].remain_time -= min;
}
}
// 当这个队列的第一个结束时间已经超过17:00,则清空该队列
if (hour * 60 + minute >= 17 * 60) {
// cout << "----------------min_index:" << min_index << endl;
mp[min_index].clear();
}else{
mp[min_index].erase(mp[min_index].begin());
}
//如果黄线以外还有用户等待,就放到该队列里,同时如果第一个结束时间已经超过17:00,就不放入
if (t <= k && n * m < k && hour * 60 + minute < 17 * 60) {
cu c = {
t, times[t]
};
mp[min_index].push_back(c);
t++;
}
//判断mp所有队列是否为空,为空就说明每个队列最后一个用户处理完了跳出循环
int sumt = 0;
for (int i = 1; i <= n; ++i) {
if(mp[i].empty()){
sumt++;
}
}
f = sumt != n;
}
for (int l = 1; l <= q; ++l) {
if (res[queries[l]]!="") {
cout << res[queries[l]] << endl;
}else{
cout << "Sorry" << endl;
}
}
}
int main() {
pat1014();
return 0;
}