PAT 1026. Table Tennis (30)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.

Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.

One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (<=10000) - the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS - the arriving time, P - the playing time in minutes of a pair of players, and tag - which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players' info, there are 2 positive integers: K (<=100) - the number of tables, and M (< K) - the number of VIP tables. The last line contains M table numbers.

Output Specification:

For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.

Sample Input:
9
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 5 0
08:12:00 10 1
20:50:00 10 0
08:01:30 15 1
20:53:00 10 1
3 1
2
Sample Output:
08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:12:00 08:16:30 5
08:10:00 08:20:00 10
20:50:00 20:50:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
3 3 2
这道题略微复杂,要注意的地方主要有三点:
1.当有多个乒乓球台空闲时,vip顾客到了会使用最小id的vip球台,而不是最小id的球台,测试以下用例:
2
10:00:00 30 1
12:00:00 30 1
5 1
3
输出正确结果应为:
0 0 2 0 0
2.题目要求每对顾客玩的时间不超过2小时,那么当顾客要求玩的时间>2小时的时候,应该截断控制,测试以下用例:
2
18:00:00 180 1
20:00:00 60 1
1 1
1
输出的正确结果应为:
18:00:00 18:00:00 0
20:00:00 20:00:00 0
2
3.虽然题目中保证客户到达时间在08:00:00到21:00:00之间,但是根据最后的8个case来看,里面还是有不在这个时间区间内到达的顾客,所以建议还是稍加控制,测试以下用例:
1
21:00:00 80 1
1 1
1
输出的正确结果应为:
0
下面是可以全部通过的代码,仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#define N 10000
#define K 100

typedef struct
{
	char arrtime[9];
	int playingtime;
	char is_vip;
}player; //size of player: 9+4+1=14B

typedef struct
{
	char opentime[9];  //从此刻看,这个桌子的开放时间
	int usedtimes;
	char is_vip;
}table;

player players[N+1];
table tables[K+1];
int table_num, vip_table_num;

int waiting_minutes(char arrtime[], char sertime[])
{
	int i, minutes = 0;
	minutes += (sertime[3] - arrtime[3]) * 10 + (sertime[4] - arrtime[4]);
	minutes += ((sertime[0] - arrtime[0]) * 10 + (sertime[1] - arrtime[1])) * 60;
	i = (sertime[6] - arrtime[6]) * 10 + sertime[7] - arrtime[7]; 
	if(i >= 30)
		minutes++;
	else if(i < -30)
		minutes--;
	return minutes;
}

char *add(char time_str[], int minutes)
{
	int h, m;
	m = ((time_str[0] - '0') * 10 + time_str[1] - '0') * 60 \
		+ (time_str[3] - '0') * 10 + time_str[4] - '0';
	m += minutes;
	h = m / 60;
	m = m % 60;
	time_str[0] = h / 10 + '0';
	time_str[1] = h % 10 + '0';
	time_str[3] = m / 10 + '0';
	time_str[4] = m % 10 + '0';
	return time_str;
}

int calc_sec(char time_str[])  // 将一个时间字符串转换成秒(从00:00:00开始计时)
{
	int s;
	s = (time_str[6] - '0') * 10 + time_str[7] - '0';
	s += (time_str[3] - '0') * 600 + (time_str[4] - '0') * 60;
	s += (time_str[0] - '0') * 36000 + (time_str[1] - '0') * 3600;
	return s;
}

int is_early_than(char time1[], char time2[])
{
	int s1, s2;
	s1 = calc_sec(time1);
	s2 = calc_sec(time2);
	if(s1 <= s2)  // 包括等于
		return 1;
	else
		return 0;
}

int is_closed(char time_str[])
{
	int last = 21 * 3600, s;
	s = calc_sec(time_str);
	if(s < last)
		return 0;
	else
		return 1;
}


int get_a_table(table tables[], int table_num, char time_now[])
{
	int i;
	for(i = 1; i <= table_num; i++)
		if(is_early_than(tables[i].opentime, time_now))
			return i;
	return 0;
}

int get_earliest_table(table tables[], int table_num)
{
	int i, num = 1;
	char earlistime[9] = "21:00:00";
	for(i = 1; i <= table_num; i++)
		if(!is_early_than(earlistime, tables[i].opentime))
		{
			strcpy(earlistime, tables[i].opentime);
			num = i;
		}
	return num;
}

int get_a_vip_table(table tables[], int table_num, char time_now[])
{
	int i;
	for(i = 1; i <= table_num; i++)
		if(is_early_than(tables[i].opentime, time_now) && tables[i].is_vip == 'Y')
			return i;
	return 0;
}


