题目
Suppose a bank has K windows open for service. There is a yellow line infront of the windows which devides the waiting area into two parts. All thecustomers have to wait in line behind the yellow line, until it is his/her turnto be served and there is a window available. It is assumed that no window canbe 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 linecontains 2 numbers: N (<=10000) - 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 acustomer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. Itis assumed that no two customers arrives at the same time.
Notice that the bank opens from 08:00 to 17:00. Anyone arrives early willhave to wait in line till 08:00, and anyone comes too late (at or after17: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 thecustomers, 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
直接模拟
代码:
一开始想差了,改得很挫,有空写过……
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX=0x7fffffff; //最大时间限制
struct cus //客户结构
{
int time; //到达时间,以秒计
int ser_time; //服务时间,以秒计
};
int To_second(int hour,int min,int sec); //将时间转换为秒
int Get_time(); //获取时间,并调用To_second()转换为秒
bool cm(const cus &c1,const cus &c2); //比较客户的到达时间,用于排序
void Put_time(long long time,int n1); //输出平均等待时间,输入为总等待时间和有效人数
int main()
{
int n,k,n1; //n,k,有效人数n1
cin>>n>>k;
int i;
deque<cus> custom; //客户队列
cus cus_temp;
for(i=0;i<n;i++)
{
cus_temp.time=Get_time();
cin>>cus_temp.ser_time;
cus_temp.ser_time*=60;
if(cus_temp.time<=61200) //17:00前的客户
custom.push_back(cus_temp);
}
long long time=28800,total_time=0; //初始时间8:00,总等待时间
if(custom.empty())
{
cout<<0.0;
return 0;
}
else
{
n1=custom.size(); //统计有效人数
sort(custom.begin(),custom.end(),cm);
for(i=0;i<n1;i++) //处理早到的人
{
if(custom[i].time<28800)
{
total_time+=(28800-custom[i].time);
custom[i].time=28800;
}
}
}
queue<cus> window[100]; //窗口……由于只有1个,其实不需要队列的
int s_time,a_time; //最小的服务时间,最早的到达时间(仅统计比当前时间晚的,用于处理所有窗口中的客户都为空后来客户的情况)……这个是多余的
for(i=0;i<k;i++) //压入各窗口第一个
{
window[i].push(custom.front());
custom.pop_front();
if(custom.empty())
break;
}
if(i<k) //如果人数少于窗口,结束
Put_time(total_time,n1);
else
{
while(!custom.empty())
{
s_time=MAX;
a_time=MAX;
for(i=0;i<k;i++)
{
if(window[i].front().time<=time) //获取已经服务中最早结束的
{
if(window[i].front().ser_time<s_time)
s_time=window[i].front().ser_time;
}
else if(window[i].front().time-time+window[i].front().ser_time<s_time) //考虑后来的是否先结束的情况
{
s_time=window[i].front().time-time+window[i].front().ser_time;
}
else if(window[i].front().time<a_time) //获取最到的来到时间(当前时间未到的人中)……这个是多余的
a_time=window[i].front().time;
}
if(s_time<MAX) //更改时间
time+=s_time;
else //更改时间(两个时间点间没有服务对象)……多余的
{
time=a_time;
continue;
}
for(i=0;i<k;i++) //扫描窗口,处理时间
{
if(window[i].front().time<time) //在时间段内被服务的,需要减服务时间
{
if(time-s_time>=window[i].front().time) //在时间段之前来的
window[i].front().ser_time-=s_time;
else //在时间段中来的
window[i].front().ser_time-=time-window[i].front().time;
}
if(window[i].front().ser_time==0) //有窗口空闲
{
window[i].pop();
if(custom.empty()) //都已经服务到,结束
{
Put_time(total_time,n1);
return 0;
}
else //压入时间
{
if(time>custom.front().time) //对已经在黄线外等待的客户计算等待,!!!对假想的将来的客户仅做压入处理
total_time+=time-custom.front().time;
window[i].push(custom.front());
custom.pop_front();
}
}
}
}
Put_time(total_time,n1);
}
return 0;
}
int To_second(int hour,int min,int sec)
{
return hour*3600+min*60+sec;
}
int Get_time()
{
int hour,min,sec;
cin>>hour;
cin.get();
cin>>min;
cin.get();
cin>>sec;
return To_second(hour,min,sec);
}
bool cm(const cus &c1,const cus &c2)
{
return c1.time<c2.time;
}
void Put_time(long long time,int n1)
{
double t=time/(60.0*n1);
cout<<fixed;
cout.precision(1);
cout<<t;
}