问题 H 算法3-7:银行排队
时间限制: 1 Sec 内存限制: 128 MB[ 提交]
题目描述
我们大多都有在银行排队的经历,唉,那坑爹的排队啊!现在就让我们来算算我们这些客户平均需要等多久吧。
每天刚开始时银行会开m个窗口来为我们total个客户办理业务,当有客户需要办理业务时,先选择可以办理业务的窗口,如果有多个窗口可以办理业务就选择空闲时间最长的窗口,如果有多个窗口空闲的时间一样长,则选择序号小的窗口办理业务。假设我们每个人来到的时间和办理业务所需要的时间(为了简化问题,采用整数表示时间)都知道了。现在请你算算我们平均需要等待多久呢?
输入
有多组测试数据,每组数据开始有两个正整数m(<20)和total(<200),后面有total对整数,对应客户先后到来的时间以及办理业务所需的时间。
输出
平均等待的时间,保留两位小数。
样例输入
2 6 1 3 4 1 5 3 9 2 13 4 13 33 14 0 3 2 2 2 4 5 4 7 2 11 3 12 3 12 4 12 1 13 3 15 4 19 1 22 3 23 22 5 0 6 0 5 0 6 7 1 7 2
样例输出
0.000.291.20
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
int top[22];
int time[22];
int qu[22][202];
int m,tol;
float tc=0;
void pu(int a,int b){
int k;
int t=cho(a,b);
//printf("%d\n",t);
top[t]++;
qu[t][top[t]]=a+b;
}
int cho(int a,int b){
int i=0;
for(i=0;i<m;i++){
if(time[i]+qu[i][top[i]]<=a){
time[i]=0;
return i;
}
}
int min=999999;
int t=0;
for(i=0;i<m;i++){
if((time[i]+qu[i][top[i]]-a)<min){
min=time[i]+qu[i][top[i]]-a;
//printf("%d\n",min);
t=i;
}
}
time[t]=min;
tc+=min;
return t;
}
int main(){
int i,j;
int a,b;
while(~scanf("%d",&m)){
scanf("%d",&tol);
for(i=0;i<tol;i++){
scanf("%d %d",&a,&b);
pu(a,b);
}
printf("%1.2f\n",tc/tol);
memset(time,0,sizeof(time));
memset(top,0,sizeof(top));
tc=0;
}
return 0;
}