Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (N**M+1)st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • Customeri will take T**i minutes to have his/her transaction processed.
  • The first N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 customers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, custome**r1 is served at windo**w1 while custome**r2 is served at windo**w2. Custome**r3 will wait in front of windo**w1 and custome**r4 will wait in front of windo**w2. Custome**r5 will wait behind the yellow line.

At 08:01, custome**r1 is done and custome**r5 enters the line in front of windo**w1 since that line seems shorter now. Custome**r2 will leave at 08:02, custome**r4 at 08:06, custome**r3 at 08:07, and finally custome**r5 at 08:10.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (≤20, number of windows), M (≤10, the maximum capacity of each line inside the yellow line), K (≤1000, number of customers), and Q (≤1000, number of customer queries).

The next line contains K positive integers, which are the processing time of the K customers.

The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.

Output Specification:

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output Sorry instead.

Sample Input:

2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7

Sample Output:

08:07
08:06
08:10
17:00
Sorry

思路

这题有个坑,如果有人在17:00之前到了窗口前,就必须完成此人的业务。即,17:00前开始服务的,就必须服务完。

我自己写了一组样例,方便大家调试自己的程序

INPUT
2 1 4 4
600 539 400 200
1 2 3 4
OUPUT
18:00
16:59
23:39
Sorry

代码

#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <limits.h>
using namespace std;
int N, M, K, Q;
int t[1000 + 10]; //记录每个人咨询的时间
vector<int> myv[30]; // 模拟每个窗口前的人的标号 1-K
int pos = 1; //要插入到黄线内的人所在的位置
int num = 0; //结束的人数
int now = 0; // 当前的时间
bool cmp1(vector<int> a, vector<int> b){
return a.size() < b.size();
} int main() {
cin >> N >> M >> K >> Q;
for(int i = 1; i <= K; i++){
cin >> t[i];
}
while(num < K){
// 进入黄线
vector<int> *min_t = min_element(myv + 1, myv + N + 1, cmp1);
while(pos <= K && (*min_t).size() != M){
int vi = min_t - myv;
myv[vi].push_back(pos);
//cout << vi << " " << pos << " " << now << endl;
min_t = min_element(myv + 1, myv + N + 1, cmp1);
pos++;
}
// 咨询业务
int flag = true;
while(flag){
if(num == K) break;
now++;
for(int i = 1; i <= N; i++){
if(myv[i].size()){
t[myv[i][0]]--;
if(t[myv[i][0]] == 0){
flag = false; //有人完成,需要插入人
t[myv[i][0]] = now;
num++;
myv[i].erase(myv[i].begin(), myv[i].begin() + 1);
//防止样例中,在540(17:00)刚刚结束时,再从第1变成第0
if(now == 540){
for(int z = 0; z < myv[i].size(); z++){
t[myv[i][z]] = INT_MAX;
}
}
}
}
}
if(now >= 540) break;
}
if(now >= 540){
//把每个队伍的第一个咨询结束,不是第一个的无法完成咨询业务(INT_MAX)
for(int i = 1; i <= N; i++){
for(int j = 0; j < myv[i].size(); j++){
if(j == 0 && t[myv[i][0]] != INT_MAX){
t[myv[i][0]] += now;
}
else{
t[myv[i][j]] = INT_MAX;
}
}
}
// 还未插入到黄线内部的人,也无法完成咨询业务
while(pos <= K){
t[pos++] = INT_MAX;
}
break;
}
}
for(int i = 1; i <= Q; i++){
int q = 0;
cin >> q;
if(t[q] == INT_MAX) cout << "Sorry" << endl;
else printf("%02d:%02d\n", 8 + t[q] / 60, t[q] % 60);
}
return 0;
}

