PAT甲级1017 Queueing at Bank

1017 Queueing at Bank (25分)

  • 测试点5:要注意只要顾客是在17点之前进入银行,那么即使他等待到了17点之后,他依然将被服务,依然需要计算等待时间

思路和前面有一题的排队问题很像,模拟整个过程;

#include
using namespace std;

struct record{
	int hour,min,sec,protime;
	int time;
	int endtime;
};

bool cmp(record a,record b){
	return a.time<b.time;
}

int main(){
	int k,n;
	cin>>n>>k;
	vector<record> r(n);
	for(int i=0;i<n;i++){
		scanf("%d:%d:%d",&r[i].hour,&r[i].min,&r[i].sec);
		r[i].time=r[i].hour*3600+r[i].min*60+r[i].sec;
		cin>>r[i].protime;
	}
	sort(r.begin(),r.end(),cmp);
	vector<record> line;
	vector<record> servline;
	int cnt=0,totalwt=0,a=0;
	int i=r[0].time;
	while(i<17*3600+1 || !line.empty()){
		totalwt=totalwt+line.size();
		if(a<n && i==r[a].time)line.push_back(r[a++]);
		int j=0;
		while(j<servline.size()){
			if(i==servline[j].endtime)servline.erase(servline.begin()+j);
			else j++;
		}
		while(servline.size()<k && i>=8*3600 && line.size()!=0){
			line[0].endtime=i+line[0].protime*60;
			servline.push_back(line[0]);
			line.erase(line.begin());
			cnt++;
		}
		i++;
	}
	printf("%.1f",totalwt/(cnt*60.0));
	return 0;
}

你可能感兴趣的:(PAT甲级刷题历程)