ZJU PAT 1017

Solar-Rain 2013-10-24 10:34:24
1017. Queueing at Bank (25)

时间限制
400 ms
内存限制
32000 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


上面是题目,这个题,很奇怪的是,我自己写的算法,至少给的测试用例已经得到正确结果了,但是提交上去以后尽然是0分,所有的case都显示答案错误,下面是代码,用VS2012写的,自己也A了20多道题目,这道题不知道问题出在哪,也可能是个很小的问题吧,反正卡在这里找不到原因还是没办法的。
下面是代码。。
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int MAX=10086;
typedef struct QJKcustomer
{
int arrivingtime;
int waitingtime;
int inservingtime;
int tooearly;
};
int compare(const void *a,const void *b)
{
int aa=*(int *)a;
int bb=*(int *)b;
return aa-bb;
}
QJKcustomer customer[MAX];
int HASH[110];
int main()
{
int N,K;
scanf("%d%d",&N,&K);
int index=0;
for(int i=0;i<N;++i)
{
int hh,mm,ss,is;
scanf("%d:%d:%d %d",&hh,&mm,&ss,&is);
if(hh<8)
{
customer[index].tooearly=8*3600-(hh*3600+mm*60+ss);
hh=8,mm=0,ss=0;
}
else if(hh>=17)continue;
hh-=8;
customer[index].arrivingtime=hh*3600+mm*60+ss;
customer[index].inservingtime=is*60;
++index;
}
qsort(customer,index,sizeof(QJKcustomer),compare);
int j=0,f=0;
memset(HASH,customer[0].arrivingtime,K);
while(f<index)
{
qsort(HASH,K,sizeof(int),compare);
if(HASH[0]>customer[f].arrivingtime+customer[f].waitingtime)
{
int needwait=HASH[0]-(customer[f].arrivingtime+customer[f].waitingtime);
for(int i=f;i<index;++i)
{
customer[i].waitingtime+=needwait;
}
HASH[0]+=customer[f].inservingtime;
++f;
}
else if(HASH[0]==customer[f].arrivingtime+customer[f].waitingtime)
{
HASH[0]+=customer[f].inservingtime;
++f;
}
else if(HASH[0]<customer[f].arrivingtime+customer[f].waitingtime)
{
int noneedwait=customer[f].arrivingtime+customer[f].waitingtime-HASH[0];
customer[f].waitingtime-=noneedwait;
HASH[0]+=customer[f].inservingtime;
++f;
}
}
int totalwaitingtime=0;
for(int i=0;i<index;++i)
{
totalwaitingtime+=customer[i].waitingtime+customer[i].tooearly;
}
printf("%.1f",totalwaitingtime/(index*60.0));
return 0;
}
...全文
60 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Solar-Rain 2013-10-24
  • 打赏
  • 举报
回复
额,又稍微改了下,居然A掉了,PAT真是神奇的东西- -!
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int MAX=10086;
typedef struct QJKcustomer
{
	int arrivingtime;
	int waitingtime;
	int inservingtime;
	int tooearly;
};
int compare(const void *a,const void *b)
{
	QJKcustomer aa=*(QJKcustomer *)a;
	QJKcustomer bb=*(QJKcustomer *)b;
	return aa.arrivingtime-bb.arrivingtime;
}
int COMPARE(const void *a,const void *b)
{
	int aa=*(int *)a;
	int bb=*(int *)b;
	return aa-bb;
}
QJKcustomer customer[MAX];
int HASH[110];
int main()
{
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	int N,K;
	scanf("%d%d",&N,&K);
	int index=0;
	for(int i=0;i<N;++i)
	{
		int hh,mm,ss,is;
		scanf("%d:%d:%d %d",&hh,&mm,&ss,&is);
		if(hh<17)
		{
			customer[index].arrivingtime=hh*3600+mm*60+ss;
			customer[index].inservingtime=is*60;
			++index;
		}
		else if(hh>=17)continue;
	}
	qsort(customer,index,sizeof(QJKcustomer),compare);
	for(int i=0;i<index;++i)
	{
		if(customer[i].arrivingtime<8*3600)
		{
			customer[i].tooearly=8*3600-customer[i].arrivingtime;
			customer[i].arrivingtime=0;
		}
		else customer[i].arrivingtime-=8*3600;
	}
	int j=0,f=0;
	memset(HASH,customer[0].arrivingtime,K);
	while(f<index)
	{
		qsort(HASH,K,sizeof(int),COMPARE);
		if(HASH[0]>customer[f].arrivingtime+customer[f].waitingtime)
		{
			int needwait=HASH[0]-(customer[f].arrivingtime+customer[f].waitingtime);
			for(int i=f;i<index;++i)
			{
				customer[i].waitingtime+=needwait;
			}
			HASH[0]+=customer[f].inservingtime;
			++f;
		}
		else if(HASH[0]==customer[f].arrivingtime+customer[f].waitingtime)
		{
			HASH[0]+=customer[f].inservingtime;
			++f;
		}
//前一楼层的代码这里改的不好,完善了下直接AC
		else if(HASH[0]<customer[f].arrivingtime+customer[f].waitingtime)
		{
			if(HASH[0]>=customer[f].arrivingtime)
			{
				customer[f].waitingtime=HASH[0]-customer[f].arrivingtime;
				HASH[0]+=customer[f].inservingtime;
			}
			else 
			{
				HASH[0]=customer[f].arrivingtime+customer[f].inservingtime;
				customer[f].waitingtime=0;
			}
			++f;
		}
	}
	int totalwaitingtime=0;
	for(int i=0;i<index;++i)
	{
		totalwaitingtime+=customer[i].waitingtime+customer[i].tooearly;
	}
	printf("%.1f",totalwaitingtime/(index*60.0));
	return 0;
}
Solar-Rain 2013-10-24
  • 打赏
  • 举报
