转载

[Java] 1014. Waiting in Line (30)-PAT甲级

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

题目大意:n个窗口,每个窗口可以排队m人。有k位用户需要服务,给出了每位用户需要的minute数,所有客户在8点开始服务,如果有窗口还没排满就入队,否则就在黄线外等候。如果有某一列有一个用户走了服务完毕了,黄线外的人就进来一个。如果同时就选窗口数小的。求q个人的服务结束时间。

如果一个客户在17:00以及以后还没有开始服务(此处不是结束服务是开始17:00)就不再服务输出sorry;如果这个服务已经开始了,无论时间多长都要等他服务完毕

PS:感谢github用户 @fs19910227 提供的pull request~

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
 
public class Main {
    static class Bank {
        final static int WorkingTime = 9 * 60;
        static final SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        static Date TimeStart;
 
        static {
            try {
                TimeStart = format.parse("08:00");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
 
        final int window_quantity, window_limit, customer_quantity;
        //客户查询索引
        final int[] customer_index;
        //排队客户 id list
        final LinkedList<Integer>[] queues;
 
        Bank(int window_quantity, int window_limit, int customer_quantity) {
            this.window_quantity = window_quantity;
            this.window_limit = window_limit;
            this.customer_quantity = customer_quantity;
            this.customer_index = new int[customer_quantity + 1];
            queues = new LinkedList[window_quantity];
            for (int i = 0; i < queues.length; i++) {
                queues[i] = new LinkedList<>();
            }
        }
 
        /**
         * 根据customer id 获取排队时间
         */
        String index(String id) {
            int idi = Integer.valueOf(id);
            int time_offset = this.customer_index[idi];
            if (time_offset == 0) {
                return "Sorry";
            }
            Calendar instance = Calendar.getInstance();
            instance.setTime(TimeStart);
            instance.add(Calendar.MINUTE, time_offset);
            return format.format(instance.getTime());
        }
 
        //计算时间
        void calculate(String[] customer_cost_array) {
            //每个队列的时间片
            int[] window_time_offset = new int[window_quantity];
 
            for (int index = 0;
                 index < customer_quantity;
                 index++) {
                int customer_id = index + 1;
                int customer_cost_time = Integer.valueOf(customer_cost_array[index]);
 
                int window_index = -1;
                //容量之内,按顺序放
                if (index < window_quantity * window_limit) {
                    window_index = index % window_quantity;
                } else { //容量之外,排在等待时间最小的
                    //求队头最小值,并出队
                    int min_customer_cost = Integer.MAX_VALUE;
                    for (int i = 0; i < window_quantity; i++) {
                        Integer cost = queues[i].peekFirst();
                        if (cost != null && cost < min_customer_cost) {
                            min_customer_cost = cost;
                            window_index = i;
                        }
                    }
                    queues[window_index].removeFirst();
                }
                //能排上队的,记一下时间
                if (window_time_offset[window_index] < WorkingTime) {
                    window_time_offset[window_index] = window_time_offset[window_index] + customer_cost_time;
                    queues[window_index].add(window_time_offset[window_index]);
                    customer_index[customer_id] = window_time_offset[window_index];
                }
            }
        }
    }
 
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] split = reader.readLine().split(" ");
        Bank bank = new Bank(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(split[2]));
        String[] line = reader.readLine().split(" ");
        bank.calculate(line);
        for (String id : reader.readLine().split(" ")) {
            System.out.println(bank.index(id));
        }
    }
}
[Java] 1014. Waiting in Line (30)-PAT甲级

(随着网站访问量的激增,服务器配置只得一再升级以维持网站不“404 Not Found”,所以网站的维护费用也在不断上涨……(目前的阿里云服务器ECS+云数据库RDS+域名购买+七牛云的费用是2200元/年),为了能不放弃该网站,所以我又把打赏链接放上来啦~所有打赏金额都会被记账并投入博客维护中,感谢厚爱,多多关照~)

原文  https://www.liuchuo.net/archives/4983
正文到此结束
Loading...