【PAT Practise】1014. Waiting in Line (30)

来源:互联网 发布:cad软件锁不起作用 编辑:程序博客网 时间:2024/05/21 09:45

【前言】

由于最近要考PAT,我正在紧急刷题中,于是顺便把自己写的30分题的代码贴一贴,算是做个记录吧。做一道贴一道。如果能够给困惑中的你一点帮助那就再好不过了。
本人是非计算机学院的小菜鸟,很多复杂数据结构都没学过,只会数组,代码也写的比较啰嗦低效, 不过总体思路应该是对的,以后水平高了我会再更新别的实现方法的,希望走过路过的大佬们不吝赐教呀。
关于这个PAT Practise系列,有几点得先声明:
1. 我的代码都是自己写的,如果有参考别的大牛的地方我会说明的;
2. 程序不保证百分百对,我只是测试了几个例子没发现错误,但空间复杂度和时间复杂度基本符合要求。

【原题】 https://www.patest.cn/contests/pat-a-practise/1014

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.
Customer[i] 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, 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. Customer5 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

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

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

【思路】

  1. 我是用二维数组实现了每个窗口的队列结构,行表示窗口,列表示黄线内队伍;
  2. 根据题意,用户应该按照窗口号由小到大依次排成行,即表现为编号1-K的用户按行优先顺序依次填充二维数组的每一行,同时我用了一个index索引来指示当前黄线内用户和黄线外用户的分界;
  3. 然后就是计算当前正在办理业务的用户行中谁费时最少,费时最少的用户最早离开,并记录下其离开的时间同时更新这一行用户还需要花费的时间,接着让这一窗口的队伍整体往前移动一下,并让黄线外用户站到队尾补缺。如果出现多个用户同时办完的情况,那么对每个窗口都进行同样的操作。如此循环,直到最后一个用户也办完离开;
  4. 值得注意的是本题应该不允许已经站在黄线内的用户再换窗口,这样会出现有些窗口已经没人而有些窗口还有不少用户的状况出现,其实是比较低效的,但既然题目这样也就没办法了。

【代码】

#include <iostream>#include<iomanip>using namespace std;#define INF 1000000//计算当前正在办理的用户最少的需要花费多少时间int min_time(int *a, int n, int *m){    int m_time = INF;    for (int i = 0; i < n; i++)    {        if (a[i] < m_time && a[i] != 0)         {            m_time = a[i];            *m = i;        }    }    return m_time;}int main(){    int N; //窗口数    int M; //黄线内最大容量    int K; //用户数    int Q; //请求时间的用户数    cin >> N >> M >> K >> Q;    int time_cost[1000] = {0}; //每个用户的交易需花费的处理时间    for (int i = 1; i <= K; i++) //用户编号从1-K        cin >> time_cost[i];     int id_query[1000]; //请求时间的用户编号    for (int i = 0; i < Q; i++)        cin >> id_query[i];    int id_in_yellow[10][20] = {0}; //每个窗口的黄线内排队情况, 行为窗口, 列为队伍    int index = 1; //对1-K的用户队列的索引    int line_num[20] = {0}; //每个窗口前队伍各排了几行    for (int i = 0; i < M; i++)    {        for (int j = 0; j < N; j++) { //窗口号从小到大一行行依次排队            id_in_yellow[i][j] = index;            index++;            line_num[j]++;            if (index > K)                break;        }        if (index > K) //如果黄线内就已经够容纳所有用户了, 就直接跳出循环            break;    }    int t_cus_num = K; //用来计数的    int cur_time = 0; //当前时间到8:00的时间间隔, 初始为0    int cur_time_cost[20]; //当前所有窗口前第一排用户还需要花多少时间才能办完业务    int t_cus_time[1000]; //每个用户共需要花费多少时间才能办完, 以8:00为参考时间    while (t_cus_num >= 1)    {        for (int i = 0; i < N; i++)        {            cur_time_cost[i] = time_cost[id_in_yellow[0][i]];            //cout << id_in_yellow[0][i] << " " << cur_time_cost[i] << " ";        }        int min_window; //正在办理的用户费时最少的是在哪个窗口        int m_time = min_time(cur_time_cost, N, &min_window);         cur_time += m_time;         t_cus_num--; //经过m_time时间, 费时最少的办完离去        t_cus_time[id_in_yellow[0][min_window]] = cur_time; //记录下该用户共花费了多少时间        for (int i = 0; i < N; i++) //调整当前的队伍        {            if (time_cost[id_in_yellow[0][i]] != 0) //==0意味着当前窗口没人了                time_cost[id_in_yellow[0][i]] -= m_time;            if (time_cost[id_in_yellow[0][i]] == 0)            {                if (line_num[i] > 0) { //当前窗口还有人, 就把队伍整体往前挪一挪                    for (int j = 0; j < line_num[i]; j++)                        id_in_yellow[j][i] = id_in_yellow[j + 1][i];                    line_num[i]--;                }                if (index <= K) //如果黄线外还有人, 就补个缺                {                    id_in_yellow[line_num[i]][i] = index;                    line_num[i]++;                    index++;                }            }        }    }    for (int i = 0; i < Q; i++)    {        if (t_cus_time[id_query[i]] > 540) //银行下班了, 当天办不完了            cout << "Sorry" << endl;        else            cout << setfill('0') << setw(2) << (t_cus_time[id_query[i]] / 60 + 8) << ":" << setfill('0') << setw(2) << t_cus_time[id_query[i]] % 60 << endl;    }    system("pause");    return 0;}

原创粉丝点击