pat 1017 Queueing at Bank(23分)

实质上是一个有截止时间的多任务调度问题,多个窗口,多个顾客,一个等待队列,截止时间为17点,算平均等待时间。思路是进行模拟,以i来模拟时间一秒一秒,每一秒都遍历所有窗口,若有窗口空闲且有顾客在等待,就服务。差一个点没过,不知是何种情况。需要考虑的情况有:

1. 全部都在17点以后到,这样结果为0.0

2.一个窗口服务完后,再遇到顾客,得服务

3.若顾客17点前到,但17点前无法得到服务,该顾客要不要算在等待的里面?

代码:

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int NUM=10005;
struct customer
{
	int arrive;
	int process;
}cust[NUM];
int window[105],server[105];
bool operator<(const customer &x, const customer &y)
{
	return x.arrive<y.arrive;

}
// int cmp(const customer &x, const customer &y)
// {
// 	return 
// }
int main()
{
	int n,k,i,j;
	int hh,mm,ss,begin,end;

	freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);

	cin>>n>>k;
	for(i=0;i<n;i++){
		cin>>hh;
		getchar();
		cin>>mm;
		getchar();
		cin>>ss;

		cin>>cust[i].process;
		cust[i].arrive=hh*3600+mm*60+ss;
		

	}
	begin=8*3600;
	end=17*3600;
	sort(cust,cust+n);
	double sum=0.0f;
	int index=0;
	for(j=0;j<k;j++){
		window[j]=begin;//每个窗口的空闲时间都是8点

		server[j]=0;//窗口都空闲
	}
	for(i=begin;i<=end;i++){//i来模拟每一秒的时间

			for(j=0;j<k;j++){//询问每个窗口
				if(window[j]==i&&i!=begin)
					server[j]=0;
				if(server[j]==0&&cust[index].arrive<=i&&index<n){//有窗口空闲且有顾客在等待
					window[j]=i+cust[index].process*60;//服务
					sum+=i-cust[index].arrive;//加上其等待时间
					//printf("%d\n",i-cust[index].arrive);
					index++;
					server[j]=1;//窗口设为忙
					
				}
			}
			

		if(index==n||cust[index].arrive>end)//顾客服务完了或顾客到达时间超过17点了
			break;
		


	}
	if(index>0){
		sum=sum/(60*index);
		printf("%.1lf",sum);
	} else {//所有顾客都17点后才来
		printf("0.0");
	}
	
	return 0;


}

若要看AC代码,据说用优先队列特别简单,请参考:

http://linest.iteye.com/blog/1423588
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章