PAT 1014 Waiting in Line (模拟)的更多相关文章

  1. PAT 1014 Waiting in Line (模拟)

    1014. Waiting in Line (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  2. PAT 1014. Waiting in Line

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

  3. 1014. Waiting in Line (模拟)

    n个窗口就有n个队列,模拟这n个队列就可以了.需要注意的是,一个人在选择排队窗口的时候,他会选择排队人数最少的窗口,如果存在多个窗口排队的人数相同,那么他会选择编码最小的窗口. Note that s ...

  4. PAT 1014 Waiting in Line (30分) 一个简单的思路

    这题写了有一点时间,最开始想着优化一下时间,用优先队列去做,但是发现有锅,因为忽略了队的长度. 然后思考过后,觉得用时间线来模拟最好做,先把窗口前的队列填满,这样保证了队列的长度是统一的,这样的话如果 ...

  5. PAT 甲级 1014 Waiting in Line (30 分)(queue的使用,模拟题,有个大坑)

    1014 Waiting in Line (30 分)   Suppose a bank has N windows open for service. There is a yellow line ...

  6. PAT甲级1014. Waiting in Line

    PAT甲级1014. Waiting in Line 题意: 假设银行有N个窗口可以开放服务.窗前有一条黄线,将等候区分为两部分.客户要排队的规则是: 每个窗口前面的黄线内的空间足以包含与M个客户的一 ...

  7. 1014 Waiting in Line (30分)

    1014 Waiting in Line (30分)   Suppose a bank has N windows open for service. There is a yellow line i ...

  8. PAT A 1014. Waiting in Line (30)【队列模拟】

    题目:https://www.patest.cn/contests/pat-a-practise/1014 思路: 直接模拟类的题. 线内的各个窗口各为一个队,线外的为一个,按时间模拟出队.入队. 注 ...

  9. PTA (Advanced Level) 1014 Waiting in Line

    Waiting in Line Suppose a bank has N windows open for service. There is a yellow line in front of th ...

随机推荐

  1. centos python版本升级到3.x

    Linux(CentOS)下将Python的版本升级为3.6.2的方法 1.检查确认系统的相关信息 查看内核版本[root@zstest1 ~]# cat /etc/redhat-release Ce ...

  2. python:复制文件及文件夹

    #!/usr/bin/python# -*- coding:utf-8 -*- import shutil #shutil.copy(文件1,文件2)#将源内容复制到目标文件中.d.txt不存在则创建 ...

  3. C++ 实例练习-替换原生数组

    C++ 实例练习-替换原生数组 main.cpp #include <stdio.h> #include "intarray.h" int main(int argc, ...

  4. django 搭建一个投票类网站(三)

    之前修改index的视图的代码,工作原理是先试用loader方法加载视图,然后HTTPResponse方法初始化一个HTTPResponse对象并返回给浏览器.对于很多django视图来说,他们的工作 ...

  5. LocalDate和LocalTime的用法介绍

    原文:LocalDate和LocalTime的用法介绍 在JAVA中,常用的处理日期和时间的类主要有Date,Calendar,而在JDK1.8中,新增了两个处理日期和时间的类,一个是LocalDat ...

  6. C++ windows线程操作(转)

    参考 1._beginthreadex创建线程 DWORD m_dwMSGTID; // 线程ID HANDLE m_hMSG; // 线程句柄 m_hMSG = (HANDLE)_beginthre ...

  7. JS高级---逆推继承看原型

    逆推继承看原型 function F1(age) { this.age = age; } function F2(age) { this.age = age; } F2.prototype = new ...

  8. SQLServer使用链接服务器远程查询

    --创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' exec sp_addlinkedsrvlogi ...

  9. PHP windoews调用OpenOffice实现word/ppt转PDF

    1.安装免费的openOffice软件 2.需要JDK支持 3.安装完openOffice后,在开始--运行中输入Dcomcnfg打开组件服务.在组件服务—计算机—我的电脑—DCOMP配置中 4. 先 ...

  10. Docker - 最近的踩到的一些坑

    概述 最近学习 docker 遇到的 坑 1. dockerfile: 安装命令 概述 安装命令 坑 选项参数里, 一定要 带 -y 不带的话, 基本会阻塞构建 2. 其他: 处理问题, 一定不能慌 ...