PAT-1017 Queueing at Bank

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 (≤104) - 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

思路

  • 将时间转换成秒数,每个窗口存放当前用户完成业务的时间;例如08:00:05完成,那么存放的就是08:00:05-00:00:00的秒数;
  • 对于在8点前来的人,遍历,计算他们等待的秒数,然后设置他们的arriving_time为:08:00:00,这样后面减去arriving_time就不会重复计算了;
  • 需要注意窗口数比顾客数多的情况;
  • emm,把数组开在全局,开在main出现段错误了。。(针对牛客网的测试,PAT不会…)
  • 对于顾客,对他们进行排序,然后把窗口填满,对于这些顾客,是不用等待的(早于8点的等待时间已经算了),此时分为两种情况:顾客到了,发现窗口全部沾满;另外一种是窗口空出来了,顾客还没到。前者显然需要等待,后者直接进行业务的办理。因此前者等待时间为:窗口完成业务时间减去顾客到达时间,同时更新窗口时间为窗口完全业务的时间+顾客处理业务的时间;后者不用计算等待时间,窗口时间更新为用户到达时间+用户完成业务的时间。(注意:窗口存放的时间是秒,用户处理业务是分,注意单位)

Code

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

int time_convert_seconds(string time)
{
    
    
    int res = 0;
    int hour = (time[0] - '0') * 10 + time[1] - '0';
    int mins = (time[3] - '0') * 10 + time[4] - '0';
    int seconds = (time[6] - '0') * 10 + time[7] - '0';
    res = hour * 3600 + mins * 60 + seconds;
    return res;
}
struct customer
{
    
    
    string arriving_time; // 到达时间
    int processing_time;  // 办理业务的分钟数
};
bool cmp(customer a, customer b)
{
    
    
    return a.arriving_time < b.arriving_time;
}
// vector<customer>customers(10005);
customer customers[10005];    // 全局数组,不然牛客网会出现段错误
int main()
{
    
    
    int n, k;
    cin >> n >> k;
    int sum = 0;
    double total_mins = 0.0;
    vector<int> window(k, 0); // 每个窗口用户结束业务后的时间,从8点到此时所有seconds
    // vector<customer> customers(n);
    
    for (int i = 0; i < n; i++)
        cin >> customers[i].arriving_time >> customers[i].processing_time;
    // sort(customers.begin(), customers.end(), cmp);
    sort(customers, customers+n,cmp);
    for (int i = 0; i < n; i++)  // 计算所有用户在7点前到的需要等待时间
    {
    
    
        if (customers[i].arriving_time <= "08:00:00")
        {
    
    
            total_mins += time_convert_seconds("08:00:00") - time_convert_seconds(customers[i].arriving_time);
            customers[i].arriving_time = "08:00:00";
        }
        if (customers[i].arriving_time <= "17:00:00")
            sum ++;
    }
    int index = 0;
    for (int i = 0; i < k; i++)
    {
    
    
        if (customers[i].arriving_time <= "17:00:00")
        {
    
    
            if (customers[i].arriving_time <= "08:00:00")
                window[i] = time_convert_seconds("08:00:00") + customers[i].processing_time * 60;
            else
                window[i] = time_convert_seconds(customers[i].arriving_time) + customers[i].processing_time * 60;
            index++;
        }
    }
    // 需要注意窗口数大于顾客数的情况
    while (index >=k && index < n && customers[index].arriving_time <="17:00:00")
    {
    
    
        if (customers[index].arriving_time <= "17:00:00")
        {
    
    
            int min_time = window[0];
            int min_time_index = 0;
            for (int i = 1; i < k; i++)
            {
    
    
                if (min_time > window[i])
                {
    
    
                    min_time = window[i];
                    min_time_index = i;
                }
            }
            // 用户来银行发现窗口全部有人,所以得等待
            if (time_convert_seconds(customers[index].arriving_time) <= window[min_time_index])
            {
    
    
                total_mins += window[min_time_index] - time_convert_seconds(customers[index].arriving_time);
                window[min_time_index] += customers[index].processing_time * 60;
            }
            else
            {
    
     // 窗口此时没人,用户过一会直接办理业务,无需等待
                window[min_time_index] = time_convert_seconds(customers[index].arriving_time) + customers[index].processing_time * 60;
            }
        }
        index++;
    }

    cout << fixed << setprecision(1) << total_mins / (sum * 60) << endl;

    return 0;
}

大佬的Code

思路最奇特的就是将所有窗口的值作为08:00:00,然后push到优先队列中。这样顾客与top的值进行比较,也不用担心在8点前到的顾客的等待时间没有算上。Orz

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <queue>
using namespace std;

struct node
{
    
    
    int arriving_time;
    int processing_time;
};
node customers[10005];

int time_convert_seconds(string time)
{
    
    
    int res = 0;
    int hour = (time[0] - '0') * 10 + time[1] - '0';
    int mins = (time[3] - '0') * 10 + time[4] - '0';
    int seconds = (time[6] - '0') * 10 + time[7] - '0';
    res = hour * 3600 + mins * 60 + seconds;
    return res;
}
bool cmp(node a, node b)
{
    
    
    return a.arriving_time < b.arriving_time;
}
int main()
{
    
    
    int n, k;
    cin >> n >> k;
    int cnt = 0; // cnt服务人数,total为总的等待时间
    double total = 0.0;
    string time;
    int processing_time;
    for (int i = 1; i <= n; i++)
    {
    
    
        cin >> time >> processing_time;
        if (time_convert_seconds(time) <= 61200)
        {
    
     // 超过17点不服务了
            customers[++cnt].processing_time = processing_time * 60;
            customers[cnt].arriving_time = time_convert_seconds(time);
        }
    }
    sort(customers + 1, customers + 1 + cnt, cmp);
    priority_queue<int, vector<int>, greater<int>> q;
    for (int i = 1; i <= k; i++)
        q.push(28800);
    for (int i = 1; i <= cnt; i++)
    {
    
    
        if (customers[i].arriving_time < q.top())
        {
    
     // 说明顾客到了,但是窗口没有空闲,所以等待
            int temp = q.top();
            total += q.top() - customers[i].arriving_time;
            q.pop();
            q.push(temp + customers[i].processing_time);
        }
        else
        {
    
    
            q.pop();
            q.push(customers[i].arriving_time + customers[i].processing_time);
        }
    }
    if (cnt != 0)
    {
    
    
        cout << fixed << setprecision(1) << total / (60 * cnt) << endl;
    }
    else
    {
    
    
        cout << "0.0" << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42100456/article/details/108956757