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]);
}

Codeforces Round 894 (Div. 3)

Gift Carpet

从左到右每列贪心取即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void solve() {
int m = io.nextInt(), n = io.nextInt();
String[] g = new String[m];
for (int i = 0; i < m; i++) {
g[i] = io.next();
}
int idx = 0;
String s = "vika";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[j].charAt(i) == s.charAt(idx)) {
idx++;
break;
}
}
if (idx == s.length()) {
io.println("YES");
return;
}
}
io.println("NO");
}

Sequence Game

构造题。当 \(b_{i-1}>b_{i}\) 时,在两个数中间添加一个 \(1\) 即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void solve() {
int n = io.nextInt();
int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = io.nextInt();
}
List<Integer> ans = new ArrayList<>();
ans.add(b[0]);
for (int i = 1; i < n; i++) {
if (b[i] < b[i - 1]) ans.add(1);
ans.add(b[i]);
}
io.println(ans.size());
for (int x : ans) io.print(x + " ");
io.println();
}

Flower City Fence

阅读理解。题目中的“对角线对称”这个概念根本不用管,就是不断对区间做加法,然后判断是否和原数组相等,可以使用差分 + 前缀和解决。看完题解,发现其实也可以 \(O(1)\) 空间解决,因为数组是非递增的,按顺序遍历就行,具体见代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void solve() {
int n = io.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = io.nextInt();
}
for (int i = 0, j = n; i < n; i++) {
for (; j > 0 && a[j - 1] <= i; j--) ;
if (a[i] != j) {
io.println("NO");
return;
}
}
io.println("YES");
}

Ice Cream Balls

题目描述很有问题,其实就是问从一个长度为 \(m\) 的序列中选两个值组成集合,使得不同集合的数目恰好为 \(n\) 的 \(m\) 是多少。可以先二分求 \(x\),使得 \(C_{x}^{2}\leq n\) 且 \(C_{x+1}^{2}>n\)。然后答案就是 \(x+(n-C_{x}^{2})\),表示 \([1,n-C_{x}^{2}]\) 范围内的每个数各取两个,以及 \([n-C_{x}^{2}+1,x]\) 范围内的每个数各取一个。PS:读题很容易漏掉恰好两个字。

1
2
3
4
5
6
7
8
9
10
public static void solve() {
long n = io.nextLong();
long lo = 2, hi = (long) 1e9 * 2;
while (lo <= hi) {
long mid = lo + (hi - lo) / 2;
if (mid * (mid - 1) / 2 <= n) lo = mid + 1;
else hi = mid - 1;
}
io.println(hi + (n - hi * (hi - 1) / 2));
}

Kolya and Movie Theatre

做这道题时漏掉“开业前一天去过电影院”这个条件,导致想了半天。答案要求最多看 \(m\) 部电影的最大娱乐价值,首先我们可以观察到娱乐值的下降幅度只与最后一次去电影院的日期 \(x\) 有关,即下降幅度为 \(x\cdot d\)。所以我们可以从前往后枚举 \(x\),并且维护最大长度为 \(m\) 的优先队列,来保证最多看 \(m\) 部电影。需要注意电影的娱乐值可能是负数,而我们只需要在优先队列中存储正数即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void solve() {
int n = io.nextInt(), m = io.nextInt(), d = io.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = io.nextInt();
PriorityQueue<Integer> q = new PriorityQueue<>();
long sum = 0L, ans = 0L;
for (int i = 0; i < n; i++) {
if (a[i] <= 0) continue;
q.offer(a[i]);
sum += a[i];
if (q.size() > m) sum -= q.poll();
ans = Math.max(ans, sum - (long) d * (i + 1));
}
io.println(ans);
}

Magic Will Save the World

初见时想到的是二分时间 + 动态规划,赛后优化发现可以直接动态规划做。我是用背包做的,\(dp[i][j]\) 表示前 \(i\) 个怪物使用 \(j\) 点法术值能够击败的怪物总强度最大是多少,然后枚举水法术值计算答案。但是其实可以不用这样,我们只需要知道怪物的子集的所有可能强度是多少,然后枚举所有能够到达的强度即可。(C++ 位图很方便)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void solve() {
int w = io.nextInt(), f = io.nextInt(), n = io.nextInt();
int sum = 0;
int[] s = new int[n];
for (int i = 0; i < n; i++) {
s[i] = io.nextInt();
sum += s[i];
}
boolean[] dp = new boolean[sum + 1];
dp[0] = true;
for (int i = 0; i < n; i++) {
for (int j = sum; j >= s[i]; j--) {
dp[j] = dp[j] || dp[j - s[i]];
}
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i <= sum; i++) {
if (!dp[i]) continue;
ans = Math.min(ans, Math.max((i + w - 1) / w, (sum - i + f - 1) / f));
}
io.println(ans);
}

The Great Equalizer

很容易就可以得出结论,设备的输出值是数组的最大值 + 排序后相邻元素的最大差值,但是不知道怎么维护。使用 C++ 的 multiset 很容易写,详细见大佬的代码