1026 Table Tennis (30)完全不会

本文介绍了一个游戏厅模拟系统的实现过程,该系统通过C++编程语言模拟了玩家进入游戏厅后的排队、服务流程,并考虑了VIP玩家的优先级。系统包括玩家类和桌子类,能够跟踪每个玩家的服务时间和桌子的使用情况。
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <algorithm>
using namespace std;
class ctable
{
public:
enum{opentime=8*3600,closetime=21*3600};
int curtime;
int vip;
int servernum;
ctable(){curtime=opentime;vip=0;servernum=0;}
bool operator < (const ctable& tb) const {return curtime<tb.curtime;}
};


class cplay
{
public:
int arrivetime;
int lasttime;
int vip;
int servertime;
bool bservered;
cplay(){arrivetime=0;vip=0;servertime=0;bservered=false;}
friend bool CmpArrTime(const cplay &py1,const cplay &py2);
friend bool CmpSerTime(const cplay &py1,const cplay &py2);
};


bool CmpArrTime(const cplay &py1,const cplay &py2) {return py1.arrivetime<py2.arrivetime;}
bool CmpSerTime(const cplay &py1,const cplay &py2) 
{
if(py1.servertime==py2.servertime)
return py1.arrivetime<py2.arrivetime;
else
return py1.servertime<py2.servertime;
}
class CA
{
public:
void init();
void run();
void output();
static int timetoint(string &stime);
static string inttotime(int itime);
static int difinttime(int t1,int t2);
vector<ctable> tables;
vector<cplay> plays;
};
string CA::inttotime(int itime)
{
int hh,mm,ss;
hh=itime/3600;
mm=itime/60%60;
ss=itime%60;
ostringstream ostr;
ostr<<setfill('0')<<setw(2)<<hh<<":"<<setw(2)<<mm<<":"<<setw(2)<<ss;
return ostr.str();
}
int CA::timetoint(string &stime)
{
int hh,mm,ss;
istringstream istr(stime);
char ch;
istr>>hh>>ch>>mm>>ch>>ss;
return (hh*3600+mm*60+ss);
}
int CA::difinttime(int t1,int t2)
{
return (t1-t2+30)/60;
}
void CA::init()
{
int n,m,k,vipno;
string arrtime;
cplay py;
ctable tb;
cin>>n;
while(n-->0)
{
cin>>arrtime>>py.lasttime>>py.vip;
py.arrivetime=timetoint(arrtime);
py.lasttime*=60;
plays.push_back(py);
}
cin>>m>>k;
while(m-->0)
{
tables.push_back(tb);
}
while(k-->0)
{
cin>>vipno;
tables[vipno-1].vip=1;
}
}
void CA::output()
{
sort(plays.begin(),plays.end(),CmpSerTime);
vector<cplay>::iterator ipy;
for(ipy=plays.begin();ipy!=plays.end();++ipy)
{
if(!ipy->bservered) continue;
if (ipy->servertime>=ctable::closetime) break;
cout<<inttotime(ipy->arrivetime)<<" "<<inttotime(ipy->servertime)<<" "<<difinttime(ipy->servertime,ipy->arrivetime)<<endl;
}
vector<ctable>::iterator itb=tables.begin();
cout<<itb->servernum;
for(++itb;itb!=tables.end();++itb)
{
cout<<" "<<itb->servernum;
}
}
void CA::run()
{
init();
vector<ctable>::iterator itb,itb1;
vector<cplay>::iterator ipy,ipy1;
vector<vector<ctable>::iterator> tblist;
vector<vector<cplay>::iterator> pylist;
sort(plays.begin(),plays.end(),CmpArrTime);
bool flag;
for(ipy=plays.begin();ipy!=plays.end();)
{
flag=false;
if(ipy->bservered) {++ipy;continue;};
itb=min_element(tables.begin(),tables.end());
int timepoint=ipy->arrivetime>itb->curtime?ipy->arrivetime:itb->curtime;
if(timepoint>=ctable::closetime) break;
for(itb1=tables.begin();itb1!=tables.end();++itb1) 
{
if(itb1->curtime<=timepoint) tblist.push_back(itb1);
}
for(ipy1=plays.begin();ipy1!=plays.end();++ipy1)
{
if(ipy1->arrivetime<=timepoint&&!ipy1->bservered) pylist.push_back(ipy1);
}
if(tblist.size()>1||pylist.size()>1)
{
int i,j;
for(i=0;i<tblist.size();++i)
{
itb1=tblist[i];
if(itb1->vip==1)
{
for(j=0;j<pylist.size();++j)
{
ipy1=pylist[j];
if(ipy1->vip==1)
{
ipy1->servertime=(ipy1->arrivetime>itb1->curtime?ipy1->arrivetime:itb1->curtime);
ipy1->bservered=true;
itb1->servernum++;
itb1->curtime=ipy1->servertime+(ipy1->lasttime<=2*3600?ipy1->lasttime:2*3600);
flag=true;
}
}
}
}
}
if(!flag)
{
ipy1=pylist.front();
itb1=tblist.front();
ipy1->servertime=(ipy1->arrivetime>itb1->curtime?ipy1->arrivetime:itb1->curtime);
ipy1->bservered=true;
itb1->curtime=ipy1->servertime+ipy1->lasttime;
itb1->servernum++;
}
ipy=plays.begin();
tblist.clear();
pylist.clear();
}
output();
}




int main()
{
// freopen("test.in","r",stdin);
CA *a=new CA;
a->run();
return 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: 10 20:52:00 10 0 08:00:00 20 0 08:02:00 30 0 20:51:00 10 0 08:10:00 30 0 08:12:00 10 1 20:40:00 13 0 08:01:30 15 1 20:53:00 10 1 20:54:00 10 0 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:40:00 20:40:00 0 20:51:00 20:51:00 0 20:52:00 20:52:00 0 20:53:00 20:53:00 0 4 3 2 鸣谢用户 黄卓斌 补充数据!鸣谢用户 徐向荣 修正标程! 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB C++ (g++) 1
最新发布
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值