1026 Table Tennis

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


题型分类:快乐模拟

题目大意:球桌里面有普通球桌和vip球桌,球员里面也分为普通球员和vip球员。球员按时间的先后请求使用球桌,有一条特殊的规则是:如果vip球桌空闲出来,并且此时有在等待的vip球员,那么把这个球桌分配给vip球员。最后按使用球桌的先后时间,依次输出球员的开始使用球桌的时间、球员到达俱乐部的时间,等待球桌的时间。

解题思路:模拟+排序


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;

const int maxn = 10010;
const int maxk = 110;
const int INF = 0x3f3f3f3f;

typedef struct player {
	int arriveTime;
	int processTime;
	int serveTime;
	bool isVip;
	player() {
		serveTime = INF;
		isVip = false;
	}
} player;

typedef struct table {
	int edTime;
	int serveNum;
	bool isVip;
	table() {
		isVip = false;
		serveNum = 0;
	}
} table;

vector<player> p;
table t[maxk];
int N;

int convertToSecond(int hh, int mm, int ss);
void initTable(int K);
bool cmpArriveTime(player a, player b);
int nextVipPlayer(int now);
void allocateTable(int tableId, int playerId);
bool cmpServeTime(player a, player b);

int main(int argc, char** argv) {
	scanf("%d", &N);
	int stTime = convertToSecond(8, 0, 0), edTime = convertToSecond(21, 0, 0);
	for(int i = 0; i < N; i++) { //输入球员的信息
		player temp;
		int hh, mm, ss, process, isVip;
		scanf("%d:%d:%d %d %d", &hh, &mm, &ss, &process, &temp.isVip);
		temp.processTime = process > 120 ? 7200 : process * 60; //最多服务2小时
		temp.arriveTime = convertToSecond(hh, mm, ss);
		if(temp.arriveTime < edTime){
			p.push_back(temp);
		}
	}
	int K, M, vipTable;
	scanf("%d %d", &K, &M);
	for(int i = 0; i < M; i++) { //输入球桌的信息
		scanf("%d", &vipTable);
		t[vipTable].isVip = true;
	}
	initTable(K); //初始化球桌的结束服务时间
	sort(p.begin(), p.end(), cmpArriveTime); //按到来的先后顺序排序
	int playerId = 0, vipPlayerId = nextVipPlayer(-1); //初始化为最早到的vip球员序号
	while(playerId < p.size()) {
		if(p[playerId].isVip == true && playerId < vipPlayerId) { //跳过已经服务过的vip球员
			playerId++;
			continue;
		}
		int tableId = -1, minEdTime = INF;
		for(int i = 1; i <= K; i++) { //找到首先空出来的球桌
			if(t[i].edTime < minEdTime) {
				tableId = i;
				minEdTime = t[i].edTime;
			}
		}
		if(t[tableId].edTime >= edTime) { //到达关门时间,循环结束
			break;
		}
		if(t[tableId].isVip == true) { //如果空闲的球桌是vip球桌
			if(p[playerId].isVip == true) { //最前面的就是vip球员,分配给该球员
				allocateTable(tableId, playerId);
				playerId++;
				vipPlayerId = nextVipPlayer(vipPlayerId);
			} else { //最前面的是普通球员
				if(vipPlayerId < p.size() && p[vipPlayerId].arriveTime <= t[tableId].edTime) { //队列中存在vip球员
					allocateTable(tableId, vipPlayerId);
					vipPlayerId = nextVipPlayer(vipPlayerId);
				} else { //队列中不存在vip球员,把球桌分给队列中的最前面的球员
					allocateTable(tableId, playerId);
					playerId++;
				}
			}
		} else { //最先空出来的是普通球桌
			if(p[playerId].isVip == true) { //最前面的是vip球员
				int vipTableId = -1, vipMin = INF;
				for(int i = 1; i <= K; i++) {
					if(t[i].isVip == true && t[i].edTime < vipMin) { //寻找最先空出的vip球桌
						vipMin = t[i].edTime;
						vipTableId = i;
					}
				}
				if(vipTableId != -1 && p[playerId].arriveTime >= t[vipTableId].edTime) { //找到最先空出的vip球桌,并且vip球员已经在等待了
					allocateTable(vipTableId, playerId);
				} else { //不满足条件,分配普通球桌
					allocateTable(tableId, playerId);
				}
				playerId++;
				vipPlayerId = nextVipPlayer(vipPlayerId);
			} else {
				allocateTable(tableId, playerId);
				playerId++;
			}
		}
	}
	sort(p.begin(), p.end(), cmpServeTime); //按服务顺序排序
	for(int i = 0; i < p.size() && p[i].serveTime <= edTime; i++) {
		printf("%02d:%02d:%02d ", p[i].arriveTime / 3600, p[i].arriveTime / 60 % 60, p[i].arriveTime % 60);
		printf("%02d:%02d:%02d ", p[i].serveTime / 3600, p[i].serveTime / 60 % 60, p[i].serveTime % 60);
		printf("%.0f\n", round((p[i].serveTime - p[i].arriveTime) / 60.0));
	}
	for(int i = 1; i <= K; i++) {
		if(i != 1) printf(" ");
		printf("%d", t[i].serveNum);
	}
	return 0;
}

