【PAT 甲级】A1014 Waiting in Line (30分)

【版本一:暴力模拟,19分】
都给孩子模拟哭了…什么破题啊烦!

#include <bits/stdc++.h>
using namespace std;
#define MAX 0x3fffffff
struct node
{
	int t;
	int finish_time=-1;
};
int wq[11][21];			//window-queue:the id of the person
node cstm[1010];
int main(void)
{
	int N,M,K,Q;
	scanf("%d%d%d%d",&N,&M,&K,&Q);
	for(int i=1;i<=K;i++)
	scanf("%d",&cstm[i].t);
	for(int i=0;i<M;i++)
	for(int j=0;j<N;j++)
	wq[i][j]=0;			//initially, no customers
	int q0=1;			//the first person behind the yellow line
	int clk=0;			//time clock in real time
	bool flag=true;		//still have customers
	for(int i=0;i<M;i++)
	for(int j=0;j<N;j++)
	{
		wq[i][j]=q0;q0++;
		if(q0>K) break;
	}
	while(clk<=540&&flag)
	{
		int min=MAX;
		for(int i=0;i<N;i++)
		{
			if(wq[0][i]!=0 &&cstm[wq[0][i]].t<min)
			min=cstm[wq[0][i]].t;
		}
		if(min==MAX) flag=false;						//all the customers were served
		clk+=min;
		for(int i=0;i<N;i++)
		{
			if(wq[0][i]!=0)
			{
				cstm[wq[0][i]].t-=min;
				if(cstm[wq[0][i]].t==0)					//the customer finished his transaction
				{
					cstm[wq[0][i]].finish_time=clk;
					if(q0<=K)							//still have customers
					{
						for(int j=0;j<M-1;j++)
						wq[j][i]=wq[j+1][i];
						wq[M-1][i]=q0;
						q0++;
					}
					else
					{
						int j;
						for(j=0;wq[j+1][i]!=0;j++)
						wq[j][i]=wq[j+1][i];
						wq[j][i]=0;
					}
				}
			}
		}
	}
	int temp;
	for(int i=0;i<Q;i++)
	{
		scanf("%d",&temp);
		int n=cstm[temp].finish_time;
		if(n==-1||n>540) printf("Sorry");
		else
		{
			int hour=8,minute;
			while(n>=60)
			{
				hour++;n-=60;
			}
			minute=n;
			printf("%02d:%02d",hour,minute);
		}
		if(i!=Q-1) printf("\n");
	}
}

【版本二:22分】
坑点:不应该一巴掌拍死所有超过17:00的,而只针对那些17:00(含)之后才开始办业务的…这样就又过了一个测试点- -
其实就是把刚才的边界条件里的等号去掉就可以啦——clk<540

【版本三:30分,AC】
刚才的考虑一点都不严密,可能在clk>=540的时候还有点没被处理,结构体需要引入一个bool值保存当前点在540min前是否被处理了。如果没被处理,应该接着处理。

#include <bits/stdc++.h>
using namespace std;
#define MAX 0x3fffffff
struct node
{
	int t;
	int finish_time=-1;
	bool start=false; 
};
int wq[11][21];			//window-queue:the id of the person
node cstm[1010];
int main(void)
{
	int N,M,K,Q;
	scanf("%d%d%d%d",&N,&M,&K,&Q);
	for(int i=1;i<=K;i++)
	scanf("%d",&cstm[i].t);
	for(int i=0;i<M;i++)
	for(int j=0;j<N;j++)
	wq[i][j]=0;			//initially, no customers
	int q0=1;			//the first person behind the yellow line
	int clk=0;			//time clock in real time
	for(int i=0;i<M;i++)
	for(int j=0;j<N;j++)
	{
		wq[i][j]=q0;q0++;
		if(q0>K) break;
	}
	while(clk<540)
	{
		int min=MAX;
		for(int i=0;i<N;i++)
		{
			if(wq[0][i]!=0 &&cstm[wq[0][i]].t<min)
			min=cstm[wq[0][i]].t;
		}
		if(min==MAX) break;						//all the customers were served
		clk+=min;
		for(int i=0;i<N;i++)
		{
			if(wq[0][i]!=0)
			{
				cstm[wq[0][i]].t-=min;
				cstm[wq[0][i]].start=true;				//start the transaction
				if(cstm[wq[0][i]].t==0)					//the customer finished his transaction
				{
					cstm[wq[0][i]].finish_time=clk;
					if(q0<=K)							//still have customers
					{
						for(int j=0;j<M-1;j++)
						wq[j][i]=wq[j+1][i];
						wq[M-1][i]=q0;
						q0++;
					}
					else
					{
						int j;
						for(j=0;wq[j+1][i]!=0;j++)
						wq[j][i]=wq[j+1][i];
						wq[j][i]=0;
					}
				}
			}
		}
	}
	for(int i=0;i<N;i++)
	{
		if(wq[0][i]!=0&&cstm[wq[0][i]].start)
		cstm[wq[0][i]].finish_time=clk+cstm[wq[0][i]].t;
	}
	int temp;
	for(int i=0;i<Q;i++)
	{
		scanf("%d",&temp);
		int n=cstm[temp].finish_time;
		if(n==-1) printf("Sorry");
		else printf("%02d:%02d",8+n/60,n%60);
		if(i!=Q-1) printf("\n");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值