1. 程式人生 > >PAT 1017 Queueing at Bank

PAT 1017 Queueing at Bank

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 (≤10410^4) - 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<cstdio>
#include<queue> 
#include<algorithm>
#include<cmath>
#define ON 8*60*60//開始時間
#define OFF 17*60*60//不能超過的時間
using namespace std;
int num(char c){
	return c-'0';
}
struct node{
	int start,last,window,time,wait;//開始時間,最後執行完該顧客需求時的時間,顧客所在視窗,顧客需求時間,等待時間
	int h,m,s;
}cus[10010];
int N,K;
bool cmp1(node &a,node &b){//按出現時間排序
	if(a.h!=b.h) return a.h<b.h;
	if(a.m!=b.m) return a.m<b.m;
	if(a.s!=b.s) return a.s<b.s;
	return a.time<b.time;
}

struct cmp{//利用優先佇列求出當前最快結束的視窗
	bool operator()(node a,node b){
		if(a.last!=b.last)
			return a.last>b.last;//降序 
		return a.window>b.window;
	}
}; 

priority_queue<node,vector<node>,cmp>Q;
int cnt[110];
int main(int argc, char** argv) {
	scanf("%d%d",&N,&K);
	for(int i=0;i<N;i++){
		char input[10];
		scanf("%s%d",input,&cus[i].time);
		cus[i].time*=60;//記得全部化成秒
		cus[i].h=num(input[0])*10+num(input[1]);
		cus[i].m=num(input[3])*10+num(input[4]);
		cus[i].s=num(input[6])*10+num(input[7]);
		cus[i].start=cus[i].h*60*60+cus[i].m*60+cus[i].s;//s 
		if(cus[i].h>17||(cus[i].h==17&&(cus[i].m>0||cus[i].s>0))){//超過17點,捨棄
			i--;
			N--;
		}
	}
	sort(cus,cus+N,cmp1);

	for(int i=0;i<K;i++){//利用cnt[i]表示第i個視窗最後結束所需要的時間
		cnt[i]=ON;
	}
	int sum=0,ind=-1;
	for(int i=0;i<N;i++){
		if(Q.size()<K){
			ind=cus[i].window=i%K;
			cus[i].last=cus[i].time+max(cnt[ind],cus[i].start);//這個地方有可能出現的時間晚於前i個視窗執行的時間,中間有空餘;第二個情況是出現的時間早於8點,均能處理
			cus[i].wait=cus[i].last-cus[i].time-cus[i].start;//第i個顧客的等待時間
			Q.push(cus[i]);
			cnt[ind]=cus[i].last;//更新每個視窗最後的時間
		}
		else{
			node tmp=Q.top();
			Q.pop();
			ind=cus[i].window=tmp.window;	
			cus[i].last=cus[i].time+max(cnt[ind],cus[i].start);
			cus[i].wait=cus[i].last-cus[i].time-cus[i].start;
			Q.push(cus[i]);
			cnt[ind]=cus[i].last; 
		}
	}
	
	int count=0;
	for(int i=0;i<N;i++){
//		if(cus[i].last<=OFF){//超過17點?? 這個地方表示最後的執行時間超過17點也符合題意,只有到達時間超過17點不算等待,因此捨去這種情況。
			count++;
			sum+=cus[i].wait;
//		}
	}
	printf("%.1lf",sum/(60.0*count));
	return 0;
}

相關推薦

PAT 1017 Queueing at Bank[一般]

problem lis iostream n) using ber 數組下標 pos card 1017 Queueing at Bank (25)(25 分)提問 Suppose a bank has K windows open for service. Ther

PAT 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 part

PAT 1017 Queueing at Bank

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

PAT 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 cust

PAT 1017. Queueing at Bank (25)

// //1017. Queueing at Bank (25) // accept #include <iostream> #include <algorithm> using namespace std; typedef struct {

pat 1017. Queueing at Bank (模擬優先佇列)

好久沒有做pat了,重新開刷 本想從這道水題入手可以快點,結果卻慘遭滑鐵盧,到第二天才全部通過 廢話說完看題,模擬題,模擬的是多視窗排隊。 建議把時間統一轉化為秒做處理。 我選擇了用map儲存依次到達的顧客(題設保證了到達時間都不相同),用priority_queue模擬

[PAT甲級]1017. Queueing at Bank (25)(銀行辦理業務平均等待時間)

1017. Queueing at Bank (25) 原題連結 相似題目 1014. Waiting in Line (30) Suppose a bank has K windows open for service. There is a yello

PAT 甲級 1017. Queueing at Bank

最後一個例子沒過 不過有巨巨發現我程式碼的bug,為什麼最後一個例子沒過,請聯絡我 #include<stdio.h> #include<algorithm> #include<vector> #include<queue>

PAT 甲級 1017 Queueing at Bank

-o n) true rst pos for %d from sum https://pintia.cn/problem-sets/994805342720868352/problems/994805491530579968 Suppose a bank has K

1017 Queueing at Bank

tar 客戶 題意 continue 參與 custom div using 窗口 題意:銀行有K個窗口用於服務,給出所有人的達到時間T和服務時間P,計算所有被服務的客戶的平均等待時間。任何客戶的服務時間不得超過60分鐘。早於08:00到的,要等到08:00;在17:00:

1017 Queueing at Bank - 優先佇列+模擬

題意: 有n個人k個視窗,每個視窗服務一個人,顧客按到來的先後順序排隊,當視窗沒人的時候,就過去,問平均等待時間 思路: 這種時間題肯定要先轉為秒,這樣好算啦 然後顧客要是在8點之前到的話要等待 程式碼如下: #include<iostream> #include

1017 Queueing at Bank (我自己寫的模擬時間的版本)

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 custom

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.

PAT1017:Queueing at Bank

scan ssi spec pen contain 內存 兩種 end output 1017. Queueing at Bank (25) 時間限制 400 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者

1017 Queueing at [email protected]

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 par

PAT 1017 A除以B

轉換成 輸入 ems problem class size str href emp https://pintia.cn/problem-sets/994805260223102976/problems/994805305181847552 本題要求計算A/B,其中A是

PAT 1017 A除以B (20分)

1017 A除以B (20 分) 本題要求計算 A/BA/BA/B,其中 AAA 是不超過 1000 位的正整數,BBB 是 1 位正整數。你需要輸出商數 QQQ 和餘數 RRR,使得 A=B×Q+R

程式設計題目: PAT 1017. A除以B (20)

本題要求計算A/B,其中A是不超過1000位的正整數,B是1位正整數。你需要輸出商數Q和餘數R,使得A = B * Q + R成立。 輸入格式: 輸入在1行中依次給出A和B,中間以1空格分隔。 輸出格式: 在1行中依次輸出Q和R,中間以1空格分隔。 輸入樣例: 123456789050987654

PAT乙級 1017大數除法

1017 A除以B (20 分) 本題要求計算 A/B,其中 A 是不超過 1000 位的正整數,B 是 1 位正整數。你需要輸出商數 Q 和餘數 R,使得 A=B×Q+R成立。  

PAT甲級1017 (模擬排序)

課前分析 感覺和1016差不多, 就模擬一下幾個佇列, 有空位立即插入並計算等待時間, 沒空位計算最早處理完的使用者的離去時間,並更新為當前時間等等。 (1) vector a(10); //定義了10個整型元素的向量(尖括號中為元素型別名,它可以是任何合法的資