PAT Table Tennis

链接: https://www.nowcoder.com/questionTerminal/f499b30897e34f3ea33ad767daa6716a
来源:牛客网

[编程题]Table Tennis (30)
A table tennis club has N tables available to the public. The tablesare numbered from 1 to N. For any pair of players, if there are sometables open when they arrive, they will be assigned to the availabletable with the smallest number. If all the tables are occupied, theywill have to wait in a queue. It is assumed that every pair of playerscan play for at most 2 hours.
Your job is to count for everyone in queue their waiting time, and foreach table the number of players it has served for the day.
One thing that makes this procedure a bit complicated is that the clubreserves 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 cantake it. On the other hand, if when it is the turn of a VIP pair, yetno VIP table is available, they can be assigned as any ordinary players.
输入描述:
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.


输出描述:
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.
链接:https://www.nowcoder.com/questionTerminal/f499b30897e34f3ea33ad767daa6716a
来源:牛客网


   
   

输入

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

输出

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.cpp : 定义控制台应用程序的入口点。

//#include "stdafx.h"
#include"stdio.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<deque>
#include<math.h>
using namespace std;
int n;
int caltime(int h,int m,int s){//this function and the following one are most common tools to calculate time
	return h*60*60+m*60+s;


}


void caltime2(int t,int &h,int &m,int &s){
	h=t/3600;
	m=t/60%60;
	s=t%60;




}


struct node{
	int arr,p,tag,ser;//represent every pair of players ,including arriving time,playing time,tag and serving //time
};
vector<node*>vec;
int k,m,h,mi,s,p,tag;
int vip[110]={0};//mark the vip tables
int t[110]={0};//when will the table become available
bool cmp(node* a,node* b){
	return a->arr<b->arr;
}
int pickone(node *no){
	int min=21*3600,sel=-1;
	if(no->tag==1){//if the customer is an vip,pick vip table first
		for(int i=1;i<=k;i++){
			if(t[i]<=no->arr&&t[i]<min&&vip[i]==1){sel=i;break;}
		}
	}
	if(sel==-1){//if it's a normal customer,pick the table available with the smallest number
		for(int i=1;i<=k;i++){
			if(t[i]<=no->arr&&t[i]<min){sel=i;break;}
		}}if(sel==-1){
			for(int i=1;i<=k;i++)//if all the tables are occupied ,pick the one which will be finished //earliest
				if(t[i]<min){min=t[i];sel=i;}
		}
		return sel;


}


deque<node*>q;
int num[110]={0};


int main(){
	//freopen("d://jin.txt","r",stdin);
	cin>>n;
	for(int i=0;i<n;i++){
		scanf("%d:%d:%d %d %d",&h,&mi,&s,&p,&tag);
		node* no=new node();
		no->arr=caltime(h,mi,s);
		if(p>120)p=120;//this is a specifice easily ignored
		no->p=p*60;
		no->tag=tag;
		if(no->arr<21*3600)
			vec.push_back(no);


	}
	cin>>k>>m;
	for(int i=0;i<m;i++)
	{cin>>p;
	vip[p]=1;
	}
	sort(vec.begin(),vec.end(),cmp);
	int table;


	while(vec.size()!=0&&(table=pickone(vec[0]),table!=-1)){
		node *no=NULL;vector<node*>::iterator it=vec.begin();
		if(vip[table]==1){//if it is a vip table,it prefer the first vip customer;


			int serv=max(t[table],vec[0]->arr);


			while(it!=vec.end()){//find the vip customer
				no=*it;
				if(no->tag&&no->arr<=serv)break;
				it++;
			}
			if(it==vec.end())it=vec.begin();


		}
		else it=vec.begin();
		no=*it;


		int serv=max(t[table],no->arr);
		no->ser=serv;
		t[table]=serv+no->p;


		num[table]++;


		if(!q.empty()){node *n2=q.back();
		if(no->ser==n2->ser&&no->tag>n2->tag){
			q.pop_back();q.push_back(no);
			q.push_back(n2);//customers with the same serve time,vip is served first
		}
		else q.push_back(no);}
		else q.push_back(no);


		vec.erase(it);
	}  


	while(!q.empty()){
		node* no=q.front();
		caltime2(no->arr,h,mi,s);
		printf("%02d:%02d:%02d ",h,mi,s);
		caltime2(no->ser,h,mi,s);
		printf("%02d:%02d:%02d ",h,mi,s);
		cout<<(no->ser-no->arr+30)/60<<endl;;
		q.pop_front();
	}


	for(int i=1;i<=k;i++)
	{cout<<num[i];
	if(i!=k)cout<<" ";
	}






	//freopen("CON","r",stdin);
	//system("pause");


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值