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:

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.
Customeri will take Ti 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, customer1 is served at window 1 while customer 2 is served at window 2 Customer3 will wait in front of window1 and customer 4 will wait in front of window 2. Customer5 will wait behind the yellow line.
At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 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).

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

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

题目大意:银行从8:00开始营业,一共n个柜台,每个柜台前可以排m个人,剩下的人排成一队。每个人选择最短的路径,当最短的队列不唯一的时候,选择最小的柜台。求完成时间。

注意:当等待时间超过15:00,输出sorry, 在15:00之前开始的,完成时间可以超过15:00

分析:我发现该题有人用1到540进行模拟,其实不需要。

当人数确定时,他们排在哪里随即确定,因此根据输入即可求出他们排在哪个柜台,而等待时间就是该柜台之前的人时间之和。

1:题目要求每个人排最短的队列,分析一下即可得知排队有两种策略:

前n*m个人按照1-N的次序循环填充柜台,第i个人一定在第i%N个柜台

后k-n*m个人排到最短的队列,最短的队列意思是队列第一个人最先完成的队列。

2: 注意如果等待时间超过了15-8=7个小时,则输出sorry

3:计算时使用分钟即可,在输出时转为时间。

4: 一个老哥和我的思路十分相似,见https://blog.csdn.net/weixin_43847416/article/details/104500056

不过他的代码略显繁琐,我根据自己的思路进行优化。如下

#include<iostream>
#include<queue>
using namespace std;
const int N=1e3+10;
int n,m,k,q;
int tim[N];//每个人的处理时间
queue<int> win[30]; 
int costmin[N];//每个人的总的花费时间
int main(){
    scanf("%d%d%d%d",&n,&m,&k,&q);
    for(int i=1;i<=k;i++) {
        scanf("%d",&costmin[i]);
        tim[i] = costmin[i];
        int Cur=1;//选择一个柜台
        if(i <= n*m) {
            Cur=(i-1)%n+1;        
        }
        else{
            int minn=costmin[win[1].front()];
            for(int j=2;j<=n;j++) {
                if(costmin[win[j].front()] < minn) {
                    Cur=j;
                    minn=costmin[win[Cur].front()];
                }
            }
            win[Cur].pop();
        }
        if(!win[Cur].empty()) costmin[i]+=costmin[win[Cur].back()];
        win[Cur].push(i);
    }
    while(q--){
        int x;
        scanf("%d",&x);
        if(costmin[x]-tim[x]>=540) printf("Sorry\n");
        else printf("%02d:%02d\n",(costmin[x]+480)/60,costmin[x]%60);
    }
    return 0;
}

下面是我自己的代码,不同点在于柜台存储的不是人的编号,而是时间。这种方式更加简洁,遗憾的是最后一个点没有过。我对比了代码,百思不得其解。

希望我在二刷时能够发现问题所在。

#include<iostream>
#include<vector>
using namespace std;
int Ans[1001];
int End[21] = {0};
vector<int> Windows[21];
int main(){
    int N,M,K,Q;
    cin>>N>>M>>K>>Q;
    for(int i=1;i<=K;i++){
        int T;
        cin>>T;
        int Cur=-1;//该顾客去哪个窗口
        if(i<=N*M){
            Cur=(i-1)%N;
        }    
        else{
            int min=0xfffffff;
            for(int j=0;j<N;j++){
                if(min>Windows[j][0]){
                    min=Windows[j][0];
                    Cur=j;
                }
            }
            Windows[Cur].erase(Windows[Cur].begin());
        }
        Windows[Cur].push_back(T);
        Ans[i]=(End[Cur]>=540)?-1:(End[Cur]+T);
        End[Cur]+=T;
    }
    for(int i=0;i<Q;i++){
        int Query;
        cin>>Query;
        if(Ans[Query]==-1) cout<<"Sorry"<<endl;
        else printf("%02d:%02d\n",8+Ans[Query]/60,Ans[Query]%60);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值