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 12Sample 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 03 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;}
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 1026. Table Tennis (30)
- 雷军现身淘宝问答:小米6青春版是假的,亮银目前无法量产
- 俄罗斯解除对微信封锁
- Asp.Net MVC5代码的筛选器(ActionFilter)执行递归链表分析
- 微软花了几个小时阐述他们打算如何超越亚马逊,亚马逊表示受宠若惊 | Build 2017
- 苹果和高通争的不只是专利授权费,更是下一个时代?
- 1026. Table Tennis (30)
- nodejs http post请求java后台数据,java端拿不到参数的问题
- JVM Exception in thread "main" java.lang.StackOverflowError
- java修行之路(四)
- Google关闭Android 7.x Beta项目,迎接全新Android O
- 注意力数字乔迁新居 “旋转空间”设计别有内涵
- 骁龙845曝光,这回泄密的不是“猪队友”,是自家官网
- 微软Build 2017开发者大会临近,今年的主题是边缘计算
- 百度发布Deep Speaker:大规模声纹识别的端对端系统