文章目录
找素数
原题链接
问题描述
输入及输出格式
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
参考样例
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
数据范围及运行限制
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
解题思路
线性筛,不了解的可以去看灵茶神艾府 欧拉筛【力扣周赛 326】LeetCode
参考代码
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = 1500000;
static boolean[] vis = new boolean[N];
static int[] primes = new int[100010];
static int cur = 0;
public static void main(String[] args) throws Exception {
for (int i = 2; i < N; i++) {
if (!vis[i]) {
primes[cur++] = i;
if (cur == 100002) {
out.println(primes[cur - 1]);
break;
}
}
for (int j = 0; j < cur; j++) {
int p = primes[j];
if (p * i >= N) break;
vis[p * i] = true;
if (i % p == 0) break;
}
}
out.flush();
in.close();
}
}
图书排列
原题链接
问题描述
输入及输出格式
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
参考样例
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
数据范围及运行限制
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
解题思路
考察回溯,不熟悉回溯的推荐看灵神的算法精讲课
参考代码
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// vis[i] 表示当前数字是否已经使用过
static boolean[] vis = new boolean[10];
static int ans = 0, pre = -1;
/**
*
* @param cur 当前遍历到第几个数字
* @param pre 上一个数字
*/
public static void dfs(int cur, int pre) {
if (cur == 10) {
ans++;
return;
}
for (int i = 0; i < 10; i++) {
// 如果当前是第一个数,选哪个不收任何限制
// 如果不是第一个数,那么当前选的数字和上一个数的差的绝对值必须大于 1
if (cur == 0 || (!vis[i] && Math.abs(i - pre) > 1)) {
vis[i] = true;
dfs(cur + 1, i);
vis[i] = false;
}
}
}
public static void main(String[] args) throws Exception {
dfs(0, -1);
out.println(ans);
out.flush();
in.close();
}
}
日志统计
原题链接
问题描述
输入及输出格式
参考样例
数据范围及运行限制
最大运行时间:1s
最大运行内存: 256M
解题思路
将每一个 id
的点赞时刻都记录下来
- 如果某个
id
的点赞数量< k
,那么这个id
肯定不满足条件 - 如果某个
id
的点赞数量>= k
,对这个id
的点赞记录按照点赞时间
升序排序,然后依次检查每个长度为k
的区间的左右端点,如果差值< d
,说明在这个时间段点赞数满足要求,输出答案即可。
参考代码
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5 + 10, n, d, k;
// key: id value: 点赞时间的集合
static TreeMap<Integer, List<Integer>> map = new TreeMap<>();
public static void main(String[] args) throws Exception {
// 输入 + 预处理
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
d = Integer.parseInt(s[1]);
k = Integer.parseInt(s[2]);
for (int i = 0; i < n; i++) {
String[] ss = in.readLine().split(" ");
int t = Integer.parseInt(ss[0]), id = Integer.parseInt(ss[1]);
List<Integer> list = map.getOrDefault(id, new ArrayList<>());
list.add(t);
map.put(id, list);
}
// TreeMap 默认就是按照 key 升序排序
for (int key: map.keySet()) {
List<Integer> list = map.get(key);
int sz = list.size();
// 点赞数量不满足要求
if (sz < k) continue;
// 对点赞时间按照升序排序
Collections.sort(list);
// 依次检查每个时间段
for (int i = 0; i <= sz - k; i++) {
if (list.get(i + k - 1) - list.get(i) < d) {
out.println(key);
break;
}
}
}
out.flush();
in.close();
}
}