PAT甲级1017. Queueing at Bank

题意:

假设一家银行有K台开放服务。窗前有一条黄线,将等候区分为两部分。所有的客户都必须在黄线后面排队,直到他/她轮到服务,并有一个可用的窗口。

假设一个客户不能占用1个小时以上的窗口。

现在考虑到每个客户的到达时间T和处理时间P,您应该告诉所有客户的平均等待时间。

输入:

每个输入文件包含一个测试用例。对于每种情况,

第一行包含2个数字:N(<= 10000) - 客户总数,K(<= 100) - 窗口数。然后N行跟随,每个包含2次:HH:MM:SS - 到达时间,P - 客户的处理时间(以分钟为单位)。这里HH在[00,23]的范围内,MM和SS都在[00,59]中。

假设没有两个客户同时到达。

请注意,银行营业时间为08:00至17:00。任何人提前到达,必须等到08:00,任何人来得太晚(17:00:01之间或之后)将不会服务也不会计入平均水平。

输出:

对于每个测试用例,

在一行中打印所有客户的平均等待时间,在几分钟内精确到小数点后1位。

思路:

模拟排队。我用的优先队列。跟前面一道题有点像。

注意点:

  • 只要在17:00或者17:00前到都可以被服务
  • 都在17:00后到达的话,注意0为除数的情况

ac代码:

C++

// pat1017.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<unordered_map>
#include<iomanip> using namespace std; struct custom
{
double arriving;
int process;
double ending;
int window;
custom() : arriving(0), process(0), ending(0), window(0) {};
}; vector<custom> cus; struct mycmp {
bool operator()(custom& a, custom& b) const
{
return a.ending> b.ending;
}
}; static bool cmp(custom a, custom b)
{
return a.arriving < b.arriving;
} double counttime(string a, string b)
{
double ahour, bhour, aminute, bminute, asecond, bsecond;
ahour = stoi(a.substr(0, 2));
aminute = stoi(a.substr(3, 2));
asecond = stoi(a.substr(6, 2));
bhour = stoi(b.substr(0, 2));
bminute = stoi(b.substr(3, 2));
bsecond = stoi(b.substr(6, 2)); double res = 0;
res = (ahour - bhour) * 60 + (aminute - bminute) + (asecond - bsecond) / 60;
return res;
} int main()
{
int n, m;
string arrive;
priority_queue<custom, vector<custom>, mycmp> q; cin >> n >> m;
for (int i = 0; i < n; i++)
{
custom c;
cin >> arrive >> c.process;
c.arriving = counttime(arrive, "08:00:00");
if (c.process > 60) c.process = 60;
cus.push_back(c);
} sort(cus.begin(), cus.end(),cmp); for (int i = 0; i < m; i++)
{
custom c;
//c.window = i;
c.ending = 0;
q.push(c);
} int len = cus.size();
int pos = 0;
double wait = 0;
while (pos < len && cus[pos].arriving <= 540)
{
custom t = q.top();
q.pop();
//if (t.ending > 540) break; //只要来了排在队伍就可以被服务
if (cus[pos].arriving >= t.ending)
{
wait += 0;
cus[pos].ending = cus[pos].arriving + cus[pos].process;
}
else
{
wait += t.ending - cus[pos].arriving;
cus[pos].ending = t.ending + cus[pos].process;
}
//cus[pos].window = t.window;
q.push(cus[pos]);
pos++;
} double res;
if (pos == 0) res = 0; //防止所有客户17:00后到,0为除数
else res = wait / pos;
cout << fixed << setprecision(1) << res << endl;
//for (int i = 0; i < cus.size(); i++)
// cout << cus[i].arriving << " " << cus[i].process << endl;
return 0;
}

PAT甲级1017. Queueing at Bank的更多相关文章

  1. PAT 甲级 1017 Queueing at Bank (25 分)(模拟题,有点思维小技巧,第二次做才理清思路)

    1017 Queueing at Bank (25 分)   Suppose a bank has K windows open for service. There is a yellow line ...

  2. PAT 甲级 1017 Queueing at Bank

    https://pintia.cn/problem-sets/994805342720868352/problems/994805491530579968 Suppose a bank has K w ...

  3. PAT甲级——A1017 Queueing at Bank

    Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...

  4. PAT 1017 Queueing at Bank[一般]

    1017 Queueing at Bank (25)(25 分)提问 Suppose a bank has K windows open for service. There is a yellow ...

  5. PAT 1017 Queueing at Bank (模拟)

    1017. Queueing at Bank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Supp ...

  6. PAT甲级1017题解——模拟排序

    题目分析: 本题我第一次尝试去做的时候用的是优先队列,但是效率不仅代码量很大,而且还有测试样例过不去,很显然没有找到一个好的数据结构来解决这道题目(随着逐渐的刷PAT甲级的题会发现有时选择一个好的解题 ...

  7. 【PAT甲级】1017 Queueing at Bank (25 分)

    题意: 输入两个正整数N,K(N<=10000,k<=100)分别表示用户的数量以及银行柜台的数量,接下来N行输入一个字符串(格式为HH:MM:SS)和一个正整数,分别表示一位用户到达银行 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. [转载]理解Tomcat的Classpath-常见问题以及如何解决

    摘自: http://www.linuxidc.com/Linux/2011-08/41684.htm 在很多Apache Tomcat用户论坛,一个问题经常被提出,那就是如何配置Tomcat的cla ...

  2. Spring mvc知识点总结——面试篇

    一.MVC思想MVC(Model-View-Controller)三元组的概念:1.Model(模型):数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或JavaBean组件(包含数 ...

  3. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. 使用 PHP 构建的 Web 应用如何避免 XSS 攻击

    本文首先简单介绍开发测试人员如何对 Web 应用进行 XSS 漏洞测试,如何借助工具绕过客户端 JavaScript 校验输入恶意数据:然后针对使用 PHP 语言构建的 Web 站点,从在输出端对动态 ...

  5. C语言小程序之整除

    看到有人要求用C语言写这样一个小程序,就拿来温习一下 需求:输出从1到2015这2015个自然数中,能被4或5整除,但不能被30整除的数,并计算有多少个数.   #include<stdio.h ...

  6. oracle去掉字段值中的某些字符串

    我想去掉字段值中的“_” select replace(fdisplayname,'_','') from SHENZHENJM1222.B replace 第一个参数:字段/值,第二个参数时替换字符 ...

  7. PHP实现插入排序

    插入排序思想: 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法. 它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描, 找到相应位置并插入.插入排序 ...

  8. 禁止viewpager不可滚动

    import android.content.Context; import android.support.v4.view.ViewPager; import android.util.Attrib ...

  9. scrollview和viewpager滑动冲突

    import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; i ...

  10. React Native升级方法——升级到最新版本0.59

    React Native最近有大动作,于2019年3月12日发布新版本0.59.主要有两点值得升级:支持React Hooks:升级了JavaScriptCore,使Android性能有大幅提升.据用 ...