• PAT A1017 Queueing at Bank


    1017 Queueing at Bank

    分数 25

    作者 CHEN, Yue

    单位 浙江大学

    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:

    1. 7 3
    2. 07:55:00 16
    3. 17:00:01 2
    4. 07:59:59 15
    5. 08:01:00 60
    6. 08:00:00 30
    7. 08:00:02 2
    8. 08:03:00 10

    Sample Output:

    8.2

     
    敢情这个题是只要在17:00:00之前来到银行,就能够得到服务,不管在此之前已经
    有了多少人了。

    用优先队列存储每个窗口完成服务的时间(注意是完成),让时间越早完成,越先出队
    进行服务下一个顾客;

    以后读入这种标准时间的题目,直接用整型读入,转化为分钟或者秒存储,比用字符串
    读入处理方便许多。

    1. /*
    2. 敢情这个题是只要在170000之前来到银行,就能够得到服务,不管在此之前已经
    3. 有了多少人了。
    4. 用优先队列存储每个窗口完成服务的时间(注意是完成),让时间越早完成,越先出队
    5. 进行服务下一个顾客;
    6. 以后读入这种标准时间的题目,直接用整型读入,转化为分钟或者秒存储,比用字符串
    7. 读入处理方便许多。
    8. */
    9. #include <iostream>
    10. #include <algorithm>
    11. #include <queue>
    12. using namespace std;
    13. typedef long long LL;
    14. struct Infor
    15. {
    16. int arr;
    17. int ser;
    18. bool operator < (const Infor &a) const
    19. {
    20. return arr < a.arr;
    21. }
    22. };
    23. const int N = 1e4+10;
    24. struct Infor a[N];
    25. int n, m;
    26. int to_sec(int h, int m, int s)
    27. {
    28. return h*3600 + m*60 + s;
    29. }
    30. void Read()
    31. {
    32. cin >> n >> m;
    33. for(int i=0; i<n; ++i)
    34. {
    35. int h, m, s, ser;
    36. scanf("%d:%d:%d %d", &h, &m, &s, &ser);
    37. if(ser > 60)
    38. ser = 60;
    39. ser *= 60;
    40. int arr = to_sec(h, m ,s);
    41. a[i] = {arr, ser};
    42. }
    43. }
    44. double deal()
    45. {
    46. double sum = 0;
    47. int cnt = 0;
    48. priority_queue<int, vector<int>, greater<int>> pq;
    49. sort(a, a+n);
    50. int ope = to_sec(8, 0, 0), cls = to_sec(17, 0, 0);
    51. //窗口服务开始,给每个窗口安排一个顾客
    52. for(int i=0; i<m && i<n; ++i)
    53. {
    54. int t = a[i].arr;
    55. if(t > cls)
    56. break;
    57. if(t < ope)
    58. {
    59. sum += ope - t;
    60. pq.push(ope+a[i].ser);
    61. }
    62. else
    63. pq.push(t + a[i].ser);
    64. ++cnt;
    65. }
    66. //窗口先完成服务先出队
    67. for(int i=m; i<n; ++i)
    68. {
    69. int u = pq.top();
    70. pq.pop();
    71. int t = a[i].arr;
    72. if(t > cls)
    73. break;
    74. if(t >= u)
    75. pq.push(t + a[i].ser);
    76. else
    77. {
    78. sum += u - t;
    79. pq.push(u + a[i].ser);
    80. }
    81. ++cnt;
    82. }
    83. if(cnt == 0)
    84. return 0.0;
    85. return sum/ 60 / cnt;
    86. }
    87. int main()
    88. {
    89. Read();
    90. printf("%.1f\n",deal());
    91. return 0;
    92. }

    用字符串读入时间的代码:

    1. /*
    2. 敢情这个题是只要在170000之前来到银行,就能够得到服务,不管在此之前已经
    3. 有了多少人了。
    4. 用优先队列存储每个窗口完成服务的时间(注意是完成),让时间越早完成,越先出队
    5. 进行服务下一个顾客;
    6. */
    7. /*
    8. #include <iostream>
    9. #include <algorithm>
    10. #include <queue>
    11. using namespace std;
    12. typedef long long LL;
    13. struct Infor
    14. {
    15. string come;
    16. int ser;
    17. bool operator < (const Infor &a) const
    18. {
    19. return come < a.come;
    20. }
    21. };
    22. const int N = 1e4+10;
    23. struct Infor a[N];
    24. int n, m;
    25. void Read()
    26. {
    27. cin >> n >> m;
    28. for(int i=0; i<n; ++i)
    29. {
    30. string come;
    31. int ser;
    32. cin >> come >> ser;
    33. if(ser > 60)
    34. ser = 60;
    35. ser *= 60;
    36. a[i] = {come, ser};
    37. }
    38. }
    39. int to_clock(string s)
    40. {
    41. int c = 0;
    42. int h = stoi(s.substr(0, 2));
    43. int m = stoi(s.substr(3, 2));
    44. int sec = stoi(s.substr(6, 2));
    45. c = h * 3600 + m * 60 + sec;
    46. return c;
    47. }
    48. double deal()
    49. {
    50. double sum = 0;
    51. int cnt = 0;
    52. priority_queue<LL, vector<LL>, greater<LL>> pq;
    53. sort(a, a+n);
    54. LL ope = to_clock("08:00:00"), cls = to_clock("17:00:00");
    55. //窗口服务开始,给每个窗口安排一个顾客
    56. for(int i=0; i<m && i<n; ++i)
    57. {
    58. LL t = to_clock(a[i].come);
    59. if(t > cls)
    60. break;
    61. if(t < ope)
    62. {
    63. sum += ope - t;
    64. pq.push(ope+a[i].ser);
    65. }
    66. else
    67. pq.push(t + a[i].ser);
    68. ++cnt;
    69. }
    70. //窗口先完成服务先出队
    71. for(int i=m; i<n; ++i)
    72. {
    73. LL u = pq.top();
    74. pq.pop();
    75. LL t = to_clock(a[i].come);
    76. if(t > cls)
    77. break;
    78. if(t >= u)
    79. pq.push(t + a[i].ser);
    80. else
    81. {
    82. sum += u - t;
    83. pq.push(u + a[i].ser);
    84. }
    85. ++cnt;
    86. }
    87. if(cnt == 0)
    88. return 0.0;
    89. return sum/ 60 / cnt;
    90. }
    91. int main()
    92. {
    93. Read();
    94. printf("%.1f\n",deal());
    95. return 0;
    96. }
    97. */

  • 相关阅读:
    Oracle导入出现问题
    Docker容器技术实战1
    【centos7中使用docker安装KLEE】
    nodejs+vue中学信息技术线上学习系统-计算机毕业设计
    ant使用import导入另外一个build文件到当前项目中
    Jemter代理服务器录制脚本,优化后形成性能测试场景
    Go语言开发小技巧&易错点100例(九)
    Scrum Master的技能和经验要求
    Invalid prop: custom validator check failed for prop “percentage
    阿里云 服务网格 ASM
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/128128584