第 113 场力扣夜喵双周赛

使数组成为递增数组的最少右移次数

直接从最小值开始判断数组是否递增,或者可以找到第一个递减的位置,然后再判断数组是否递增(因为如果数组满足条件,则其最多只有一个递减段)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int minimumRightShifts(List<Integer> nums) {
int n = nums.size(), idx = -1, min = 101;
for (int i = 0; i < n; i++) {
if (nums.get(i) < min) {
min = nums.get(i);
idx = i;
}
}
for (int i = 0; i < n - 1; i++) {
int x = (idx + i) % n, y = (x + 1) % n;
if (nums.get(x) > nums.get(y)) return -1;
}
return (n - idx) % n;
}
}

删除数对后的最小数组长度

贪心,比赛时我是用双指针做的,前半部分和后半部分进行匹配(当时边界想了很久,真笨!)。其他做法,参考题解:【小羊肖恩】数学 + 贪心:解决较长数组脑筋急转弯问题的关键。(因为 HashMap 很慢,所以用双指针会更快。)

方法一:贪心

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int minLengthAfterRemovals(List<Integer> nums) {
int n = nums.size(), i = 0;
for (int j = (n + 1) / 2; j < n; j++) {
if (nums.get(i) < nums.get(j)) {
i++;
}
}
return n - i * 2;
}
}

方法二:贪心 + 数学

1
2
3
4
5
6
7
8
9
10
class Solution {
public int minLengthAfterRemovals(List<Integer> nums) {
int n = nums.size(), max = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int x : nums) {
max = Math.max(max, map.merge(x, 1, Integer::sum));
}
return 2 * max <= n ? n % 2 : n - (n - max) * 2;
}
}

统计距离为 k 的点对

枚举 \(x_{1}\oplus x_{2}\) 的值为 \(p\),可以得到 \(y_{1}\oplus y_{2}\) 的值为 \(k-p\)。可以使用 HashMap 对前缀中的值计数来求解,需要注意循环的顺序,如果调换顺序会使代码变复杂,会花费更多的时间计算答案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int countPairs(List<List<Integer>> coordinates, int k) {
int ans = 0;
Map<List<Integer>, Integer> map = new HashMap<>();
for (var c : coordinates) {
int x = c.get(0), y = c.get(1);
for (int i = 0; i <= k; i++) {
ans += map.getOrDefault(List.of(x ^ i, y ^ (k - i)), 0);
}
map.merge(c, 1, Integer::sum);
}
return ans;
}
}

可以到达每一个节点的最少边反转次数

换根 DP,关键是要想到建立反向边,并为边添加相应的边权。

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
class Solution {
public int[] minEdgeReversals(int n, int[][] edges) {
List<int[]>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (var e : edges) {
int u = e[0], v = e[1];
g[u].add(new int[]{v, 0});
g[v].add(new int[]{u, 1});
}
int[] ans = new int[n];
ans[0] = dfs(0, -1, g);
dfs2(0, -1, g, ans);
return ans;
}

private int dfs(int x, int fa, List<int[]>[] g) {
int res = 0;
for (int t[] : g[x]) {
int y = t[0], w = t[1];
if (y == fa) continue;
res += dfs(y, x, g) + w;
}
return res;
}

private void dfs2(int x, int fa, List<int[]>[] g, int[] ans) {
for (int t[] : g[x]) {
int y = t[0], w = t[1];
if (y == fa) continue;
ans[y] = ans[x] + (w == 0 ? 1 : -1);
dfs2(y, x, g, ans);
}
}
}

AtCoder Beginner Contest 320

Leyland Number

模拟。

1
2
3
4
5
6
7
8
9
10
11
12
public static void solve() {
int a = io.nextInt(), b = io.nextInt();
int x = 1;
for (int i = 0; i < b; i++) {
x *= a;
}
int y = 1;
for (int i = 0; i < a; i++) {
y *= b;
}
io.println(x + y);
}

Longest Palindrome

模拟。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void solve() {
String s = io.next();
int n = s.length();
int ans = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
String a = s.substring(i, j + 1);
String b = new StringBuilder(a).reverse().toString();
if (a.equals(b)) {
ans = Math.max(ans, j - i + 1);
}
}
}
io.println(ans);
}

Slot Strategy 2 (Easy)

暴力枚举每个转盘的时间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void solve() {
int n = 3, m = io.nextInt();
int ans = Integer.MAX_VALUE;
String[] s = new String[n];
for (int i = 0; i < n; i++) {
s[i] = io.next();
}
for (int i = 0; i < n * m; i++) {
char a = s[0].charAt(i % m);
for (int j = 0; j < n * m; j++) {
if (i == j) continue;
char b = s[1].charAt(j % m);
for (int k = 0; k < n * m; k++) {
if (i == k || j == k) continue;
char c = s[2].charAt(k % m);
if (a == b && b == c) {
ans = Math.min(ans, Math.max(i, Math.max(j, k)));
}
}
}
}
io.println(ans == Integer.MAX_VALUE ? -1 : ans);
}

Relative Position

DFS 模拟,需要注意给的不是一棵树,所以在 DFS 时要使用访问数组,而不能用父结点。

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
public static void solve() {
int n = io.nextInt(), m = io.nextInt();
List<int[]>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int i = 0; i < m; i++) {
int a = io.nextInt() - 1, b = io.nextInt() - 1, x = io.nextInt(), y = io.nextInt();
g[a].add(new int[]{b, x, y});
g[b].add(new int[]{a, -x, -y});
}
long[] X = new long[n];
long[] Y = new long[n];
Arrays.fill(X, Long.MAX_VALUE);
Arrays.fill(Y, Long.MAX_VALUE);
boolean[] vis = new boolean[n];
dfs(0, vis, g, 0, 0, X, Y);
for (int i = 0; i < n; i++) {
if (X[i] != Long.MAX_VALUE && Y[i] != Long.MAX_VALUE) {
io.println(X[i] + " " + Y[i]);
} else {
io.println("undecidable");
}
}
}

