1026. Table Tennis 解析

你们尽管看我的程序,看得懂的算我输!!! (我自己都看不懂了......)

这个排队好麻烦。

我是按桌子来分类的。

1、VIP桌可用 :有人等待,有VIP先给VIP,没有给队列最前面的人。

没有人等待,给队列最前面的人。

2、普通桌可用:按顺序来。

3、VIP桌和普通桌都可以用:有人等待,有VIP先给VIP分配VIP桌,没有VIP给队列最前的人。优先号小的桌子

    没人等待,给队列最前面的人。优先号小的桌子。

(但愿逻辑没错吧,我也乱了_(:з)∠)_)

……………………更新线……………………

果不其然 我自己看不懂了2333333

http://blog.csdn.net/sunbaigui/article/details/8656863

http://www.cnblogs.com/aiwz/p/6154036.html

贴两个链接吧,第一个是原作者,第二个是解释。

主体思想还是队伍里面有VIP就先给VIP分配,没有就给最前面的分配。大神写的代码的思想挺有意思的~~


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
#include <queue>

#define MAX 10010
#define MAX2 110
#define EndTime 75600

using namespace std;

int N;
int Table, VipTable;
int ServeCount;//服务人数计数

vector <int> Vipnum;

struct Node{
	string arrive;
	int arriveTime;
	int time;
	int tag;
	int serveTime;
	int waitTime;
	bool isServe;
	int TabNum;
};

Node c[MAX];


struct Tab {
	int Num;
	int time;
	int count;
};

vector <Tab> t; // 普通
vector <Tab> tv;// VIP



int c2int(char c){
	return int(c - '0');
}


int str2int(string s){
	int sum = 0;
	sum	+= 3600 * (c2int(s[0]) * 10 + c2int(s[1]));
	sum += 60 * (c2int(s[3]) * 10 + c2int(s[4]));
	sum += c2int(s[6]) * 10 + c2int(s[7]);
	return sum;
}

bool cmp(Node n1, Node n2) {
	return str2int(n1.arrive) < str2int(n2.arrive);
}

bool cmp2(Tab t1, Tab t2) {
	return t1.Num < t2.Num;
}

bool cmp3(Node n1, Node n2) {
	return n1.serveTime < n2.serveTime;
}

int MinVipTab() {
	int ti = -1, min = INT_MAX;
	for (int i = 0; i < tv.size(); i++) {
		if (min > tv[i].time) {
			min = tv[i].time;
			ti = i;
		}
	}
	return ti;
}

int MinTab() {
	int ti = -1, min = INT_MAX;
	for (int i = 0; i < t.size(); i++) {
		if (min > t[i].time) {
			min = t[i].time;
			ti = i;
		}
	}
	return ti;
}


