Bootstrap

7-3 银行排队问题之单队列多窗口服务 (25 分)

7-3 银行排队问题之单队列多窗口服务 (25 分)

假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。

本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间,并且统计每个窗口服务了多少名顾客。

输入格式:

输入第1行给出正整数N(≤1000),为顾客总人数;随后N行,每行给出一位顾客的到达时间T和事务处理时间P,并且假设输入数据已经按到达时间先后排好了顺序;最后一行给出正整数K(≤10),为开设的营业窗口数。这里假设每位顾客事务被处理的最长时间为60分钟。

输出格式:

在第一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。

在第二行中按编号递增顺序输出每个窗口服务了多少名顾客,数字之间用1个空格分隔,行末不能有多余空格。

输入
9
0 20
1 15
1 61
2 10
10 5
10 3
30 18
31 25
31 2
3

题目分析: 错了几次,纯粹的模拟,注意客人到窗口的时间和窗口开放时间的关系即可,若客人比所有窗口的开放时间都小,客人就会进最小的那个,到来时间只要比最小大,从左到右遍历,第一个进去即可。
你也可以这样想,假如我早早的来到黄线等候,那么我肯定是等到最近一个available的窗口,假如我来的比较晚,那么我肯定是从左向右,那个available就去那个。

代码

>#include <bits/stdc++.h>
using namespace std;
struct consumer
{
	int arr; //arrive
	int pro; //process
	int wait;
}a[1001];
struct gate
{
	int stt=0; //start time
	int n=0;
}b[11],ss[11];
bool cmp(struct consumer x,struct consumer y)
{
	return x.wait>y.wait;
}
bool cmmp(struct gate x,struct gate y)
{
	return x.stt>y.stt;
}
int main()
{
	int n,ar,pr;
	double waitsum=0;
	cin>>n;
	for (int i=0;i<n;i++)
	{
		cin>>ar>>pr;
		if (pr>60) pr=60;
		a[i].arr=ar;
		a[i].pro=pr;
	}
	int k,count=0;
	cin>>k;
	while (count<n)
	{                                        //到的时间比所有窗口的stt都晚,就进入第一个窗口,否则进入第一个stt最小的窗口.
//		for (int i=0;i<k;i++)
//		{
//			printf("%d的开始时间是%d\n\n",i,b[i].stt);
//		}
		int t=0;
		for (int i=1;i<k;i++)
		{
			if (b[i].stt<b[t].stt)
			{
				t=i;
			}
		}
		if (a[count].arr<=b[t].stt)
		{
			b[t].n++;
			a[count].wait=b[t].stt-a[count].arr;
			waitsum+=a[count].wait;
			b[t].stt+=a[count].pro;
			count++;
		}
		else if (a[count].arr>b[t].stt)
		{
			int appr;
			for (int i=0;i<k;i++)
			{
				if (a[count].arr>=b[i].stt)
				{
					appr=i;
					break;
				}
			}
			b[appr].n++;
			a[count].wait=0;
			b[appr].stt=a[count].arr+a[count].pro;
			count++;
		}
	}
	int sss=0;
	for (int i=0;i<k;i++)
	{
		ss[sss++].n=b[i].n;
	}
	sort(a,a+count,cmp);
	sort(b,b+k,cmmp);
	printf("%.1lf %d %d",waitsum/n,a[0].wait,b[0].stt);
	printf("\n");
	printf("%d",ss[0].n);
	for (int i=1;i<k;i++)
	{
		printf(" %d",ss[i].n);
	}
}
;