[PAT] A1017 Queueing at Bank

【思路】

1:将所有满足条件的(到来时间点在17点之前的)客户放入结构体中,结构体的长度就是需要服务的客户的个数。结构体按照到达时间排序。

2:wend数组表示某个窗口的结束时间,一开始所有窗口的值都为8点整。每一个客户到来的时候,选择最早结束时间的窗口。如果最早结束时间比客户到得还早,那么他一来就能被服务,更新wend的值;如果最早结束时间比他晚,他需要等待,累加等待的时间,然后更新wend的值。

【坑】

测试点5:需要服务的客户数validn可能为0。此时它不能作为除数,所以要分开写。

测试点1:validn可能比窗口数k还小,我先用了一个循环是处理在窗口还没满的情况的,循环次数应该是k和validn当中较小的值,一开始没注意,直接写为了k。

测试点4:题目说“It is assumed that no window can be occupied by a single customer for more than 1 hour.”一开始也没注意,但是后来发现测试数据并没有用上这个条件,不明白。拒绝服务会出错,啥都不处理或者是需要的时间大于60分钟的只服务60分钟之后不再处理了都可以过这个测试数据。个人觉得他的意思可能是如果服务时间大于60分钟,就只服务60分钟,之后也不管了。感觉是题目不严谨。

【tips】

1:输入时间后直接转化为与00:00:00相差(或与08:00:00?但是有人到早的,不想出现负数嘿嘿)的分钟数(double类型),包括需要的时间和窗口处理结束时间wend都统一这么表示,就比较方便。

2:只需要计算平均等待时间,客户谁是谁不重要,但是要注意需要的服务时间need和到达时间arrive要跟着走,所以用结构体存了。

3:表达式计算中,加减连接的每一项都是double,才会返回double。如下函数,这些具体数字必须手动写成小数的形式。

double TransTime(string a)

{

    return ((a[0] - '0') * 10 + a[1] - '0') * 60.0 + ((a[3] - '0') * 10 + a[4] - '0')*1.0 + ((a[6] - '0') * 10 + a[7] - '0') / 60.0;

}

【AC代码】

#include<iostream>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;
#define N 10000
#define K 100
struct people {
    double arrive;
    double need;
};
bool cmp(people a, people b){
    return a.arrive < b.arrive;
}
double TransTime(string a){
    return ((a[0] - '0') * 10 + a[1] - '0') * 60.0 + ((a[3] - '0') * 10 + a[4] - '0')*1.0 + ((a[6] - '0') * 10 + a[7] - '0') / 60.0;
}
int FindMin(double a[], int k){
    double min = a[0];
    int imin = 0;
    for (int i = 1; i < k; i++)
        if (min > a[i]){
            min = a[i];
            imin = i;
        }
    return imin;
}

int main(){
    int n, k;
    cin >> n >> k;
    int validn = n;
    int i;
    string time;
    double wend[K];
    people client[N];
    for (i = 0; i < validn; i++){
        cin >> time;
        cin >> client[i].need;
        //if (client[i].need > 60)client[i].need = 60;
        if (time > "17:00:00"){
            i--;
            validn--;
        }
        else
            client[i].arrive = TransTime(time);
    }
    sort(client, client + validn, cmp);
    /*for (i = 0; i < validn; i++)
        cout << (double)client[i].arrive << " " << client[i].need << endl;
    */
   
    double wait = 0.00;
    for (i = 0; i < (k>validn?validn:k); i++){
        if (client[i].arrive < 480){ //8点前到了
            wait += 480 - client[i].arrive;
            wend[i] = 480 + client[i].need;
        }
        else
            wend[i] = client[i].arrive + client[i].need;
    }
    while (i < validn){
        int kmin = FindMin(wend, k);
        double wendmin = wend[kmin];
        if (client[i].arrive < wendmin){ //有空位之前到了
            wait += wendmin - client[i].arrive;
            wend[kmin] += client[i].need;
        }
        else
            wend[kmin] = client[i].arrive + client[i].need;
        i++;
    }
    if (validn == 0)
        cout << "0.0";
    else printf("%.1f", wait / validn);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Queueing theory is a mathematical study of waiting lines or queues that arise in various real-life scenarios, such as customer service, traffic congestion, hospital emergency rooms, and telecommunications networks. Basic queueing theory involves the following concepts: 1. Arrival Process: This is the process of customers arriving at the queue. The arrival process can be modeled using different distributions, such as Poisson or exponential. 2. Service Process: This is the process of serving customers in the queue. The service process can also be modeled using different distributions, such as Poisson or exponential. 3. Queue Length: This is the number of customers waiting in the queue at any given time. 4. Queue Occupancy: This is the proportion of time that the server is busy serving customers. 5. System Capacity: This is the maximum number of customers that the system can handle at any given time. 6. Utilization: This is the proportion of time that the server is busy serving customers compared to the total time. 7. Waiting Time: This is the time that a customer spends waiting in the queue before being served. 8. Service Time: This is the time that a customer spends being served by the server. 9. Queueing Models: There are different queueing models that can be used to analyze queueing systems, such as the M/M/1 model, M/M/c model, M/G/1 model, and M/D/1 model. 10. Performance Measures: Different performance measures can be used to evaluate queueing systems, such as average waiting time, average queue length, and system throughput.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值