第 360 场力扣周赛

距离原点最远的点

核心:要距离原点最远,那么可选的位置肯定是向同一个方向移动。

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int furthestDistanceFromOrigin(String moves) {
int n = moves.length(), dis = 0, cnt = 0;
for (int i = 0; i < n; i++) {
char c = moves.charAt(i);
if (c == 'L') dis--;
else if (c == 'R') dis++;
else cnt++;
}
return Math.max(cnt - dis, cnt + dis);
}
}

找出美丽数组的最小和

和上周一样的题目。

1
2
3
4
5
6
class Solution {
public long minimumPossibleSum(int n, int target) {
long m = Math.min(target / 2, n);
return (m * (m + 1) + (target * 2 + n - m - 1) * (n - m)) / 2;
}
}

使子序列的和等于目标的最少操作次数

比赛时思路满天飞,各种乱写。其实最后的思路是对的,但是基于之前的代码改写,导致有很多 Bug。赛后 15 分钟 AC。从低位到高位枚举 \(target\) 中的 \(1\),假设当前 \(1\) 对应的值为 \(x\),那么 \(nums\) 中所有小于等于 \(x\) 的值都可以用来填补 \(x\),如果不够那么肯定需要将下一个大于 \(x\) 的值分解为 \(x\)。(更优的做法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int minOperations(List<Integer> nums, int target) {
Collections.sort(nums);
int n = nums.size();
int idx = 0, sum = 0, ans = 0;
for (int i = target; i != 0; ) {
int x = i & -i;
i -= x;
while (idx < n && nums.get(idx) <= x) {
sum += nums.get(idx++);
}
sum -= x;
if (sum < 0) {
if (idx == n) return -1;
ans += Integer.numberOfTrailingZeros(nums.get(idx) / x);
sum += nums.get(idx++);
}
}
return ans;
}
}

在传球游戏中最大化函数值

参考大佬的题解

方法一:倍增 DP

因为 CPU 缓存的原因,数组开成 new int[35][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
class Solution {
public long getMaxFunctionValue(List<Integer> receiver, long k) {
int n = receiver.size();
int[][] f = new int[n][35];
long[][] w = new long[n][35];
for (int i = 0; i < n; i++) {
f[i][0] = receiver.get(i);
w[i][0] = i;
}
for (int j = 1; j < 35; j++) {
for (int i = 0; i < n; i++) {
f[i][j] = f[f[i][j - 1]][j - 1];
w[i][j] = w[i][j - 1] + w[f[i][j - 1]][j - 1];
}
}
long ans = 0L;
for (int i = 0; i < n; i++) {
long cur = 0L;
int pos = i;
for (int j = 0; j < 35; j++) {
if ((k >> j & 1) == 0) continue;
cur += w[pos][j];
pos = f[pos][j];
}
ans = Math.max(ans, cur + pos);
}
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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Solution {
public long getMaxFunctionValue(List<Integer> receiver, long k) {
int n = receiver.size();
// 建立环外节点的反向边
int[] in = new int[n];
List<Integer>[] reverse = new List[n];
Arrays.setAll(reverse, r -> new ArrayList<>());
for (int i = 0; i < n; i++) {
in[receiver.get(i)]++;
reverse[receiver.get(i)].add(i);
}
// 拓扑序去除环外节点
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (in[i] == 0) q.add(i);
}
while (!q.isEmpty()) {
int x = q.poll();
if (--in[receiver.get(x)] == 0) {
q.offer(receiver.get(x));
}
}
// 计算每个环的前缀和,并记录每个节点在哪个环的哪个位置
int[] cirNum = new int[n];
int[] cirPos = new int[n];
boolean[] vis = new boolean[n];
List<List<Long>> circles = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (!vis[i] && in[i] != 0) {
List<Long> cir = new ArrayList<>();
cir.add(0L); // 前缀和的冗余节点
// 存储环的节点,并记录每个节点在哪个环的哪个位置
for (int cur = i; !vis[cur]; cur = receiver.get(cur)) {
vis[cur] = true;
cirNum[cur] = circles.size();
cirPos[cur] = cir.size();
cir.add((long) cur);
}
// 重复存储环的节点,方便计算从任意节点开始和结束的价值和
for (int t = cir.size() - 1, j = 1; t > 0; t--, j++) {
cir.add(cir.get(j));
}
// 计算前缀和
for (int j = 1; j < cir.size(); j++) {
cir.set(j, cir.get(j) + cir.get(j - 1));
}
circles.add(cir);
}
}
// 对环内的每个节点向环外进行 dfs,从而计算出以每个节点作为起点的价值和
long ans = 0L;
// 存储环外节点的前缀和
List<Long> outSum = new ArrayList<>();
outSum.add(0L);
for (int i = 0; i < n; i++) {
// 注意传递 k + 1,表示总节点数量
if (in[i] != 0) ans = Math.max(ans, dfs(i, circles.get(cirNum[i]), cirPos[i], reverse, in, outSum, k + 1));
}
return ans;
}

private long dfs(int x, List<Long> cir, int pos, List<Integer>[] reverse, int[] in, List<Long> outSum, long k) {
long res = 0L;
int outLen = outSum.size() - 1;
if (outLen < k) {
int n = cir.size() / 2; // 因为 cir 多存储了 n - 1 个环内节点,以及一个冗余节点,所以 cir.size() / 2 就是环的长度
res = (k - outLen) / n * cir.get(n) + cir.get(pos + (int) ((k - outLen) % n) - 1) - cir.get(pos - 1);
}
res += outSum.get(outLen) - outSum.get((int) Math.max(0L, outLen - k));
for (int y : reverse[x]) {
if (in[y] != 0) continue;
outSum.add(outSum.get(outLen) + y);
res = Math.max(res, dfs(y, cir, pos, reverse, in, outSum, k));
outSum.remove(outLen + 1);
}
return res;
}
}

Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)

