1014 Waiting in Line (30 分)

本文介绍了一个银行排队系统的模拟算法,该算法考虑了多个服务窗口、等待区容量限制及客户选择最短队伍的行为。通过C++和C语言实现,解决了如何预测客户完成业务的确切时间问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

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

C++:

/*
 @Date    : 2018-03-17 13:17:44
 @Author  : 酸饺子 (changzheng300@foxmail.com)
 @Link    : https://github.com/SourDumplings
 @Version : $Id$
*/

/*
https://www.patest.cn/contests/pat-a-practise/1014
注意是17:00之前开始服务的顾客输出结束时间,
否则说Sorry
 */

#include <iostream>
#include <cstdio>
#include <string>
#include <queue>

using namespace std;

struct Customer
{
    int serveTime, needTime;
};

struct Window
{
    int resTime = 0;
    queue<int> wQ;
};

static const int MAXN = 21, MAXK = 1001, INF = INT32_MAX;
static int N, M, K, Q;
static Customer C[MAXK];
static Window W[MAXN];

static int nowTime = 0;

int find_avail_window()
{
    int minResTime = INF, availW = 0;
    for (unsigned w = 0; w < N; ++w)
    {
        if (W[w].resTime < minResTime)
        {
            minResTime = W[w].resTime;
            availW = w;
        }
    }
    for (unsigned w = 0; w < N; ++w)
    {
        if (W[w].resTime != INF)
        {
            W[w].resTime -= minResTime;
        }
    }
    nowTime += minResTime;
    return availW;
}

void print_time(int minutes)
{
    int h = minutes / 60;
    int minR = minutes - h * 60;
    printf("%02d:%02d\n", h + 8, minR);
    return;
}

int main(int argc, char const *argv[])
{
    scanf("%d %d %d %d", &N, &M, &K, &Q);
    for (unsigned i = 0; i < K; ++i)
    {
        scanf("%d", &C[i].needTime);
    }
    int outC, w = 0;
    for (outC = 0; outC < K && outC < N * M; ++outC)
    {
        W[w].wQ.push(outC);
        w = (w + 1) % N;
    }
    int servedNum = 0;
    for (unsigned w = 0; w < N && !W[w].wQ.empty(); ++w)
    {
        W[w].resTime = C[W[w].wQ.front()].needTime;
        C[W[w].wQ.front()].serveTime = 0;
        W[w].wQ.pop();
        ++servedNum;
    }
    while (servedNum < K)
    {
        int w = find_avail_window();
        if (W[w].wQ.empty() && outC == K)
        {
            W[w].resTime = INF;
            continue;
        }
        int nextC = W[w].wQ.front(); W[w].wQ.pop();
        C[nextC].serveTime = nowTime;
        // printf("%d is served at %d\n", nextC+1, nowTime);
        W[w].resTime = C[nextC].needTime;
        if (outC < K)
        {
            // printf("at time %d, %d goes to wW %d\n", nowTime, outC+1, w);
            W[w].wQ.push(outC++);
        }
        ++servedNum;
    }
    for (unsigned i = 0; i < Q; ++i)
    {
        int c;
        scanf("%d", &c);
        if (C[c-1].serveTime >= 540)
        {
            printf("Sorry\n");
        }
        else
            print_time(C[c-1].serveTime + C[c-1].needTime);
    }
    return 0;
}

C:

/*
 @Date    : 2017-11-29 11:26:22
 @Author  : 酸饺子 (changzheng300@foxmail.com)
 @Link    : https://github.com/SourDumplings
 @Version : $Id$
*/

/*
https://www.patest.cn/contests/pat-a-practise/1014
 */

#include <stdio.h>
#include <stdlib.h>

#define MAXK 1000
#define MAXN 20
#define ERROR -1
#define INFINITY 99999

int N, M, K, Q_q;

typedef struct QUEUE *Queue;
struct QUEUE
{
    int maxsize;
    int front, rear;
    int *data; // 存储的是每个人的下标
    int size;
};

int IsFull(Queue Q)
{
    return Q->front == (Q->rear + 1) % Q->maxsize;
}

int IsEmpty(Queue Q)
{
    return Q->rear == Q->front;
}

void AddQ(Queue Q, int X)
{
    if (!IsFull(Q))
    {
        Q->rear = (Q->rear + 1) % Q->maxsize;
        Q->data[Q->rear] = X;
        Q->size++;
    }
    return;
}