// 从当前下标cur_index开始,在players[]里面往后找,看在时间time_before之前,能不能找到一个vip player
// 找到了就返回1,并把这个vip player放在cur_index上,其它后挪一位,否则返回0
int get_a_vip_player(player players[], int pairs, int cur_index, char time_before[])
{
	int found = 0, i, j;
	player p;
	for(i = cur_index + 1; i <= pairs; i++)
	{
		if(!is_early_than(players[i].arrtime, time_before))
			break;
		if(players[i].is_vip == 'Y')
		{
			found = 1;
			p = players[i];
			for(j = i; j > cur_index; j--)
				players[j] = players[j - 1];
			players[cur_index] = p;
			break;
		}
	}
	return found;
}

int cmp(const void *a, const void *b)
{
	int i;
	player *c, *d;
	c = (player *)a;
	d = (player *)b;
	for(i = 0; i < 9; i++)
		if((c->arrtime)[i] != (d->arrtime)[i])
			return ((c->arrtime)[i]) - ((d->arrtime)[i]);
	return 0;
}


int main()
{
	int i, j, pairs, num, vip_num, playingtime, is_vip;
	char time_str[9];
	scanf("%d", &pairs);
	i = 1;
	while(i <= pairs)
	{
		scanf("%s%d%d", &time_str, &playingtime, &is_vip);
		if(playingtime > 120)
			playingtime = 120;
		if(is_vip)
		{
			strcpy(players[i].arrtime, time_str);
			players[i].playingtime = playingtime;
			players[i].is_vip = 'Y';
		}
		else
		{
			strcpy(players[i].arrtime, time_str);
			players[i].playingtime = playingtime;
			players[i].is_vip = 'N';
		}
		i++;
	}
	qsort(&(players[1]), pairs, sizeof(player), cmp);
	
	scanf("%d%d", &table_num, &vip_table_num);
	for(i = 1; i <= table_num; i++)
	{
		tables[i].usedtimes = 0;
		tables[i].is_vip = 'N';
		strcpy(tables[i].opentime, "08:00:00");
	}
	for(i = 1; i <= vip_table_num; i++)
	{
		scanf("%d", &j);
		tables[j].is_vip = 'Y';
	}
	
	//开始遍历
	for(i = 1; i <= pairs; i++)
	{
		if(is_closed(players[i].arrtime))
			break;
		num = get_a_table(tables, table_num, players[i].arrtime);
		vip_num = get_a_vip_table(tables, table_num, players[i].arrtime);
		if(num > 0)  //表示还有空桌子
		{
			if(players[i].is_vip == 'Y' && vip_num > 0) //如果是vip player并且有空vip table
			{
				tables[vip_num].usedtimes++;
				strcpy(tables[vip_num].opentime, players[i].arrtime);
				add(tables[vip_num].opentime, players[i].playingtime);
				printf("%s %s %d\n", players[i].arrtime, players[i].arrtime, 0);
			}
			else
			{
				tables[num].usedtimes++;
				strcpy(tables[num].opentime, players[i].arrtime);
				add(tables[num].opentime, players[i].playingtime);
				printf("%s %s %d\n", players[i].arrtime, players[i].arrtime, 0);
			}
		}
		else if(num == 0)  //没有空桌了,当前这个人必须要等了
		{
			num = get_earliest_table(tables, table_num);
			if(is_closed(tables[num].opentime))
				break;
			if(tables[num].is_vip == 'N') // 当前桌不是vip桌
			{
				tables[num].usedtimes++;
				printf("%s %s %d\n", players[i].arrtime, tables[num].opentime, \
					 waiting_minutes(players[i].arrtime, tables[num].opentime));
				add(tables[num].opentime, players[i].playingtime);
			}
			else  // 当前桌正好是vip桌
			{
				if(players[i].is_vip == 'Y')  // 当前player正好是vip player
				{
					tables[num].usedtimes++;
					printf("%s %s %d\n", players[i].arrtime, tables[num].opentime, \
						 waiting_minutes(players[i].arrtime, tables[num].opentime));
					add(tables[num].opentime, players[i].playingtime);
				}
				else  // 当前player不是vip player
				{
					get_a_vip_player(players, pairs, i, tables[num].opentime);
					tables[num].usedtimes++;
					printf("%s %s %d\n", players[i].arrtime, tables[num].opentime, \
						 waiting_minutes(players[i].arrtime, tables[num].opentime));
					add(tables[num].opentime, players[i].playingtime);
				}
			}
		}
	}
	for(i = 1; i <= table_num; i++){
		printf("%d", tables[i].usedtimes);		
		if(i != table_num)
			printf(" ");
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值