码迷,mamicode.com
首页 > 其他好文 > 详细

PTA A1014

时间:2019-09-21 00:46:20      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:方法   ace   alt   ext   details   open   做了   等于   ndt   

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 < N; i++)
    {
        if (window[i].queue.length == 0)
            return i;
        if (window[i].queue.length != M && window[i].queue.length < min)
        {
            tmp = i;
            min = window[i].queue.length;
        }
    }
    return tmp;
}

int is_full()
{
    for (int i = 0; i < N; i++)
        if (window[i].queue.length != M)
            return 0;
    return 1;
}

int is_empty()
{
    for (int i = 0; i < N; i++)
        if (window[i].queue.length != 0)
            return 0;
    return 1;
}

void count_time()
{
    int p = 1;
    while (p <= K)
    {
        if (!is_empty())
        {
            for (int i = 0; i < N; i++)
            {
                if (person[window[i].queue.q[window[i].queue.front]].poptime + person[window[i].queue.q[window[i].queue.front]].dealtime == nowtime)
                    pop_person(&window[i].queue);
            }
        }
        while (!is_full() && p <= K)
        {
            int min = find_min();
            push_person(&window[min].queue, p);
            person[p].poptime = window[min].endtime;
            window[min].endtime += person[p].dealtime;
            p++;
        }
        nowtime++;
    }
}

int main(void)
{
    scanf("%d %d %d %d", &N, &M, &K, &Q);
    for (int i = 1; i <= K; i++)
        scanf("%d", &person[i].dealtime);
    count_time();
    for (int i = 0; i < Q; i++)
    {
        int q;
        scanf("%d", &q);
        if (person[q].poptime < 540)
            printf("%02d:%02d\n",8+(person[q].poptime+person[q].dealtime)/60, (person[q].poptime + person[q].dealtime) % 60);
        else
            printf("Sorry\n");
    }
    system("pause");
}

从客户角度

#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 < N; i++)
    {
        if (window[i].firsttime < window[push].firsttime)
            push = i;
    }
    return push;
}

void count_time()
{
    int p = 1;
    while (p <= K)
    {
        int push;
        if (p <= M * N)
            push = (p - 1) % N;
        else
            push = find_min();
        if (window[push].queue.length == M)
        {
            pop_person(&window[push].queue);
            window[push].firsttime += person[window[push].queue.q[window[push].queue.front]].dealtime;
        }
        push_person(&window[push].queue, p);
        if (window[push].queue.length == 1)
            window[push].firsttime += person[window[push].queue.q[window[push].queue.front]].dealtime;
        person[p].poptime = window[push].endtime;
        window[push].endtime += person[p].dealtime;
        p++;
    }
}

int main(void)
{
    scanf("%d %d %d %d", &N, &M, &K, &Q);
    for (int i = 1; i <= K; i++)
        scanf("%d", &person[i].dealtime);
    count_time();
    for (int i = 0; i < Q; i++)
    {
        int q;
        scanf("%d", &q);
        if (person[q].poptime < 540)
            printf("%02d:%02d\n",8+(person[q].poptime+person[q].dealtime)/60, (person[q].poptime + person[q].dealtime) % 60);
        else
            printf("sorry\n");
    }
    system("pause");
}

参考博客

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

PTA A1014

标签:方法   ace   alt   ext   details   open   做了   等于   ndt   

原文地址:https://www.cnblogs.com/z-y-k/p/11561143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!