void Serve(Node * c) {
	int pv = MinVipTab();
	int p = MinTab();

	int HaveVip = false;
	int isFind = false;
	
	int ServeI = 0;
	bool st = true, isFindVip = false, isWait = false;

	vector <int> NoWait;//不需要等待
	vector <int> Wait;//需要等待


	if (tv[pv].time < t[p].time) { //VIP桌可用
		for (int i = 0; i < N; i++) {
			if (!c[i].isServe) {
				if (c[i].arriveTime < tv[pv].time) { //要等待
					Wait.push_back(i); 
					isWait = true;
				}
				else {
					if (isWait) break; // 有需要等待的 必然没有不等待的
					else { //不需要等待 则直接用 没有人同时到
						NoWait.push_back(i); break;
					}
					
				}
			}
		}

		if (Wait.size() != 0) {//有需要等待的 
			int ti = Wait[0];
			for (int i = 0; i < Wait.size(); i++) {//查找是否有VIP
				if (c[Wait[i]].tag == 1) {
//					isFindVip = true;
					ti = Wait[i];
					break;
				}
			}
			
			c[ti].waitTime = tv[pv].time - c[ti].arriveTime;
			c[ti].serveTime = tv[pv].time;
			if (c[ti].serveTime < EndTime) {
				c[ti].isServe = true;
				tv[pv].count++;
				c[ti].TabNum = tv[pv].Num;
			}
			ServeCount--;
			tv[pv].time += c[ti].time;
		}

		else {//不需要等待
			int ti = NoWait[0];
			c[ti].waitTime = 0;
			c[ti].serveTime = c[ti].arriveTime;
			tv[pv].time = c[ti].arriveTime;
			if (c[ti].serveTime < EndTime) {
				c[ti].isServe = true;
				tv[pv].count++;
				c[ti].TabNum = tv[pv].Num;
			}
			ServeCount--;
			tv[pv].time += c[ti].time;
		}
	}
	
	else if(tv[pv].time > t[p].time){ //普通桌可用
		int ti = 0;
		for (int i = 0; i < N; i++) {
			if (!c[i].isServe) {
				ti = i;
				break;
			}
		}

		if (c[ti].arriveTime < t[p].time) { //需要等待
			c[ti].waitTime = t[p].time - c[ti].arriveTime;
			c[ti].serveTime = t[p].time;
			if (c[ti].serveTime < EndTime) {
				c[ti].isServe = true;
				t[p].count++;
				c[ti].TabNum = t[p].Num;
			}
			ServeCount--;
			t[p].time += c[ti].time;
		}
		else {
			c[ti].waitTime = 0;
			c[ti].serveTime = c[ti].arriveTime;
			t[p].time = c[ti].arriveTime;
			if (c[ti].serveTime < EndTime) {
				c[ti].isServe = true;
				t[p].count++;
				c[ti].TabNum = t[p].Num;
			}
			ServeCount--;
			t[p].time += c[ti].time;
		}

	}

	else { //普通桌和VIP桌相同
		for (int i = 0; i < N; i++) {
			if (!c[i].isServe) {
				if (c[i].arriveTime < tv[pv].time) { //要等待
					Wait.push_back(i);
					isWait = true;
				}
				else {
					if (isWait) break; // 有需要等待的 必然没有不等待的
					else { //不需要等待 则直接用 没有人同时到
						NoWait.push_back(i); break;
					}
				}
			}
		}
		
		if (Wait.size() != 0) {//有需要等待的 
			int ti = Wait[0];
			for (int i = 0; i < Wait.size(); i++) {//查找是否有VIP
				if (c[Wait[i]].tag == 1) {
					isFindVip = true;
					ti = Wait[i];
					break;
				}
			}

			if (isFindVip) {//有vip
				c[ti].waitTime = tv[pv].time - c[ti].arriveTime;
				c[ti].serveTime = tv[pv].time;
				if (c[ti].serveTime < EndTime) {
					c[ti].isServe = true;
					tv[pv].count++;
					c[ti].TabNum = tv[pv].Num;
				}
				ServeCount--;
				tv[pv].time += c[ti].time;
			}

			else {
				c[ti].waitTime = t[p].time - c[ti].arriveTime;
				c[ti].serveTime = t[p].time;
				if (c[ti].serveTime < EndTime) {
					c[ti].isServe = true;
					t[p].count++;
					c[ti].TabNum = t[p].Num;
				}
				ServeCount--;
				t[p].time += c[ti].time;
			}

		}
		else {//VIP桌和普通桌都能用 选号小的
			int ti = NoWait[0];
			if (c[ti].tag == 1) {//VIP				
				c[ti].waitTime = 0;
				c[ti].serveTime = c[ti].arriveTime;
				tv[pv].time = c[ti].arriveTime;
				if (c[ti].serveTime < EndTime) {
					c[ti].isServe = true;
					tv[pv].count++;
					c[ti].TabNum = tv[pv].Num;
				}
				ServeCount--;
				tv[pv].time += c[ti].time;
			}
			else {
				if (tv[pv].Num > t[p].Num) {

					c[ti].waitTime = 0;
					c[ti].serveTime = c[ti].arriveTime;
					t[p].time = c[ti].arriveTime;
					if (c[ti].serveTime < EndTime) {
						c[ti].isServe = true;
						t[p].count++;
						c[ti].TabNum = t[p].Num;
					}
					ServeCount--;
					t[p].time += c[ti].time;
				}
				else {
					c[ti].waitTime = 0;
					c[ti].serveTime = c[ti].arriveTime;
					tv[pv].time = c[ti].arriveTime;
					if (c[ti].serveTime < EndTime) {
						c[ti].isServe = true;
						tv[pv].count++;
						c[ti].TabNum = tv[pv].Num;
					}
					ServeCount--;
					tv[pv].time += c[ti].time;
				}
			}
		}

	}

}






