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 ...
随机推荐
- Java [leetcode 30]Substring with Concatenation of All Words
题目描述: You are given a string, s, and a list of words, words, that are all of the same length. Find a ...
- hdu 1712 ACboy needs your help
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Android 自动换行流式布局的RadioGroup
效果图 用法 使用FlowRadioGroup代替RadioGroup 代码 import android.content.Context; import android.util.Attribute ...
- 【Java基础之容器】Iterator
Iterator: ->所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象 ->Iterator对象称作迭代器,用以方便的实 ...
- SqlSugar轻量ORM
蓝灯软件数据股份有限公司项目,代码开源. SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法. 学习列表 0.功能更新 1.SqlSuga ...
- java jvm学习笔记十(策略和保护域)
欢迎转载请说明出处:http://blog.csdn.net/yfqnihao/article/details/8271415 前面一节,我们做了一个简单的实验,来说明什么是策略文件,在文章的最后,也 ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- 单元测试之获取Spring下所有Bean
单元测试中,针对接口的测试是必须的,但是如何非常方便的获取Spring注册的Bean呢? 如果可以获取所有的Bean,这样就可以将这个方法放到基类中,方便后面所有单元测试类的使用,具体实现如下: im ...
- Zabbix探索:LDAP的认证方式
这两天部署了Zabbix测试环境,终于用Puppet部署完成了.总是存在一些小问题,如服务不起动啦之类的. LDAP验证方式配置 刚刚配置Zabbix的用户管理,使用LDAP方式认证. 比较惊喜的是L ...
- 富文本HTML编辑器UEditor
Baidu百度开源富文本HTML编辑器UEditor JS代码网 发表于: 2013-10-30 分类:HTML编辑器 点击:2133 UEditor是由百度web前端研发部开发所见即所得富文本H ...