int DeleteQ(Queue Q)
{
    if (!IsEmpty(Q))
    {
        Q->front = (Q->front + 1) % Q->maxsize;
        Q->size--;
        return Q->data[Q->front];
    }
    else
    {
        return ERROR;
    }
}

void OutPutTime(int min)
{
    int hour = min / 60 + 8;
    min %= 60;
    printf("%02d:%02d\n", hour, min);
    return;
}

int processtime[MAXK];
int queries[MAXK];
int w[MAXN]; // 记录该窗口还有几分钟完事
int nowtime = 0;
int finishtime[MAXK];

int FindAvailWin(void)
{
    int i, minretime = INFINITY, min_i;
    for (i = 0; i < N; i++)
    {
        if (w[i] < minretime)
        {
            minretime = w[i];
            min_i = i;
        }
    }
    if (minretime == INFINITY)
    {
        // 证明所有窗口前的队伍都空,服务结束了
        min_i = ERROR;
    }
    else
    {
        nowtime += minretime;
        for (i = 0; i < N; i++)
        {
            if (w[i] != INFINITY)
            {
                w[i] -= minretime;
                if (w[i] < 0)
                {
                    w[i] = 0;
                }
            }
        }
    }
    return min_i;
}

void WaitInBank(Queue Q, Queue WQ[])
{
    int i;
    int winavail;
    int nextcustomer;
    int leavecustomer;
    int qavail = 0;
    int count = 0;
    for (i = 0; i < N && !IsEmpty(Q); i++)
    {
        // 初始化先让前N个人到窗口办事
        nextcustomer = DeleteQ(Q);
        w[i] = processtime[nextcustomer];
        AddQ(WQ[i], nextcustomer);
    }
    while (!IsEmpty(Q))
    {
        // 再把每个窗口的队伍填满,最多填N*M - N个
        if (count == N * M - N)
        {
            break;
        }
        nextcustomer = DeleteQ(Q);
        AddQ(WQ[qavail], nextcustomer);
        qavail = (qavail + 1) % N;
        count++;
    }
    while (1)
    {
        // 找到最早办完事的窗口
        winavail = FindAvailWin();
        if (winavail == ERROR)
        {
            // 服务结束了
            break;
        }
        else if (IsEmpty(WQ[winavail]))
        {
            w[winavail] = INFINITY; // 代表该窗口前的队伍空了,需要换下一个窗口
            continue;
        }
        leavecustomer = DeleteQ(WQ[winavail]);
        finishtime[leavecustomer] = nowtime;
        if (!IsEmpty(Q))
        {
            // 在黄线外的下一个顾客入黄线内的队伍
            nextcustomer = DeleteQ(Q);
            AddQ(WQ[winavail], nextcustomer);
        }
        // 最早办完事的窗口的最前面的人接受服务
        int f;
        f = (WQ[winavail]->front + 1) % WQ[winavail]->maxsize;
        nextcustomer = WQ[winavail]->data[f];
        w[winavail] = processtime[nextcustomer];
    }
    return;
}

int main()
{
    scanf("%d %d %d %d", &N, &M, &K, &Q_q);
    int i;
    for (i = 0; i < K; i++)
    {
        scanf("%d", &processtime[i]);
    }
    int q;
    for (i = 0; i < Q_q; i++)
    {
        scanf("%d", &q);
        queries[i] = q - 1;
    }

    Queue Q, WQ[N]; // 在黄线外的人的队列和在各个窗口黄线里的人的队列
    Q = (Queue)malloc(sizeof(struct QUEUE));
    Q->data = (int *)malloc((K+1) * sizeof(int));
    Q->maxsize = K + 1;
    Q->front = Q->rear = 0;
    for (i = 0; i < K; i++)
    {
        AddQ(Q, i);
    }
    for (i = 0; i < N; i++)
    {
        WQ[i] = (Queue)malloc(sizeof(struct QUEUE));
        WQ[i]->maxsize = M + 1;
        WQ[i]->front = WQ[i]->rear = 0;
        WQ[i]->size = 0;
        WQ[i]->data = (int *)malloc((M+1) * sizeof(int));
    }

    WaitInBank(Q, WQ);

    for (i = 0; i < Q_q; i++)
    {
        if (finishtime[queries[i]] - processtime[queries[i]] < 540)
        {
            OutPutTime(finishtime[queries[i]]);
        }
        else
        {
            printf("Sorry\n");
        }
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值