【问题描述】
饱了么”外卖系统中维护着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
放入两个代码
第二个是我自己写的,超过内存限制了,就百度修改了一下。
代码中有注释,就不额外补充了
借鉴
第一个
package test_1458;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class Main_test {
//n家店,m条信息,t时刻结束
static int n,m,t;
static Map<Integer, ArrayList<Integer>> map = new TreeMap<Integer, ArrayList<Integer>>();
static int result=0;
static Scanner cin = new Scanner(System.in);
public static void main(String[] args) {
// TODO 自动生成的方法存根
n=cin.nextInt();
m=cin.nextInt();
t=cin.nextInt();
//添加数据,按照店铺id号进行分类
for(int i=1;i<=m;i++) {
int time = cin.nextInt();
int id = cin.nextInt();
if (map.containsKey(id)) {
map.get(id).add(time);
}else {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(time);
map.put(id, temp);
}
}
//对map进行遍历
ArrayList< Map.Entry<Integer, ArrayList<Integer>>> list = new
ArrayList<Map.Entry<Integer, ArrayList<Integer>>>(map.entrySet());
//每条list中是一家店铺的消息
for(int i=0;i<list.size();i++){
//取出第i条list
Entry<Integer, ArrayList<Integer>> entry = list.get(i);
//取出第i条list的value值,即时刻
ArrayList<Integer> arrayList = entry.getValue();
int num=0;
boolean flag=false;
int []count = new int [t+2];
//存在时刻,即有订单,则进行加1
for(int j=0;j<arrayList.size();j++) {
count[arrayList.get(j)]++;
}
//对于一家的各个时刻的计算
for(int j=1;j<=t;j++) {
if (count[j] == 0) {
if (num > 0)
num--;
} else {
num += count[j] * 2;
}
if (num <= 3)
flag = false;
if (num > 5)
flag = true;
}
if (flag)
result++;
}
System.out.println(result);
}
}
第二个
package test_1458;
import java.util.Scanner;
public class Main {
//n家店,m条信息,t时刻结束
static int n,m,t;
static int [][]data;//存m条订单
static int [][]shop;//存第n家店铺,第t时刻的订单
static int []sum; //第n家店铺的优先级
static boolean []max;//第n家店是否被加入默认缓存,默认是false
static int result = 0;
static int []temp = new int [2];
static Scanner cin = new Scanner(System.in);
public static void main(String[] args) {
// TODO 自动生成的方法存根
n=cin.nextInt();
m=cin.nextInt();
t=cin.nextInt();
data = new int [m][2];
shop = new int [n+1][t+1];
sum = new int [n+1];
max = new boolean [n+1];
for(int i=0;i<m;i++) {
for(int j=0;j<2;j++) {
data[i][j]=cin.nextInt();
}
}
//第i家店铺,在j时刻的订单默认为0;
for(int i=1;i<=n;i++) {
for(int j=1;j<=t;j++) {
shop[i][j]=0;
}
//店铺优先级默认为0
sum[i]=0;
}
//排序,按照时间升序进行排列
paixv();
//遍历店铺
for(int i=1;i<=n;i++) {
//遍历订单,查询指定店铺的订单时刻
for(int j=0;j<m;j++) {
if (data[j][1]==i) {
shop[i][data[j][0]]++;
}
}
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=t;j++) {
//第i时刻有订单数
if (shop[i][j]>0) {
sum[i]+=2*shop[i][j];
}else {
if (sum[i]>0) {
sum[i]--;
}
}
//判断是否加入默认缓存中
if (sum[i]>=5) {
max[i]=true;
}
if (sum[i]<=3) {
max[i]=false;
}
}
}
for(int i=1;i<=n;i++) {
if (max[i]) {
result++;
}
}
System.out.println(result);
}
static void paixv() {
//冒泡排序
for(int i=0;i<m-1;i++) {//冒泡排序轮数
for(int j=0;j<m-1-i;j++) {
if(data[j][0]>data[j+1][0]) {
temp[0]=data[j][0];
temp[1]=data[j][1];
data[j][0]=data[j+1][0];
data[j][1]=data[j+1][1];
data[j+1][0]=temp[0];
data[j+1][1]=temp[1];
}
}
}
}
}