Increasing and Decreasing

比赛时漏看第三个条件,搞半天。而且似乎倒着减会比较容易做(差不多)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void solve() {
int x = io.nextInt(), y = io.nextInt(), n = io.nextInt();
int z = (1 + n - 1) * (n - 1) / 2;
if (z > y - x) {
io.println(-1);
return;
}
io.print(x + " ");
int d = x + y - x - z;
for (int i = n - 1; i >= 1; i--) {
d += i;
io.print(d + " ");
}
io.println();
}

Swap and Reverse

找规律。第一个操作表明奇数下标相互连通,偶数下标相互连通。第二个操作,如果 \(k\) 是奇数,则连通性不会改变,分别对奇偶字母排序,然后构造即可;如果 \(k\) 是偶数,则奇数下标和偶数下标相互连通,对所有字母排序即可。

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(), k = io.nextInt();
char[] s = io.next().toCharArray();
if (k % 2 == 0) {
Arrays.sort(s);
io.println(new String(s));
} else {
PriorityQueue<Character> list1 = new PriorityQueue<>();
PriorityQueue<Character> list2 = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
if (i % 2 == 0) list1.add(s[i]);
else list2.add(s[i]);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i % 2 == 0) sb.append(list1.poll());
else sb.append(list2.poll());
}
io.println(sb.toString());
}
}

Divisor Chain

比赛时瞎猜 AC 的,当时是想从 \(1\) 开始构造到 \(x\),过程比答案复杂。正解是从 \(x\) 一直减去最低有效位的一(必定是除数),直到 \(x\) 等于 \(2\) 的幂(只剩一个一),然后让 \(x\) 一直减去 \(\frac{x}{2}\) 即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void solve() {
int x = io.nextInt();
List<Integer> ans = new ArrayList<>();
ans.add(x);
while ((x & (x - 1)) != 0) {
x &= (x - 1);
ans.add(x);
}
while (x != 1) {
x /= 2;
ans.add(x);
}
io.println(ans.size());
for (int y : ans) {
io.print(y + " ");
}
io.println();
}

Matrix Cascade

使用差分数组维护从上到下的翻转次数,需要注意的是正负需要分开存,正数每层左移一位,负数每层右移一位。PS:这题 \(p\) 和 \(q\) 总是写错,Debug 很久。以及大佬的代码看不懂。

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
public static void solve() {
int n = io.nextInt();
char[][] a = new char[n][];
for (int i = 0; i < n; i++) {
a[i] = io.next().toCharArray();
}
int ans = 0;
int[] p = new int[n + 1];
int[] q = new int[n + 1];
int[] sum = new int[n + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] - '0' != sum[j + 1] % 2) {
ans++;
p[j] ^= 1;
q[j + 1] ^= 1;
}
}
p[0] ^= p[1];
for (int j = 1; j < n; j++) {
p[j] = p[j + 1];
}
q[n] ^= q[n - 1];
for (int j = n - 1; j > 0; j--) {
q[j] = q[j - 1];
}
for (int j = 0; j < n; j++) {
sum[j + 1] = sum[j] ^ p[j] ^ q[j];
}
}
io.println(ans);
}

Guess Game

有点难以描述,超出能力范围了。这是一个比较好理解的做法,分别考虑每一位。从最低位开始,如果前缀相同,那么就计算当前位 \(0\) 和 \(1\) 的个数,只有爱丽丝拿 \(1\),鲍勃拿 \(1\) 或 \(0\) 的情况,当前位才会多走一轮。初始时,设置答案为 \(n \times n\),因为每个组合至少会走一轮。最后需要使用快速幂求 \(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
private static final int MOD = 998244353;

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);
long ans = (long) n * n;
for (int t = 0; t < 30; t++) {
for (int l = 0, r = 0; l < n; l = r) {
int[] cnt = new int[2];
while (r < n && a[l] / 2 == a[r] / 2) {
cnt[a[r] % 2]++;
r++;
}
ans += (long) cnt[1] * (cnt[1] + cnt[0]);
}
for (int i = 0; i < n; i++) {
a[i] /= 2;
}
}
ans = ans % MOD * pow(n, MOD - 2) % MOD * pow(n, MOD - 2) % MOD;
io.println(ans);
}

