A1014 Waiting in Line (30 分)

题目内容

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 (NM+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 Ti 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 custmers 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, customer1 is served at window1 while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer will wait behind the yellow line.

At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 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

单词

transaction

英 /træn'zækʃ(ə)n; trɑːn-; -'sæk-/ 美 /træn'zækʃən/

n. 交易;事务;办理;会报,学报

题目分析

这道题我差不多前前后后做了四遍,给了我一些教训,首先是30分题一定要提前规划好程序,解决这个问题我们需要哪些数据,说出来有点尴尬,一开始这题我甚至没有用队列,另外一定要仔细读题,是17:00之后来的客人不服务,17:00之前来的客人就算是到半夜也要服务完。另外是Sorry,不是sorry!

这题其实对我来说还是很有难度的,主要是不知道怎么把时间抽象出来,每一个窗口的每一个人服务时间都不同,用什么来保存,怎么保存每个人进入窗口的时间,很多问题。

这道题我最后用了两种方法A掉,一个是用一个int变量模拟时间,哪一个人进入窗口的时间加上服务时间等于现在时间就出队,然后再插入新的人,另一个是直接把人一个一个插入窗口,如果窗口都满了,那么把所有窗口中第一个服务好的哪个人出队列。两种方法的代码我都在下面贴出,思路参考了很多大佬,尤其是一个用65行C代码A掉这题的大佬。

具体代码

从时间角度

#include<stdio.h>
#include<stdlib.h>
#define MAXW 22
#define MAXQ 11
#define MAXP 1005
#define time int

struct Queue
{
int q[MAXQ];
int length;
int front;
int rear;
};

struct Window
{
time endtime;
struct Queue queue;
};

struct Person
{
time poptime;
time dealtime;
};

struct Window window[MAXW];
struct Person person[MAXP];
int N, M, K, Q;
time nowtime = 0;

void push_person(struct Queue *queue,int m)
{
queue->q[queue->rear] = m;
queue->rear = (queue->rear + 1) % (M + 1);
queue->length++;
}

int pop_person(struct Queue *queue)
{
int tmp = queue->q[queue->front];
queue->front = (queue->front + 1) % (M + 1);
queue->length--;
return tmp;
}

int find_min()
{
int tmp = -1;
int min = 999;
for (int i = 0; i

从客户角度

#include<stdio.h>
#include<stdlib.h>
#define MAXW 22
#define MAXQ 11
#define MAXP 1005
#define time int

struct Queue
{
int q[MAXQ];
int length;
int front;
int rear;
};

struct Window
{
time firsttime;
time endtime;
struct Queue queue;
};

struct Person
{
time poptime;
time dealtime;
};

struct Window window[MAXW];
struct Person person[MAXP];
int N, M, K, Q;

void push_person(struct Queue *queue,int m)
{
queue->q[queue->rear] = m;
queue->rear = (queue->rear + 1) % (M + 1);
queue->length++;
}

int pop_person(struct Queue *queue)
{
int tmp = queue->q[queue->front];
queue->front = (queue->front + 1) % (M + 1);
queue->length--;
return tmp;
}

int find_min()
{
int push = 0;
for (int i = 0; i

参考博客

1014 Waiting in Line (30 point(s)) - C语言 PAT 甲级

PTA A1014的更多相关文章

  1. 浙大PTA - - 堆中的路径

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...

  2. 浙大PTA - - File Transfer

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...

  3. ERROR<53761> - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...

    LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Direc ...

  4. PTA中提交Java程序的一些套路

    201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...

  5. PTA分享码-Java

    主要用于Java语法练习,非竞赛类题目.   1. Java入门          959dbf0b7729daa61d379ec95fb8ddb0   2. Java基本语法   23bd8870e ...

  6. C语言第一次实验报告————PTA实验1.2.3内容

    一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...

  7. PTA题---求两个有序序列中位数所体现的思想。

    ---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A​0​​,A​1​​, ...

  8. 第十四,十五周PTA作业

    1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...

  9. 第七周PTA作业

    第一题: #include<stdio.h> int main() { ; ; ){ sum=sum+i; i++; } printf("sum = %d\n",sum ...

随机推荐

  1. neural_transfer风格迁移

    ContentLoss 首先是要定义一个内容差异损失函数,这里直接调用functional.mse_loss(input,self.target)就可以计算出其内容差异损失. 注意这里一般是定义一个网 ...

  2. Zabbix-绘制动态拓扑图基础篇

    一.实验环境 1.1 zabbix 4.0.2 二.实验需求介绍 公司希望网络拓扑能够动态反应物理接口的状态或者业务的状态,希望将网络拓扑显示到大屏上 三.Zabbix在绘制拓扑的优缺点 3.1 优点 ...

  3. 曹工杂谈:一例简单的Jar包冲突解决示例

    Jar包冲突的相关文章: 了不得,我可能发现了Jar 包冲突的秘密   一.前言 jar包冲突分多种,简单理解来说,就是同package且同名的类在多个jar包内出现,如果两个jar包在同一个clas ...

  4. Docker学习总结(三)--常用命令

    镜像相关命令 查看镜像 docker images 返回列表字段含义如下: 字段名称 字段含义 REPOSITORY 镜像名称 TAG 镜像标签 IMAGE ID 镜像 ID CREATED 镜像创建 ...

  5. ionic3.x脚手架(基于个人项目自用)

    ionic3项目开发脚手架(基于个人练习项目) 一.    基于ionic3的生产环境搭建 1.    配置安卓SDK: 安装jdk  --->  安装AndroidSDK (1)      安 ...

  6. Leetcode之回溯法专题-46. 全排列(Permutations)

    Leetcode之回溯法专题-46. 全排列(Permutations) 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3, ...

  7. C#开发学习人工智能的第一步

    前言 作为一个软件开发者,我们除了要学会复制,黏贴,还要学会调用API和优秀的开源类库. 也许,有人说C#做不了人工智能,如果你相信了,那只能说明你的思想还是狭隘的. 做不了人工智能的不是C#这种语言 ...

  8. 深入理解 Handler 消息机制

    记得很多年前的一次面试中,面试官问了这么一个问题,你在项目中一般如何实现线程切换? 他的本意应该是考察 RxJava 的使用,只是我的答案是 Handler,他也就没有再追问下去了.在早期 Andro ...

  9. 除了FastJson,你还有选择: Gson简易指南

    前言 这个周末被几个技术博主的同一篇公众号文章 fastjson又被发现漏洞,这次危害可导致服务瘫痪! 刷屏,离之前漏洞事件没多久,fastjson 又出现严重 Bug.目前项目中不少使用了 fast ...

  10. 使用Elasticsearch7 java api实现pdf全文检索

    前提:pdf的内容是文字形式而不是图片形式! 一.方法 Elasticsearch实现pdf的全文检索,原理是将pdf转换为Base64,然后提取pdf的文字内容然后将其存储起来. Elasticse ...