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

 题目说明: 

算法笔记最后一道题,属于模拟题,就是那种考场上的第一题或者第二题的样子,25分的题,陷阱一般贼多,反而是30分的题有套路,有模板,好拿分。

如果vip玩家达到时有很多空闲的桌子,他总是优先的去选择是vip且桌子号最小的那个,当没有vip桌子空闲时再选择桌子号最小的空闲桌子。

注意:是一抢vip,也就是说,若  VIP  号码大,而同时  普通  号码小,选择VIP号码大的那个。

明白题目逻辑,写代码就好办了;

让我们来重新梳理一下题目的逻辑:

题目的逻辑:

1.如果等待队列非空
      1.1  等桌子空闲,如果一个桌子空闲了出来
           1.1.1 如果当前的桌子是vip,则优先选择等待队列中的vip
            1.1.2如果当前的桌子不是vip,号码小,但是可能存在和该桌子同时结束的另一个桌子号码较大,而该大号桌子是vip
                       1.1.2.1如果下一个玩家是vip,则优先选择空闲桌子中是vip且号码较小的
                       1.1.2.2如果下一个玩家不是vip,则直接选择该桌子。
   2. 如果等待队列是空的,
        2.1等玩家到来,如果到达了一个玩家
              2.1.1如果该玩家是vip
                   2.1.1.1如果空闲桌子中存在vip桌子,则优先选择空闲桌子中是vip且号码较小的
                    2.1.1.2如果空闲桌子中无vip桌子,则优先选择空闲桌子号码较小的
             2.1.2如果该玩家不是vip,则选择空闲桌子中号码较小的。


这里需要注意两点代码的写法:

1、如何判定等待队列为空?
        对于最快空闲的桌子,如果  下一到达的玩家的到达时间  早于  最快空闲的桌子的空闲时间,则等待队列非空
2、如何获取等待队列中的vip玩家?
        遍历这个等待队列,如果存在一个未被服务过的vip玩家,寻找到在等待队列中的第一个vip玩家。在这里,等待队列就是
        那些还没有被服务过且到达时间不晚于(<=)最快空闲桌子的空闲时间点的玩家。

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>
#include<cmath>    
#include<set>  
#include<stack>
#include<queue>
#include<cstdio> 
#include<climits>
using namespace std;
typedef long long ll;
struct	table{
	int endtime,num;
	bool vip;
};
struct play{
	int arrive,use,start;//到达时间,使用时间,开始时间
	bool served,vip; //是否服务,vip?
};
int cmp1(play a,play b){
	return a.arrive<b.arrive;//先对数据进行排序,按到达的时间升序	
}
int cmp2(play a,play b){
	return a.start<b.start;	//最后输出的时候为什么是8:12:00在8:10:00的前面呢?就是因为是按
}                           //开始使用的时间升序的
vector<play>p;
vector<table>t;

 //找到personid及之后的,并且到达(arrive)时间不晚于before的,未服务的,且为vip的 person-id
    如果没找到,则返回-1 ,findvip(personId,minEndTime);

int findvip(int personId,int minendtime){
	for(int i=personId;i<p.size()&&p[i].arrive<=minendtime;i++){
		if(!p[i].served&&p[i].vip) return i;
	}
	return -1;
}
// 更新以personId的玩家对,和tableId的桌子的信息
//    包括:
  //      1、将该玩家对的开始时间赋值为到达时间和可用桌子结束时间中的较大值
    //    2、将该玩家对的服务状态赋值为以服务过
      //  3、将桌子的结束时间信息更新为该玩家对的开始时间加该玩家的使用时间
        //4、将该桌子服务的玩家数量加一

void update(int personid,int tableid){
	p[personid].start=max(p[personid].arrive,t[tableid].endtime);
	p[personid].served=1;
	t[tableid].endtime=p[personid].start+p[personid].use;
	t[tableid].num++;		
}