回复
改了HASH[0]<customer[f].arrivingtime下的情况,又过了3个case,太费劲了

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int MAX=10086;
typedef struct QJKcustomer
{
	int arrivingtime;
	int waitingtime;
	int inservingtime;
	int tooearly;
};
int compare(const void *a,const void *b)
{
	QJKcustomer aa=*(QJKcustomer *)a;
	QJKcustomer bb=*(QJKcustomer *)b;
	return aa.arrivingtime-bb.arrivingtime;
}
int COMPARE(const void *a,const void *b)
{
	int aa=*(int *)a;
	int bb=*(int *)b;
	return aa-bb;
}
QJKcustomer customer[MAX];
int HASH[110];
int main()
{
	int N,K;
	scanf("%d%d",&N,&K);
	int index=0;
	for(int i=0;i<N;++i)
	{
		int hh,mm,ss,is;
		scanf("%d:%d:%d %d",&hh,&mm,&ss,&is);
		if(hh<17)
		{
			customer[index].arrivingtime=hh*3600+mm*60+ss;
			customer[index].inservingtime=is*60;
			++index;
		}
		else if(hh>=17)continue;
	}
	qsort(customer,index,sizeof(QJKcustomer),compare);
	for(int i=0;i<index;++i)
	{
		if(customer[i].arrivingtime<8*3600)
		{
			customer[i].tooearly=8*3600-customer[i].arrivingtime;
			customer[i].arrivingtime=0;
		}
		else customer[i].arrivingtime-=8*3600;
	}
	int j=0,f=0;
	memset(HASH,customer[0].arrivingtime,K);
	while(f<index)
	{
		qsort(HASH,K,sizeof(int),COMPARE);
		if(HASH[0]>customer[f].arrivingtime+customer[f].waitingtime)
		{
			int needwait=HASH[0]-(customer[f].arrivingtime+customer[f].waitingtime);
			for(int i=f;i<index;++i)
			{
				customer[i].waitingtime+=needwait;
			}
			HASH[0]+=customer[f].inservingtime;
			++f;
		}
		else if(HASH[0]==customer[f].arrivingtime+customer[f].waitingtime)
		{
			HASH[0]+=customer[f].inservingtime;
			++f;
		}
//下面是修改的部分
		else if(HASH[0]<customer[f].arrivingtime+customer[f].waitingtime)
		{
			int noneedwait=customer[f].arrivingtime+customer[f].waitingtime-HASH[0];
			if(customer[f].waitingtime>=noneedwait)customer[f].waitingtime-=noneedwait;
			else 
			{
				if(HASH[0]<=customer[f].arrivingtime)
				{
					HASH[0]=customer[f].arrivingtime+customer[f].inservingtime;
					customer[f].waitingtime=0;
				}
				else
				{
					customer[f].waitingtime=HASH[0]-customer[f].arrivingtime;
					HASH[0]+=customer[f].inservingtime;
				}
			}
			++f;
		}
	}
	int totalwaitingtime=0;
	for(int i=0;i<index;++i)
	{
		totalwaitingtime+=customer[i].waitingtime+customer[i].tooearly;
	}
	printf("%.1f",totalwaitingtime/(index*60.0));
	return 0;
}
Solar-Rain 2013-10-24
  • 打赏
  • 举报
回复
顾客的排序哪里貌似出了问题,稍微改了下以后,一样的输出结果,貌似只pass了case1,fk! 马上就考试了,这怎么能行。

64,645

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