pat-1014* Waiting in Line

1014:
排队服务问题,队列实现。
注意条件控制。

#include <iostream>
using namespace std;
#include<queue>
#include<map>
#include<stdio.h>

struct People
{
	int id;
	int cost;
	bool start;
};
queue<People> q[30];
queue<People> wait;
map<int,int> finish;

void format(int n)
{
	if(n<0)
		printf("Sorry\n");
	else
		printf("%02d:%02d\n",8+n/60,n%60);
}

int main()
{
	int win;
	int cap;
	int num;
	int ask;
	int id;

	cin>>win;
	cin>>cap;
	cin>>num;
	cin>>ask;

	for(int i=1;i<=num;i++)
	{
		People p;
		p.id = i;
		p.start =false;
		cin>>p.cost;
		wait.push(p);
	}

	//initial
	while(q[win-1].size()!=cap && wait.size()!=0)
	{
		for(int k=0;k<win;k++)
		{
			if(q[k].size()<cap )
			{
				if(wait.size()>0)
				{
					q[k].push(wait.front());
				//cout<<"initial put "<<wait.front().id<<" into "<<k<<endl;
					wait.pop();
				}
			}
		}
	}

	for(int i=1;i<=540;i++) //time
	{
		for(int k=0;k<win;k++)
		{
			if(q[k].size()>0) //still have people
			{
				q[k].front().cost--;
				q[k].front().start = true;
				if(q[k].front().cost==0)
				{
					finish[q[k].front().id]=i;
					//cout<<q[k].front().id<<" pop from "<<k<<endl;
					q[k].pop();
					
					if(wait.size()>0) //into the queue
					{
						//cout<<wait.front().id<<" push into "<<k<<endl;
						q[k].push(wait.front());
						wait.pop();
					}
				}
			}
		}
	}

	for(int k=0;k<win;k++)
	{
		while(q[k].size()>0) 
		{
			if(q[k].front().start==true)
			{
				finish[q[k].front().id]=540+q[k].front().cost;
			}
			else
			{
				finish[q[k].front().id]=-1;
			}
			q[k].pop();
		}
	}

	while(wait.size()>0) 
	{
		finish[wait.front().id]=-1;
		wait.pop();
	}

	for(int i=0;i<ask;i++)
	{
		cin>>id;
		format(finish[id]);
	}

}

你可能感兴趣的:(wait)