“饱了么”外卖系统中维护着N 家外卖店,编号1 N。每家外卖店都有
一个优先级,初始时(0 时刻) 优先级都为0。
每经过1 个时间单位,如果外卖店没有订单,则优先级会减少1,最低减
到0;而如果外卖店有订单,则优先级不减反加,每有一单优先级加2。
如果某家外卖店某时刻优先级大于5,则会被系统加入优先缓存中;如果
优先级小于等于3,则会被清除出优先缓存。
给定T 时刻以内的M 条订单信息,请你计算T 时刻时有多少外卖店在优
先缓存中。
【输入格式】
第一行包含3 个整数N、M 和T。
以下M 行每行包含两个整数ts 和id,表示ts 时刻编号id 的外卖店收到
一个订单。
【输出格式】
输出一个整数代表答案。
【样例输入】
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2
【样例输出】
1
【样例解释】
6 时刻时,1 号店优先级降到3,被移除出优先缓存;2 号店优先级升到6,
加入优先缓存。所以是有1 家店(2 号) 在优先缓存中。
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class 外卖店优先级 {
static int N;
static int M;
static int T;
static int[][] table;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
T = sc.nextInt();
table = new int[M][2];
//id集合,用来存放商家的id号
Set<Integer> idset = new HashSet<Integer>();
for(int i = 0; i < M; i++) {
table[i][0] = sc.nextInt();
table[i][1] = sc.nextInt();
idset.add(table[i][1]);
}
//缓存列表
Set<Integer> cache = new HashSet<Integer>();
for(Integer id : idset) {
//用来存放每个商家的订单
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int i = 0; i < M; i++) {
if(table[i][1] == id) {
tmp.add(table[i][0]);
}
}
int pre = 0;//外卖店的优先级
Collections.sort(tmp);
for(int ts = 1; ts <= T; ts++) {
//判断外卖店中ts时刻是否有订单
if(tmp.contains(ts)) {//有订单
for(int t = 0; t < tmp.size(); t++) {
if(tmp.get(t) == ts) {
pre += 2;
}else if(tmp.get(t) > ts){
break;
}else {
continue;
}
}
if(pre > 5) {
cache.add(id);
}
}else {//无订单
if(pre >= 1) {
pre -= 1;
}else {
pre = 0;
}
if(pre <= 3) {
cache.remove(id);
}
}
}
}
System.out.println(cache.size());
}
}