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 }
  • 相关阅读:
    用JS + WCF打造轻量级WebPart
    提高WCF服务并发能力的简单处理办法
    利用JQuery实现更简单的Ajax跨域请求
    WCF Testing Tool(转)
    [转贴]一个有趣的布局
    [转贴].net中上传视频并将各种视频文件转换成.flv格式
    IE5,IE6,IE7,IE8的css兼容性列表[转自MSDN]
    [转贴]Castle 开发系列文章
    ie6,ie7,ff 的css兼容hack写法
    ExtJs学习笔记(23)ScriptTagProxy+XTemplate+WCF跨域取数据
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11204075.html
Copyright © 2011-2022 走看看