PAT1017. Queueing at Bank

写了pat 有段时间了,也是感觉自己作为非计算机专业学生,写pat的困难,所以决定写blog,来记录自己以非计算机专业重走计算机之路的过程。


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
分析:模拟队列
code:样例 3,4,5不对,不知道什么原因。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;

struct cus{
	float hour;
	float mini;
	float second;
	float starttime;
	float waittime;
	int pro;
	int flag;
}customer[100001]; 

int endtime[100001];
vector<int> vec[100];

bool cmp(const cus &a,const cus &b)
{	
	if(a.flag!=b.flag)
		return a.flag>b.flag;
	if(a.hour!=b.hour)
		return a.hour<b.hour;
		if(a.mini!=b.mini)
			return a.mini<b.mini;
			if(a.second!=b.second)
				return a.second<b.second;
			//	return a.flag>b.flag;
}
int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=0;i<m;i++)
		vec[i].clear();
	memset(customer,0,sizeof(customer));
	for(int i=0;i<n;i++)
	{
		scanf("%f:%f:%f %d",&customer[i].hour,&customer[i].mini,&customer[i].second,&customer[i].pro);
		float h=customer[i].hour;
		float mi=customer[i].mini;
		float se=customer[i].second;
		if(h<8)
		{
			customer[i].starttime=8*3600;
			customer[i].waittime+=(7-h)*3600.0+3600.0-(mi*60+se);
		}
		else{
			customer[i].starttime=h*3600.0+mi*60+se;
		}
		if(customer[i].pro>60)
		customer[i].pro=60;
		if(h>17||(h==17&&mi>=0&&se>=1))
		{
			customer[i].flag=0;
		}
		else customer[i].flag=1;
	}
	sort(customer,customer+n,cmp);
		int k=0;
		while(1)
		
		{	
			if(customer[k].flag ==0) 
			{
				k++;break;
			}
			else
			int min=1<<30;
			for(int j=0;j<m;j++)
			{
				if(vec[j].empty())
				vec[j].push_back(k);
				endtime[j]=customer[k].starttime+customer[k].pro*60;
				k++;
			}
			while(k<n)
			{	
				if(customer[k].flag==0) {
				k++;continue;}
				int j;
				int	min=endtime[0];
				int chulie=0;
				for( j=0;j<m;j++)
				{
					
					if(min>endtime[j])
					{
						min=endtime[j];
						chulie=j;
					}
				}
				int ww=-customer[k].starttime+min;
				if(ww>0) customer[k].starttime=min;
				
				{
					vec[j].clear();
					vec[j].push_back(k);
					endtime[chulie]=customer[k].starttime+customer[k].pro*60;
					customer[k].waittime+=ww?ww:0;
					k++;
				}
			
			}
			
			if(k>=n) break;
		}
		int count=0;
		float sum=0;
		for(int i=0;i<n;i++)
		{
			if(customer[i].flag==1)
			{
				sum+=customer[i].waittime;
				count++;
			}
		}
		sum=sum/60.0;
		printf("%.1f\n",sum/count);
}

 不知道上述的code错在哪里,下面是ac代码,基本思路是一致的。

只不过是直接输入到达时间,和需要在银行的时间。

#include <stdio.h>
 #include <string.h>
 #include <algorithm>
 using namespace std;
 
 int N,K,wait_time=0;
 struct cus 
 {
     int arrive_time;
     int need_time;
 };
 
 struct cus customer[10002];
 
 struct win
 {
     int nat;
 };
 
 struct win windows[102];
 
 bool cmp(struct cus a,struct cus b)
 {
     return a.arrive_time<b.arrive_time;
 }
 
 int faw(int arrive_time)
 {
     int i;
     for(i=0;i<K;i++) 
	 {
         if(windows[i].nat<=arrive_time)
		{
             return i;
         }
     }
     return -1;
 }
 
 int few()
 {
     int i;
     int e=0;
     for(i=1;i<K;i++) {
         if(windows[i].nat<windows[e].nat) {
             e=i;
         }
     }
     return e;
 }
 
 int main()
 {
     scanf("%d%d",&N,&K);
     int i;
     char arrive_time[20];
     int need_time;
     for(i=0;i<K;i++) 
         windows[i].nat=3600*8;
     int len=0;
     for(i=0;i<N;i++) {
         int h,m,s;
         scanf("%s%d",arrive_time,&need_time);
         if(strcmp(arrive_time,"17:00:00")>0)
             continue;
         
         sscanf(arrive_time,"%d:%d:%d",&h,&m,&s);
         if(h<8)
             wait_time+=8*3600-(3600*h+60*m+s);
         customer[len].arrive_time=3600*h+60*m+s;
         customer[len++].need_time=need_time*60;
     }
     N=len;
 
     sort(customer,customer+N,cmp);
 
     for(i=0;i<N;i++) {
         int w=faw(customer[i].arrive_time);
         if(w>=0) 
		 {//找到空闲窗口
             if(customer[i].arrive_time<8*3600) 
			 {
                 windows[w].nat=8*3600+customer[i].need_time;
             } else
			 {
                 windows[w].nat=customer[i].arrive_time+customer[i].need_time;
             }
         } else { //找不到空闲窗口
             w=few();
             if(customer[i].arrive_time<8*3600) 
			 {//如果到得早 窗口的下个可用时间等于当前下个可用时间加新来顾客所需要服务时间
                 wait_time+=windows[w].nat-8*3600;
                 windows[w].nat=windows[w].nat+customer[i].need_time;
             } else
			 {
                 wait_time+=windows[w].nat-customer[i].arrive_time;
                 windows[w].nat=windows[w].nat+customer[i].need_time;
             }                
 
         }
     }
 
     printf("%.1f\n",1.0*wait_time/60.0/N);
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值