zoukankan      html  css  js  c++  java
  • PAT甲级——A1017 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 (≤) - the total number of customers, and K (≤) - 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

    将时间化为秒更便于计算
     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     8 
     9 struct Person
    10 {
    11     int arriveTime, serverTime, popTime;
    12 };
    13 
    14 int N, K;
    15 
    16 int main()
    17 {
    18     cin >> N >> K;
    19     vector<Person*>window(K);//就排一个人办业务,不需要用队列
    20     vector<Person*>data;
    21     double waitTime = 0.0;
    22     for (int i = 0; i < N; ++i)
    23     {
    24         int hh, mm, ss, tt;
    25         Person* one = new Person;        
    26         scanf("%d:%d:%d %d", &hh, &mm, &ss, &tt);
    27         one->arriveTime = hh * 3600 + mm * 60 + ss;
    28         one->serverTime = tt * 60;
    29         if (one->arriveTime <= (17 * 3600))//下班了,不算
    30             data.push_back(one);
    31     }
    32     sort(data.begin(), data.end(), [](Person* a, Person* b) {return a->arriveTime < b->arriveTime; });
    33     for (int i = 0; i < data.size(); ++i)
    34     {
    35         if (i < K)
    36         {
    37             if (data[i]->arriveTime < (8 * 3600))//来早了
    38                 waitTime += 8 * 3600 - data[i]->arriveTime; 
    39             data[i]->popTime = data[i]->serverTime + (data[i]->arriveTime < (8 * 3600) ? 8 * 3600 : data[i]->arriveTime);
    40             window[i%K] = data[i];
    41         }
    42         else
    43         {
    44             int index = 0, minTime = window[0]->popTime;
    45             for (int j = 0; j < K; ++j)
    46             {
    47                 if (minTime > window[j]->popTime)
    48                 {
    49                     index = j;
    50                     minTime = window[j]->popTime;
    51                 }
    52             }
    53             waitTime += data[i]->arriveTime < minTime ? (minTime - data[i]->arriveTime) : 0;//早到就等待
    54             data[i]->popTime = data[i]->serverTime + (data[i]->arriveTime < minTime ? minTime : data[i]->arriveTime);
    55             window[index] = data[i];
    56         }
    57     }
    58     if (data.size() == 0)
    59         printf("0.0
    ");
    60     else
    61         printf("%0.1f
    ", (waitTime / (60 * data.size())));
    62     return 0;
    63 }
  • 相关阅读:
    iOS UI(布局)约束是什么?view1.attr1 = view2.attr2 * multiplier + constant
    编程范式-声明式编程
    平庸的投资人和优秀的投资人差在哪儿
    编程语言的发展趋势及未来方向
    再读:编程语言的发展趋势及未来方向
    编程语言的发展趋势:声明式动态并发
    命令式编程与声明式编程
    声明式(编程)语言是解释型语言
    DSL-领域特定语言(英语:domain-specific language、DSL)
    声明式编程
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11204075.html
Copyright © 2011-2022 走看看