1026 Table Tennis (30分)

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.

题意

有k张乒乓球桌,有的是vip桌。有n对玩家来打乒乓,有的玩家是VIP玩家。
当他们到达时,如果没有空桌子他们就排队等待。
这时候如果有VIP桌子空出来了,那么就优先给队列里的VIP玩家,如果没有VIP玩家就给普通玩家。
如果普通桌子空出来了,就给队列里排在最前的玩家。
如果玩家到达的时候有好多桌子可以选择,那么他会选择桌号最小的那张,VIP玩家会优先选择桌号最小的VIP桌子【这题意真的…】。
每对玩家最多只能玩两个小时。
营业时间是上午八点到晚上九点。如果在晚上九点还没开始玩就不能玩了。
最后输出每对玩家到达时间,开始玩的时间和(四舍五入后的)等待时间。

思路

这里选择为每一个玩家寻找桌子的方法。先将玩家按先来后到的顺序进行排列,然后对于每一个玩家:

如果是普通玩家:

  1. 先看有没有空闲的桌子,如果有的话,把编号最小的桌子分配给他
  2. 如果没有,则选择一个最早空闲的桌子,分配给他。

如果是vip玩家:

  1. 先看有没有空闲的vip桌子,如果有的话,把编号最小的vip桌子分给他
  2. 如果没有,再看有没有空闲的桌子(普通桌子),如果有的话,把编号最小的桌子分配给他
  3. 如果还是没有,则选则一个最早空闲的桌子,分配给他

需要注意的点:

  1. 任何玩家,当play时长超过两小时时,都将被压缩为两小时
  2. 如果为普通玩家分配到了vip桌子,需要看看排队等待的有没有vip玩家,因为此时vip玩家和普通玩家都在等桌子,如果有空闲的vip桌子了会优先分给队列中的vip玩家
  3. 给定的玩家play时长单位为分钟
  4. 输出玩家的等待时间时,单位为分钟,单位从秒转化为分钟时需要四舍五入
  5. 给的样例输入中
    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
    最后两行的意思为:有3张桌子,其中1张为vip桌子,vip桌子的编号是2(桌子的编号是从1开始),这个我看了好久才知道是什么意思,可能是英语不行吧。。。

代码

已经做了排版了,注释太多显得有点乱,又精简了不少,不知道你们能不能看的懂,或者说有耐心看。。。

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int startTime = 8 * 3600;	// 开门时间:08:00:00
const int endTime = 21 * 3600;	// 关门时间:21:00:00

struct Player {					// 玩家
	int arriveTime;				// 来的时间
	int waitTime;				// 等待空桌子的时间
	int getTableTime;			// 分配到桌子的时间
	int playTime;				// 玩多长时间
	int vip;					// 是不是vip玩家
};
struct Table
{
	int finishTime;				// 这张桌子什么时候空闲,即正在使用桌子的玩家什么时候结束使用
	int cnt;					// 有多少玩家使用了这张桌子
	int vip;					// 这是不是vip桌子
};

// 按玩家先来后到排序
bool cmpByArriveTime(Player& a, Player& b) {
	return a.arriveTime < b.arriveTime;
}
// 按分配到桌子的时间排序
bool cmpByGetTableTime(Player& a, Player& b) {
	return a.getTableTime < b.getTableTime;
}
// 选择一个桌子,返回桌子下标
int select(vector<Table>& table, int arriveTime, int vip)
{
	// 如果是vip玩家,先找有没有空闲的vip桌子,找到则return
	if (vip) {
		for (int i = 1; i < table.size(); i++) {
			if (table[i].finishTime <= arriveTime && table[i].vip == 1)
				return i;
		}
	}

	// 找一个空闲的桌子(如果vip玩家没有找到空闲的vip桌子,那么再找到的空闲桌子就是普通的了)
	for (int i = 1; i < table.size(); i++) {
		if (table[i].finishTime <= arriveTime)
			return i;
	}

	// 如果没有空闲的桌子,则寻找一个最先空闲的桌子
	int choice = 1;
	for (int i = 2; i < table.size(); i++) {
		if (table[i].finishTime < table[choice].finishTime)
			choice = i;
	}
	return choice;
}

