1. 程式人生 > >**甲級PAT1014 Waiting in Line (已經通過全部測試,找到一個奇怪的坑但是不知道為什麼,跪求大佬回覆)

**甲級PAT1014 Waiting in Line (已經通過全部測試,找到一個奇怪的坑但是不知道為什麼,跪求大佬回覆)

1014 Waiting in Line (30)(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 customers 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, customer~1~ is served at window~1~ while customer~2~ is served at window~2~. Customer~3~ will wait in front of window~1~ and customer~4~ will wait in front of window~2~. Customer~5~ will wait behind the yellow line.

At 08:01, customer~1~ is done and customer~5~ enters the line in front of window~1~ since that line seems shorter now. Customer~2~ will leave at 08:02, customer~4~ at 08:06, customer~3~ at 08:07, and finally customer~5~ 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

題目要求: 

這題就相當於模擬銀行在執行時候的樣子,有N個視窗,設有黃色警戒線,每個視窗黃色警戒線裡最多能有M個人,一共有K個客戶,銀行從上午8:00開始營業,一共有K個客戶(id從1~N)每個客戶在選擇黃線時會選擇最短的線路等待。如果有兩條或更多條具有相同長度的線,客戶將總是選擇最小的視窗。依次對隊伍中每個人進行服務,服務完的客戶離開下一人繼續服務。如果客戶在下午17:00之前(不包括17:00)還沒有開始服務,則不能進行服務。輸出Q個客戶查詢到自己的結束時間,若能服務,則按格式輸出時間,若不能服務,則輸出sorry

解題思路:

模擬銀行的排隊情況可以用佇列,因為佇列具有先來先服務的性質,和銀行排隊的要求相同。每一個視窗對應一個佇列,記錄佇列的進出情況。題目要求中的每個客戶在選擇黃線時會選擇最短的線路等待。如果有兩條或更多條具有相同長度的線,客戶將總是選擇最小的視窗。等同於在警戒線外的人需要等待哪個視窗在服務的人最先離開,就去哪個視窗,對於同時有多個視窗有人離開,則選擇最小的視窗。所以離開和進入是同時發生的。

我這裡選用的思路是若有人進入佇列則就知道他結束的時間,所以需要用last[i]保留第i號視窗中佇列中最後一個人的id。結束時間是先用分鐘計時,從0開始,每個視窗初始第一個被服務的人結束時間直接為他所需服務時間,其他後進入的人的結束時間為佇列中最後一個人的結束時間和他本身所需時間之和。而且判斷該客戶能否服務並不是看他的結束時間是否>540而是看未進入佇列時最後一個人的結束時間。當佇列中最後一個人的服務時間如果>=540,說明將要進入佇列的這個人一定不能服務,用陣列exceed[i]為1記錄第i個客戶不能進行服務,而且同時要更新last陣列的值。

首先需要初始化所有佇列的情況,因為黃線內在開始服務時就已經站滿,進行M次迴圈,從0號視窗開始到N號視窗,依次按id號增大的順序。然後如果有客戶在黃線外則需要模擬進隊出隊的操作。如何讓等待的使用者選擇最短的線路等待,這個就要模擬時間的進行。從第0分鐘到第540分鐘,如果視窗佇列中正在服務的第一個到了他的結束時間,就離開隊伍,這時下一個id的人就會進入到當前視窗的隊伍。同初始化進入佇列一樣。

注意:

1.對於佇列陣列不能直接寫成queue<int> q[5]會報錯,而是要用結構體,將佇列放入結構體中

2.這裡是17:00之前開始服務,對於之前開始服務但是服務結束時間超過17:00的,不是輸出Sorry而是對應結束時間,當然由於這題小時的範圍限制8-17,分鐘的範圍限制在0-59,因此哪怕超時也不會超過18:00的。

3.當客戶進入一個視窗的隊伍中,哪怕其他視窗都沒有人了,他也不會換隊

4.輸出格式要正確。printf("%02d:%02d\n",h,m); 這樣如果不夠2位可以在左邊補0

5.若540分鐘模擬結束,還有客戶沒有進入隊伍,則剩下的客戶全部都不能進行服務,更新exceed陣列的狀態,如果少了這一步,第四個測試樣例會報錯。

6.不能理解的坑)按照我這種思路,在對模擬進出佇列時的過程中若此時客戶的id已經超過了客戶最大的id值N,則需要break,否則最後一個樣例怎麼也通不過。按我的想法來說break是否有應該不影響才對,不break只是會對之後的id進行計算,雖然好像會產生陣列越界的情況,c++是允許越界的,但是查詢的時候,id還是在範圍內,範圍內的資料肯定會對的。所以沒想明白。

完整程式碼:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 22
#define maxm 12
#define maxk 1002

typedef struct MulQueue{
	queue<int> q;
}mulqueue;

mulqueue mulq[maxn]; //每個視窗的佇列 

