【PAT】1014. Waiting in Line (30)

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • Customer[i] will take T[i] minutes to have his/her transaction processed.
  • The first N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is served at window1while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer5 will wait behind the yellow line.

At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.

Input

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer queries).

The next line contains K positive integers, which are the processing time of the K customers.

The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.

Output

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output "Sorry" instead.

Sample Input
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
Sample Output
08:07
08:06
08:10
17:00

Sorry

题意:银行有winNum个窗口,每个窗口的黄线内可以容纳maxCap个人。顾客每次都是挑选黄线内人数最少的窗口进入。

分析:模拟队列。

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
int maxCap;

struct win{
	int id; //窗口id 
	int num; //该窗口目前黄线内的人数 
	int firstFinish;  //该窗口排在第一个的顾客业务结束时的时间 
	queue<int> cus; //该窗口受理的顾客 
};

bool cmp1(win a, win b){
	if(a.num != b.num){
		if(a.num<b.num)
		  return true;
		else
		  return false;
	}else if(a.num==b.num && a.id!=b.id){
		if(a.id<b.id)
		  return true;
		else
		  return false;
	}
}

bool cmp2(win a, win b){
	if(a.firstFinish != b.firstFinish){
		if(a.firstFinish < b.firstFinish)
		  return true;
		else
		  return false;
	}else if(a.firstFinish == b.firstFinish && a.id != b.id){
		if(a.id < b.id)
		 return true;
		else
		 return false;
	}
}

struct customer{
	int proTime; //业务处理时间 
	int finishTime;	 //最终处理好的时间 
};

int main(int argc, char** argv) {
	int winNum, cusNum, queNum;
	scanf("%d%d%d%d",&winNum, &maxCap, &cusNum, &queNum);
	int i;
	vector<win> wins;
	for(i=0; i< winNum; i++){
		win w;
		w.id = i;
		w.num = 0;
	 	w.firstFinish = 0;
		wins.push_back(w);
	}
	
	vector<customer> cus(cusNum+1);
	
	int time;
	for(i=1; i<=cusNum; i++){
		scanf("%d",&time);
		cus[i].proTime = time;
	 	sort(wins.begin(),wins.end(),cmp1);	 	
		if(wins[0].num < maxCap){
			if(wins[0].firstFinish == 0){
				wins[0].firstFinish += time;
			}
			wins[0].num ++;
			wins[0].cus.push(i);
		}else{
			sort(wins.begin(), wins.end(), cmp2);
			int index = wins[0].cus.front();//这个窗口第一个顾客 
			cus[index].finishTime = wins[0].firstFinish;
			wins[0].cus.pop();
			wins[0].cus.push(i);
			index = wins[0].cus.front();
			wins[0].firstFinish += cus[index].proTime;
			wins[0].num++;			
		}
	}	
	
	for(i=0; i<winNum; i++){
		win w = wins[i];
		while(!w.cus.empty()){
			int index = w.cus.front();
			cus[index].finishTime = w.firstFinish;			
			w.cus.pop();
			if(!w.cus.empty())
			  w.firstFinish = w.firstFinish + cus[w.cus.front()].proTime;	
		}		
	}
	
	int index;
	for(i=0; i<queNum; i++){
		scanf("%d",&index);	
		int time = cus[index].finishTime;
		int proTime = cus[index].proTime;
		if((time-proTime)>=(17-8)*60){
			//如果受理时间是17:00之后的,则输出sorry 
			printf("Sorry\n");
		}else{
			int h = 8 + time/60;
			int m = time%60;
			printf("%02d:%02d\n",h,m);
		}
	}
	return 0;
}




以下是另一种写法,但是有两组数据过不了,待改进:

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int N,M,K,Q;

struct customer{
	customer(){
		h=8; m=0; 
	}
	int h,m,pro;
};
vector<customer> cus; 

struct windows{
	windows(){
		h=8; m=0; 
	}
	int id;
	int h,m;
	queue<int> c;
	bool operator > (const windows &b)const{
		if(c.size()!=b.c.size()){
			return c.size() > b.c.size();
		}else if((h*60+m)!=(b.h*60+b.m)){
			return (h*60+m)>(b.h*60+b.m);
		}else{
			return id > b.id;	
		}		
	} 
};
priority_queue<windows, vector<windows>, greater<windows> > win;

int main(){
	scanf("%d%d%d%d",&N,&M,&K,&Q);
	cus.resize(K);	
	int i;
	for(i=0; i<N; i++){
		windows w;
		w.id = i;
		win.push(w);
	}
	int h,m,pro;
	for(i=0; i<K; i++){
		scanf("%d",&pro);
		cus[i].pro = pro;
		windows w = win.top();
		win.pop();
		h=w.h;  m=w.m;
		if(w.c.size()==0){
			//第一个客户进入窗口
			w.m = (m+pro)%60;
			w.h = h + (m+pro)/60;
			w.c.push(i); 
		}else if(w.c.size()>0 && w.c.size()<M){
			w.c.push(i);
		}else if(w.c.size()==M){
			cus[w.c.front()].h = w.h;
			cus[w.c.front()].m = w.m;
			w.c.pop();
			w.c.push(i);
			int firstCus = w.c.front();
			pro = cus[firstCus].pro;
			w.m = (m+pro)%60;
			w.h = h + (m+pro)/60;	
		}
		win.push(w);
	}	

	while(!win.empty())	{
		windows w = win.top();
		win.pop();
		while(!w.c.empty()){
			int index = w.c.front();
			cus[index].h = w.h;
			cus[index].m = w.m;
			w.c.pop();			
			if(!w.c.empty()){
				h = w.h; 
				m = w.m;
				pro = cus[w.c.front()].pro;
				w.m = (m + pro)%60;
				w.h = h + (m + pro)/60;
			}
		}		
	} 
	int q;
	for(i=0; i<Q; i++){
		scanf("%d",&q);
		h = cus[q-1].h;
		m = cus[q-1].m;		
		if((h*60+m-cus[q-1].pro)>=(17*60)){
			printf("Sorry\n");
		}else{
			printf("%02d:%02d\n",h,m);
		}
	}
	return 0;
}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章