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.
Customer[i] will take T[i] 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 that 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
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
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.
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
第一次的代码:
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.
Customer[i] will take T[i] 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 that 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
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
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.
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
模拟题,没有什么很好的方法,只能按照题目的设定去模拟,要特别注意小细节。
注意题目中:"Note that since the bank is closed everyday after 17:00, forthose customers who cannot be served before 17:00, you must output 'Sorry'instead"
17:00之前已经开始办理业务的, 都应该输出其结束时间。
第一次做这道题的时候自己写代码来管理list,内存管理方面的代码看起来比较复杂。
第二次使用了标准库的vector和queue,提高了效率。然而为了调高效率优化小细节的时候没有注意,在case4卡了很久…
第二次的代码:
/* 模拟题,没有什么很好的方法,只能按照题目的设定去模拟,要特别注意小细节 */
#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
const int STARTTIME = 8*60;
const int ENDTIME = 17*60;
class person
{
public:
person(): start_time(STARTTIME), processing_time(0), finish_time(0) {};
int start_time;
int processing_time;
int finish_time;
};
int N,M,K,Q;
vector<person> customers;
vector< queue<int> > windows;
int main()
{
int windowIndex = 0, customerIndex = 0;
int min_time;
cin >> N >> M >> K >> Q;
customers.resize(K);
windows.resize(N);
for (int i = 0; i != K; ++i)
{
cin >> customers[i].processing_time;
}
/* 先处理前 N*M 个, 只需要按照顺序排队即可*/
while (customerIndex < K && customerIndex < N*M)
{
windowIndex %= N;
if (!windows[windowIndex].empty())
{
customers[customerIndex].start_time = customers[windows[windowIndex].back()].finish_time;
}
customers[customerIndex].finish_time = customers[customerIndex].start_time + customers[customerIndex].processing_time;
windows[windowIndex++].push(customerIndex++);
}
while (customerIndex < K)
{
windowIndex = 0;
min_time = customers[windows[windowIndex].front()].finish_time;
for (int i = 1; i != N; ++i)
{
if (min_time > customers[windows[i].front()].finish_time)
{
min_time = customers[windows[i].front()].finish_time;
windowIndex = i;
}
}
customers[customerIndex].start_time = customers[windows[windowIndex].back()].finish_time;
/* 排在这个窗口的顾客不能不能接受服务, 但其它窗口还可以排队, 不要弄混淆了队列第一个顾客的结束时间和队列最后一个人的结束时间 */
/* 下面这样跳出是错误的*/
//if (customers[customerIndex].start_time >= ENDTIME)
//{
// break;
//}
customers[customerIndex].finish_time = customers[customerIndex].start_time + customers[customerIndex].processing_time;
windows[windowIndex].pop();
windows[windowIndex].push(customerIndex++);
}
for (int i = 0; i != Q; ++i)
{
cin >> customerIndex;
--customerIndex;
//if (0 != customers[customerIndex].finish_time)
if (STARTTIME <= customers[customerIndex].start_time && customers[customerIndex].start_time < ENDTIME)
{
printf("%02d:%02d\n", customers[customerIndex].finish_time/60, customers[customerIndex].finish_time%60);
}
else
{
printf("Sorry\n");
}
}
return 0;
}
#if 0
/* case 4不能通过*/
/*
选择排在哪个窗口的函数firstAvalibleWindow()设计有问题。
如果在开始的时候直接输入一个非常大的数字,将直接导致程序结束。
尝试输入:
2 2 7 5
600 2 6 4 3 1 2
3 4 5 6 7
*/
#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
const int STARTTIME = 8*60;
const int ENDTIME = 17*60;
class person
{
public:
person(): processing_time(0), finish_time(0) {};
int processing_time;
int finish_time;
};
class window
{
public:
window(): finish_time(STARTTIME), line() {};
int finish_time; /* 存储队列最后一个成员的结束时间*/
queue<int> line; /* */
};
int N,M,K,Q;
vector<person> customers;
vector<window> bank;
int firstAvalibleWindow()
{
int ret = 0;
int min_time = ENDTIME;
int min_len = M+1;
bool flag = bank[N-1].line.size() == M ? true : false;
for (int i = 0; i != N; ++i)
{
if (flag)
{
if (min_time > bank[i].line.front())
{
min_time = bank[i].line.front();
ret = i;
}
}
else
{
if (min_len > bank[i].line.size())
{
min_len = bank[i].line.size();
ret = i;
}
}
}
/* time to close*/
if (bank[ret].finish_time >= ENDTIME)
{
return -1;
}
return ret;
}
int main()
{
int windowIndex, customerIndex = 0;
cin >> N >> M >> K >> Q;
customers.resize(K);
bank.resize(N);
for (int i = 0; i != K; ++i)
{
cin >> customers[i].processing_time;
}
while (customerIndex != K)
{
windowIndex = firstAvalibleWindow();
if (-1 == windowIndex)
{
break;
}
customers[customerIndex].finish_time = bank[windowIndex].finish_time + customers[customerIndex].processing_time;
bank[windowIndex].finish_time = customers[customerIndex].finish_time;
bank[windowIndex].line.push(customers[customerIndex].finish_time);
if (bank[windowIndex].line.size() > M)
{
bank[windowIndex].line.pop();
}
++customerIndex;
}
for (int i = 0; i != Q; ++i)
{
cin >> customerIndex;
--customerIndex;
if (0 != customers[customerIndex].finish_time)
{
printf("%02d:%02d\n", customers[customerIndex].finish_time/60, customers[customerIndex].finish_time%60);
}
else
{
printf("Sorry\n");
}
}
return 0;
}
#endif
第一次的代码:
/* 注意题目中:"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"*/
/* 17:00之前已经开始办理业务的, 都应该输出其结束时间*/
#include <iostream>
#include <stdio.h>
using namespace std;
#define OPENTIME 480
typedef struct node
{
int startTime;
int endTime;
struct node *next;
}node;
typedef struct elem
{
node *head; // 该柜台每天处理的第一个顾客
int personInQueue;
node *first; // 指向正在该柜台正在处理的顾客
node *last; // 指向该柜台正在排队的最后一名顾客
}elem;
typedef struct outcome
{
int startTime;
int endTime;
}outcome;
outcome queueCase1(elem *list, int windowsNum, int transactionTime)
{
int index = 0;
node *temp = NULL;
outcome ret;
for (int i = 1; i != windowsNum; ++i)
{
if (list[i].personInQueue < list[0].personInQueue)
{
index = i;
break;
}
}
++list[index].personInQueue;
temp = new node;
if (NULL == list[index].head)
{
temp->startTime = OPENTIME;
list[index].head = temp;
list[index].first = temp;
list[index].last = temp;
}
else
{
temp->startTime = list[index].last->endTime;
list[index].last->next = temp;
list[index].last = temp;
}
temp->next = NULL;
temp->endTime = temp->startTime + transactionTime;
ret.startTime = temp->startTime;
ret.endTime = temp->endTime;
return ret;
}
outcome queueCase2(elem *list, int windowsNum, int transactionTime)
{
int index = 0;
node *temp = NULL;
outcome ret;
int endtime = list[0].first->endTime;
for (int i = 1; i != windowsNum; ++i)
{
if (list[i].first->endTime < endtime)
{
endtime = list[i].first->endTime;
index = i;
}
}
temp = new node;
temp->startTime = list[index].last->endTime;
temp->endTime = temp->startTime + transactionTime;
temp->next = NULL;
list[index].last->next = temp;
list[index].last = list[index].last->next;
list[index].first = list[index].first->next;
ret.startTime = temp->startTime;
ret.endTime = temp->endTime;
return ret;
}
outcome addtolist(elem *list, int windowsNum, int maxCapcity, int transactionTime)
{
outcome ret;
if (maxCapcity == list[windowsNum-1].personInQueue)
{
ret = queueCase2(list, windowsNum, transactionTime);
}
else
{
ret = queueCase1(list, windowsNum, transactionTime);
}
return ret;
}
void destorylist(elem *&list, int windowsNum)
{
node *pre = NULL;
node *cur = NULL;
for (int i = 0; i != windowsNum; ++i)
{
pre = cur = list[i].head;
while(pre)
{
cur = cur->next;
delete pre;
pre = cur;
}
}
delete[] list;
list = NULL;
}
int main()
{
int windowsNum;
int maxCapcity;
int customerNum;
int queriesNum;
int *transactionTime = NULL;
outcome *result = NULL;
elem *list = NULL;
int *personId = NULL;
cin >> windowsNum >> maxCapcity >> customerNum >> queriesNum;
transactionTime = new int[customerNum];
personId = new int[queriesNum];
if (NULL == transactionTime)
{
return -1;
}
for (int i = 0; i != customerNum; ++i)
{
cin >> transactionTime[i];
}
for (int i = 0; i != queriesNum; ++i)
{
cin >> personId[i];
}
list = new elem[windowsNum];
for (int i = 0; i != windowsNum; ++i)
{
list[i].head = NULL;
list[i].personInQueue = 0;
list[i].first = NULL;
list[i].last = NULL;
}
result = new outcome[customerNum];
for (int i = 0; i != customerNum; ++i)
{
result[i] = addtolist(list, windowsNum, maxCapcity, transactionTime[i]);
}
for (int i = 0; i != queriesNum; ++i)
{
int index = personId[i]-1;
if (OPENTIME <= result[index].startTime && result[index].startTime < 17*60)
{
printf("%02d:%02d\n", result[index].endTime/60, result[index].endTime%60);
}
else
{
cout << "Sorry" << endl;
}
}
destorylist(list, windowsNum);
delete[] transactionTime;
delete[] result;
delete[] personId;
system("pause");
return 0;
}