1017 Queueing at Bank (25 分)

1017 Queueing at Bank (25 分)

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 (≤10​4​​) - 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

更新

  • 更新了代码简洁度和注释
  • 可能还是不够简洁吧 下次记得看看别人的代码
  • 下次吧Orz
#include <bits/stdc++.h>
using namespace std;

const int maxn = 10010;
const int INF = 0x3f3f3f;
int window[101]; //每个窗口的时间总数
int allPeople, allWaitTime; //参与办理的总人数,所有人等待总时间

struct node
{
    int h,m,s;
    int processtime;
    int time; //以秒数为单位的到达时间
    node(){}
    node(int h, int m, int s):h(h),m(m),s(s){}

}q[maxn];

int cul(int h, int m, int s);
bool cmp(const node a, const node b);

int startTime = cul(8, 0, 0);
int endTime = cul(17, 0, 0);

int main()
{
    int n, k;
    scanf("%d%d",&n,&k);
    for(int i = 0; i < n; i++){
        scanf("%d:%d:%d %d",&q[i].h,&q[i].m,&q[i].s,&q[i].processtime);
        q[i].processtime *= 60;//化为秒单位
        q[i].time = cul(q[i].h, q[i].m, q[i].s);
        if(q[i].time <= endTime) allPeople++; //此处计算总人数
    }
    sort(q, q + n, cmp); //将顾客按到达时间排序
    for(int i = 0; i < k; i++) window[i] = startTime; //每个窗口8点才开始
    for(int i = 0; i < n; i++){
        int time = q[i].time;
        if(time > endTime) break;
        if(time < startTime){
            allWaitTime += startTime - time;
            time = startTime;
        }
        bool flag = false;
        int fastWindow = 0, fastTime = INF;
        for(int j = 0; j < k; j++){
            if(!flag && time >= window[j]){ //找到空闲窗口,更新空闲窗口
                window[j] = time + q[i].processtime;
                flag = true;
            }
            if(window[j] < fastTime){ //找结束最早的窗口,在当前客户到达时没有空闲窗口时再选择此窗口
                fastTime = window[j];
                fastWindow = j;
            }
        }
        if(!flag){ //如果当期没有空闲的窗口,就等结束最早的窗口fastWindow
            allWaitTime += window[fastWindow] - time; //更新等待时间
            window[fastWindow] += q[i].processtime; //更新fastwindow的值为当前客户的开始办理时间+办理时间
        }
    }

    printf("%.1f",(allWaitTime/60.0) / allPeople);
    return 0;
}

int cul(int h, int m, int s)
{
    return s + m * 60 + h * 60 * 60; //化为以秒为单位
}

bool cmp(const node a, const node b)
{
    return cul(a.h, a.m, a.s) < cul(b.h, b.m, b.s);
}

tips

  • 一做逻辑题就头疼
  • 写得稍稍有点复杂(其实还好?就是变量名比较长) 还好一次过了
  • 下次再整理下(√)
  • 主要就是将时间转化为以秒为单位,每个窗口也是一个整型值,那么当前顾客的时间大于窗口的时间,则可以在该窗口办理(当然要在所有窗口中选择时间最小的窗口,即fastWindow)

code

#include <bits/stdc++.h>
using namespace std;

const int maxn = 10010;
const int INF = 0x3f3f3f;
bool isUsing[101] = {false};
int window[101];

struct node
{
    int h,m,s;
    int processtime;
    node(){}
    node(int h, int m, int s):h(h),m(m),s(s){}

}q[maxn];

int cul(const node a)
{
    return a.s + a.m * 60 + a.h * 60 * 60; //秒
}

bool cmp(const node a, const node b)
{
    return cul(a) < cul(b);
}

int main()
{
    int n, k;
    scanf("%d%d",&n,&k);
    for(int i = 0; i < n; i++){
        scanf("%d:%d:%d %d",&q[i].h,&q[i].m,&q[i].s,&q[i].processtime);
        q[i].processtime *= 60;//划为秒单位
    }
    sort(q, q + n, cmp);
//    for(int i = 0; i < n; i++){
//        printf("%d:%d:%d\n",q[i].h,q[i].m,q[i].s);
//    }
    int allWaitTime = 0;
    int allPeople = 0;
    int startTime = cul(node(8, 0, 0));
    int endTime = cul(node(17, 0, 0));
   // printf("%d %d\n",startTime, endTime);
    for(int i = 0; i < k; i++) window[i] = startTime;
    for(int i = 0; i < n; i++){
        int time = cul(q[i]);
       // printf("%d\n",time);
        if(time > endTime) break;
        allPeople++;
        if(time < startTime){
            allWaitTime += startTime - time; //减反了
           // printf("%d\n",time - startTime);
            time = startTime; //先计算后赋值

        }
        bool FindWindow = false;
        int FastWindow = 0, fastTime = INF;
        for(int j = 0; j < k; j++){
           // printf("窗口%d=%d 客户%d=%d\n",j, window[j], i, time);
            if(!FindWindow && time >= window[j]){
                window[j] = time + q[i].processtime;
                FindWindow = true;
            }
            if(window[j] < fastTime){
                fastTime = window[j];
                FastWindow = j;
            }
        }
        if(!FindWindow){
           // printf("最快的窗口%d=%d\n",FastWindow,window[FastWindow]);
            allWaitTime += window[FastWindow] - time;
            window[FastWindow]  += q[i].processtime;
        }
    }

    printf("%.1f",(allWaitTime/60.0) / allPeople);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值