(Java)1014 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.

 假设一家银行有N个窗口可供服务。窗户前有一条黄线,把等候区分成两部分。顾客排队等候的规则是:
每个窗口前面的黄线内的空间足以容纳M个客户。因此,当所有N条线路都满了时,在(NM+1)条线路之后(包括)的所有客户将不得不在黄线之后的线路中等待。
每个顾客在越过黄线时都会选择最短的队伍等候。如果有两条或两条以上的线具有相同的长度,客户将始终选择数量最小的窗口。
客户i​会带走Ti​分钟内处理他/她的交易。
前N位客户的服务时间假定为上午8:00。

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 customers 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.

现在,考虑到每个客户的处理时间,您应该告诉客户完成业务的确切时间。
例如,假设一家银行有2个窗口,每个窗口可能有2名客户在黄线内等待。有5位客户在等待,交易时间分别为1、2、6、4和3分钟。早上08:00,客户1​在1号窗口供应​while customer2​在2号窗口供应​.客户3​将在窗前等候1​和客户4​将在窗前等候2​.客户5​将在黄线后面等候。
08:01,客户1​已完成,客户5​进入窗口前面的行1​因为这条线现在似乎更短了。客户2​将于08:02离开,客户4​08:06,客户3​08:07,最后是客户5​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.

每个输入文件包含一个测试用例。每种情况以包含4个正整数的行开始:N(≤20,窗口数)、M(≤10,黄线内每行的最大容量)、K(≤1000,客户数)和Q(≤1000、客户查询数)。
下一行包含K个正整数,这是K个客户的处理时间。
最后一行包含Q个正整数,表示询问完成交易时间的客户。客户编号从1到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.

对于每个Q客户,在一行中打印他/她的交易完成时间,格式为HH:MM,其中HH在[08,17],MM在[00,59]。请注意,由于银行每天在17:00之后关闭,对于那些在17:00之前无法服务的客户,您必须输出Sorry。

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;
import java.util.Queue;

class Main{
    public static void main(String[] args)throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(bf);
        in.nextToken();
        int n = (int)in.nval;
        in.nextToken();
        int m = (int)in.nval;
        in.nextToken();
        int k = (int)in.nval;
        in.nextToken();
        int q = (int)in.nval;
        customer []c = new customer[k];
        //
        int cursor = 0;
        for(int i = 0 ; i < k;i++)
        {
            in.nextToken();
            c[i] = new customer((int)in.nval );
        }
        //定义了一个队列数组
        Queue<Integer> []queue = new LinkedList[n];
        //这里一定要创建队列对象,不能省略的
        for(int i = 0 ; i < n ;i++)
        {
            queue[i] = new LinkedList<>();
        }
        for(int time = 480 ; time < 1020 ;time ++)
        {
            //送客
            for(int i = 0 ; i < n ;i++)
            {
                //如果当前窗口有人
                if(queue[i].size() != 0)
                {
                    //正在处理的人
                    int j = queue[i].peek();
                    //如果处理好了
                    if(c[j].leave_time == time)
                    {
                        //出队
                        queue[i].poll();
                    }
                }
            }
            //入队
            //把排队看成m排,n列
            for(int j = 1 ; j <= m ;j++)
            {
                for(int i = 0 ; i < n ;i++)
                {
                    if(queue[i].size() < j)
                    {
                        if(cursor < k)
                        {
                            queue[i].offer(cursor);
                            //下一位顾客
                            cursor++;
                        }
                    }
                }
            }
            //迎客
            for(int i = 0 ;i < n ;i++)
            {
                if(queue[i].size() != 0)
                {
                    int j = queue[i].peek();
                    if(c[j].leave_time == 0)
                    {
                        c[j].leave_time = time + c[j].process_time;
                    }
                }
            }
        }
        while(q-- > 0)
        {
            in.nextToken();
            //因为顾客编号从1开始,但是我把顾客看成从0开始,所以减1
            int i = (int)in.nval - 1;
            if(c[i].leave_time == 0)
            {
                System.out.println("Sorry");
            }
            else
            {
                System.out.printf("%02d:%02d\n",c[i].leave_time / 60 ,c[i].leave_time % 60);
            }
        }
    }
}
//顾客类
class customer{
    //处理时间 和 离开时间
    int process_time,leave_time = 0;
    public customer(int process_time)
    {
        this.process_time = process_time;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值