void printTime(int num) {
	int HH;
	int MM;
	int SS;

	HH = num / 3600;
	MM = num % 3600 / 60;
	SS = num % 3600 % 60;
	
	printf("%02d", HH);
	cout << ":";
	printf("%02d", MM);
	cout << ":";
	printf("%02d", SS);
}

void printMM(int MM){
	int isC = MM % 60;
	MM /= 60;
	if (isC >= 30) MM++;
	cout << MM;
}


int main() {
	cin >> N;
	ServeCount = N;
	
	for (int i = 0; i < N; i++) {
		cin >> c[i].arrive >> c[i].time >> c[i].tag;
		if (c[i].time > 120) {
			c[i].time = 120;
		}
		c[i].time *= 60;
		c[i].arriveTime = str2int(c[i].arrive);
		c[i].isServe = false;

/*		if (c[i].arriveTime <= 8 * 3600)
			while (1);			*/	
	}

	cin >> Table >> VipTable;

	
	

	Tab  tempt;
	tempt.time = 8*3600;
	tempt.count = 0;

	for (int i = 0; i < VipTable; i++) {
		cin >> tempt.Num;
		tempt.Num--;	
		tv.push_back(tempt);
	}

	sort(&tv[0], &tv[0] + VipTable, cmp2);

	for (int i = 0,j =0; i < Table; i++) {
		if (i != tv[j].Num) {
			tempt.Num = i;
			t.push_back(tempt);
		}
	}


#ifdef _DEBUG
	for (int i = 0; i < t.size(); i++) {
		cout << t[i].Num << " " << t[i].time << " " << t[i].count << endl;
	}
	for (int i = 0; i < tv.size(); i++) {
		cout << tv[i].Num << " " << tv[i].time << " " << tv[i].count << endl;
	}
#endif

	sort(&c[0], &c[0] + N, cmp);

#ifdef _DEBUG
	for (int i = 0; i < N; i++) {
		cout << c[i].arrive << " " << c[i].time /60 << " "
			<< str2int(c[i].arrive) << " ";
		if (c[i].tag == 1)
			cout << " VIP ";
		else
			cout << " NoV ";
		cout << endl;
	}
#endif

	//for (int i = 0; i < N; i++) {
	//	Serve(c[i]);
	//}

	while (ServeCount) {
		Serve(c);
	}

	sort(&c[0], &c[0] + N, cmp3);


	for (int i = 0; i < N; i++) {
		if (c[i].isServe) {
			cout << c[i].arrive << " ";
			printTime(c[i].serveTime);
			cout << " ";
			printMM(c[i].waitTime);	
#ifdef _DEBUG
			cout << " " << c[i].TabNum + 1;
			if (c[i].tag == 1)
				cout << " VIP ";
			else
				cout << " NoV ";
#endif
			cout << endl;
		}
	}

	vector <Tab> golbeT;
	for (int i = 0; i < tv.size(); i++) {
		golbeT.push_back(tv[i]);
	}
	for (int i = 0; i < t.size(); i++) {
		golbeT.push_back(t[i]);
	}
	sort(&golbeT[0], &golbeT[0] + Table, cmp2);

	for (int i = 0; i < Table -1 ; i++) {
		cout << golbeT[i].count << " ";
	}
	cout << golbeT[Table - 1].count << endl;


	system("pause");

	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值