队列问题
解题报告
// 银行排队问题
// 模拟队列
// 两种情况
// 1. 如果有空闲窗口,直接处理客户业务;
// 2. 否则,等待最先处理完毕的窗口。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#ifdef ONLINE_JUDGE
#else
#include <fstream>
#endif
using namespace std;
struct Time
{
int hour;
int min;
int sec;
};
struct Customer
{
int arrival;
int waittime;
};
int tos(const Time a)
{
return (a.hour * 3600 + a.min * 60 + a.sec);
}
bool cmp(const Customer & a, const Customer & b)
{
return a.arrival < b.arrival;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
ifstream cin("case.txt");
#endif // ONLINE_JUDGE
int n, w;
cin >> n >> w;
// input customers' time arived
vector<Customer>customer;
Customer cus;
Time time;
char c;
const int noserver = 17 * 3600;
for (int i = 0;i < n;++i)
{
cin >> time.hour >> c >> time.min >> c >> time.sec >> cus.waittime;
// 筛选数据
cus.arrival = tos(time);
cus.waittime *= 60;
if (tos(time) < noserver)
{
customer.push_back(cus);
}
}
sort(customer.begin(), customer.end(), cmp);
//窗口时间线
vector<int>windows;
const int starttime = 8 * 3600;
for (int i = 0;i < w;++i)
{
windows.push_back(starttime);
}
// 处理逻辑
double sum = 0;
n = customer.size();
for (int i = 0;i < n;++i)
{
int min = 10000001;
int index = 0;
int j = 0;
for (;j < w;++j)
{
if (customer[i].arrival >= windows[j])
{
windows[j] = customer[i].arrival + customer[i].waittime;
break;
}
else
{
if (windows[j] < min)
{
min = windows[j];
index = j;
}
}
}
if (j == w)
{
sum += min - customer[i].arrival;
windows[index] += customer[i].waittime;
}
}
// 秒数转分钟
sum = (sum * 1.0 / 60 ) / n;
printf("%.1lf\n", sum);
getchar();
return 0;
}
(全文未完)
参考资料
排队论及其应用浅析.pdf