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 很容易写,详细见大佬的代码

Educational Codeforces Round 153 (Rated for Div. 2)

Not a Substring

构造题。如果 \(s\) 中存在连续相同的括号,则可以构造交替出现的括号;如果 \(s\) 是交替出现的括号,那么就构造连续的括号,此时包含的唯一交替的括号就是 \(()\),特判一下即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void solve() {
char[] s = io.next().toCharArray();
int n = s.length;
boolean ok = false;
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 1]) {
ok = true;
break;
}
}
if (new String(s).equals("()")) {
io.println("NO");
return;
}
io.println("YES");
if (ok) io.println("()".repeat(n));
else io.println("(".repeat(n) + ")".repeat(n));
}

Fancy Coins

数学题。假设最终使用 \(x\) 枚价值为 \(1\) 的硬币,\(y\) 枚价值为 \(k\) 的硬币。如果 \(x\) 大于等于 \(k\),我们总是将其合成为价值为 \(k\) 的硬币,所以可以保证 \(x\) 小于 \(k\)。显然 \(x=m\bmod k\),\(y=\frac{m}{k}\)。那么需要补充多少花色硬币呢?易知,需要补充 \(\max(0,x-a_{1})\) 个价值为 \(1\) 的花色硬币,和 \(\max (0,y-a_{k}-\max (0,\frac{a_{1}-x}{k}))\) 个价值为 \(k\) 的花色硬币。

1
2
3
4
5
public static void solve() {
int m = io.nextInt(), k = io.nextInt(), a1 = io.nextInt(), ak = io.nextInt();
int ans = Math.max(0, m % k - a1) + Math.max(0, m / k - ak - Math.max(0, a1 - m % k) / k);
io.println(ans);
}

Game on Permutation

一开始的想法是,如果某个元素左边恰好只有一个小于它的元素,那么该位置就是胜位。然而暴力找每个位置左边比它小的元素个数的时间复杂度是 \(O(n^{2})\),赛时就不知道怎么优化。其实我们可以知道,给定一个序列,胜位是固定不变的。所以可以考虑维护左边的最小元素(表示下一步是否可以下棋)和最小的胜位(如果大于最小胜位,则当前位必输),然后就可以很方便的模拟出答案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void solve() {
int n = io.nextInt();
int[] p = new int[n];
int ans = 0, min = n + 1, minWin = n + 1;
for (int i = 0; i < n; i++) {
p[i] = io.nextInt();
if (min < p[i] && p[i] <= minWin) {
ans++;
minWin = Math.min(minWin, p[i]);
}
min = Math.min(min, p[i]);
}
io.println(ans);
}

Balanced String

不会不会。。o(╥﹏╥)o

AtCoder Beginner Contest 315

tcdr

模拟。

Java

1
2
3
4
5
6
7
8
9
public static void solve() {
String s = io.next();
var sb = new StringBuilder();
Set<Character> set = Set.of('a', 'e', 'i', 'o', 'u');
for (char c : s.toCharArray()) {
if (!set.contains(c)) sb.append(c);
}
io.println(sb.toString());
}

C++

1
2
3
4
5
6
7
8
void solve() {
string s;
cin >> s;
s.erase(remove_if(s.begin(), s.end(), [&](char c) {
return set{'a', 'e', 'i', 'o', 'u'}.count(c);
}), s.end());
cout << s << "\n";
}

The Middle Day

