PAT Advanced 1026 Table Tennis (30)

问题描述:

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

 

开始写毕业设计的博主终于开始了摸鱼的生活。。。

博主开心的打开了PAT的提交列表,每天从里面拿出一点自己的遗产出来发博客。。。

看到那些熟悉的题目,和当时自己写的长得吓人的代码,博主不禁想起那些过去的日子。。。

(p.s. 反正我现在也读不懂自己的程序,复制粘贴就对了。。。)

AC代码:

#include<bits/stdc++.h>
using namespace std;  
struct user
{
	string arrive_time;
	int server_time;
	int vip;
	user(string at,int st,int v)
	{
		arrive_time=at;
		server_time=st;
		vip=v;
	}
	user(const user &u)
	{
		arrive_time=u.arrive_time;
		server_time=u.server_time;
		vip=u.vip;
	}
};
struct table
{
	bool used;
	bool vip;
	int sum_of_user;
	table(bool u,bool v,int sou)
	{
		used=u;
		vip=v;
		sum_of_user=sou;
	}
	table(const table& ta)
	{
		vip=ta.vip;
		used=ta.used;
		sum_of_user=ta.sum_of_user;
	}
};
inline void timeadd(string &t,int& min)
{
	int th,tm,ts;
	char a[3];
	char b[3];
	sscanf(t.c_str(),"%d:%d:%d",&th,&tm,&ts);
	int h=min/60;
	int m=min%60;
	th=h+th;
	tm=tm+m;
	if(tm>=60)
	{
		th++;
		tm=tm-60;
	}
	if(th<10)
		sprintf(a,"0%d",th);
	else
		sprintf(a,"%d",th);
	if(tm<10)
		sprintf(b,"0%d",tm);
	else
		sprintf(b,"%d",tm);
	t.replace(0,2,a);
	t.replace(3,2,b);
}
inline int timemin(string &s1,string &s2)
{
	int th1,tm1,ts1;
	int th2,tm2,ts2;
	int s;
	sscanf(s1.c_str(),"%d:%d:%d",&th1,&tm1,&ts1);
	sscanf(s2.c_str(),"%d:%d:%d",&th2,&tm2,&ts2);
	if(ts2-ts1>=30) s=1;
	else if(ts2-ts1<=-30) s=-1;
	else s=0;
	return ((th2-th1)*60+(tm2-tm1)+s);
}
int main()
{ 
  //  freopen("data.txt","r", stdin);  
	int n;
	map <string,user> m;
	map <string,user> sm;
	map <string,user>::iterator itm;
	list <user> waiting;
	list<list<user>::iterator> itl_vip;
	list<user>::iterator itl;
	char s[9];
	string ss;
	string se;
	int a,v,st,vt;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%s",&s);
		ss=s;
		scanf("%d %d",&a,&v);
		a=(a>120?120:a);
		user u(ss,a,v);
		m.insert(pair<string,user>(ss,u));
	}
	se="08:00:00";
	ss="21:00:00";
	user u(ss,0,1);
	m.insert(pair<string,user>(ss,u));
	scanf("%d %d",&st,&vt);
	vector<int> vip_table_num;
	map<string,int> table_et;
	map<string,int>::iterator itet;
	table ex(true,false,0);
	vector <table> ta(st+1,ex);
	n=0;
	scanf("%d",&n);
	int next_vip_table=n;
	ta[n].vip=true;
	ta[0].used=false;
	vip_table_num.push_back(n);
	for(int i=1;i<vt;i++)
	{
		scanf("%d",&n);
		ta[n].vip=true;
		vip_table_num.push_back(n);
	}
	int pp=0;
	for(itm=m.begin();itm!=m.end();++itm)
	{
		for(;;)
		{
			if((table_et.empty())||((table_et.begin()->first)>(itm->first))||((table_et.begin()->first)>=ss))
			break;
			else
			{
				n=table_et.begin()->second;				
				if(ta[n].vip&&!(itl_vip.empty()))
				{
					ta[n].used=false;
					ta[n].sum_of_user++;
					string endt=table_et.begin()->first;
					v=(*(*itl_vip.begin())).server_time;
					(*(*itl_vip.begin())).server_time=timemin((*(*itl_vip.begin())).arrive_time,endt);
					sm.insert(pair<string,user>(endt,*(*itl_vip.begin()))); 
					timeadd(endt,v);
					waiting.erase(*itl_vip.begin());
					itl_vip.pop_front();
					table_et.erase(table_et.begin());
					table_et.insert(pair<string,int>(endt,n));
				}
				else
				{
					if(waiting.empty())
					{
						st++;
						ta[n].used=true;
						if(ta[n].vip)
						{
							if(next_vip_table)
							next_vip_table=min(next_vip_table,n);
							else
							next_vip_table=n;
						}
						table_et.erase(table_et.begin());
					}
					else
					{
						ta[n].used=false;
						ta[n].sum_of_user++;
						string endt=table_et.begin()->first;
						v=(*waiting.begin()).server_time;
						(*waiting.begin()).server_time=timemin((*waiting.begin()).arrive_time,endt);
						sm.insert(pair<string,user>(endt,*waiting.begin())); 
						timeadd(endt,v);
						if((*waiting.begin()).vip)
						itl_vip.pop_front();
						waiting.pop_front();
						table_et.erase(table_et.begin());
						table_et.insert(pair<string,int>(endt,n));						
					}
				}
			}
		}
		if((itm->first)<se||(itm->first)>=ss)	continue;
		if(next_vip_table&&itm->second.vip)
		{
			ta[next_vip_table].used=false;
			ta[next_vip_table].sum_of_user++;
			string endt=itm->first;
			timeadd(endt,itm->second.server_time);
			table_et.insert(pair<string,int>(endt,next_vip_table));
			itm->second.server_time=0;
			sm.insert(pair<string,user>(itm->first,itm->second)); 
			if(--st)
			{
				int i;
				for(i=0;i<vt;i++)
				if(ta[vip_table_num[i]].used)
				next_vip_table=vip_table_num[i];
				if(i==vt)
				next_vip_table=0;
			}
			else
			next_vip_table=0;
		}
		else
		{
			if(st)
			{
				for(int i=1;i<ta.size();i++)
				if(ta[i].used)
				{
					n=i;
					break;
				}
				ta[n].used=false;
				ta[n].sum_of_user++;
				string endt=itm->first;
				timeadd(endt,itm->second.server_time);
				table_et.insert(pair<string,int>(endt,n));
				itm->second.server_time=0;
				sm.insert(pair<string,user>(itm->first,itm->second)); 
				st--;
				if(ta[n].vip)
				{
					if(st)
					{
						int i;
						for(i=0;i<vt;i++)
						if(ta[vip_table_num[i]].used)
						next_vip_table=vip_table_num[i];
						if(i==vt)
						next_vip_table=0;
					}
					else
					next_vip_table=0;
				}
			}
			else
			{
				waiting.push_back(itm->second);
				if(itm->second.vip)
				{
					itl=waiting.end();
					itl--;
					itl_vip.push_back(itl);
				}
			}
		}
	}
	for(itm=sm.begin();itm!=sm.end();++itm)
	printf("%s %s %d\n",itm->second.arrive_time.c_str(),itm->first.c_str(),itm->second.server_time);
	if(ta.size()>1)
	printf("%d",ta[1].sum_of_user);
	for(int i=2;i<ta.size();i++)
	printf(" %d",ta[i].sum_of_user);
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值