1026 Table Tennis (30分)

据说是PAT最难的一道模拟题,情况很复杂,第二次做了,依旧是折磨人的小妖精(* ̄︶ ̄)。
这次主要是栽在条件判断上了,一定要小心数组越界!而且这种错很难找
(代码里※标注的地方,就是我找错找了好久的地方)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <iomanip>
#include <set>

using namespace std;
int N,K,M,m;  
int cnt[200];//每张桌子的服务人数
vector<int> vtable;
set<int> VIP;//VIP桌子编号
int Table[200];//每张桌子是否被使用 1-K
int Maxp=2*3600;//最长玩耍的时间(min)


int transtime(int h, int m, int s)
{
	return h * 3600 + m * 60 + s;
}
struct Cnode {
	int atime;//到达时间
	int ptime;
	Cnode(){}
	Cnode(int a, int b)
	{
		atime = a;
		ptime = b;
	}
};

vector<Cnode> V;  //vip顾客
vector<Cnode> P;  //普通顾客
vector<Cnode> ans;

bool cmp1(Cnode a, Cnode b)
{
	return a.atime < b.atime;//先到者优先
}


struct tablenode {
	int ID;
	int stime;
	tablenode(){}
	tablenode(int a, int b)
	{
		ID = a;
		stime = b;
	}
	bool operator<(const tablenode &a)const {
		if (stime == a.stime) return ID > a.ID;
		else return stime > a.stime;
	}
};
priority_queue<tablenode> Q;


int main()
{
	memset(Table, 0, sizeof(Table));
	memset(cnt, 0, sizeof(cnt));
	scanf("%d", &N);
	int open = 8 * 3600;
	int close = 21 * 3600;
	
	int h, m, s,p,tag;
	for (int i = 0; i < N; i++)
	{
		scanf("%d:%d:%d %d %d", &h, &m, &s, &p, &tag);
		p = p * 60;
		if (p > Maxp) p=Maxp;
		int t = transtime(h, m, s);
		Cnode nc = Cnode(t, p);
		if(tag==1){
			V.push_back(nc);
		}
		else {
			P.push_back(nc);
		}

	}
	scanf("%d %d", &K,&M);
	for (int i = 0; i < M; i++)
	{
		scanf("%d", &m);
		VIP.insert(m);
		vtable.push_back(m);
	}
	sort(vtable.begin(), vtable.end());
	sort(P.begin(), P.end(), cmp1);
	sort(V.begin(), V.end(), cmp1);


	int posv = 0;  //最大值 V.size()
	int posp = 0;  //最大值P.size()
	int Iv = 0;  //最大值:M-1
	int numt = 1;  //1-K
	//初始化K位顾客
    bool fg=true;
	while (posv + posp < K)
	{
		if (posv + posp == N) break;
		//※※※※※※※※
		if (posv == V.size() ||(posp<P.size()&&P[posp].atime < V[posv].atime))
		{
			if (P[posp].atime >= close) {fg=false;  break;}
			while (Table[numt] != 0) ++numt;
			cnt[numt]++;
			Table[numt] = 1;
			if (VIP.count(numt)) Iv++;
			int st;
			if (P[posp].atime <= open) st = open;
			else st = P[posp].atime;
			
			tablenode ta = tablenode(numt,st+P[posp].ptime);
			Q.push(ta);
			
			Cnode ca = Cnode(P[posp].atime, st);
			ans.push_back(ca);
			posp++;
		}
		//※※※※※※※※※※
		else if(posp==P.size()||(posv<V.size()&&V[posv].atime<P[posp].atime)){
			if (V[posv].atime >= close) {fg=false;  break;}
			if (Iv <= M - 1) { //vip桌子未满
				cnt[vtable[Iv]]++;
				Table[vtable[Iv]] = 1;
				//Iv++;

				int st;
				if (V[posv].atime <= open) st = open;
				else st = V[posv].atime;

				tablenode tb = tablenode(vtable[Iv], st + V[posv].ptime);
				Q.push(tb);

				Cnode cb = Cnode(V[posv].atime, st);
				ans.push_back(cb);
				Iv++;
				posv++;

			} 
			else { //当普通顾客
 				while (Table[numt] != 0) ++numt;
				cnt[numt]++;
				Table[numt] = 1;
				//if (VIP.count(numt)) numv++;
				int st;
				if (V[posv].atime <= open) st = open;
				else st = V[posv].atime;

				tablenode tb = tablenode(numt, st + V[posv].ptime);
				Q.push(tb);

				Cnode cb = Cnode(V[posv].atime, st);
				ans.push_back(cb);
				posv++;


			}

		}
		
	}
	
	while (posv + posp < N&&fg)
	{
		if (Q.empty()) break;
		tablenode tmp = Q.top();
		Q.pop();
		if (tmp.stime >= close) break;
		bool flag = true;
		if (VIP.count(tmp.ID) && posv < V.size())
		{//※※※※※※※※※※
			if (posp == P.size() || V[posv].atime < tmp.stime || V[posv].atime < P[posp].atime) {
				
					if (V[posv].atime >= close) break;
					cnt[tmp.ID]++;
					int st;
					if (V[posv].atime > tmp.stime) st = V[posv].atime;
					else st = tmp.stime;

					tablenode tb = tablenode(tmp.ID, st + V[posv].ptime);
					Q.push(tb);

					Cnode cb = Cnode(V[posv].atime, st);
					ans.push_back(cb);
					posv++;
					flag = false;
				

			}
		}
			if (flag) {  //顺次安排
			//※※※※※※※※※※※
				if (posv == V.size() ||(posp<P.size()&&P[posp].atime < V[posv].atime) ){
					if (P[posp].atime >= close) break;
					cnt[tmp.ID]++;

					int st;
					if (P[posp].atime > tmp.stime) st = P[posp].atime;
					else st = tmp.stime;

					tablenode ta = tablenode(tmp.ID, st + P[posp].ptime);
					Q.push(ta);

					Cnode ca = Cnode(P[posp].atime, st);
					ans.push_back(ca);
					posp++;

				}
				//※※※※※※※※※※※
				else if (posp == P.size() || (posv<V.size()&&V[posv].atime < P[posp].atime)) {
					if (V[posv].atime >= close) break;
					cnt[tmp.ID]++;
					int st;
					if (V[posv].atime > tmp.stime) st = V[posv].atime;
					else st = tmp.stime;

					tablenode tb = tablenode(tmp.ID, st + V[posv].ptime);
					Q.push(tb);

					Cnode cb = Cnode(V[posv].atime, st);
					ans.push_back(cb);
					posv++;

				}

			}
	}
	
int h1, m1, s1;
    double w;
	for (int i = 0; i < ans.size(); i++)
	{
		h = ans[i].atime / 3600;
		m = ans[i].atime % 3600 / 60;  //到达时间
		s = ans[i].atime % 60;

		h1 = ans[i].ptime / 3600;
		m1 = ans[i].ptime % 3600 / 60;  //服务时间
		s1 = ans[i].ptime % 60;

		w = double(ans[i].ptime - ans[i].atime) / 60;
        int iw=w+0.5;
		printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", h, m, s, h1, m1, s1, iw);
	}

	printf("%d", cnt[1]);
	for (int i = 2; i <= K; i++)
	{
		printf(" %d", cnt[i]);
	}
	printf("\n");

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值