PAT (Advanced Level) 1017 Queueing at Bank (模拟)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w419387229/article/details/81606720

不是很喜欢模拟的题,比较麻烦,通常逻辑不会有错,但是写的过程中,代码有点小问题很难找出来。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,k;
struct Customer{
	int h, m, s, p, t, wait = 0;
	bool operator < (const Customer &a) const {
		return t > a.t;
	}
};

struct Window{
	int endTime = 28800;
	bool operator < (const Window &a) const {
		return endTime > a.endTime;
	}
};

int main(){
	float waitTime = 0, cnt = 0;
	scanf("%d%d",&n,&k);
	priority_queue<Window> wq;
	for(int i = 0; i < k; i++){
		Window tmp;
		tmp.endTime;
		wq.push(tmp);	
	}
	priority_queue<Customer> cq;
	for(int i = 0; i < n; i++){
		Customer tmp;
		scanf("%d:%d:%d %d",&tmp.h,&tmp.m,&tmp.s,&tmp.p);
		tmp.t = tmp.h*60*60+tmp.m*60+tmp.s;
		if(tmp.t <= 61200){
			cq.push(tmp);
			cnt++;
		}
	}
	while(!cq.empty()){
		Customer cus = cq.top();
		cq.pop();
		Window win = wq.top();
		wq.pop();
			if(win.endTime >= cus.t){//早来了,需要等待 
				int flag = 1; 
				cus.wait += win.endTime - cus.t;

				if(cus.p <= 60){//小于1h处理完就走 
					flag = 0;
					win.endTime += cus.p*60;
				}	
				else{//大于1h,处理完还需要继续入队等待 
					win.endTime += 3600;
					cus.p -= 60;
					cus.t = win.endTime;
					cq.push(cus);
				}
				wq.push(win);
				if(!flag){
					waitTime += cus.wait;
				
				}
			}
			else{//不用等待的 
				int flag = 1;
				if(cus.p <= 60){
					flag = 0;
					win.endTime = cus.t+cus.p*60;
				}
				else{
					win.endTime = cus.t+3600;
					cus.p -= 60;
					cus.t = win.endTime;
					cq.push(cus);
				}
				wq.push(win);
				if(!flag){
					waitTime += cus.wait;
				
				}
			}
	}
	if(cnt == 0)
		printf("0.0");
	else
		printf("%.1f",(waitTime/60/cnt));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/w419387229/article/details/81606720