int main(){
	int n,m,k,vipnum;	
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int h,m,s,use,vip,arrive;
		scanf("%d:%d:%d %d %d",&h,&m,&s,&use,&vip);
		arrive=h*3600+m*60+s;
		use=use>120?7200:use*60;
		p.push_back({arrive,use,0,0,vip>0});
	}
	sort(p.begin(),p.end(),cmp1);
	scanf("%d %d",&k,&m);
	for(int i=0;i<k;i++)
		t.push_back({28800,0,0});
	for(int i=0;i<m;i++){
		scanf("%d",&vipnum);
		t[vipnum-1].vip=1;	
	}
	for(int i=0;i<p.size();){

        //找到最先空闲的桌子,如果多个桌子同时空闲,则返回桌子号最小的那个
		int minendtime=INT_MAX,minendid;
		for(int j=0;j<k;j++){
			if(minendtime>t[j].endtime) {
				minendtime=t[j].endtime;
				minendid=j;
			}
		}
    //如果最先空闲的桌子空闲的太晚了,或者当前序列中的第一位玩家对达到的时间太晚了,就退出循环
		if(minendtime>=75600||p[i].arrive>=75600) break;
  //声明新的变量,personId为经过调整选择后最终的开始使用桌子的玩家对索引,tableId为为经过调整选择后最终的开始被使用的桌子
		int personid=i,tableid=minendid;
 //如果当前的最早空闲且号最小的桌子空闲时,存在玩家对已经在等待了
		if(minendtime>=p[i].arrive){
 //并且当前的最早空闲且号最小的桌子是vip,寻找是vip的且未服务过的,玩家对到达时间不晚于minEndTime的玩家对索引
			if(t[tableid].vip){
				int vipid=findvip(personid,minendtime);
				personid=vipid!=-1?vipid:personid;
			}else if(p[i].vip){//虽然当前的最早空闲且号最小的桌子不是vip,但是还可能存在同时空闲,桌号更大的桌子是vip
				for(int j=0;j<k;j++){
					if(t[j].vip&&t[j].endtime<=p[personid].arrive){
						tableid=j;
						break;
					}
				}
			}//如果当前的桌子非vip,且当前的序列的第一个玩家对非vip,顺序选择即可,换句话说,personId和tableId无需调整						
		}else{
 /*
                如果当前的最早空闲的桌子空闲时,没有玩家在等待序列中,即当一个玩家到达时,应该是至少有一个桌子是空闲的
                我们总是希望选择空闲中的桌子中桌子号最小的,如果到达了一个vip玩家对,并且存在空闲的vip桌子,我们选择空闲中的vip桌中号最小的
                在这里,我们不管是否是vip,先得到空闲中的桌子中桌子号最小的,如果当前到达的玩家对未vip,并且存在空闲的vip桌子,
                我们用空闲中的vip桌中号最小的那个桌子覆盖之前得到的tableId
*/
			for(int j=0;j<k;j++){
				if(t[j].endtime<=p[personid].arrive){
					tableid=j;
					break;	
				}
			}
			if(p[personid].vip){
				for(int j=0;j<k;j++){ //尝试寻找空闲的vip桌子并调整tableId,顺序找到即退出得到的就是号码最小的
					if(t[j].vip&&t[j].endtime<=p[personid].arrive){
						tableid=j;
						break;
					}
				}
			}
		}
		update(personid,tableid);		
		while(i<p.size()&&p[i].served)i++;		
	}
	sort(p.begin(),p.end(),cmp2);
	for(int i=0;i<p.size();i++){
		if(p[i].served){
			int wait=p[i].start-p[i].arrive;
			printf("%02d:%02d:%02d %02d:%02d:%02d %d\n",p[i].arrive/3600,
		    	p[i].arrive%3600/60,p[i].arrive%60,p[i].start/3600,
			    p[i].start%3600/60,p[i].start%60,(int)(1.0*wait/60+0.5));
		}
	}
	for(int i=0;i<k;i++) {
		if(i!=0) printf(" ");
		printf("%d",t[i].num);
	}
	return 0;			
}

PS:算法笔记上的代码和柳神都是一样的,但以下数据通过不了,pat数据待完善。。。

4 

16:42:22 101 1 
11:57:37 6 1 
12:47:28 3 0 
08:17:16 67 0 
3 2 
2 1

答案是4 0 0

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值