int convertToSecond(int hh, int mm, int ss) {
	return hh * 3600 + mm * 60 + ss;
}

void initTable(int K) {
	for(int i = 1; i <= K; i++) {
		t[i].edTime = convertToSecond(8, 0, 0);
	}
}

bool cmpArriveTime(player a, player b) {
	return a.arriveTime < b.arriveTime;
}

int nextVipPlayer(int iVIP) {
	iVIP++;
	while(iVIP < p.size() && p[iVIP].isVip == false){
		iVIP++;
	}
	return iVIP; //队列中没有vip球员了
}

void allocateTable(int tableId, int playerId) {
	t[tableId].serveNum++;
	if(t[tableId].edTime > p[playerId].arriveTime) { //球员已经在等了
		p[playerId].serveTime = t[tableId].edTime;
	} else { //球员还没到
		p[playerId].serveTime = p[playerId].arriveTime;
	}
	t[tableId].edTime = p[playerId].serveTime + p[playerId].processTime;
}

bool cmpServeTime(player a, player b) {
	return a.serveTime < b.serveTime;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The following is the data that you can add to your input file (as an example). Notice that the first line is going to be a line representing your own hobbies. In my case, it is the Vitaly,table tennis,chess,hacking line. Your goal is to create a class called Student. Every Student will contain a name (String) and an ArrayList<String> storing hobbies. Then, you will add all those students from the file into an ArrayList<Student>, with each Student having a separate name and ArrayList of hobbies. Here is an example file containing students (the first line will always represent yourself). NOTE: eventually, we will have a different file containing all our real names and hobbies so that we could find out with how many people each of us share the same hobby. Vitaly,table tennis,chess,hacking Sean,cooking,guitar,rainbow six Nolan,gym,piano,reading,video games Jack,cooking,swimming,music Ray,piano,video games,volleyball Emily,crochet,drawing,gardening,tuba,violin Hudson,anime,video games,trumpet Matt,piano,Reading,video games,traveling Alex,swimming,video games,saxophone Roman,piano,dancing,art Teddy,chess,lifting,swimming Sarah,baking,reading,singing,theatre Maya,violin,knitting,reading,billiards Amy,art,gaming,guitar,table tennis Daniel,video games,tennis,soccer,biking,trumpet Derek,cooking,flute,gaming,swimming,table tennis Daisey,video games,guitar,cleaning,drawing,animated shows,reading,shopping Lily,flute,ocarina,video games,baking Stella,roller skating,sudoku,watching baseball,harp Sophie,viola,ukulele,piano,video games
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值