PAT 1014. Waiting in Line

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <functional>
#include <string>
#include <queue>
/************************************************************************/
/* 
何时进入是问题的关键,要设法找出进入的条件:
当所有都满时,找最先处理完的一个;
否则找最少人等的那个;
坑爹点在于38行最小时间的初始值,不是我们认为的24*60
*/
/************************************************************************/
using namespace std;

int N,M,K,Q;

int winTimes[25];
vector<int> winQueues[25];
int fTimes[1005];

int main()
{
	scanf("%d %d %d %d",&N,&M,&K,&Q);
	for(int i=0;i<N;i++)
		winTimes[i] = 8*60;
	int curTime = 8*60;

	for(int i=0;i<K;i++)
	{
		int pTime;
		scanf("%d",&pTime);
		int minNum=1005;
		int minNumIndex=-1;
		int minTime = 1<<30;
		int minTimeIndex = -1;
		for(int k=0;k<N;k++)
		{
			if(winQueues[k].size()<minNum)
			{
				minNum = winQueues[k].size();
				minNumIndex = k;
			}
			if(winQueues[k].size()!=0 && fTimes[winQueues[k][0]]<minTime)
			{
				minTime = fTimes[winQueues[k][0]];
				minTimeIndex = k;
			}
		}
		if(minNum<M)
		{
			
			winQueues[minNumIndex].push_back(i);
			if(winTimes[minNumIndex]>=17*60)
			{
				continue;
			}
			winTimes[minNumIndex]+= pTime;
			fTimes[i] = winTimes[minNumIndex];
		}else
		{
			curTime = minTime;
			for(int k=0;k<N;k++)
			{
				if(winQueues[k][0] <= minTime)
					winQueues[k].erase(winQueues[k].begin());
			}
			
			winQueues[minTimeIndex].push_back(i);
			if(winTimes[minTimeIndex]>=17*60)
			{
				continue;
			}
			winTimes[minTimeIndex]+= pTime;
			fTimes[i] = winTimes[minTimeIndex];
		}
	}
	for(int i=0;i<Q;i++)
	{
		int id;
		scanf("%d",&id);
		id--;
		if(fTimes[id]==0)
			printf("Sorry\n");
		else
			printf("%02d:%02d\n",fTimes[id]/60,fTimes[id]%60);
	}
	return 0;
}



你可能感兴趣的:(PAT 1014. Waiting in Line)