问题
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.
Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤104) - the total number of customers, and K (≤100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.
Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.
Output Specification:
For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.
Sample Input:
7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10
Sample Output:
8.2
解决方法
题目大意
有n个客户,k个窗⼝。已知每个客户的到达时间和需要的时⻓,如果有窗⼝就依次过去,
如果没有窗⼝就在⻩线外等候(⻩线外只有⼀个队伍,先来先服务),求客户的平均等待时⻓。银⾏
开放时间为8点到17点,再8点之前不开⻔,8点之前来的⼈都要等待,在17点后来的⼈不被服务。
第一次代码
思路
这道题属于模拟类型。
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
int cometime, processtime;
}tmp;
vector<node>v;
bool cmp(node a, node b)
{
return a.cometime < b.cometime;
}
int main()
{
int n, k, hh, mm, ss, pt, cometime = 0;
double totaltime = 0;
scanf("%d %d", &n, &k);
vector<int>endtime(k, 28800);//8点
for (int i = 0; i < n; i++)
{
scanf("%d:%d:%d %d", &hh, &mm, &ss, &pt);
cometime = hh * 3600 + mm * 60 + ss;
tmp.cometime = cometime;
tmp.processtime = pt*60;
if (cometime <= 61200) v.push_back(tmp);
}
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++)
{
int index =0, minendtime = endtime[0];//不要顺手写成-1了
for (int j = 1; j < k; j++)
{
if (endtime[j] < minendtime)
{
minendtime = endtime[j];
index = j;
}
}
if (endtime[index] <= v[i].cometime) endtime[index] = v[i].cometime + v[i].processtime;
else
{
totaltime += (endtime[index] - v[i].cometime);
endtime[index] += v[i].processtime;
}
}
/*if (v.size() == 0) printf("0.0");
else */printf("%.1f", totaltime / 60.0 / v.size());
return 0;
}
第二次代码(更好)
思路
这种解法是在柳神的博客一看到的,昨晚就去看了一下关于优先队列的东西。
使用一个优先队列维护窗口办理完业务的时间~如果最早结束服务的窗口时间早于客户的到达时间,不需要等待,直接执行 q.push(v[i].come + v[i].time);否则客户的等待时间是q.top() - v[i].come~
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int cometime, processtime;
}tmp;
bool cmp(node a, node b)
{
return a.cometime < b.cometime;
}
vector<node>v;
int main()
{
int n, k, hh, mm, ss, pt,cometime=0;
double total = 0;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++)
{
scanf("%d:%d:%d %d", &hh, &mm, &ss,&pt);
cometime = hh * 3600 + mm * 60 + ss;
tmp.cometime = cometime;
tmp.processtime = pt * 60;
if (cometime <= 61200) v.push_back(tmp);
}
sort(v.begin(), v.end(), cmp);
priority_queue<int, vector<int>, greater<int>>q;
for (int i = 0; i < k; i++) q.push(28800);
for (int i = 0; i < v.size(); i++)
{
if (q.top() <= v[i].cometime)
{
q.push(v[i].cometime + v[i].processtime);
q.pop();
}
else
{
total += q.top() - v[i].cometime;
q.push(q.top() + v[i].processtime);
q.pop();
}
}
printf("%.1f", total / 60.0 / v.size());
return 0;
}
后记
两种方法思路大体一样,但是方法我觉得第二种更胜一筹。更加容易理解,但是我第一次写,优先队列的使用不熟悉,需要多加熟悉。