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 ...
随机推荐
- HTML.ActionLink 和Html.Action和 Url.Action 的区别
1. html.ActionLink生成一个<a href=".."></a>标记..例如:@Html.ActionLink(“链接文本”.“someact ...
- Erlang入门(五)——补遗
暂时搞不到<Programming Erlang>,最近就一直在看Erlang自带的例子和Reference Manual.基础语法方面有一些过去遗漏或者没有注意的,断断续续仅记于此. 1 ...
- Servlet3.0学习总结(一)——使用注解标注Servlet
一.Servlet3.0介绍 Servlet3.0是Java EE6规范的一部分,Servlet3.0提供了注解(annotation),使得不再需要在web.xml文件中进行Servlet的部署描述 ...
- 4、什么构成了我们Android应用程序?(七大件)
一.应用程序四大组件 [Activity] Activity是Android应用程序的一个界面,可以通过这个界面查看联系人,打电话戒玩游戏. b. 一个应用程序通常包含多个Activity. c. A ...
- lightoj 1014
判断到根号n即可,另外使用dfs输出,不需要另开数组再排序. #include<cmath> #include<cstdio> int P, L, len, cnt; void ...
- C#发送简单的HTTP POST请求给传统的ASP网页。
设计思路 创建HTTPWebRequest类的一个实例,设置这个对象的Method属性为"POST",ContentType属性为"application/x-/www- ...
- Unicode中跟汉字相关的一些内容的总结陈词
UniHan 这几天琢磨着怎么方便的给汉字注音, 因为要知道具体哪些Unicode是给汉字用的, 就读了读Unicode的官方文档. 目前unicode已经发展到了7.0. 不看不知道, 发现Unic ...
- Linux新手必看:浅谈如何学习linux
本文在Creative Commons许可证下发布 一.起步 首先,应该为自己创造一个学习linux的环境--在电脑上装一个linux或unix问题1:版本的选择 北美用redhat,欧洲用SuSE, ...
- 树-伸展树(Splay Tree)
伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...
- 解析XML最快速的方式
采用提JAXB技术 1.根据xml生成xsd 执行:java -jar trang.jar a.xml a.xsd 2.根据java的xjc来生成实现类 执行:xjc a.xsd 注:在执行前最好把数 ...