1017. Queueing at Bank (25)

1017. Queueing at Bank (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=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 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



题意:

1.输入N(顾客数),K(银行窗口数)
2.N行,输入每个顾客的到达时间和需要的服务时间
3.输出平均每个顾客要等待的时间

注意:

1.银行的上班时间是08:00到17:00,到达时间大于17:00的顾客都不被服务,且不计入计算平均等待时间时的顾客数。
2.每个顾客占用窗口的时间上限是60分钟

思路:

1.还是排队的问题,跟1016相似
2.简单的就是每个窗口的最大容纳数为1
3.按达到时间排队,我将时间都换算成秒来计算
4.窗口最近结束服务时间,设其初始值为8*60*60
5.每次给窗口值排序,从小到大,下一个顾客要去的窗口是值最小的那个

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
struct node 
{
  string atime;//输入的到达时间
  int arr;//将到达时间换算成秒
  int ptime;//输入的需要的服务时间
};
bool cmp(node a,node b)//顾客的排序函数
{
  return a.atime<b.atime;
}
bool cmp2(int a,int b)//窗口的排序函数
{
  return a<b;
}
int main()
{
  int N,K;
  cin>>N>>K;
  node customer[N];//N个顾客
  int windows[K];//K个窗口
  for(int i=0;i<N;i++)
    {
      cin>>customer[i].atime>>customer[i].ptime;
      //换算到达时间
      customer[i].arr=((customer[i].atime[6]-'0')*10+customer[i].atime[7]-'0')
    +((customer[i].atime[3]-'0')*10+customer[i].atime[4]-'0')*60
    +((customer[i].atime[0]-'0')*10+customer[i].atime[1]-'0')*60*60;
      if(customer[i].ptime<=60)customer[i].ptime*=60;
      else customer[i].ptime=60*60;
    }
  sort(customer,customer+N,cmp);//顾客按达到先后排序
  //for(int i=0;i<N;i++)cout<<customer[i].atime<<"\t"<<customer[i].arr<<"\t"<<customer[i].ptime<<endl;
  for(int i=0;i<K;i++)windows[i]=8*60*60;//窗口赋初始值,银行8点上班,换算成秒
  int wait=0;//累计等待时间,秒
  for(int i=0;i<N;i++)
    {
      if(customer[i].arr>17*60*60)//到达时间超过上班结束时间
    {
      N=i;//改变计算平均的人数
      break;
    }
      sort(windows,windows+K,cmp2);
      //cout<<"i: "<<i<<"\t"<<windows[0]<<"\t"<<windows[1]<<"\t"<<windows[2]<<endl;
      //若达到时间大于窗口时间,则无需等待,窗口时间更新为达到时间加服务时间      if(customer[i].arr>windows[0])windows[0]=customer[i].arr+customer[i].ptime;
      else
    {
      wait+=abs(windows[0]-customer[i].arr);
      windows[0]+=customer[i].ptime;//窗口时间更新
    }
      //cout<<"wait:"<<wait<<endl;
    }
  printf("%.1lf\n",(double)wait/60/N);//保留一位小数
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值