模拟。

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void solve() {
int m = io.nextInt();
int[] d = new int[m];
int tot = 0;
for (int i = 0; i < m; i++) {
d[i] = io.nextInt();
tot += d[i];
}
int mid = (tot + 1) / 2;
for (int i = 0; i < m; i++) {
if (mid <= d[i]) {
io.println(i + 1 + " " + mid);
return;
}
mid -= d[i];
}
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void solve() {
int m;
cin >> m;
int tot = 0;
vector<int> d(m);
for (int i = 0; i < m; i++) {
cin >> d[i];
tot += d[i];
}
int mid = (tot + 1) / 2;
for (int i = 0; i < m; i++) {
if (mid <= d[i]) {
cout << i + 1 << " " << mid << "\n";
return;
}
mid -= d[i];
}
}

Flavors

模拟。

Java

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
public static void solve() {
int n = io.nextInt();
List<Integer>[] buckets = new List[n + 1];
Arrays.setAll(buckets, k -> new ArrayList<>());
for (int i = 0; i < n; i++) {
int f = io.nextInt(), s = io.nextInt();
buckets[f].add(s);
}
int ans = 0, max1 = 0, max2 = 0;
for (var bucket : buckets) {
if (bucket.isEmpty()) continue;
Collections.sort(bucket, (a, b) -> b - a);
int a = bucket.get(0);
if (a > max1) {
max2 = max1;
max1 = a;
} else if (a > max2) {
max2 = a;
}
if (bucket.size() < 2) continue;
int b = bucket.get(1);
ans = Math.max(ans, a + b / 2);
}
ans = Math.max(ans, max1 + max2);
io.println(ans);
}

C++

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
void solve() {
int n;
cin >> n;
vector<vector<int>> buckets(n + 1);
for (int i = 0; i < n; i++) {
int f, s;
cin >> f >> s;
buckets[f].push_back(s);
}
int ans = 0, max1 = 0, max2 = 0;
for (auto &bucket : buckets) {
if (bucket.empty()) continue;
nth_element(bucket.begin(), bucket.begin() + 1, bucket.end(), greater());
if (bucket[0] > max1) {
max2 = max1;
max1 = bucket[0];
} else if (bucket[0] > max2) {
max2 = bucket[0];
}
if (bucket.size() < 2) continue;
ans = max(ans, bucket[0] + bucket[1] / 2);
}
ans = max(ans, max1 + max2);
cout << ans << "\n";
}

Magical Cookies

算是暴力吧。首先最多执行 \(m+n\) 次操作,然后每次操作将所有行和列遍历,判断是否可以标记。如果不优化,那么遍历的复杂度是 \(O(mn)\),总时间复杂度就是 \(O(mn(m+n))\),会超时。可以维护剩余的行数 \(r\) 和剩余的列数 \(c\),那么如果某行的某颜色的数量等于列数,那么就说明可以标记该行,列同理。这样我们就可以只维护行列中的每个颜色有多少饼干,而不需要维护位置关系,从而将遍历的时间复杂度降为 \(O(26(m+n))\)。

Java

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
public static void solve() {
int m = io.nextInt(), n = io.nextInt();
String[] arr = new String[m];
for (int i = 0; i < m; i++) {
arr[i] = io.next();
}
int[][] row = new int[m][26];
int[][] col = new int[n][26];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
row[i][arr[i].charAt(j) - 'a']++;
col[j][arr[i].charAt(j) - 'a']++;
}
}
int r = m, c = n;
boolean[] vr = new boolean[m];
boolean[] vc = new boolean[n];
for (int k = 0; k < m + n; k++) {
List<int[]> mr = new ArrayList<>();
List<int[]> mc = new ArrayList<>();
for (int i = 0; i < m; i++) {
if (vr[i]) continue;
for (int j = 0; j < 26; j++) {
if (row[i][j] == c && c >= 2) {
mr.add(new int[]{i, j});
}
}
}
for (int i = 0; i < n; i++) {
if (vc[i]) continue;
for (int j = 0; j < 26; j++) {
if (col[i][j] == r && r >= 2) {
mc.add(new int[]{i, j});
}
}
}
for (int[] p : mr) {
r--;
vr[p[0]] = true;
for (int i = 0; i < n; i++) {
col[i][p[1]]--;
}
}
for (int[] p : mc) {
c--;
vc[p[0]] = true;
for (int i = 0; i < m; i++) {
row[i][p[1]]--;
}
}
}
io.println(r * c);
}

Prerequisites

首先找到第 \(1\) 本书的所有前置书,然后对所有书进行拓扑排序,将之前找到的前置书按拓扑排序的倒序打印即可。或者直接 DFS。。

Java

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
public static void solve() {
int n = io.nextInt();
int[] indegree = new int[n];
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int i = 0; i < n; i++) {
int c = io.nextInt();
for (int j = 0; j < c; j++) {
int q = io.nextInt() - 1;
g[i].add(q);
indegree[q]++;
}
}

boolean[] mark = new boolean[n];
Queue<Integer> q = new LinkedList<>();
q.offer(0);
while (!q.isEmpty()) {
int x = q.poll();
for (int y : g[x]) {
if (mark[y]) continue;
mark[y] = true;
q.offer(y);
}
}

for (int i = 0; i < n; i++) {
if (indegree[i] == 0) {
q.offer(i);
}
}
Deque<Integer> ans = new ArrayDeque<>();
while (!q.isEmpty()) {
int x = q.poll();
if (mark[x]) ans.push(x);
for (int y : g[x]) {
if (--indegree[y] == 0) {
q.offer(y);
}
}
}
while (!ans.isEmpty()) io.print(ans.pop() + 1 + " ");
io.println();
}

C++

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
void solve() {
int n;
cin >> n;

vector<vector<int>> adj(n);
for (int i = 0; i < n; i++) {
int c;
cin >> c;
for (int j = 0; j < c; j++) {
int q;
cin >> q;
q--;
adj[i].push_back(q);
}
}

vector<bool> mark(n);
auto dfs = [&](auto self, int x) {
if (mark[x]) {
return;
}
for (auto y : adj[x]) {
self(self, y);
}
mark[x] = true;
if (x != 0) {
std::cout << x + 1 << " ";
}
};
dfs(dfs, 0);
cout << "\n";
}

Shortcuts

动态规划,调试好久。。如果所有点都选,那么答案最多为 \(10^{9}\),所以可以确定不选的点不会超过 \(30\)。然后定义状态 \(dp[i][j]\) 表示到达第 \(i\) 个点并且总共跳过 \(j\) 个点的最短距离。如何想到定义该状态呢,因为答案和具体选哪几个点无关,只和最短距离以及跳过多少个点有关,大概是这样吧。

Java

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 c = 30;
int n = io.nextInt();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; i++) {
x[i] = io.nextInt();
y[i] = io.nextInt();
}
double[][] dp = new double[n][c];
for (int i = 0; i < n; i++) {
Arrays.fill(dp[i], Integer.MAX_VALUE);
}
dp[0][0] = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < c; j++) {
for (int k = i + 1; k < n && k - i - 1 + j < c; k++) {
int nj = j + k - i - 1;
dp[k][nj] = Math.min(dp[k][nj], dp[i][j] + Math.sqrt((x[i] - x[k]) * (x[i] - x[k]) + (y[i] - y[k]) * (y[i] - y[k])));
}
}
}
double ans = Integer.MAX_VALUE;
for (int i = 0; i < c; i++) {
ans = Math.min(ans, dp[n - 1][i] + (i == 0 ? 0 : 1 << (i - 1)));
}
io.println(ans);
}