天天看點

1017. Queueing at Bank (25)1017. Queueing at Bank (25)

1017. Queueing at Bank (25)

時間限制 400 ms

記憶體限制 65536 kB

代碼長度限制 16000 B

判題程式 Standard 作者 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 (<=10000) - 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

這道題的代碼還是寫的比較滿意的,用了兩個優先隊列模拟排隊的過程。優先隊列預設從小到大排列,top()出的是最大元,對于裡面存的是結構體custom,重載了運算符<,對于裡面元素是int的window優先隊列,priority_queue<int, vector<int>, greater<int>>window;三個參數分别表示隊列中的元素類型,指定容器,排序規則為從大到小,中greater要#include<functional>;window中存的是目前視窗結束的時間。

#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<cstdio>
#include<functional>
using namespace std;
struct custom
{
public:
  int arrival;
  int process;
  string time;
  custom(string a, int b) :time(a), process(b) {
    arrival = Transtime(time);
  };
  
  bool operator < (const custom& c1)const
  {
    return this->arrival > c1.arrival;
  }
private:
  int Transtime(string time)
  {
    int res = 0;
    res = ((time[0] - '0') * 10 + time[1] - '0') * 3600 +
      ((time[3] - '0') * 10 + time[4] - '0') * 60 +
      (time[6] - '0') * 10 + time[7] - '0';
    return res;
  }

};





int main()
{
  
  int N, K;
  cin >> N >> K;
  priority_queue<custom>people;
  priority_queue<int, vector<int>, greater<int>>window;
  string time;
  int i, pro, tmp;
  for (i = 0; i < N; i++)
  {
    cin >> time >> pro;
    
    if (time <= "17:00:00")
    {
      custom person(time, pro);
      people.push(person);
    }
  }

  vector<int>waiting;
  
  int now = 8 * 60 * 60;
  int j = people.size();
  for (i = 0; i < j; i++)
  {
    custom firstperson = people.top();
    people.pop();
    if (i < K)
    {
      if (firstperson.arrival < now)
      {
        waiting.push_back(now - firstperson.arrival);
        window.push(now + firstperson.process*60);
      }
      else
      {
        waiting.push_back(0);
        window.push(firstperson.arrival + firstperson.process * 60);
      }

    }
    else
    {
      now = window.top();
      window.pop();
      
      if (firstperson.arrival < now)
      {
        waiting.push_back(now - firstperson.arrival);
        window.push(now + firstperson.process * 60);
      }
      else
      {
        waiting.push_back(0);
        window.push(firstperson.arrival + firstperson.process * 60);
      }
    }
  }
  double average = 0;
  vector<int>::iterator it;
  for (it = waiting.begin(); it != waiting.end(); it++)
    average += *it;
  average = average / (waiting.size() * 60);
  printf("%.1f", average);
  
  return 0;

}