private static int pow(int a, int n) {
long res = 1, x = a;
while (n != 0) {
if (n % 2 == 1) {
res = (res * x) % MOD;
}
x = (x * x) % MOD;
n >>= 1;
}
return (int) res;
}

AtCoder Beginner Contest 317

Potions

二分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void solve() {
int n = io.nextInt(), h = io.nextInt(), x = io.nextInt();
int[] p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = io.nextInt();
}
x = x - h;

int lo = 0, hi = n - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (p[mid] < x) lo = mid + 1;
else hi = mid - 1;
}
io.println(lo + 1);
}

MissingNo.

求和公式。

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void solve() {
int n = io.nextInt();
int sum = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int a = io.nextInt();
sum += a;
min = Math.min(min, a);
max = Math.max(max, a);
}
io.println((min + max) * (max - min + 1) / 2 - sum);
}

Remembering the Days

暴力 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
private static int ans = Integer.MIN_VALUE;

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 u = io.nextInt() - 1, v = io.nextInt() - 1, w = io.nextInt();
g[u].add(new int[]{v, w});
g[v].add(new int[]{u, w});
}

boolean[] vis = new boolean[n];
for (int i = 0; i < n; i++) {
dfs(i, 0, g, vis);
}
io.println(ans);
}

private static void dfs(int x, int dis, List<int[]>[] g, boolean[] vis) {
vis[x] = true;
ans = Math.max(ans, dis);
for (int[] t : g[x]) {
int y = t[0], w = t[1];
if (!vis[y]) {
dfs(y, dis + w, g, vis);
}
}
vis[x] = false;
}

President

01 背包,比赛时转移方程弄错了一个细节,本来以为和答案是等价的,赛后改下就过了。

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
public static void solve() {
int n = io.nextInt();
int sum = 0;
int[] x = new int[n];
int[] y = new int[n];
int[] z = new int[n];
for (int i = 0; i < n; i++) {
x[i] = io.nextInt();
y[i] = io.nextInt();
z[i] = io.nextInt();
sum += z[i];
}

long[] dp = new long[sum + 1];
Arrays.fill(dp, Long.MAX_VALUE);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = sum; j >= z[i]; j--) {
if (dp[j - z[i]] == Long.MAX_VALUE) continue;
dp[j] = Math.min(dp[j], dp[j - z[i]] + Math.max(0, (y[i] - x[i] + 1) / 2));
}
}

long ans = Long.MAX_VALUE;
for (int i = (sum + 1) / 2; i <= sum; i++) {
ans = Math.min(ans, dp[i]);
}
io.println(ans);
}

Avoid Eye Contact

模拟题,比赛时想复杂了,直接模拟就好。

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public static void solve() {
int h = io.nextInt(), w = io.nextInt();
String[] a = new String[h];
for (int i = 0; i < h; i++) {
a[i] = io.next();
}
int s = -1, g = -1;
boolean[][] mark = new boolean[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char c = a[i].charAt(j);
if (c == 'S') {
s = i * w + j;
} else if (c == 'G') {
g = i * w + j;
} else if (c == '#') {
mark[i][j] = true;
} else if (c == '^') {
mark[i][j] = true;
for (int k = i - 1; k >= 0 && a[k].charAt(j) == '.'; k--) {
mark[k][j] = true;
}
} else if (c == 'v') {
mark[i][j] = true;
for (int k = i + 1; k < h && a[k].charAt(j) == '.'; k++) {
mark[k][j] = true;
}
} else if (c == '<') {
mark[i][j] = true;
for (int k = j - 1; k >= 0 && a[i].charAt(k) == '.'; k--) {
mark[i][k] = true;
}
} else if (c == '>') {
mark[i][j] = true;
for (int k = j + 1; k < w && a[i].charAt(k) == '.'; k++) {
mark[i][k] = true;
}
}
}
}
int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
int[] dis = new int[h * w];
Arrays.fill(dis, -1);
dis[s] = 0;
Queue<Integer> q = new LinkedList<>();
q.offer(s);
while (!q.isEmpty()) {
int z = q.poll();
int x = z / w, y = z % w;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w || mark[nx][ny]) continue;
int nz = nx * w + ny;
if (dis[nz] != -1) continue;
dis[nz] = dis[z] + 1;
q.offer(nz);
}
}
io.println(dis[g]);
}