1026. Table Tennis (30)

1.整体思路为:先遍历vip桌子和vip玩家,如果有匹配成功的,进行匹配;然后再遍历一轮所有的桌子和等候的玩家,有匹配的进行匹配;

2.被卡的地方:

1)关于秒的四舍五入,是按照大于等于30秒,进一分钟,小于30秒则舍去的原则;最后的一个测试用例就是检验这个30秒的

2)在遍历所有的玩家时,由于可能存在已经被前面服务了的vip玩家,所以需要额外加一个处理,使得playIndex指向的是未被服务的玩家;

3)主要是忘记了遍历vip玩家时,同样存在2)中的问题,因为在遍历所有玩家的时候,有可能vip玩家也被服务安排了,所以需要在遍历vip玩家中加一个处理,使得vipIndex指向未被服务的vip玩家。

3.下面是一些比较重要的测试用例:

//边缘测试例子
1
21:00:00 10 1
3 3
1 2 3

1
20:59:59 10 1
3 3
1 2 3

0
3 1
2

//vip桌子分配例子,有vip桌子时,优先把vip分到编号小的vip桌子,而不是编号小的vip桌子
4
06:00:00 30 1
08:00:00 30 1
10:00:00 30 1
12:00:00 30 1
5 1
3
答案为:
06:00:00 08:00:00 120
08:00:00 08:00:00 0
10:00:00 10:00:00 0
12:00:00 12:00:00 0
1 0 3 0 0

//超过两小时的例子
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

//关于四舍五入为1分钟的例子,大约等于30秒才+1分钟,小于30则不+
3
07:59:31 60 1
07:59:29 60 1
08:00:30 100 1
2 1
1
答案为:
1
07:59:29 08:00:00 1
07:59:31 08:00:00 0
08:00:30 09:00:00 60
2 1


AC的源代码:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
#define maxServe 46810
/*
//正常的检测例子,全部为普通玩家
9
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:29 5 0
08:12:00 10 0
20:50:00 10 0
08:01:30 15 0
20:53:00 10 0
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:10:29 08:16:30 6
08:12:00 08:20:00 8
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 table
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 0
答案为:
08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:10:00 08:16:30 7
08:12:00 08:20:00 8
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和viptable
9
20:52:00 10 0
07:59:59 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
答案为:
07:59:59 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
21:00:00 10 1
3 3
1 2 3

1
20:59:59 10 1
3 3
1 2 3

0
3 1
2

//vip桌子分配例子,有vip桌子时,优先把vip分到编号小的vip桌子,而不是编号小的vip桌子
4
06:00:00 30 1
08:00:00 30 1
10:00:00 30 1
12:00:00 30 1
5 1
3
答案为:
06:00:00 08:00:00 120
08:00:00 08:00:00 0
10:00:00 10:00:00 0
12:00:00 12:00:00 0
1 0 3 0 0

//超过两小时的例子
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

//关于四舍五入为1分钟的例子,大约等于30秒才+1分钟,小于30则不+
3
07:59:31 60 1
07:59:29 60 1
08:00:30 100 1
2 1
1
答案为:
1
07:59:29 08:00:00 1
07:59:31 08:00:00 0
08:00:30 09:00:00 60
2 1
*/
string int2str(int a)
{
	char n = a / 10 + '0';
	char m = a % 10 + '0';
	string tmp = "";
	tmp += n;
	tmp += m;
	return tmp;

}
int char2int(char a)
{
	int tmp = a - '0';
	return tmp;
}
int time2int(string a)
{
	int hour = char2int(a[0]) * 10 + char2int(a[1]) - 8;
	int min = char2int(a[3]) * 10 + char2int(a[4]);
	int sec = char2int(a[6]) * 10 + char2int(a[7]);
	return hour * 3600 + min * 60 + sec;
}
struct playerNode{
	string arrive;
	int playTime, waitTime, serve;
	bool isVip;
	string serveStr();
	int waitInt();
	int serveTableNum;
	playerNode() :arrive(""), serve(maxServe), playTime(0), waitTime(0), isVip(false){};
	playerNode(string a, int p, bool v) :arrive(a), serve(maxServe), playTime(p), waitTime(0), isVip(v){};
};
string playerNode::serveStr()
{
	int hour = serve / 3600;
	int min = (serve - hour * 3600) / 60;
	int sec = serve % 60;
	return int2str(hour + 8) + ":" + int2str(min) + ":" + int2str(sec);
}
int playerNode::waitInt()
{
	int diff = serve - time2int(arrive);
	int min = diff / 60;
	if (diff % 60 >= 30)
		min++;
	return min;
}
struct tableNode{
	bool isVip;
	int playIndex;//当前玩家index
	int serveNum;//已经服务的玩家数量

