PAT1017. Queueing at Bank (25)
来源:互联网 发布:桌面规划软件 编辑:程序博客网 时间:2024/05/22 17:16
题目如下:
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 307:55:00 1617:00:01 207:59:59 1508:01:00 6008:00:00 3008:00:02 208:03:00 10Sample Output:
8.2
#include <iostream>#include <vector>#include <string>#include <queue>#include <algorithm>#include <iomanip>#define START 28800#define END 61200using namespace std;struct Customer {int arriveTime;int processTime;int waitingTime;int startTime;int endTime;};int N, K;vector<Customer> customers;queue<Customer> windows[101];double total = 0;int countNum = 0;bool compara(Customer a, Customer b){if (a.arriveTime < b.arriveTime)return true;elsereturn false;}int findEmptyWindow(){for (int i = 0;i < K;i++){if (windows[i].size() == 0)return i;}return -1;}int findEarliestWindow(){int min = -1;for (int i = 0;i < K;i++){if (windows[i].size() != 0){min = i;break;}}if (min == -1)return min;for (int i = 0;i < K;i++){if (windows[i].size() != 0 && windows[min].front().endTime > windows[i].front().endTime){min = i;}}return min;}void enterWindow(Customer c){if (c.arriveTime <= END){int index = findEarliestWindow();if (index != -1)//不是全空{if (c.arriveTime >= windows[index].front().endTime){total += (double)windows[index].front().waitingTime / 60;countNum += 1;c.startTime = c.arriveTime;c.waitingTime = 0;c.endTime = c.startTime + c.processTime;windows[index].pop();windows[index].push(c);}else{int i = findEmptyWindow();if (i != -1)//有空窗口{if (c.arriveTime < START){c.startTime = START;}else{c.startTime = c.arriveTime;}c.waitingTime = c.startTime - c.arriveTime;c.endTime = c.startTime + c.processTime;windows[i].push(c);return;}else{total += (double)windows[index].front().waitingTime / 60;countNum += 1;c.startTime = windows[index].front().endTime;c.waitingTime = c.startTime - c.arriveTime;c.endTime = c.startTime + c.processTime;windows[index].pop();windows[index].push(c);}}}else //全空{if (c.arriveTime < START)c.startTime = START;elsec.startTime = c.arriveTime;c.waitingTime = c.startTime - c.arriveTime;c.endTime = c.startTime + c.processTime;windows[0].push(c);}}}void outWindow(){for (int i = 0;i < K;i++){if (windows[i].size() != 0){total += (double)windows[i].front().waitingTime / 60;countNum += 1;windows[i].pop();}}}int main(){cin >> N >> K;string s;Customer temp;for (int i = 0;i < N;i++){cin >> s >> temp.processTime;temp.arriveTime = atoi(s.substr(0, 2).c_str()) * 60 * 60 + atoi(s.substr(3, 5).c_str()) * 60 + atoi(s.substr(6, 8).c_str());temp.processTime *= 60;customers.push_back(temp);}sort(customers.begin(),customers.end(),compara);for (int i = 0;i < N;i++){enterWindow(customers[i]);}outWindow();cout << fixed << setprecision(1) << total / countNum << endl;}
一次通过全部用例。
- PAT1017. Queueing at Bank (25)
- PAT1017. Queueing at Bank
- PAT1017. Queueing at Bank
- pat1017 Queueing at Bank
- PAT1017. Queueing at Bank
- PAT1017. Queueing at Bank
- Queueing at Bank (25)
- Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 1017. Queueing at Bank (25)
- 文件夹已经存在的情况下调用mkdir函数
- DAO模板类【了解】
- swing的高级布局
- 通用的 cmakeLists.txt 文件
- OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解
- PAT1017. Queueing at Bank (25)
- int转String的三种方法性能分析
- Download ERS-1 precise (DGM-E04) orbits
- npm 安装错误及处理方法
- Handler源码解析基于ThreadLocal来进行分析
- 1.jquery源码分析-无 new 构造
- BZOJ4868 [Shoi2017]期末考试
- CAN总线基础知识(一)
- 关于TbSchedule任务调度管理框架的整合部署