Codeforces Round 911 (Div. 2)

Cover in Water

只要存在三个连续的空格,就可以执行两次操作一,再多次执行操作二,来装满所有空格。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void solve() {
int n = io.nextInt();
String s = io.next();
String[] arr = s.split("#");
int ans = 0;
for (String t : arr) {
int m = t.length();
if (m >= 3) {
io.println(2);
return;
}
ans += m;
}
io.println(ans);
}

Laura and Operations

注意题目说的是剩下一种类型的数字,而不是一个数字。如果剩下数字 \(1\),那么首先将 \(2\) 和 \(3\) 抵消,如果 \(2\) 多于 \(3\),那么多出的数量如果是偶数,就可以将该数量的一半执行操作,再做一次抵消,最后就只剩下 \(1\);反之亦然。

1
2
3
4
5
6
7
8
9
10
11
12
public static void solve() {
int a = io.nextInt(), b = io.nextInt(), c = io.nextInt();
if (Math.abs(b - c) % 2 == 0) io.print(1);
else io.print(0);
io.print(" ");
if (Math.abs(a - c) % 2 == 0) io.print(1);
else io.print(0);
io.print(" ");
if (Math.abs(a - b) % 2 == 0) io.print(1);
else io.print(0);
io.println();
}

Anji’s Binary Tree

做一次后序遍历即可。题目说的是选择任意字母替换,而不是选择其他节点上的字母替换。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void solve() {
int n = io.nextInt();
String s = io.next();
int[][] g = new int[n][];
for (int i = 0; i < n; i++) {
int l = io.nextInt() - 1, r = io.nextInt() - 1;
g[i] = new int[]{l, r};
}
io.println(dfs(0, g, s));
}

private static int dfs(int x, int[][] g, String s) {
if (g[x][0] == -1 && g[x][1] == -1) {
return 0;
}
int res = Integer.MAX_VALUE;
if (g[x][0] != -1) {
res = Math.min(res, dfs(g[x][0],g, s) + (s.charAt(x) != 'L' ? 1 : 0));
}
if (g[x][1] != -1) {
res = Math.min(res, dfs(g[x][1],g, s) + (s.charAt(x) != 'R' ? 1 : 0));
}
return res;
}

Small GCD

\(f(a,b,c)\) 表示 \(a,b,c\) 中最小的两个数的 \(\gcd\),而我们要求出给定数组的所有不同下标构成的三元组的 \(f\) 之和。暴力的想法是枚举中间值,然后计算以该值为中心构成的三元组的 \(\gcd\) 之和,时间复杂度为 \(O(n^{2})\)。正确的做法:由于数据范围比较小,我们可以首先计算出 \([1,N]\) 范围内每个数的所有约数,然后排序数组,对数组中的每个数枚举它的约数,从而计算出以该约数的倍数作为最大公约数的三元组的个数,然后利用容斥原理得到以该约数作为最大公约数的三元组的个数,最后可以计算出答案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
private static final int N = 100000;
private static final List<Integer>[] aux;

static {
aux = new List[N + 1];
Arrays.setAll(aux, k -> new ArrayList<>());
for (int i = 1; i <= N; i++) {
for (int j = i; j <= N; j += i) {
aux[j].add(i);
}
}
}

public static void solve() {
int n = io.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = io.nextInt();
}
Arrays.sort(a);

int[] c = new int[N + 1];
long[] f = new long[N + 1];
for (int i = 0; i < n; i++) {
for (int x : aux[a[i]]) {
f[x] += (long) c[x] * (n - i - 1);
c[x]++;
}
}

long ans = 0L;
for (int i = N; i >= 1; i--) {
for (int j = i + i; j <= N; j += i) {
f[i] -= f[j];
}
ans += f[i] * i;
}
io.println(ans);
}

Transitive Graph

似乎是和强连通分量相关的题目,有空可以补一下。

第 373 场力扣周赛

循环移位后的矩阵相似检查

模拟。有个性质,如果左移 \(k\) 位之后相等,则右移 \(k\) 位也必定相等。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public boolean areSimilar(int[][] mat, int k) {
int m = mat.length, n = mat[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] != mat[i][((j + (i % 2 == 0 ? 1 : -1) * k) % n + n) % n]) {
return false;
}
}
}
return true;
}
}

统计美丽子字符串 I

