1026. Table Tennis (30)


1026. Table Tennis (30)


#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;
}


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值