0
点赞
收藏
分享

微信扫一扫

Java蓝桥杯2019真题试题G:外卖优先级(超简洁,易懂,有注释,不懂顺便问)

拾光的Shelly 2022-02-13 阅读 69

试题G:外卖店优先级

Java题解

public class gTakeOut {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); // n家店
        int m = sc.nextInt(); // m行数据
        int t = sc.nextInt(); // 总时间t

        int[][] arr = new int[n][t]; // arr[i][j]表示id店在ts时刻的订单数
        for (int i = 0; i < m; i++) {
            // ts 时刻编号 id的外卖店收到一个订单
            int ts = sc.nextInt();
            int id = sc.nextInt();

            arr[id - 1][ts - 1] += 1;
        }

        int count = 0; // 最后留在缓存的店数
        for (int i = 0; i < n; i++) { // 遍历每家店
            int level = 0; // 优先级
            for (int j = 0; j < t; j++) { // 遍历每个时刻
                // 如果订单数为0,且优先级不为0,优先级减少
                // 优先级最低减到0
                if (arr[i][j] == 0) {
                    if (level == 0) continue;
                    level--;
                } else { // 如果订单数不为0,每有1单优先级加2
                    level += arr[i][j] * 2;
                }
            }
            // 遍历完即为最后一刻
            // 判断优先级是否大于5
            if (level > 5) count++;
        }
        System.out.println(count);
    }
}
举报

相关推荐

0 条评论