将元音字母看作 \(1\),非元音字母看作 \(-1\),使用前缀和 + 哈希表的技巧,可以得到若干个分组,每组中任意两个下标构成的子数组都满足条件一。然后我们可以暴力判断所有满足条件一的子数组的长度是否满足条件二,时间复杂度为 \(O(n^{2})\)。(补充:可以纯暴力做,不需要分组。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int beautifulSubstrings(String s, int k) {
int n = s.length(), sum = 0;
Set<Character> set = Set.of('a', 'e', 'i', 'o', 'u');
Map<Integer, List<Integer>> map = new HashMap<>();
map.computeIfAbsent(0, t -> new ArrayList<>()).add(-1);
for (int i = 0; i < n; i++) {
sum += set.contains(s.charAt(i)) ? 1 : -1;
map.computeIfAbsent(sum, t -> new ArrayList<>()).add(i);
}
int ans = 0;
for (var list : map.values()) {
for (int j = 0; j < list.size() ; j++) {
for (int i = 0; i < j; i++) {
int len = (list.get(j) - list.get(i)) / 2;
if (len * len % k == 0) {
ans++;
}
}
}
}
return ans;
}
}

交换得到字典序最小的数组

如果 \(|nums[i]-nums[j]|<=limit\),那么就可以交换 \(nums[i]\) 和 \(nums[j]\),该交换的性质具有传递性,所以我们可以对原数组进行排序,只要相邻元素的差值小于等于 \(limit\),它们就在同一个可交换集合中。这样可以将原数组划分为若干可交换集合,然后对每个集合排序,从小到大排列即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length;
var aux = new Integer[n];
for (int i = 0; i < n; i++) {
aux[i] = i;
}
Arrays.sort(aux, (i, j) -> nums[i] - nums[j]);

int pre = -limit;
List<List<Integer>> buckets = new ArrayList<>();
for (int i : aux) {
if (nums[i] - pre > limit) {
buckets.add(new ArrayList<>());
}
buckets.get(buckets.size() - 1).add(i);
pre = nums[i];
}

int[] ans = new int[n];
for (var bucket : buckets) {
List<Integer> pos = new ArrayList<>();
pos.addAll(bucket);
Collections.sort(pos);
for (int i = 0; i < pos.size(); i++) {
ans[pos.get(i)] = nums[bucket.get(i)];
}
}
return ans;
}
}

统计美丽子字符串 II

朴素做法的瓶颈在 \((\frac{L}{2})^{2}\bmod{k}=0\) 的判断上,可以通过将条件二变换为 \(L\bmod{k^{\prime}}=0\),然后使用前缀和以及下标模 \(k^{\prime}\) 的值来分组,这样同组内的下标两两组合得到的必定是满足两个条件的子数组。灵神题解,时间复杂度 \(O(n+\sqrt{k})\)。还有另一种枚举的做法,题解

第 118 场力扣夜喵双周赛

查找包含给定字符的单词

模拟。

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public List<Integer> findWordsContaining(String[] words, char x) {
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
if (words[i].contains(x + "")) {
ans.add(i);
}
}
return ans;
}
}

最大化网格图中正方形空洞的面积

分别求出行和列的最长连续线段,然后最大正方形面积就是两者最小值加一的平方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
Arrays.sort(hBars);
Arrays.sort(vBars);
int maxH = 0, maxV = 0;
for (int i = 0, j = 0; j < hBars.length; j++) {
if (hBars[j] - hBars[i] == j - i) {
maxH = Math.max(maxH, j - i + 1);
} else {
i = j;
}
}
for (int i = 0, j = 0; j < vBars.length; j++) {
if (vBars[j] - vBars[i] == j - i) {
maxV = Math.max(maxV, j - i + 1);
} else {
i = j;
}
}
int len = Math.min(maxH, maxV) + 1;
return len * len;
}
}

购买水果需要的最少金币数

动态规划,\(dp[i]\) 表示获取 \([i,n]\) 范围内所有水果所需的最少金币数,有 \(dp[i]=prices[i]+\min_{j=i+1}^{2i+1}{dp[j]}\),时间复杂度 \(O(n^{2})\)。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int minimumCoins(int[] prices) {
int n = prices.length;
for (int i = (n + 1) / 2 - 1; i > 0; i--) {
int min = Integer.MAX_VALUE;
for (int j = i + 1; j <= 2 * i + 1; j++) {
min = Math.min(min, prices[j - 1]);
}
prices[i - 1] += min;
}
return prices[0];
}
}

单调队列优化,时间复杂度 \(O(n)\)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int minimumCoins(int[] prices) {
int n = prices.length;
Deque<Integer> q = new ArrayDeque<>();
for (int i = n; i > 0; i--) {
while (!q.isEmpty() && q.peekFirst() > 2 * i + 1) {
q.pollFirst();
}
if (i <= (n + 1) / 2 - 1) {
prices[i - 1] += prices[q.peekFirst() - 1];
}
while (!q.isEmpty() && prices[q.peekLast() - 1] >= prices[i - 1]) {
q.pollLast();
}
q.offerLast(i);
}
return prices[q.peekLast() - 1];
}
}

找到最大非递减数组的长度

单调队列优化 DP,随缘补题。