PAT A1014-Waiting in Line

PAT A1014-Waiting in Line

题目

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 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, 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

解题思路

N个窗口对应N个队列,队列的长度是有限制的,而黄线内的人员排队是选择最短的队列进行排队,如果队长同等那么选择窗口标号更小的队列排队。在人数充足的情况下:1、首先应该先填满窗口,让每个窗口都有人,并且在填窗口时代表有人正在窗口接受服务,所以此时要计算开始时间离开时间,2、然后填满黄线前的N个队列,3、最后不断把顾客往黄线里面填直到出现黄线前的队列是满的,所有顾客都已经进到黄线区域中即可,这个过程也涉及到窗口会服务队首的人所以也要相应地计算顾客在窗口得到服务的开始时间和结束时间。这三个过程用三个循环可以完成,必须限定循环进行的条件之一是i小于总人数。经过这个过程最终得到N个队列,N个队列互不关联,进行N次循环,把队列所有元素取出计算得到服务结束时间,最后把得到的结果打印出来即可。

题目读懂了就完成一大半了,题目理解错了,那就白瞎几个小时去找错。之前把17:00关门那个信息理解成了17:00之后就不再对客户提供服务,然后错误地写出判断条件是客户服务结束时间在17:00后的打印sorry,结果找半天错,很难受。完了现在仔细一读,其实姥姥说得也挺明确了for those customers who cannot be served before 17:00, you must output Sorry instead.——在17:00没能被服务的客户需要输出sorry。就是服务开始时间在17:00及以后的客户就不能被服务了,但是17:00之前就开始服务的客户依然是能被服务的。

仔细读题很重要。

代码

#include <stdio.h>
#include <queue>

using namespace std;
typedef struct{
    int StartTime;
    int ProcessedTime;
    int LeaveTime;
} WINDOWS;
typedef struct{
    int num;
    int P;
} CUSTOMER;

int main() {
    int Open = 8 * 60, Close = 17 * 60;
    int N, M, K, Q;
    scanf("%d %d %d %d", &N, &M, &K, &Q);
    WINDOWS windows[N];
    queue<CUSTOMER> InsideLine[N];// 每个窗口前面小于M人
    CUSTOMER customer[K];
    for(int i = 0; i < K; i++){
        scanf("%d", &customer[i].P);
        customer[i].num = i + 1;
    }
    int P[Q+1];
    for(int i = 0; i < Q; i++){
        scanf("%d", &P[i]);
    }
   
    int min, index, res[2000];
    for(int i = 0; i < N && i < K; i++){
        windows[i].LeaveTime = Open + customer[i].P;
        windows[i].ProcessedTime = customer[i].P;
        windows[i].StartTime = Open;
        InsideLine[i].push(customer[i]);
        res[customer[i].num] = windows[i].LeaveTime;
    }
    for(int i = N; i < (N * M) && i < K; i++){
        InsideLine[i%N].push(customer[i]);
    }
    for(int i = M * N; i < K; i++){
        
        min = 100000;
        index = 0;
        for(int j = 0; j < N; j++){
            if(windows[j].ProcessedTime < min){
                min = windows[j].ProcessedTime;
                index = j;
            }
        }
        for(int j = 0; j < N; j++){
            windows[j].ProcessedTime -= min;
        }
        InsideLine[index].pop();
        windows[index].ProcessedTime = InsideLine[index].front().P;
        windows[index].StartTime = windows[index].LeaveTime;
        windows[index].LeaveTime = windows[index].StartTime + windows[index].ProcessedTime;
        res[InsideLine[index].front().num] = windows[index].LeaveTime;
        InsideLine[index].push(customer[i]);
    }
    for(int j = 0; j < N; j++){
        if(InsideLine[j].empty()) continue;
        InsideLine[j].pop();
        while(!InsideLine[j].empty()){
            windows[j].ProcessedTime = InsideLine[j].front().P;
            windows[j].StartTime = windows[j].LeaveTime;
            windows[j].LeaveTime = windows[j].StartTime + windows[j].ProcessedTime;
            res[InsideLine[j].front().num] = windows[j].LeaveTime;
            InsideLine[j].pop();
        }
    }
    for(int i = 0; i < Q; i++){
        if(res[P[i]] - customer[P[i]-1].P < Close){
            printf("%02d:%02d\n", res[P[i]]/60, res[P[i]]%60);
        }else{
            printf("Sorry\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。下面详细介绍C语言的基本概念和语法。 1. 变量和数据类型 在C语言中,变量用于存储数据,数据类型用于定义变量的类型和范围。C语言支持多种数据类型,包括基本数据类型(如int、float、char等)和复合数据类型(如结构体、联合等)。 2. 运算符 C语言中常用的运算符包括算术运算符(如+、、、/等)、关系运算符(如==、!=、、=、<、<=等)、逻辑运算符(如&&、||、!等)。此外,还有位运算符(如&、|、^等)和指针运算符(如、等)。 3. 控制结构 C语言中常用的控制结构包括if语句、循环语句(如for、while等)和switch语句。通过这些控制结构,可以实现程序的分支、循环和多路选择等功能。 4. 函数 函数是C语言中用于封装代码的单元,可以实现代码的复用和模块化。C语言中定义函数使用关键字“void”或返回值类型(如int、float等),并通过“{”和“}”括起来的代码块来实现函数的功能。 5. 指针 指针是C语言中用于存储变量地址的变量。通过指针,可以实现对内存的间接访问和修改。C语言中定义指针使用星号()符号,指向数组、字符串和结构体等数据结构时,还需要注意数组名和字符串常量的特殊性质。 6. 数组和字符串 数组是C语言中用于存储同类型数据的结构,可以通过索引访问和修改数组中的元素。字符串是C语言中用于存储文本数据的特殊类型,通常以字符串常量的形式出现,用双引号("...")括起来,末尾自动添加'\0'字符。 7. 结构体和联合 结构体和联合是C语言中用于存储不同类型数据的复合数据类型。结构体由多个成员组成,每个成员可以是不同的数据类型;联合由多个变量组成,它们共用同一块内存空间。通过结构体和联合,可以实现数据的封装和抽象。 8. 文件操作 C语言中通过文件操作函数(如fopen、fclose、fread、fwrite等)实现对文件的读写操作。文件操作函数通常返回文件指针,用于表示打开的文件。通过文件指针,可以进行文件的定位、读写等操作。 总之,C语言是一种功能强大、灵活高效的编程语言,广泛应用于各种领域。掌握C语言的基本语法和数据结构,可以为编程学习和实践打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值