1014 waiting in line 思路?

PAT 1014 Waiting in line

我是过不了了, 可能思路某个地方有问题, 仅此记录, 有时间再看

题目大意:

  1. 刚开始所有人在一条黄线外. 等待银行开业, 共有 k 个人,每个人的业务需要窗口处理ki分钟, 黄线内可以容纳 m个人, 共有 n个窗口;
    即: n个队列, 每个队列容量为m, 共有k个人等待进入队列, 进入队列后, 每个人出队时需要ki min;
  2. 银行在8:00 开业, 这个时候, 黄线外等待的人依次进入黄线
  3. 进入规则:
    1. 选择最短的队伍, 选择之后不能改变
    2. 若有多个相等最短的队列, 选择标号最小的队列排队,
    3. 若所有队列都满了, 则继续在黄线外等待, 直到有队列空出来, 若两个队列同时空出来, 选择标号最小的进入

思路(我是真不知道哪里有问题):

  • 数据结构:
    wins[n]一个队列数组来模拟窗口的队列
    time[k]来记录每个人 剩余 的处理时间
    wait 记录有几个人进入了黄线
    ans[i]记录第i个人完成时所花费的时间
  • 算法
    • 进入队列:
      第一次进入时: 依次进入(每个队列进一个人反复) 直到所有队列满;

        i = 0
        while(有队列未满){
        	wait++ 进入队列wins[i];
        	i = (i+1) % n;
        }		
      
    • 模拟出队:
      用cost_time 记录时间, 每循环一次cost_time++, time[所有队列第一个人]-- 循环直到time[]所有为0, 即所有处理完成;

        while(!allfinished(time)){
        	while(有队列未满){
        		找到最短对列pos, 若有多个最短, 找标号最小的
        		wait++ 进入队列wins[pos]
        	}
        	cost_time++;
        	for(i = 0; i < n; i++){  // 给每个队列的第一个人的处理剩余时间-1, 若处理完成则出队
        		if( --time[wins[i]] == 0){
        			tmp = wins[i].popFirst();
        			// 记录这个人出队时间
        			ans[tmp] = cost_time;
        		}	
        	}
        }
      
    • 打印结果:
      check[i]为需要查询的人

        Print(ans[check[0]]);
        for (i = 1; i < q; i++)
        {
            printf("\n");
            Print(ans[check[i]]);
        }
      
        void Print(int min)
        {
            if (min > 540)
            {
                printf("Sorry");
                return;
            }
            int h = min / 60 + 8;
            int m = min % 60;
        
            printf("%02d:%02d", h, m);
        }
      

代码在最后

题目


Suppose a bank has NNN 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 MMM customers. Hence when all the NNN lines are full, all the customers after (and including) the (NM+1)(NM+1)(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.
  • CustomeriCustomer_iCustomeri will take TiT_iTi minutes to have his/her transaction processed.
  • The first NNN 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, customer1customer_1customer1 is served at window1window_1window1 while customer2customer_2customer2 is served at window2window_2window2. Customer3Customer_3Customer3 will wait in front of window1window_1window1 and customer4customer_4customer4 will wait in front of window2window_2window2. Customer5Customer_5Customer5 will wait behind the yellow line.

At 08:01, customer1customer_1customer1 is done and customer5customer_5customer5 enters the line in front of window1window_1window1 since that line seems shorter now. Customer2Customer_2Customer2 will leave at 08:02, customer4customer_4customer4 at 08:06, customer3customer_3customer3 at 08:07, and finally customer5customer_5customer5 at 08:10.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: NNN (≤20\le 2020, number of windows), MMM (≤10\le 1010, the maximum capacity of each line inside the yellow line), KKK (≤1000\le 10001000, number of customers), and QQQ (≤1000\le 10001000, number of customer queries).

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

The last line contains QQQ 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 KKK.

Output Specification:

For each of the QQQ 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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB

c代码:

#include <stdio.h>
#include <stdlib.h>

int cost_time = 0;
int n, m, q;
int ans[1001];
typedef struct Que
{
    int que[21];
    int start, end;
} Que;

int allFinished(int time[], int k)
{
    int i;
    for (i = 0; i < k; i++)
    {
        if (time[i] > 0)
            return 0;
    }
    return 1;
}

void push(Que *que, int wait)
{
    que->que[que->end] = wait;
    que->end = (que->end + 1) % m;
}

// 将弹出的元素的时间置为0, 并
int pop(Que *que, int check[], int cur)
{
    int i;
    for (i = 0; i < q; i++)
    {
        if (check[i] == que->que[que->start])
        {
            ans[check[i]] = cur;
            break;
        }
        // printf("&& %d %d", cur, que->que[que->start]);
    }
    que->que[que->start] = -1;
    que->start = (que->start + 1) % m;

    return 1;
}

void Print(int min)
{
    if (min > 540)
    {
        printf("Sorry");
        return;
    }
    int h = min / 60 + 8;
    int m = min % 60;

    printf("%02d:%02d", h, m);
}

int full(Que wins[])
{
    int i, j;
    for (j = 0; j < n; j++)
    {
        for (i = 0; i < m; i++)
        {
            if (wins[j].que[i] == -1)
            {
                return 0;
            }
        }
    }
    return 1;
}

int main(void)
{
    int k;
    scanf("%d%d%d%d", &n, &m, &k, &q);
    int time[k];
    int check[q];
    int i, j;
    Que wins[n];
    for (j = 0; j < n; j++)
    {
        for (i = 0; i < m; i++)
        {
            wins[j].que[i] = -1;
        }
        wins[j].start = wins[j].end = 0;
    }
    for (i = 0; i < k; i++)
    {
        scanf("%d", &time[i]);
    }
    for (i = 0; i < q; i++)
    {
        scanf("%d", &check[i]);
        check[i]--;
    }

    int wait = 0;

    while (!allFinished(time, k))
    {
        while (!full(wins) && wait < k)
        {
            int shortest = 50;
            int pos = -1;
            for (i = 0; i < n; i++)
            {
                //(wins[i].end != wins[i].start) || (wins[i].que[wins[i].start] == -1)

                if (((wins[i].end != wins[i].start) || (wins[i].que[wins[i].start] == -1)) && wait < k) //还可以入队列
                {
                    if (((wins[i].end - wins[i].start) % m) < shortest)
                    {
                        pos = i;
                        shortest = ((wins[i].end - wins[i].start) % m);
                    }
                }
            }
            if (pos >= 0)
            {
                push(&wins[pos], wait);
                wait++;
            }
        }
        cost_time++;
        for (i = 0; i < n; i++)
        {
            if (--time[wins[i].que[wins[i].start]] == 0)
            {
                pop(&wins[i], check, cost_time);
            }
        }
    }
    Print(ans[check[0]]);
    for (i = 1; i < q; i++)
    {
        printf("\n");
        Print(ans[check[i]]);
    }

    system("pause");
    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_40978095/article/details/83270570