外卖店的优先级

蓝桥杯其他真题点这里👈

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main{
    static int n,m,t;   //n:店铺、m:订单数量、t:有效时间
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static final int N = 100010;
    static int[] last = new int[N]; //表示第i个店铺上一次有订单的时刻
    static int[] score = new int[N];    //表示第i个店铺当前的优先级
    static boolean[] st = new boolean[N];   //表示第i个店铺是否处在优先队列;
    static PII[] p = new PII[N];

    public static void main(String[] args)throws IOException {
        String[] init = in.readLine().split(" ");
        n = Integer.parseInt(init[0]);
        m = Integer.parseInt(init[1]);
        t = Integer.parseInt(init[2]);

        //读取每个订单到p里
        for (int i = 0; i < m; i++) {
            String[] data = in.readLine().split(" ");
            int ts = Integer.parseInt(data[0]); //拿到时刻
            int id = Integer.parseInt(data[1]); //拿到id
            p[i] = new PII(ts,id);
        }
        Arrays.sort(p,0,m);

        //每次只处理相同时刻相同id的订单
        for (int i = 0; i < m;) {
            int j = i;
            while (j < m && p[i].id == p[j].id && p[i].ts == p[j].ts) j++;
            //这样 j - i 就是这一批相同订单的数量

            int ts = p[i].ts,id = p[i].id, cnt = j - i;
            //重新更新i
            i = j;

            //先把ts时刻之前,没有订单的时间段的优先级减去
            score[id] -= ts - last[id] - 1; //两端都是有订单的,要-1
                                            //就比如算(1,50)之间有多少数
            if (score[id] < 0) score[id] = 0;
            if (score[id] <= 3) st[id] = false; //退出优先级队列中

            //这时便可以处理当前时刻的订单
            score[id] += cnt * 2;
            if (score[id] > 5) st[id] = true;   //加入优先级队列中

            //最后把当前时刻在last[]里标记为该店铺最后一次出现订单的时刻
            last[id] = ts;
        }

        //因为上一个一个for循环只是处理有订单的时刻
        //最后一段没有订单要统一处理
        for (int i = 1; i <= n; i++) {
            if (last[i] < t){
                //这个位置不需要-1,这个时候相当于计算(1,50]之间有多少数
                //最后一秒t是没有订单的
                score[i] -= t - last[i];
                if (score[i] <= 3 ) st[i] = false;
            }
        }
        
        //最后再判断st[]数组里有多少个true;
        int res = 0;
        for (int i = 1; i <= n; i++) {
            if (st[i] == true) res ++;
        }

        System.out.println(res);
        in.close();
    }
    static class PII implements Comparable<PII>{
        int ts;
        int id;
        public PII(int ts, int id) {
            this.id = id;
            this.ts = ts;
        }
        @Override
        public int compareTo(PII o) {
            if (this.ts != o.ts) return Integer.compare(ts,o.ts);
            return Integer.compare(id,o.id);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值