外卖店优先级:用户登录
题目描述
"饱了么"外卖系统中维护着 N 家外卖店,编号 1 ∼ N。每家外卖店都有 一个优先级,初始时 (0 时刻) 优先级都为 0。
每经过 1 个时间单位,如果外卖店没有订单,则优先级会减少 1,最低减 到 0;而如果外卖店有订单,则优先级不减反加,每有一单优先级加 2。
如果某家外卖店某时刻优先级大于 5,则会被系统加入优先缓存中;如果 优先级小于等于 3,则会被清除出优先缓存。
给定 T 时刻以内的 M 条订单信息,请你计算 T时刻时有多少外卖店在优 先缓存中?
输入描述
第一行包含 3 个整数 N,M,T。
以下 MM 行每行包含两个整数 ts,id,表示 ts 时刻编号 id 的外卖店收到一个订单。
其中,1≤N,M,T≤105,1≤ts≤T,1≤id≤N。
输出描述
输出一个整数代表答案。
输入输出样例
示例
输入
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2
输出
1
样例解释:
6 时刻时,1 号店优先级降到 3,被移除出优先缓存;2 号店优先级升到 6, 加入优先缓存。所以是有 1 家店 (2 号) 在优先缓存中。
运行限制
- 最大运行时间:2s
- 最大运行内存: 256M
代码1:
import java.util.*;
public class Main07 {
static Scanner in = new Scanner(System.in);
static int n;
static int m;
static int t;
static Map<Integer, ArrayList<Integer>> map = new HashMap<>();
static int result;
public static void main (String[] args){
n = in.nextInt(); //n家店
m = in.nextInt(); //m个订单
t = in.nextInt(); //t个时刻
for (int i = 1; i <= m; i++) {
int time = in.nextInt();//time时刻
int id = in.nextInt(); //在time时刻收到的店铺id
if (map.containsKey(id)) { //判断有没有店
map.get(id).add(time); //在店后添加这一时刻
} else {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(time);//没有创建新的并添加这一时刻
map.put(id, temp);
}
}
//System.out.println(map);
//将结构放到列表用迭代器输出
Set<Integer> set = map.keySet();
//System.out.println(set);
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
Integer key = it.next();//逐个获取id
ArrayList<Integer> list = map.get(key);//获取id对应的时刻
int num = 0;
int[] count = new int[t + 1];//+1方便统计订单数
boolean flag = false;
for (int j = 0; j < list.size(); j++) {
count[list.get(j)]++;//统计在不同时刻此id出现次数
}
for (int j = 1; j <= t; j++) {
if (count[j] == 0) {//没有出现
if (num > 0) num--;//优先级-1;
if (num <= 3) flag = false;//连续3次没有出现,退出缓存
} else {
num += count[j] * 2;//出现一次优先级+2
if (num > 5) flag = true;//大于5加入缓存
}
}
if (flag) result++;//缓存+1
}
System.out.println(result);
}
}
代码2:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class 外卖店优先级 {
public static void main(String[] args) {
Scanner scan =new Scanner(System.in);
int N = scan.nextInt(); // N 加外卖店
int M = scan.nextInt(); // M 条订单
int T = scan.nextInt(); // T 时间内
Set<Integer> set= new HashSet<>(); // 缓存
int[][] dd = new int[T+1][N+1]; // 时间订单
long[] arr = new long[N+1]; // 店家优先级记录
for(int i = 0;i < M;i++){
dd[scan.nextInt()][scan.nextInt()]++;
}
for(int i = 1; i <= T; i++){
for(int j = 1; j <= N ;j++){
arr[j] += (dd[i][j] > 0 ? dd[i][j] * 2 : -1);
if(arr[j] < 0) arr[j] =0;
if(arr[j] > 5)set.add(j);
if(arr[j] <= 3)set.remove(j);
}
}
System.out.println(set.size());
}
}
第几个幸运数字:用户登录
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
到 X 星球旅行的游客都被发给一个整数,作为游客编号。
X 星的国王有个怪癖,他只喜欢数字 3,5 和 7。
国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。
我们来看前 10 个幸运数字是:
3 5 7 9 15 21 25 27 35 45
因而第 11 个幸运数字是: 49
小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。
请你帮小明计算一下,59084709587505 是第几个幸运数字。
运行限制
- 最大运行时间:1s
- 最大运行内存: 128M
代码:
public class 第几个幸运数字 {
public static void main(String[] args) {
/**
* 依照题目: 所求 n是的因子是 3 5 7;
* 所以必能被3 5 7 的次方乘出来
* 例如 105 = 3 x 5 x 7
* 3 5 7 就是 105 其中的一个因子
* */
long n = 59084709587505L;
int ans = 0;
long san = 0,qi = 0,wu = 0;
for (int i = 0; Math.pow(3, i) < n; i++) {
for (int j = 0; Math.pow(5, j) < n; j++) {
for (int j2 = 0; Math.pow(7, j2) < n; j2++) {
/**
* 1 * 1 * 7
* 1 * 1 * 49
* 以此类推
* 1 * 5 * 1
* 1 * 5 * 7
*
* */
if(Math.pow(3, i) * Math.pow(5, j) * Math.pow(7, j2) < n)
{
ans++;
}
}
}
}
System.out.println(ans);
}
}