1026. Table Tennis (30)

5 篇文章 0 订阅

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
//做这个题目快做疯了,真不知道自己第一次是怎么想出来的,参考以前的代码重新写了一遍
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <climits>
using namespace std;
struct Play
{
  int start, p;
  bool operator < (const Play &player) const
  {
    return start < player.start;
  }
};
struct Table
{
  int freetime, number;
  Table():freetime(0), number(0) {}
};

int MAXTIME = 13 * 3600;
vector<Play> players;
vector<Play> vplayers;
set<int> vtable;
vector<Table> table;

void printTime(int time, int serve)
{
  printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", time / 3600 + 8, (time / 60) % 60, time % 60, serve / 3600 + 8, (serve / 60) % 60, serve % 60, (serve - time + 30) / 60);
}
int main(void)
{
  int N, K, M, hour, minite, second, p, tag, vip;
  scanf("%d", &N);
  Play player;
  for (int i = 0; i < N; ++i)
  {
    scanf("%d:%d:%d%d%d", &hour,&minite, &second,&p, &tag);
    player.start = (hour - 8) * 3600 + minite * 60 + second;
            player.p = p > 120 ? 7200 : p * 60; 
    if (tag == 1)
      vplayers.push_back(player);
    else
      players.push_back(player);
  }
  scanf("%d%d", &K, &M);
  table.resize(K);
  for (int i = 0; i < M; ++i)
  {
    scanf("%d", &vip);
    vtable.insert(--vip);
  }

  sort (players.begin(), players.end());
  sort (vplayers.begin(), vplayers.end());
  int length = players.size(), vlength = vplayers.size(), index1 = 0, index2 = 0;
  int startime, servetime;
  while(index1 < length || index2 < vlength)
  {  //贵宾
    if (index1 < length && index2 < vlength)
    {
      //如果有普通客户和贵宾,则找出时间来的最早那个
      if (players[index1].start < vplayers[index2].start)
      {
        startime = players[index1].start;
        tag = 0;
      }
      else
      {
        startime = vplayers[index2].start;
        tag = 1;
      }
    }
    if (index1 < length && index2 == vlength)
    {
      //只有普通客户
      startime = players[index1].start;
      tag = 0;
    }
    if (index1 == length && index2 < vlength)
    {
      //只有贵宾
      startime = vplayers[index2].start;
      tag = 1;
    }
	if (startime >= MAXTIME) //用户来的时间都超过了营业时间
		break;
    //1、寻找空着数字最小的席位; 2、如果没有空着的席位,寻找最早结束的席位
    int minIndex = -1, minStart = INT_MAX;
    if (tag)
    {  
      for (set<int>::iterator iter = vtable.begin(); iter != vtable.end(); ++iter)
      {
        if (table[*iter].freetime <= startime)
        {
          minIndex = *iter;
          break;
        }
      }
    }
    if (minIndex == -1)
    {  
      for (int j = 0; j < K; ++j)
      {
        if (table[j].freetime <= startime)
        {
          minIndex = j;
          break;
        }
        if (table[j].freetime < minStart)
        {
          minStart = table[j].freetime;
          minIndex = j;
        }

      }  
    }
    if (table[minIndex].freetime >= MAXTIME)
      break;
<span style="white-space:pre">	</span>//如果minIndex的时间比starttime小,并且它是贵宾桌,很可能它会分配给vip用户,这个用户必须满足这个贵宾桌freetime之前来到,否则它会直接非配给starttime。
    if (index2 < vlength && vtable.find(minIndex) != vtable.end() && vplayers[index2].start <= table[minIndex].freetime)
      tag = 1;
    
    if (table[minIndex].freetime < startime)
      servetime = startime;
    else
      servetime = table[minIndex].freetime;
    ++table[minIndex].number;
    if (tag)
    {
      table[minIndex].freetime = servetime + vplayers[index2].p;
      printTime(vplayers[index2].start, servetime);
      ++index2;
    }
    else
    {
      table[minIndex].freetime = servetime + players[index1].p;
      printTime(players[index1].start, servetime);
      ++index1;
    }
    
  }
  printf("%d", table[0].number);
  for (int i = 1; i < K; ++i)
    printf(" %d", table[i].number);
  printf("\n");
  return 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值