1026. Table Tennis 解析

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

这个排队好麻烦。

我是按桌子来分类的。

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

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

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

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

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

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

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

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

http://blog.youkuaiyun.com/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;
}


统计下文中单词出现的个数存入字典,统计出现字数最多的三个单词和最少三个单词。删除原文中出现次数第二多的单词,其他单词保持顺序不变,先试试删除单词后的文章。 Sports help everyone to keep healthy, happy, and efficient. So 1 pay special attention to games,especially table-tennis. Table tennis is my favorite game. I play it almost every day Table-tennis is an ideal game for us because it brings the whole body into action. It strengthens our muscles, expands our lungs, promotes the circulation of the blood, and causes a healthy action of the skin. Besides, it is very amusing and does not cost us much money. Table-tennis is very moderate; it is not so rough as football. It is an indoor game and can be played even on rainy days. Thus, it is my favorite kind of exercise. One morning a fox sees a cock.He think,"This is my breakfast. He comes up to the cock and says,"I know you can sing very well.Can you sing for me? The cock is glad.He closes his eyes and begins to sing.The fox sees that and catches him in his mouth and carries him away. The people in the field see the fox.They cry,"Look, look!The fox is carrying the cock away. The cock says to the fox,"Mr Fox,do you understand?The people say you are carrying their cock away.Tell them it is yours.Not theirs. The fox opens his mouth and says,"The cock is mine, not yours.Just then the cock runs away from the fox and flies into the tree.多加注释,要流程图
最新发布
06-03
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值