Pat 甲级 训练真题集 1017!

1017. Queueing at Bank (25)

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=10000) - the total number of customers, and K (<=100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:

7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10

Sample Output:

8.2

代码

有个段错误没找到很尴尬。。。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<deque>
#include<queue>
using namespace std;

#define START 28800
#define END 61200


class Person{
public:
	string arrive_time;
	int process_time;
	int priority;

	bool operator < (const Person &p)const{
		return priority>p.priority;
	}
};
bool less_than (const Person &p1,const Person &p2){
		return p1.arrive_time<p2.arrive_time;
	}

int string_interger(string s){
	int x;
	stringstream ss;
		ss<<s;
		ss>>x;
	return x;
}
int compute_sum(string arrive_time,int process_time){
	int x,sum=0;
	string H,M,S;
	H=arrive_time.substr(0,2);
	M=arrive_time.substr(3,2);
	S=arrive_time.substr(6,2);		
		x=string_interger(H);
		sum+=x*3600;
		x=string_interger(M);
		sum+=x*60;
		x=string_interger(S);
		sum+=x;


	sum+=process_time*60;
	return sum;

}
int main(){

	int waiting_time=0;

	int N,K;
	cin>>N>>K;
	string arrive_time;
	int process_time;

	deque<Person> deque;
	priority_queue<Person> pri_que;

	for(int i=0;i<N;++i){
		cin>>arrive_time>>process_time;
		Person p;
		p.arrive_time=arrive_time;
		p.process_time=process_time;	
		p.priority=compute_sum(arrive_time,process_time);
		deque.push_back(p);
	}
	sort(deque.begin(),deque.end(),less_than);

	for(int i=0;i<deque.size();++i){
		if(deque[i].arrive_time.compare("08:00:00")<0){
			waiting_time+=START-compute_sum(deque[i].arrive_time,0);
			deque[i].priority=compute_sum("08:00:00",deque[i].process_time);
		}else{
			break;
		}
	}
	for(int i=deque.size()-1;i>=0;--i){
		if(deque[i].arrive_time.compare("17:00:01")>=0){
			deque.pop_back();
			i=deque.size();
		}else{
			break;
		}
	}
	int num;
	num=deque.size();
	for(int i=0;i<K;++i){
		if(!deque.empty()){
			pri_que.push(deque[i]);
		}else{
			printf("%0.1f",0.0);
			return 0;
		}
	}
	for(int i=0;i<K;++i){
		if(!deque.empty()){
			deque.pop_front();
		}
		
	}
	while(!deque.empty()){
		Person p;
		p=pri_que.top();	
		pri_que.pop();
		
		deque[0].priority=(p.priority>compute_sum(deque[0].arrive_time,0))?p.priority+compute_sum("00:00:00",deque[0].process_time):deque[0].priority;
		pri_que.push(deque[0]);
		waiting_time+=(p.priority>compute_sum(deque[0].arrive_time,0))?p.priority-compute_sum(deque[0].arrive_time,0):0;
		deque.pop_front();
	}	
	printf("%0.1f",float(waiting_time)/(60*num));
	return 0;
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