	tableNode() :isVip(false), playIndex(-1), serveNum(0) {};
};
bool cmp(const playerNode&a, const playerNode&b)
{
	return a.arrive < b.arrive;
}
bool cmp2(const playerNode&a, const playerNode&b)
{//按照服务时间排序
	return a.serve < b.serve;
}
int main(void)
{
	int playerNum;
	cin >> playerNum;
	vector<playerNode> player(playerNum);
	vector<int> vipPlayer(0);
	for (int i = 0; i < playerNum; i++)
	{//输入玩家信息
		string arrive;
		int playTime;
		bool isVip;
		cin >> arrive >> playTime >> isVip;
		if (playTime > 120)
			playTime = 120;
		playTime *= 60;
		player[i].arrive = arrive;
		player[i].playTime = playTime;
		player[i].isVip = isVip;
	}
	sort(player.begin(), player.end(), cmp);

	for (int i = 0; i < playerNum; i++)
	{//建立vip队列
		if (player[i].isVip) vipPlayer.push_back(i);
	}

	int tableNum;
	int vipTableNum;
	cin >> tableNum >> vipTableNum;
	vector<tableNode> table(tableNum);
	vector<int> vipTable(0);
	int vipIndex = 0;
	int playerIndex = 0;
	for (int i = 0; i < vipTableNum; i++)
	{//输入vip桌子
		int a;
		cin >> a;
		table[a - 1].isVip = true;
		vipTable.push_back(a - 1);
	}

	for (int now = 0; now < 13 * 3600; now++)
	{
		for (int i = 0; i < table.size(); i++)
		{//遍历所有桌子,处理正在玩的玩家
			if (table[i].playIndex != -1)
			{//桌子有人
				player[table[i].playIndex].playTime--;
				if (player[table[i].playIndex].playTime <= 0)
				{//清空桌子
					table[i].playIndex = -1;
				}
			}
		}
		if (vipIndex < vipPlayer.size())
		{//先服务vip玩家
			for (int j = 0; j < vipTable.size(); j++)
			{
				while (vipIndex < vipPlayer.size() && player[vipPlayer[vipIndex]].serve != maxServe)
				{//跳过已经被服务的vip玩家,主要卡在这里
					vipIndex++;
				}
				int i = vipTable[j];//用i表示vip桌子
				if (table[i].playIndex == -1 && vipIndex < vipPlayer.size() && time2int(player[vipPlayer[vipIndex]].arrive) <= now)
				{
					table[i].serveNum++;
					table[i].playIndex = vipPlayer[vipIndex];
					player[vipPlayer[vipIndex]].serveTableNum = i;
					player[vipPlayer[vipIndex]].serve = now;
					vipIndex++;

				}
			}
		}

		if (playerIndex < player.size())
		{//服务剩下的玩家,普通玩家和vip玩家混在一起
			for (int i = 0; i < table.size(); i++)
			{
				while (playerIndex < player.size() && player[playerIndex].serve != maxServe)
				{//确保当前playerIndex的玩家是没有被服务的
					playerIndex++;
				}
				if (table[i].playIndex == -1 && playerIndex < player.size() && time2int(player[playerIndex].arrive) <= now)
				{
					table[i].serveNum++;
					table[i].playIndex = playerIndex;
					player[playerIndex].serveTableNum = i;
					player[playerIndex].serve = now;
					playerIndex++;
				}
			}
		}
	}

	sort(player.begin(), player.end(), cmp2);
	for (int i = 0; i < player.size(); i++)
	{
		if (player[i].serve != maxServe)
		{
			cout << player[i].arrive << " " << player[i].serveStr() << " " << player[i].waitInt() /*<<  " " << player[i].serveTableNum*/ << endl;
		}
	}
	for (int i = 0; i < table.size(); i++)
	{
		cout << table[i].serveNum;
		if (i != table.size() - 1)
			cout << " ";
	}


	return 0;
}


  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
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
Based on the given information, I would suggest creating a class called Student with two instance variables: a String for the student's name and an ArrayList<String> for their hobbies. Then, to read the data from the file and add it to an ArrayList<Student>, you could do something like this: 1. Create an empty ArrayList<Student>. 2. Read the first line of the file and split it into a String for the name and an ArrayList<String> for the hobbies. 3. Create a new Student object with the name and hobbies from the first line and add it to the ArrayList. 4. Use a loop to read the rest of the lines from the file, create a new Student object for each one, and add it to the ArrayList. 5. Once you have read all the lines from the file, you should have an ArrayList<Student> containing all the students and their hobbies. Here's some example code to help you get started: ``` import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class Student { private String name; private ArrayList<String> hobbies; public Student(String name, ArrayList<String> hobbies) { this.name = name; this.hobbies = hobbies; } // Getters and setters for name and hobbies public static void main(String[] args) { ArrayList<Student> students = new ArrayList<Student>(); try { File file = new File("students.txt"); Scanner scanner = new Scanner(file); // Read and process the first line String[] firstLine = scanner.nextLine().split(","); String name = firstLine[0]; ArrayList<String> hobbies = new ArrayList<String>(); for (int i = 1; i < firstLine.length; i++) { hobbies.add(firstLine[i]); } students.add(new Student(name, hobbies)); // Read and process the rest of the lines while (scanner.hasNextLine()) { String[] line = scanner.nextLine().split(","); name = line[0]; hobbies = new ArrayList<String>(); for (int i = 1; i < line.length; i++) { hobbies.add(line[i]); } students.add(new Student(name, hobbies)); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("File not found!"); } // Now you have an ArrayList<Student> containing all the students and their hobbies } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值