Zhejiang University PAT Class A 1014 Waiting in Line

Posted by unreel on Sat, 05 Oct 2019 08:58:59 +0200

A simple simulation problem. The Yellow Line used to have N teams, each with a capacity of M. After the Yellow Line, we can see that there is only one team. Now we can fill up the N teams in front of the Yellow Line (if we can), whenever someone leaves, the people behind the Yellow Line will fill up, and if many people leave at the same time, they will fill up according to the number of the team from small to large.

I defined a structure called Customer to represent each queue member, store numbers and end times. The data structure I used was vector < queue < Customer > to simulate N queues. First, the queue is empty, and the double loops fill the whole queue in turn. If the best one can be finished, the next one can not be finished, which will simulate the filling of the remaining personnel. The idea is very common. Write code directly according to the meaning of the title.

But we should pay attention to the following questions:

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.

Sorry is exported if business is not started before 17:00, which is not the end time. And before 17:00, it does not include 17:00.

This process can directly write a conditional judgment, without affecting the overall queue, without considering special circumstances or changing the queuing mechanism.

I previously defined an array of res[1001] to store the end time of each person. The subscript is the person number. By subtracting the end time of the current queue member from the time he needs to do business, he can get the start time. If it is greater than or equal to 17:00, set res[i] to - 1, and if it is less than 17:00, assign res[i] to the end time. Then the team entry operation is carried out as usual.

The specific code is as follows. I just started to practice writing code, so my code may not be concise, some of the ideas are complex or not universal, forgive me.

#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;

struct Customer
{
    int index;
    int dtime;
};
vector<queue<Customer>> lines;
int res[1001];

int main()
{
    int N, M, K, Q;
    scanf("%d%d%d%d", &N, &M, &K, &Q);
    for (int i = 0; i < N; i++)
    {
        queue<Customer> line;
        lines.push_back(line);
    }
    for (int i = 0; i <= N; i++)
    {
        res[i] = -1;
    }
    for (int i = 0; i < M; i++)
    {
        int loop = true;
        for (int j = 1; j <= N; j++)
        {
            int time = 0;
            scanf("%d", &time);
            Customer c;
            c.index = i*N + j;
            if (lines[j - 1].empty() == false)
            {
                c.dtime = lines[j - 1].back().dtime + time;
            }
            else
            {
                c.dtime = time;
            }
            lines[j - 1].push(c);
            if (c.dtime - time >= 540)
            {
                res[c.index] = -1;
            }
            else
            {
                res[c.index] = c.dtime;
            }
            if (c.index == K)
            {
                loop = false;
                break;
            }
        }
        if (loop == false)
        {
            break;
        }
    }
    for (int i = 0; i < K - M*N; i++)
    {
        int bestline = 0;
        int besttime = 10000000;
        for (int j = 0; j < N; j++)
        {
            if (lines[j].front().dtime < besttime)
            {
                besttime = lines[j].front().dtime;
                bestline = j;
            }
        }
        int time = 0;
        scanf("%d", &time);
        Customer c;
        c.index = M*N + i + 1;
        c.dtime = lines[bestline].back().dtime + time;
        lines[bestline].pop();
        lines[bestline].push(c);
        if (c.dtime - time >= 540)
        {
            res[c.index] = -1;
        }
        else
        {
            res[c.index] = c.dtime;
        }
    }
    for (int i = 0; i < Q; i++)
    {
        int p = 0;
        scanf("%d", &p);
        if (res[p] == -1)
        {
            printf("Sorry\n");
            continue;
        }
        printf("%02d:%02d\n", 8 + res[p] / 60, res[p] % 60);
    }
    return 0;
}

Personally, it is really important to brush up such questions, especially if they are not proficient in English, many places will not be so straightforward and even need to use life experience. In short, code is easy, but robust code is difficult, and sometimes it takes more time to pass the last two use cases.

I hope that the PAT test can add some points to the visit to Zhejiang University.

Topics: less