PAT 1017
1017. Queueing at Bank (25)
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
又是比较繁琐的考察逻辑的题目,虽然通过了,感觉写的不是很好,也不想改进了,就先这样吧。
代码
#include <stdio.h>
#include <algorithm>
using namespace std; typedef struct Customer{
int arrivingTime,processTime;
}Customer; Customer customer[];
int reminderTime[];
int serveringNum[];
int cmp(const Customer&,const Customer&); int main()
{
int N,K,needServerNum;
int hour,min,sec,pTime,time;
int i;
const int earliestTime = * * ;
const int latestTime = * * ;
int yellowLineNum;
while(scanf("%d%d",&N,&K) != EOF){
needServerNum = ;
for(i=;i<N;++i){
scanf("%d:%d:%d %d",&hour,&min,&sec,&pTime);
time = hour * * + min * + sec;
if(time > latestTime)
continue;
customer[needServerNum].arrivingTime = time;
customer[needServerNum++].processTime = pTime * ;
}
sort(customer,customer+needServerNum,cmp);
for(i=;i<needServerNum;++i)
reminderTime[i] = customer[i].processTime;
yellowLineNum = ;
double totalServerTime = 0.0;
int serveredNum = ;
for(i=;i<K;++i){
serveringNum[i] = -;
if(yellowLineNum < needServerNum && customer[yellowLineNum].arrivingTime <= earliestTime){
serveringNum[i] = yellowLineNum;
totalServerTime += earliestTime - customer[yellowLineNum++].arrivingTime;
++serveredNum;
}
}
int nowTime = earliestTime;
while(serveredNum < needServerNum){
int minWin = -;
int num,hasWin = ;
for(i=;i<K;++i){
num = serveringNum[i];
if(num != - && (minWin == - || minWin > reminderTime[num]))
minWin = reminderTime[num];
else if(num == -){
hasWin = ;
}
}
if((minWin == -) || (hasWin && (nowTime + minWin) > customer[yellowLineNum].arrivingTime)){
minWin = customer[yellowLineNum].arrivingTime - nowTime;
nowTime = customer[yellowLineNum].arrivingTime;
}
else
nowTime += minWin;
for(i=;i<K;++i){
int num = serveringNum[i];
if(num != -){
reminderTime[num] -= minWin;
if(reminderTime[num] == ){
if(yellowLineNum < needServerNum && customer[yellowLineNum].arrivingTime <= nowTime){
serveringNum[i] = yellowLineNum;
totalServerTime += nowTime - customer[yellowLineNum++].arrivingTime;
++serveredNum;
}
else{
serveringNum[i] = -;
}
}
}
else if(yellowLineNum < needServerNum && customer[yellowLineNum].arrivingTime <= nowTime){
serveringNum[i] = yellowLineNum;
totalServerTime += nowTime - customer[yellowLineNum++].arrivingTime;
++serveredNum;
}
}
}
printf("%.1lf\n",totalServerTime / (needServerNum * ));
}
return ;
} int cmp(const Customer &a,const Customer &b)
{
return a.arrivingTime < b.arrivingTime;
}
PAT 1017的更多相关文章
- PAT 1017 A除以B(20)(代码)
1017 A除以B(20 分) 本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立. 输入格式: 输入在一 ...
- PAT 1017 Queueing at Bank[一般]
1017 Queueing at Bank (25)(25 分)提问 Suppose a bank has K windows open for service. There is a yellow ...
- pat——1017. Queueing at Bank (java中Map用法)
由PAT1017例题展开: Suppose a bank has K windows open for service. There is a yellow line in front of the ...
- PAT 1017 Queueing at Bank (模拟)
1017. Queueing at Bank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Supp ...
- PAT 1017. A除以B (20)
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- PAT 1017. Queueing at Bank
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...
- PAT 1017 A除以B
https://pintia.cn/problem-sets/994805260223102976/problems/994805305181847552 本题要求计算A/B,其中A是不超过1000位 ...
- PAT——1017. A除以B
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- PAT 1017 Queueing at Bank (25) (坑题)
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...
随机推荐
- 【转】10分钟搭建NDK的Android开发环境
原文网址:http://blog.csdn.net/u012176591/article/details/23018913 作者:金良(golden1314521@gmail.com) csdn博客: ...
- Android学习系列(15)--App列表之游标ListView(索引ListView)
游标ListView,提供索引标签,使用户能够快速定位列表项. 也可以叫索引ListView,有的人称也为Tweaked ListView,可能更形象些吧. 一看图啥都懂了: 1. ...
- [面试题] BloomFilter 无序40亿不重复 uint 整数, 给予任意的数,求是否在这40亿之中 + 无序数组中找2个相同的值
一道百度面试题(待解中) 具体:给40亿个不重复的unsigned int的整数,没排过序的,然后再给几个数,如何快速判断这几个数是否在那40亿个数当中? 分析下,首先应该是空间复杂度(40亿uint ...
- Gen_server行为分析与实践
1.简介 Gen_server实现了通用服务器client_server原理,几个不同的客户端去分享服务端管理的资源(如图),gen_server提供标准的接口函数和包含追踪功能以及错误报告来实现通用 ...
- windows和linux间互传文件
方法1:Xshell传输文件 用rz,sz命令在xshell传输文件 很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz 首先你的Ubuntu需要安 ...
- 那些跟钱有关的事儿 z
这是两段朴实的创业笔记,作者是王信文,2009年南京大学本科毕业,2009年9月到2013年3月在腾讯上海的互动娱乐部门工作,2013年3月到现在和几个前同事一起创立了莉莉丝游戏(手游刀塔传奇是他们后 ...
- 《GettingThingsDone》--GTD学习笔记(一)-GTD理论
利用春节假期阅读了<Getting Things Done>一书,下文整理了下阅读过程中做的读书笔记和心得. ==GTD理论== 一. 目的: 1. 收集需要处理的事情把它置于一个脱离大脑 ...
- JSFのAjaxタグのoneventでbegin/complete/successを使う
PrimeFacesに慣れてしまって.通常のHTMLタグでの記述方法がわからなかったりする点があった…ので.メモ. Ajaxでリクエスト送信のタイミングやレスポンスが戻るタイミングに何らか(JavaS ...
- PHP算法之二分查找和顺序查找
一.二分查找 (数组里查找某个元素) /** * 二分查找 (数组里查找某个元素) * $k为要查找的关键字(注:待查找的数组元素为奇数个)$low为查找范围的最小键值,$high为查找范围的最大键值 ...
- 宿主进程 vshost.exe
Hosting Process (vshost.exe) 宿主进程是VS的一个特性.可以提高调试的性能,可以进行部分信任调试(partial trust debugging),可以进行设计时表达式计算 ...