int needtime[maxk]; //每個客戶需要服務的時間 
int endtime[maxk];  //每個客戶結束時間 
int exceed[maxk]; //每個客戶是否超過服務時間,為0表示能服務,為1表示不能服務 
int last[maxn]; //每個視窗黃線以內每秒對應的最後一個客戶id
int N,M,K,Q; //視窗數,黃線以內最大人數,客戶數,需查詢客戶數 
int temp=1; //下一個進入黃線內的客戶id 


int main(){
	int i,j,min,first,id,front,h,m;
	cin>>N>>M>>K>>Q;
	for(i=1;i<=K;i++){
		cin>>needtime[i];
	}
	memset(last,0,sizeof(last));
	memset(endtime,0,sizeof(endtime));
	memset(exceed,0,sizeof(exceed));
	//初始化佇列狀態
	for(i=0;i<M;i++){
		for(j=0;j<N;j++){
			front = last[j]; 
			if(endtime[front]>=540){
				exceed[temp] = 1;
			}
			mulq[j].q.push(temp);
			endtime[temp] = endtime[front]+needtime[temp];
			last[j] = temp;
			temp++;	
			
		}
	} 
	//若黃線外有人等待 
	if(temp<=K){
		for(i=0;i<=540;i++){
			for(j=0;j<N;j++){
				if(!mulq[j].q.empty()){
					first =	mulq[j].q.front();
					if(endtime[first] == i){
						mulq[j].q.pop();
						front = last[j];
						if(endtime[front]>=540){
							exceed[temp] = 1;
						}
						mulq[j].q.push(temp);
						endtime[temp] = endtime[front]+needtime[temp];		
						last[j] = temp;
						temp++; 
						if(temp>K) break; //這個if(temp>K) break;和下面那個只要有一個最後一個測試點都不報錯,兩個同時沒有就報錯 
					}
				}	 
			}
			if(temp>K) break;
		}
	}

	for(i=temp;i<K+1;i++){
		exceed[i] = 1;
	}
	
	for(i=0;i<Q;i++){
		cin>>id;
		if(exceed[id] == 1){
			cout<<"Sorry"<<endl;
		}else{
			h = 8 + endtime[id] /60;
			m = 0+ endtime[id] %60;
			printf("%02d:%02d\n",h,m); 
		}
	}
	return 0;
}

忍不住想吐槽一下,這題的最後一個坑找了我好幾個小時,測試點怎麼都沒問題,能想關於測試點的坑都想遍了,還是最後試出來的這個坑。如果以後我弄明白了再來填坑。

相關推薦

**甲級PAT1014 Waiting in Line 已經通過全部測試找到一個奇怪但是知道為什麼回覆

1014 Waiting in Line (30)(30 分) Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devid

1014 Waiting in Line 30 分C++實現(已AC)

題目 題目連結:https://pintia.cn/problem-sets/994805342720868352/problems/994805498207911936 思路: 建立一個視窗列表,每個列表維護一個M長度的佇列, 以及佇列末尾的人服務結束的時間, 逐個插入客戶

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 part

模擬_1014 Waiting in Line 30 分

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

PAT-1014 Waiting in Line 30 分

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 wa

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 rul

PAT 1014 Waiting in Line 30 分

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 dev

PAT (Advanced Level) 1014 Waiting in Line 30 分

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 wa

[PAT甲級]1014. Waiting in Line (30)(銀行排隊辦理業務結束時間 佇列的應用)

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

CCF-Z字形掃描 (Python) 90分指正

問題描述 試題編號: 201412-2 試題名稱: Z字形掃描 時間限制: 2.0s 記憶體限制: 256.0MB 問題描述: 問題描述   在影象編碼的演算法中,需要將一個給定的方形矩陣進行Z字形掃描(Zigzag Scan)。給定一個n×n的矩陣,Z字形掃

字串操作函式的模擬實現指教

以下僅是自己實現的程式碼,當然裡面存在一些問題。望各位大佬能指點一二,指出其中指標使用不當之處,並指導const的用法。 #include <stdio.h> #include <string.h> char* my_strcpy(char de

1014 Waiting in Line - 模擬佇列

題意: 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.

PAT1014Waiting in Line (30) queue模擬排隊

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 fo

1014. Waiting in Line (30)

not posit con red bsp ransac blog else where Suppose a bank has N windows open for service. There is a yellow line in front of the window

PAT 1014 Waiting in Line

urn tell style tel choose malle serve -html ensure Suppose a bank has N windows open for service. There is a yellow line in front of the

1014 waiting in line 思路?

Suppose a bank has NNN windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules fo

PAT 甲級 1044 Shopping in Mars 字首和

1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in

PAT甲級 1044 Shopping in Mars二分查詢

1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has

PAT-BASIC1014——Waiting in Line

題目描述: 題目翻譯: 1014 排隊 假設一家一行共有N個視窗開放提供服務。每個視窗前面有一條黃線,將等待區域分成兩部分。客戶的等待規則如下:         每個視窗前面黃線以內的區域足夠容納M個客戶。因此,當N個視窗的該區域都滿時,所有編號在(NM

1014 Waiting in Line 佇列

Suppose a bank has NNN windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules