1026. Table Tennis (30)

来源:互联网 发布:bt条码打印软件 编辑:程序博客网 时间:2024/05/30 19:34

最后一个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:
920:52:00 10 008:00:00 20 008:02:00 30 020:51:00 10 008:10:00 5 008:12:00 10 120:50:00 10 008:01:30 15 120:53:00 10 13 12
Sample Output:
08:00:00 08:00:00 008:01:30 08:01:30 008:02:00 08:02:00 008:12:00 08:16:30 508:10:00 08:20:00 1020:50:00 20:50:00 020:51:00 20:51:00 020: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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 数学细节丢分怎么办 感觉自己老了怎么办 小学拼音不过关怎么办 小学面试不过关怎么办 小学阅读不过关怎么办 孩子计算老出错怎么办 工作中总是马虎怎么办 孩子总是计算错误怎么办 做设计老是犯错怎么办 小学生阅读总出错怎么办 写作文没思路怎么办 孩子不爱写作文怎么办 写作文没有素材怎么办 写作文没有灵感怎么办 做事工作马虎粗心大意怎么办 小孩作业马虎粗心大意怎么办 孩子写字一直错怎么办 孩子写字老错怎么办 写错字涂黑了怎么办 写错字不能涂改怎么办 孩子爱写错别字怎么办 孩子读题马虎怎么办 孩子知错不该怎么办 小孩胆小反应慢怎么办 孩孑经常流鼻血怎么办 中考考号写错了怎么办 头后仰就头晕怎么办 感觉自己要晕倒怎么办 孩子不愿动手写字怎么办 老年人恶心想吐怎么办 小学生老写错别字怎么办 突然头晕站不稳 怎么办 早上起床突然天旋地转怎么办 躺着突然感觉天旋地转怎么办 眩晕症发作时怎么办 低血糖恶心想吐怎么办 更年期头晕头胀怎么办 年轻人头晕头胀怎么办 教案:《眯眼了怎么办》 迷路了怎么办活动意图 幼儿迷路了怎么办图片