int main()
{
	int n, k, m;
	scanf("%d", &n);
	vector<Player> players(n);
	for (int i = 0; i < n; i++)
	{
		int hh, mm, ss, playtime, vip;
		scanf("%d:%d:%d %d %d", &hh, &mm, &ss, &playtime, &vip);
		players[i].arriveTime = hh * 3600 + mm * 60 + ss;
		players[i].playTime = playtime * 60;
		if (players[i].playTime > 2 * 3600)
			players[i].playTime = 2 * 3600;
		players[i].waitTime = -1;
		players[i].vip = vip;
	}

	scanf("%d %d", &k, &m);
	vector<Table> tables(k + 1, { startTime,0 ,0 });
	for (int i = 0; i < m; i++)
	{
		int temp;
		scanf("%d", &temp);
		tables[temp].vip = 1;
	}

	// 把玩家先来后到进行排序
	sort(players.begin(), players.end(), cmpByArriveTime);
	// 依次为每一个玩家寻找桌子										
	for (int i = 0; i < n; i++)
	{
		// 如果这个玩家还没有被安排(可能会有vip玩家插队的情况)
		if (players[i].waitTime == -1) {
			// 根据玩家来的时间以及vip身份找桌子											
			int index = select(tables, players[i].arriveTime, players[i].vip);
			// 将要被安排的玩家
			int who = i;
			// 如果找到的是vip桌子,则看看在桌子空闲之前有没有vip玩家在排队了
			if (tables[index].vip == 1)
			{
				// 注意这里是从这个玩家i开始往后找
				for (int j = i; j < players.size() && players[j].arriveTime <= tables[index].finishTime; j++)
				{
					// 如果队列此时存在vip,且没有被安排,则该桌子被vip抢走
					// 如果i就是vip,那么桌子还是它的																
					if (players[j].vip == 1 && players[j].waitTime == -1) {
						who = j;
						break;
					}
				}
			}


			// 如果玩家来时已经有桌子空闲,不需要等,分配到桌子的时间就是来的时间;`
			// 否则等的时间为 [桌子finishTime] -  [玩家arriveTime],分配到桌子的时间是[桌子finishTime] 
			players[who].waitTime = max(0, tables[index].finishTime - players[who].arriveTime);
			players[who].getTableTime = max(tables[index].finishTime, players[who].arriveTime);
			// 桌子下一次空闲的时间:
			tables[index].finishTime = players[who].getTableTime + players[who].playTime;

			if (who != i)	// 如果i的桌子被vip玩家抢走了
				--i;		// 真惨,下次循环再给i找桌子

			// 如果玩家在关门前得到了桌子,桌子的使用人数+1
			if (players[who].getTableTime < endTime)
				tables[index].cnt++;
		}
	}

	sort(players.begin(), players.end(), cmpByGetTableTime);
	for (auto e : players) {
		if (e.getTableTime >= endTime)
			break;
		printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", e.arriveTime / 3600, (e.arriveTime / 60) % 60, e.arriveTime % 60, e.getTableTime / 3600, (e.getTableTime / 60) % 60, e.getTableTime % 60, (int)round((double)e.waitTime / 60));
	}
	for (int i = 1; i < tables.size(); i++)
	{
		if (i == 1)
			printf("%d", tables[i].cnt);
		else
			printf(" %d", tables[i].cnt);
	}
}



可能还有一些需要注意的细节我忘了说了。。。

  • 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
I understand that you want me to create a class called Student and add all the students from the given file into an ArrayList<Student>, with each Student having a separate name and ArrayList of hobbies. Here's the code to achieve the same: ``` import java.io.*; import java.util.*; class Student { private String name; private ArrayList<String> hobbies; public Student(String name, ArrayList<String> hobbies) { this.name = name; this.hobbies = hobbies; } public String getName() { return name; } public ArrayList<String> getHobbies() { return hobbies; } } public class Main { public static void main(String[] args) throws FileNotFoundException { Scanner scanner = new Scanner(new File("students.txt")); String[] firstLine = scanner.nextLine().split(","); String myName = firstLine[0]; ArrayList<String> myHobbies = new ArrayList<>(Arrays.asList(Arrays.copyOfRange(firstLine, 1, firstLine.length))); ArrayList<Student> students = new ArrayList<>(); students.add(new Student(myName, myHobbies)); while (scanner.hasNextLine()) { String[] line = scanner.nextLine().split(","); String name = line[0]; ArrayList<String> hobbies = new ArrayList<>(Arrays.asList(Arrays.copyOfRange(line, 1, line.length))); students.add(new Student(name, hobbies)); } scanner.close(); // Now, the students ArrayList contains all the students with their respective names and hobbies // You can use this ArrayList to perform any further operations as needed } } ``` You can replace "students.txt" with the name of your input file containing the student data. Let me know if you have any further questions.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值