1026. Table Tennis (30)

考察模拟队列等待

这道题目的vip用户以及vip桌子的存在,使得要考虑的case数多了一些

另外这个题目的playtime是有截断的

#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef struct Node
{
	int arrive, process, tag;
	int serve, wait;//...
}Node;
typedef struct Table
{
	int tag;
	int freeTime, num;//...
}Table;
bool CmpArrive(Node a, Node b)
{
	return a.arrive < b.arrive;
}
bool CmpServe(Node a, Node b)
{
	if(a.serve == b.serve)
		return a.arrive < b.arrive;
	else return a.serve < b.serve;
}
#define INF 0x6FFFFFFF
//vector<bool> visited;
vector<Node> user;
vector<Table> table;
void UpdateInfo(int userID, int tableID)
{
	user[userID].serve = max(user[userID].arrive, table[tableID].freeTime);
	user[userID].wait = user[userID].serve-user[userID].arrive;
	table[tableID].num++;
	table[tableID].freeTime = user[userID].serve+min(user[userID].process, 7200);
}
int main()
{
	//input
	int n;
	scanf("%d",&n);
	
	user.resize(n);
	for(int i = 0; i < n; ++i)
	{
		int h, m, s;
		scanf("%d:%d:%d %d%d",&h,&m,&s,&user[i].process,&user[i].tag);
		user[i].arrive = h*3600+m*60+s;
		user[i].process *= 60;
		user[i].serve = INF; user[i].wait = INF;//initial variable
	}
	int k, m;
	scanf("%d%d",&k,&m);
	
	table.resize(k);
	for(int i = 0; i < k; ++i)
		table[i].freeTime = 8*3600, table[i].tag = 0, table[i].num = 0;
	for(int i = 0; i < m; ++i)
	{
		int c;
		scanf("%d",&c); c--;
		table[c].tag = 1;
	}
	//process
	sort(user.begin(), user.end(), CmpArrive);
	//visited.assign(n, false);
	for(int i = 0; i < n; ++i)
	{
		if(user[i].serve != INF) continue;
		int minFreeTime = INF;
		for(int j = 0; j < k; ++j)
			minFreeTime = min(minFreeTime, table[j].freeTime);
		int timePoint = max(user[i].arrive, minFreeTime);
		if(timePoint >= 21*3600) break;
		vector<int> userList;
		vector<int> tableList;
		for(int j = i; j < n; ++j)
			if(user[j].serve == INF && user[j].arrive <= timePoint) userList.push_back(j);
		for(int j = 0; j < k; ++j)
			if(table[j].freeTime <= timePoint) tableList.push_back(j);
		
		bool flag = false;//record if it is served
		if(userList.size() == 1 && tableList.size() >1)//vip user look for vip table
		{
			if(user[userList[0]].tag == 1)
			{
				for(int j = 0; j < tableList.size(); ++j)
				{
					if(table[tableList[j]].tag == 1)
					{
						flag = true;
						UpdateInfo(userList[0], tableList[j]);
						break;
					}
				}
			}
		}
		else if(tableList.size() == 1 && userList.size() > 1)//vip table can for vip user first
		{
			if(table[tableList[0]].tag == 1)
			{
				for(int j = 0; j < userList.size(); ++j)
				{
					if(user[userList[j]].tag == 1)
					{
						flag = true;
						UpdateInfo(userList[j], tableList[0]);
						break;
					}
				}
			}
		}
		else if(tableList.size() > 1 && userList.size() > 1)//vip table can for vip user first
		{
			for(int t = 0; t < tableList.size(); ++t)
			{
				if(table[tableList[t]].tag == 1)
				{
					for(int u = 0; u < userList.size(); ++u)
					{
						if(user[userList[u]].tag == 1)
						{
							flag = true;
							UpdateInfo(userList[u], tableList[t]);
							break;
						}
					}
				}
			}
		}
		if(!flag) UpdateInfo(userList[0], tableList[0]);
		--i;
	}
	//output
	sort(user.begin(), user.end(), CmpServe);
	for(int i = 0; i < n; ++i)
	{
		if(user[i].serve >= 21*3600) break;
		int h1, m1, s1, h2, m2, s2;
		int t = user[i].arrive;
		h1 = t/3600; t %= 3600;
		m1 = t/60; t %= 60;
		s1 = t;
		t = user[i].serve;
		h2 = t/3600; t %= 3600;
		m2 = t/60; t %= 60;
		s2 = t;
		printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", h1, m1, s1, h2, m2, s2, (user[i].wait+30)/60);
	}
	for(int i = 0; i < k; ++i)
	{
		if(i != k-1)
			printf("%d ",table[i].num);
		else printf("%d\n",table[i].num);
	}
	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
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 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值