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 ...
随机推荐
- Hibernate事务与并发问题处理(乐观锁与悲观锁)
目录 一.数据库事务的定义 二.数据库事务并发可能带来的问题 三.数据库事务隔离级别 四.使用Hibernate设置数据库隔离级别 五.使用悲观锁解决事务并发问题 六.使用乐观锁解决事务并发问题 Hi ...
- OK335xS can't reset with reboot
/*********************************************************************** * OK335xS can't reset * 说明: ...
- ubuntu下eclipse打开win下的代码中文出现乱码
问题出现的原因:因为windows下默认的编码是GBK,在ubuntu下是UTF-8所以,所以在windows下的注释,在ubuntu下就变成了乱码. 解决的方案: 1) eclipse->w ...
- POJ 1661 Help Jimmy
/*96655 's source code for M Memory: 8604 KB Time: 63 MS Language: G++ Result: Accepted */ #include& ...
- JVM的GC机制及JVM的调优方法
内存管理和垃圾回收是JVM非常关键的点,对Java性能的剖析而言,了解内存管理和垃圾回收的基本策略非常重要. 1.在程序运行过程当中,会创建大量的对象,这些对象,大部分是短周期的对象,小部分是长周期的 ...
- Js 与 TextArea
当给一个js变量赋值一个有换行的值得时候,就会报错: <!DOCTYPE HTML> <html> <head> <script src="http ...
- cocos2d-x3.0+Eclipse配置说明
假如我们已经装了JavaJDK.Cygwin,也解压了2013-08-27之后最新的AndroidSDK,其实最新的AndroidSDK已经集成了eclipse,eclipse里面已经配置好了Andr ...
- HDU1890 Robotic Sort Splay tree反转,删除
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...
- 大公司最喜欢问的Java集合类面试题
看了一些所谓大公司的JAVA面试问题,发现对于JAVA集合类的使用都比较看重似的,而自己在这方面还真的是所真甚少,抽空也学习学习吧. java.util包中包含了一系列重要的集合类,而对于集合类,主要 ...
- FreeModbus Slave 改进的eMbPoll()【worldsing 笔记】
eMbPoll()的作用是FreeMod协议通信过程中不断查询事件对列有无完速数据桢,并进行地址和CRD验证,最后运行和回复主机. 为了减小代码尺寸对eMbPoll进行改进: 原版: 1: 2: e ...