private static void dfs(int i, boolean[] vis, List<int[]>[] g, long x, long y, long[] X, long[] Y) {
X[i] = x;
Y[i] = y;
vis[i] = true;
for (int[] t : g[i]) {
int j = t[0];
if (vis[j]) continue;
long nx = t[1] + x, ny = t[2] + y;
dfs(j, vis, g, nx, ny, X, Y);
}
}

Somen Nagashi

还是模拟,可以一边输入一边处理,减少代码量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void solve() {
int n = io.nextInt(), m = io.nextInt();
long[] ans = new long[n], re = new long[n];
PriorityQueue<Integer> q = new PriorityQueue<>();
PriorityQueue<Integer> p = new PriorityQueue<>((a, b) -> Long.compare(re[a], re[b]));
for (int i = 0; i < n; i++) {
q.offer(i);
}
while (m-- != 0) {
int t = io.nextInt(), w = io.nextInt(), s = io.nextInt();
while (!p.isEmpty() && re[p.peek()] <= t) q.offer(p.poll());
if (q.isEmpty()) continue;
int x = q.peek();
ans[x] += w;
re[x] = t + s;
p.offer(q.poll());
}
for (int i = 0; i < n; i++) {
io.println(ans[i]);
}
}

Codeforces Round 897 (Div. 2)

green_gold_dog, array and permutation

让最小值减去最大值,就一定可以得到 \(n\) 个不同的差值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void solve() {
int n = io.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = io.nextInt();
}
var aux = new Integer[n];
for (int i = 0; i < n; i++) {
aux[i] = i;
}
Arrays.sort(aux, (i, j) -> a[i] - a[j]);
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
int t = aux[i];
ans[t] = n - i;
}
for (int i = 0; i < n; i++) {
io.print(ans[i] + " ");
}
io.println();
}

XOR Palindromes

算是简单的分类讨论,比赛时写的稀烂。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void solve() {
int n = io.nextInt(), cnt = 0;
char[] s = io.next().toCharArray();
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - i - 1]) {
cnt++;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= n; i++) {
if (i < cnt || i > n - cnt || (i - cnt) % 2 == 1 && n % 2 == 0) {
sb.append('0');
} else {
sb.append('1');
}
}
io.println(sb);
}

Salyg1n and the MEX Game

比赛调试一小时,疯狂超时,结果是限制太强的原因。(浪费时间。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void solve() {
int n = io.nextInt();
int[] s = new int[n];
for (int i = 0; i < n; i++) {
s[i] = io.nextInt();
}
Arrays.sort(s);
int x = n;
for (int i = 0; i < n ; i++) {
if (s[i] != i) {
x = i;
break;
}
}
while (x != -1) {
io.println(x);
io.flush();
x = io.nextInt();
}
}

Cyclic Operations

做了一个多小时 AC,有点像之前做的内向基环树,该题每个点都有个出边,所以至少有一个环。首先要特判 \(k=1\) 的情况,该情况每个位置都必须自成一个环,即 \(a_{i}=i\);其他情况所有环的长度都必须为 \(k\),这样可以保证答案存在。

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
40
41
42
43
44
45
public static void solve() {
int n = io.nextInt(), k = io.nextInt();
int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = io.nextInt() - 1;
}

int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[b[i]]++;
}
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (in[i] == 0) {
q.offer(i);
}
}
while (!q.isEmpty()) {
int x = q.poll();
if (--in[b[x]] == 0) {
q.offer(b[x]);
}
}

int cnt = 0;
boolean[] vis = new boolean[n];
for (int i = 0; i < n; i++) {
if (in[i] == 0 || vis[i]) continue;
int len = 0;
for ( ; !vis[i]; i = b[i]) {
vis[i] = true;
len++;
}
if (len != k) {
io.println("NO");
return;
}
cnt++;
}
if (k == 1 && cnt != n) {
io.println("NO");
} else {
io.println("YES");
}
}

Salyg1n and Array (simple version)

注意,\(n\) 和 \(k\) 都是偶数!手推的话可能可以做出来吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void solve() {
int n = io.nextInt(), k = io.nextInt();
int ans = 0, i;
for (i = 1; i + k - 1 <= n; i += k) {
io.println("? " + i);
io.flush();
ans ^= io.nextInt();
}
for (; i <= n; i++) {
io.println("? " + (i - k + 1));
io.flush();
ans ^= io.nextInt();
}
io.println("! " + ans);
io.flush();
}

Salyg1n and Array (hard version)

技巧性有点强,真想不到。就是如果多出一部分,可以通过两次异或算出来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void solve() {
int n = io.nextInt(), k = io.nextInt();
int ans = 0, i;
for (i = 1; i + k - 1 <= n; i += k) {
io.println("? " + i);
io.flush();
ans ^= io.nextInt();
}
int t = n - i + 1;
io.println("? " + (n - k + 1 - t / 2));
io.flush();
ans ^= io.nextInt();
io.println("? " + (n - k + 1));
io.flush();
ans ^= io.nextInt();
io.println("! " + ans);
io.flush();
}