PAT 甲级 1026 JAVA

本文详细解析了PAT甲级1026JAVA题目的代码实现,包括玩家到达时间、游戏时间、是否为VIP等信息的处理,以及如何分配普通玩家和VIP玩家到不同的桌子进行游戏。通过模拟从8点到21点的时间流程,计算每个玩家的等待时间和游戏结束时间。

PAT 甲级 1026 JAVA

import java.util.*;

public class Main {
    static Comparator2 com = new Comparator2();
    static List<Table> total_tables = new ArrayList<>();
    static List<Table> vip_tables = new ArrayList<>();
    static Queue<Player> normal_queue = new LinkedList<>();
    static Queue<Player> vip_queue = new LinkedList<>();
    static List<Player> total_member = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        for (int i = 0; i < N; i++) {
            Player p = new Player();
            String arrTime = sc.next();
            String[] spl = arrTime.split(":");
            int hour = Integer.parseInt(spl[0]);
            int min = Integer.parseInt(spl[1]);
            int sec = Integer.parseInt(spl[2]);
            p.arrivingTime = hour * 3600 + min * 60 + sec;
            p.playTime = sc.nextInt() * 60;
            if (p.playTime > 120 * 60) {
                p.playTime = 120 * 60;
            }
            p.VIP = sc.nextInt();
            total_member.add(p);
        }

        int K = sc.nextInt();

        for (int i = 0; i < K; i++) {
            total_tables.add(new Table(i));
        }
        int M = sc.nextInt();

        for (int i = 0; i < M; i++) {
            vip_tables.add(total_tables.get(sc.nextInt() - 1));
        }
        for (int i = 8 * 3600 - 1; i < 21 * 3600; i++) {
            process(i, total_member);
        }

        Collections.sort(total_member, com);

        for (Player p : total_member) {
            if (p.serveTime == 0) {
                continue;
            }
            System.out.println(exchangeTimeFormat(p.arrivingTime) + " " + exchangeTimeFormat(p.serveTime) + " " + Math.round((double)p.waitTime / 60));
        }
        StringBuffer strb = new StringBuffer();
        for (Table t : total_tables) {
            strb.append(t.times);
            strb.append(" ");
        }
        System.out.println(strb.deleteCharAt(strb.length() - 1).toString());
    }

    private static String exchangeTimeFormat(int time) {
        StringBuffer strb = new StringBuffer();
        int hour = time / 3600;
        int min = (time % 3600) / 60;
        int sec = time % 3600 % 60;
        if (hour < 10) {
            strb.append(0);
        }
        strb.append(hour);
        strb.append(":");
        if (min < 10) {
            strb.append(0);
        }
        strb.append(min);
        strb.append(":");
        if (sec < 10) {
            strb.append(0);
        }
        strb.append(sec);
        return strb.toString();
    }

    private static void process(int current_time, List<Player> total_member) {
        for (Player p : total_member) {
            if (p.endTime == current_time) {
                total_tables.get(p.table_number).isUsed = 0;
            }
            if (p.arrivingTime == current_time) {
                if (p.VIP == 1) {
                    vip_queue.add(p);
                } else {
                    normal_queue.add(p);
                }
            }
        }

        Player cur;
        if (!vip_queue.isEmpty()) {
            cur = vip_queue.peek();
            for (Table t : vip_tables) {
                if (t.isUsed == 0) {
                    t.isUsed = 1;
                    t.times++;
                    cur.serveTime = current_time;
                    cur.table_number = t.ID;
                    cur.waitTime = current_time - cur.arrivingTime;
                    cur.endTime = cur.serveTime + cur.playTime;
                    if (cur.endTime > 21 * 3600) cur.endTime = 21 * 3600;
                    vip_queue.remove();
                    return;
                }
            }

            for (Table t : total_tables) {
                if (t.isUsed == 0) {
                    t.isUsed = 1;
                    t.times++;
                    cur.serveTime = current_time;
                    cur.table_number = t.ID;
                    cur.waitTime = current_time - cur.arrivingTime;
                    cur.endTime = cur.serveTime + cur.playTime;
                    if (cur.endTime > 21 * 3600) cur.endTime = 21 * 3600;
                    vip_queue.remove();
                    return;
                }
            }

        } else if (!normal_queue.isEmpty()) {
            cur = normal_queue.peek();
            for (Table t : total_tables) {
                if (t.isUsed == 0) {
                    t.isUsed = 1;
                    t.times++;
                    cur.serveTime = current_time;
                    cur.table_number = t.ID;
                    cur.waitTime = current_time - cur.arrivingTime;
                    cur.endTime = cur.serveTime + cur.playTime;
                    if (cur.endTime > 21 * 3600) cur.endTime = 21 * 3600;
                    normal_queue.remove();
                    return;
                }
            }
        } else {
            return;
        }


    }

    private static class Player {
        int arrivingTime;
        int serveTime;
        int playTime;
        int waitTime;
        int endTime;
        int VIP;
        int table_number;
    }

    private static class Table {
        int isUsed;
        int times;
        int ID;
        Table(int ID) {
            this.ID = ID;
        }
    }

    private void decide (int currentTime, Player p1) {
        if (p1.VIP == 1) {

        }
    }

    private static class Comparator2 implements Comparator<Player> {
        @Override
        public int compare(Player p1, Player p2) {
            if (p1.serveTime > p2.serveTime) {
                return 1;
            } else {
                return -1;
            }
        }
    }


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值