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 ...
随机推荐
- amoeba安装与实现amoeba for mysql读写分离
运行环境 l CentOS6.3 l Jdk1.6.0_30 l amoeba-mysql-binary-2.2.0 l amoeba:192.168.88.17 l master1:192 ...
- Jquery AutoComplete的使用方法实例
jquery.autocomplete详解 语法: autocomplete(urlor data, [options] ) 参数: url or data:数组或者url [options]:可选项 ...
- uboot环境变量与内核MTD分区关系
uboot 与系统内核中MTD分区的关系: 分区只是内核的概念,就是说A-B地址放内核,C-D地址放文件系统,(也就是规定哪个地址区间放内核或者文件系统)等等. 1:在内核MTD中可以定义分区A~B, ...
- Spring 教程(二)
一.Spring AOP介绍 开发其实就是在不断的重构,抽象重复代码,然后进行封装.从最原始的模块化编程到面向对象编程,代码的封装越来越整齐清晰,但是依然存在重复的代码,而这些重复代码几乎都是与业务逻 ...
- [Everyday Mathematics]20150101
(1). 设 $f(x),g(x)$ 在 $[a,b]$ 上同时单调递增或单调递减, 试证: \[ (b-a)\int_a^b f(x)g(x)\mathrm{\,d}x \geq \int_a^b ...
- 判断一个面(Polygon)是不是矩形
判断一个面是不是矩形在GIS中很长用的功能,那么怎么判断一个面是不是矩形呢. 这里先要弄懂一些概念,面是什么,先看OGC标准的定义. 我的英文水平有限,(有翻译更好的请留言,如果翻译的准确将被采纳)大 ...
- CentOS上安装MySQL
1.准备RPM安装包 MySQL-server-5.6.33-1.linux_glibc2.5.x86_64 MySQL-client-5.6.33-1.linux_glibc2.5.x86_64 2 ...
- NOIP2012 国王游戏
2国王游戏 (game.cpp/c/pas) [问题描述] 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数 ...
- 5.1 CUDA atomic原子操作
和许多多线程并行问题一样,CUDA也存在互斥访问的问题,即当一个线程改变变量X,而另外一个线程在读取变量X的值,执行原子操作类似于有一个自旋锁,只有等X的变量在改变完成之后,才能执行读操作,这样可以保 ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...