1026. Table Tennis (30)

本文介绍了一个乒乓球俱乐部的调度算法,该算法旨在合理分配乒乓球桌资源并计算顾客等待时间。考虑到普通顾客与VIP顾客的不同需求,文章详细阐述了如何在有限的球桌中高效地为顾客提供服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最后一个case通过不了,超时,有没有哪些大神帮我优化一下,让我通过

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 <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <queue>

using namespace std;

struct Person{
    string arrivingTime;
    int playingTicks;
    int isVip;

    int arrivingTicks=-1;
    int servingTicks=-1;
    string servingTime;
    int hour=-1,minute=-1,second=-1;
    int leavingTicks=-1;

    Person():arrivingTime(""),playingTicks(0),isVip(0){}
    Person(string _a , int _p , int _i):arrivingTime(_a),playingTicks(_p*60),isVip(_i){
        hour=atoi(_a.substr(0,2).c_str());
        minute=atoi(_a.substr(3,2).c_str());
        second=atoi(_a.substr(6,2).c_str());
        arrivingTicks=( hour*3600+minute*60+second);
    }

    int getTicks(){return hour*3600+minute*60+second;}
    int getWaitingTime(){return (servingTicks-arrivingTicks+30)/60;}

    string getServingTime(){
        int h=servingTicks/3600;
        int m=(servingTicks/60) % 60;
        int s=servingTicks%60;
        string output="";
        output += (char)(h/10 + '0');
        output += (char)(h%10 + '0');
        output+=":";
        output += (char)(m/10 + '0');
        output += (char)(m%10 + '0');
        output+=":";
        output += (char)(s/10 + '0');
        output += (char)(s%10 + '0');
        return output;
    }
};

bool cmp0(const Person &a , const Person &b){ return a.arrivingTicks < b.arrivingTicks ;}
bool cmp1(const Person &a , const Person &b){ return a.servingTicks < b.servingTicks ;}

struct Table{
    int id;
    int isVip;
    vector<Person> player;
    int serviceCount=0;

    Table():id(0),isVip(0){}
    Table(int _id , int isVip ):id(_id),isVip(isVip){}
};

bool isFreeTable(const Table &t){return t.player.size()==0;}
bool isFreeVipTable(const Table &t){ return  (t.player.size()==0&&t.isVip);}
bool isVipPerson(const Person &p){return p.isVip;}

int main()
{
    int cnt=0;
    cin>>cnt;
    vector<Person> orgPersons , result;
    orgPersons.reserve(10001);
    result.reserve(10001);
    for(int i=0;i<cnt;i++){
        string _a;
        int _p,_i;
        cin>>_a>>_p>>_i;
        orgPersons.push_back(Person(_a ,( _p >120)?120:_p , _i));
    }
    sort(orgPersons.begin() , orgPersons.end(),cmp0);

    int numTables=0 , numVips=0 ;
    cin>>numTables>>numVips;
    vector<Table> tables;
    tables.reserve(numTables);
    for(int i=0;i<numTables;i++)tables.push_back(Table(i+1 , 0));
    for(int i=0;i<numVips;i++){
        int num=0;
        cin>>num;
        tables[num-1].isVip=1;
    }

    vector<Person> persons;
    persons.reserve(10001);
    int startTime= 8*3600;
    int endTime= 21*3600;
    for(int time=startTime ; time<endTime ; time++){
        while(orgPersons.size()>0 && time >= orgPersons.front().arrivingTicks){
            persons.push_back(orgPersons.front()); orgPersons.erase(orgPersons.begin());
        }

        for(int i=0;i<numTables;i++)
            if( tables[i].player.size()==1&& tables[i].player[0].leavingTicks == time )tables[i].player.clear();

        while(1){
            if(persons.empty())break;
            vector<Table>::iterator firstFreeTable = find_if(tables.begin() , tables.end() , isFreeTable );
            if(firstFreeTable ==tables.end()  )break;
            vector<Person>::iterator firstVip=find_if(persons.begin() , persons.end() , isVipPerson);
            vector<Table>::iterator firstFreeVipTable=find_if(tables.begin() , tables.end() , isFreeVipTable);

            if( firstVip!=persons.end() && firstFreeVipTable!=tables.end() ){
                firstFreeVipTable->player.push_back(*firstVip);
                firstFreeVipTable->player[0].servingTicks= time;
                firstFreeVipTable->player[0].leavingTicks= time + firstFreeVipTable->player[0].playingTicks;
                firstFreeVipTable->serviceCount++;
                result.push_back(firstFreeVipTable->player[0]);
                persons.erase(firstVip);
            }
            else{
                firstFreeTable->player.push_back(persons.front());
                firstFreeTable->player[0].servingTicks= time;
                firstFreeTable->player[0].leavingTicks= time + firstFreeTable->player[0].playingTicks;
                firstFreeTable->serviceCount++;
                result.push_back(firstFreeTable->player[0]);
                persons.erase(persons.begin());
            }
        }
    }

    sort(result.begin() , result.end() , cmp1);
    for(int i=0;i<result.size();i++)printf("%s %s %d\n",result[i].arrivingTime.c_str() , result[i].getServingTime().c_str() , result[i].getWaitingTime());
    for(int i=0;i<tables.size();i++){
        printf("%d",tables[i].serviceCount);
        if(i<tables.size()-1)printf(" ");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值