PAT-1014 Waiting in Line

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 rules for the customers to wait in line are:ios

  • 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 (N M+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 custmers 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 Specification:

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).windows

The next line contains K positive integers, which are the processing time of the K customers.ide

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.spa

Output Specification:

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.code

Sample Input:

2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7orm

Sample Output:

08:07
08:06
08:10
17:00
Sorry排序

算法分析:

8点开始服务,最终的时间超过下午5点不是sorry,只有下午5点开始服务才是sorry,时间选择化成分钟,从上午8点到下午5点事540分钟。顾客若排入队伍就不能离开,即便其余队伍为空也不能。只有等待前面人结束服务其才能开始服务。使用队列queue做为窗口排队,其方法主要用到q.empty(),q.pop(),q.push().下面贴出代码队列

#include <queue>
#include <iostream> 
using namespace std;

int times[1001]; //处理时间 
int asking[1001]; //完成时间 
queue<int> q[21];  //窗口 

int main(){
    int N,M,K,Q; //N窗口数量 M窗口最大人数 K顾客数量 Q查询数量
    int i,j;
    cin >> N >> M >> K >> Q;
    
    for(i=1;i<=K;i++)
        cin >>times[i];
        
    int sum = 0; // 窗口排队人数 
    int count = 1; //人数 
    for(int time=0;time<540;time++){ //按时间排序 8:00-17:00  9小时540分钟 
        for(i=0;i<N;i++){
            for(j=0;j<q[i].size();j++){
                if(time==asking[q[i].front()]){ //服务结束 
                    q[i].pop(); 
                    sum--;
                    if(!q[i].empty()){  //计算下一我的 这样队列第一个老是计算的 
                        int tmp = q[i].front();
                        asking[tmp] = time + times[tmp];
                    }
                }
            }
        }
        while(sum<N*M&&count<=K){
            int min = 0;
            for(i= 0;i<N;i++) //找到人数最小的窗口 
                if(q[min].size() > q[i].size())
                    min = i;
            if(q[min].size()==0)  //无人直接计算 ,排在第一位直接pop 
                asking[count] = time + times[count];
            if(q[min].size()<M&&count<=K){
                q[min].push(count);
                count ++;
                sum ++;
            }   
        }
    }
 
    for(i=0;i<Q;i++){
        int query;
        cin >> query;
        if(asking[query]==0)
           cout << "Sorry" <<endl;
        else{
            int hour,min;
            hour = 8 + asking[query] / 60;
            min = asking[query] % 60;
            printf("%02d:%02d\n",hour,min);
        }
    }
    return 0;
}

2020考研打卡第四十一天,你可知道星辰之变,骄阳岂是终点?千万不要小看一我的的决心。加油!!!

41,41, 哈哈绝了。继续加油呀
相关文章
相关标签/搜索