1014. Waiting in Line (30)

1.银行排队时间模拟,采用合适的结构进行编程

2.超过17:00(包括17:00)不再接受新用户,已经在处理的用户可以一直处理到17:59

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
2 1 4 4
599 540 1 2
1 2 3 4
*/
struct customNode{
	int process;
	int cost;
	string finished;
	customNode() :process(0), cost(0), finished(""){};
};
string int2str(int a)
{
	char b = a / 10 + '0';
	char c = a % 10 + '0';
	string ans = "1";
	ans += b;
	ans += c;
	return ans.substr(1);
}
string recordTime(int a)
{
	int hour = a / 60 + 8;
	int min = a % 60;
	string ans = int2str(hour) + ":" + int2str(min);
	//cout << ans << endl;
	return ans;
}
int main(void) {

	int n, m, k, q;//n为银行窗口数,m为每个窗口的黄线内容量,k个客户,q是查询序列
	cin >> n >> m >> k >> q;
	vector<customNode> custom(k);
	for (int i = 0; i < k; i++)
	{
		cin >> custom[i].process;
		custom[i].finished = "";
		custom[i].cost = custom[i].process;
	}

	vector<queue<int>> window(n);
	int input = 0;
	for (; input < n*m&input < k; input++)
	{//把客户压进窗口的队列
		window[input%n].push(input);
	}

	for (int i = 0; i < 599; i++)
	{//一直可以处理到17:59
		bool allFinished = true;
		for (int j = 0; j < n; j++)
		{
			if (window[j].empty()) continue;//队列为空意味着等待队列无人了。因为窗口一有位置,就会马上插入等待队列中的人。

			allFinished = false;

			if (i >= 540)
			{//超过17:00的不再接收新的处理
				int tmp = window[j].front();
				if (custom[tmp].process == custom[tmp].cost)
				{
					while (!window[j].empty())
					{
						window[j].pop();
					}
					continue;//直接下一个窗口
				}
			}

			int p = window[j].front();
			custom[p].process--;
			if (custom[p].process == 0)
			{//如果处理时间为0,证明客户处理完毕
				custom[p].finished = recordTime(i + 1);
				window[j].pop();//
				if (input<k&&i<539)
				{
					window[j].push(input++);
				}
			}
		}
	}


	for (int i = 0; i < q; i++)
	{//输入查询队列
		int query;
		cin >> query;
		if (custom[query - 1].process != 0)
			cout << "Sorry" << endl;
		else
			cout << custom[query - 1].finished << endl;
	}
	return 0;
}


posted @ 2015-11-04 12:44  siukwan  阅读(128)  评论(0)    收藏  举报