pat中有关于排队模拟的问题

1017. Queueing at Bank (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=10000) - 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

这道题的意思就是:在8点到 17点之间柜台可以为顾客可服务,超过17点来的顾客就不再服务了,可以针对每个顾客来考虑,首先将其按照来的顺序进行排序,然后在考虑对这个顾客的服务时候就在所有的柜台中选取一个结束时间最早的,然后max(柜台结束上个服务的时间,顾客到来的时间)就是这个顾客开始被服务的时间,加上所需的服务时间来更新这个柜台的结束时间。最后将客户开始被服务的时间减去客户到来的时间就是等待时间

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

struct customer{
    int begin;
    //int end;
    int arrival;
    int seerve;
};
customer cu[10000];
int clerk[1000];
int n,k;
bool cmp(customer c1,customer c2) {
    return c1.arrival<c2.arrival;
}
int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++){
        int h,m,s,serve;
        scanf("%d:%d:%d%d",&h,&m,&s,&serve);
        cu[i].arrival=h*3600+m*60+s;
        cu[i].seerve=serve*60;
    }
    sort(cu,cu+n,cmp);
    for(int i=0;i<1000;i++)
        clerk[i]=8*3600;
    for(int i=0;i<n;i++){//第i个顾客
        int min=24*3600;
        int idx=0;
        for(int j=0;j<k;j++){
            if(clerk[j]<min){
                min=clerk[j];
                idx=j;
            }
        }//找到最小的窗口
        cu[i].begin=max(min,cu[i].arrival);
        clerk[idx]=cu[i].begin+cu[i].seerve;
    }
    float s=n;
    int total=0;
    int endd=17*3600+1;
    for(int i=0;i<n;i++){
        if(cu[i].arrival<endd){
            total+=cu[i].begin-cu[i].arrival;
        }
        else
            s--;
    }
    float time=total;
    time=time/(60*s);
    printf("%.1f",time);
    return 0;
}

1014. Waiting in Line (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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, customer1 is served at window1while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. 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 customer5 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个用户,和他们各自的所需服务时间,问你他们各自的结束服务时间
这道题的特殊之处在于它把所有的顾客都看成在开门之前已经全部到了,因此属于供不应求,所以除了最早来的m*n个用户之外,后面的用户总是选择最早结束的那一队进入

另外可以设计这样的数据结构:每个窗口对应一个队列,每个窗口对应一个包含等待人数,最早结束时间和最晚结束时间的结构体,每考虑一个用户,如果已经满了,他选择最早结束时间最小的窗口,并且他被服务的时间为当前的最晚结束时间

#include <iostream>
#include<queue>
using namespace std;
struct customer{
    int serve;
    int begin;
};
struct clerk{
    int wait;
    int firstend;//首位的结束时间
    int lastend;
};
queue<int> qq[100];
customer cu[1000];
clerk cl[100];
int main(int argc, const char * argv[]) {
    int n,m,k,q;
    cin>>n>>m>>k>>q;
    for(int i=0;i<k;i++)
        cin>>cu[i].serve;
    for(int i=0;i<k;i++){
        int min;
        int flag=0;
        for(int j=0;j<n;j++){
         if(cl[j].wait<m)
         {
             flag=1;
             break;
         }
        }
        if(flag==1){//有空位
            min=999;
            int idx=0;
            for(int kk=0;kk<n;kk++){
                if(cl[kk].wait<min){
                    min=cl[kk].wait;
                    idx=kk;
                }
            }
            cl[idx].wait++;
            cu[i].begin=cl[idx].lastend;
            cl[idx].lastend+=cu[i].serve;
            if(qq[idx].empty())
                cl[idx].firstend+=cu[i].serve;
            qq[idx].push(cu[i].serve);
        }
            else{// 无空位
            min=999;
            int idx=0;
            for(int kk=0;kk<n;kk++){
                if(cl[kk].firstend<min){
                    idx=kk;
                    min=cl[kk].firstend;
                }
            }//找到最早结束的
                qq[idx].pop();
                cl[idx].firstend+=qq[idx].front();
                qq[idx].push(cu[i].serve);
                cu[i].begin=cl[idx].lastend;
                cl[idx].lastend+=cu[i].serve;
        }
    }
    for(int i=0;i<q;i++){
        int tmp;
        cin>>tmp;
        tmp--;
        if(cu[tmp].begin<540)
            printf("%02d:%02d\n",(cu[tmp].begin+cu[tmp].serve)/60+8,(cu[tmp].begin+cu[tmp].serve)%60);
        else
            cout<<"Sorry\n";
    }
    return 0;
}

总而言之,对于排队模拟的问题,总是根据到来的用户来考虑的,然后分别建立用户和窗口的结构体,用户被服务的时刻=max(最早可以用的窗口的结束时间,用户到来的时间),然后每次来了一个用户总是需要对该窗口的结束时间进行一下更新。

如果是多个窗口并且每个窗口有限定人数的问题,可以建立窗口的结构体,注意需要包含该窗口的第一个人的结束时间和最后一个人的结束时间。每个来的顾客的总是插到第一个人结束最早的那一队中去,并且他的被服务时间=当前窗口最后一个人的结束时间。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值