试题 G: 外卖店优先级

时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分

【问题描述】

“饱了么”外卖系统中维护着 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

试题G: 外卖店优先级 9

第十届蓝桥杯大赛软件类省赛 Java 大学 C 组

【样例输出】 1

【样例解释】 6 时刻时,1 号店优先级降到 3,被移除出优先缓存;2 号店优先级升到 6, 加入优先缓存。所以是有 1 家店 (2 号) 在优先缓存中。

【评测用例规模与约定】 对于 80% 的评测用例,1≤ N,M,T ≤10000。 对于所有评测用例,1≤ N,M,T ≤100000,1≤ts≤T,1≤id ≤ N。

这道题可以用map来写,map可以自定义下标,
如果直接加入值的话可能出现缺少问题
因为同一时间可能会有好几家店都有新订单
然后把他的值做成一个List集合,(用泛型指定以下就可以)
然后就是一些判断了
具体,看代码,不懂得在下面评论

import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap; public class waimaidianyouxianji {
static Scanner in = new Scanner(System.in);
static int n, m, t;
static Map<Integer, ArrayList<Integer>> map = new TreeMap<Integer, ArrayList<Integer>>();
static int result; public static void main(String[] args) {
n = in.nextInt();
m = in.nextInt();
t = in.nextInt();
for (int i = 1; i <= m; ++i) {
int time = in.nextInt();
int id = in.nextInt();
if (map.containsKey(id)) {
map.get(id).add(time);
} else {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(time);
map.put(id, temp);
}
} ArrayList<Map.Entry<Integer, ArrayList<Integer>>> list = new ArrayList<Map.Entry<Integer, ArrayList<Integer>>>(
map.entrySet());
for (int i = 0; i < list.size(); ++i) {
Entry<Integer, ArrayList<Integer>> entry = list.get(i);
ArrayList<Integer> list2 = entry.getValue();
int num = 0;
int[] count = new int[t + 2];
boolean flag = false;
for (int j = 0; j < list2.size(); ++j)
count[list2.get(j)]++; for (int j = 1; j <= t; ++j) {
if (count[j] == 0) {
if (num > 0)
num--;
if (num <= 3)
flag = false;
} else {
num += count[j] * 2;
if (num > 5)
flag = true;
}
}
if (flag)
result++;
}
System.out.println(result);
} }

最新文章

  1. 让Fiddler 直接抓取java程序的方法
  2. nginx-(/usr/local/nginx)配置编译
  3. pod setup 安装的最新办法(大坑啊)
  4. UVA 10089 Repackaging 数学问题
  5. UEditor上传图片到七牛云储存(c#)
  6. windows 编程 —— 子窗口 与 子窗口控件
  7. cf437B The Child and Set
  8. click和onclick本质的区别
  9. Android屏幕适配-android学习之旅(五十九)
  10. python中报错&quot;json.decoder.JSONDecodeError: Expecting value:&quot;的解决
  11. pgsql 服务遇见的问题记录
  12. git切换远程仓库地址
  13. 实战Asp.Net Core:部署应用
  14. centos6.5 nginx安装pcre错误
  15. 【PHP】解析PHP的GD库
  16. Leetcode 268.缺失数字 By Python
  17. Implementing HTTPS Everywhere in ASP.Net MVC application.
  18. Winform 对话框
  19. MAC Book 共享网络连接
  20. 1.java内存区域与内存溢出异常

热门文章

  1. [linux] VNC the connection was refused by the computer
  2. Python --表达式和运算符
  3. fragment hide/show 生命周期
  4. vue 路由钩子。
  5. SpringMVC底层执行原理
  6. 自定义spring-boot-starter
  7. search(16)- elastic4s-内嵌文件:nested and join
  8. MySQL 8.0用户及安全管理
  9. Python连接不上SQL Server的两种根治思路
  10. 国际化之fmt标签