PAT A 1014. Waiting in Line (30)【队列模拟】
题目:https://www.patest.cn/contests/pat-a-practise/1014
思路:
直接模拟类的题。
线内的各个窗口各为一个队,线外的为一个,按时间模拟出队、入队。
注意点:即使到关门时间,已经在服务中的客户(窗口第一个,接待时间早于关门时间)还是可以被服务的。其它的则不服务。
1 #include<iostream> 2 #include<vector> 3 #include<set> 4 #include<map> 5 #include<queue> 6 #include<algorithm> 7 #include<string> 8 #include<string.h> 9 using namespace std; 10 11 int N;// (<=20, number of windows) 12 int M;// (<=10, the maximum capacity of each line inside the yellow line) 13 int K;// (<=1000, number of customers) 14 int Q;// (<=1000, number of customer queries) 15 #define INF 0x6FFFFFFF 16 typedef struct Customer 17 { 18 int process; 19 int leave; 20 }Customer; 21 22 int main() 23 { 24 //input 25 scanf("%d%d%d%d",&N,&M,&K,&Q); 26 vector<Customer> cus(K); 27 for(int i = 0; i < K; ++i) 28 { 29 scanf("%d", &cus[i].process); 30 cus[i].leave = INF; 31 } 32 //process 33 vector<queue<int>> winQueue(N); 34 vector<int> timeBase(N, 0); 35 int p; 36 for(p = 0; p < N*M && p < K; ++p) 37 { 38 cus[p].leave = timeBase[p%N]+cus[p].process; 39 timeBase[p%N] = cus[p].leave; 40 winQueue[p%N].push(p); 41 } 42 //for somebody out of the normal queue 43 for(; p < K; ++p) 44 { 45 int mmin = INF; 46 int index = -1; 47 for(int j = 0; j < N; ++j) 48 { 49 int top = winQueue[j].front(); 50 if(mmin > cus[top].leave) 51 { 52 index = j; 53 mmin = cus[top].leave; 54 } 55 } 56 //then pop 57 cus[p].leave = timeBase[index]+cus[p].process; 58 timeBase[index] = cus[p].leave; 59 winQueue[index].pop(); 60 winQueue[index].push(p); 61 } 62 63 //query 64 for(int i = 0; i < Q; ++i) 65 { 66 int q; 67 scanf("%d",&q); 68 q--; 69 if(cus[q].leave-cus[q].process >= 540) 70 printf("Sorry\n"); 71 else 72 printf("%02d:%02d\n", 8+cus[q].leave/60, cus[q].leave%60); 73 } 74 return 0; 75 }