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

第 359 场力扣周赛

判别首字母缩略词

Java

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public boolean isAcronym(List<String> words, String s) {
int n = words.size(), m = s.length();
if (n != m) return false;
for (int i = 0; i < n; i++) {
if (words.get(i).charAt(0) != s.charAt(i)) {
return false;
}
}
return true;
}
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool isAcronym(vector<string>& words, string s) {
int m = words.size(), n = s.size();
if (m != n) return false;
for (int i = 0; i < n; i++) {
if (words[i][0] != s[i]) {
return false;
}
}
return true;
}
};

k-avoiding 数组的最小总和

贪心。

Java

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

C++

1
2
3
4
5
6
7
class Solution {
public:
int minimumSum(int n, int k) {
int m = min(k / 2, n);
return (m * (m + 1) + (k * 2 + n - m - 1) * (n - m)) / 2;
}
};

销售利润最大化

不从动态规划的角度思考,我首先用的是对左端点排序。如果用动态规划,那么根据转移方程就会对右端点排序,处理方式也比对左端点排序简单一些。还可以不排序做,使用桶存储相同 \(end\) 的 \(offer\),分别处理每个桶。

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int maximizeTheProfit(int n, List<List<Integer>> offers) {
Collections.sort(offers, (a, b) -> a.get(1) - b.get(1));
offers.add(List.of(n - 1, n - 1, 0));
int m = offers.size(), i = 0;
int[] leftMax = new int[n + 1];
for (var offer : offers) {
int s = offer.get(0), e = offer.get(1), g = offer.get(2);
for (; i <= e; i++) leftMax[i + 1] = leftMax[i];
leftMax[e + 1] = Math.max(leftMax[e + 1], leftMax[s] + g);
}
return leftMax[n];
}
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int maximizeTheProfit(int n, vector<vector<int>>& offers) {
vector<vector<pair<int, int>>> groups(n);
for (auto &offer : offers) {
groups[offer[1]].emplace_back(offer[0], offer[2]);
}
vector<int> f(n + 1);
for (int end = 0; end < n; end++) {
f[end + 1] = f[end];
for (auto &[start, gold] : groups[end]) {
f[end + 1] = max(f[end + 1], f[start] + gold);
}
}
return f[n];
}
};

找出最长等值子数组

Java

滑动窗口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int longestEqualSubarray(List<Integer> nums, int k) {
int n = nums.size();
Map<Integer, Integer> map = new HashMap<>();
int lo = 0, hi = 0, ans = 0;
while (hi < n) {
map.merge(nums.get(hi++), 1, Integer::sum);
if (hi - lo - map.get(nums.get(lo)) > k) {
map.merge(nums.get(lo++), -1, Integer::sum);
}
ans = Math.max(ans, map.get(nums.get(lo)));
}
while (lo + 1 < n) {
map.merge(nums.get(lo++), -1, Integer::sum);
ans = Math.max(ans, map.get(nums.get(lo)));
}
return ans;
}
}

滑动窗口(优化):

  • 优化一:观察到 \(1\leq nums[i]\leq nums.lenth\),所以可以用数组模拟哈希表。
  • 优化二:滑动窗口直接枚举右端点,这样可以枚举到所有情况。但是如何保证删除的元素数量小于等于 \(k\) 呢?当左端点的值 \(nums[i]\) 不能构成等值数组,则将左端点右移。为什么这样可以保证?当 \(nums[i]\neq nums[j]\) 时,移动左端点不影响答案;当 \(nums[i]=nums[j]\) 时,移动左端点可以保证删除的元素数量小于等于 \(k\)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int longestEqualSubarray(List<Integer> nums, int k) {
int n = nums.size(), ans = 0;
int[] map = new int[n + 1];
for (int i = 0, j = 0; j < n; j++) {
map[nums.get(j)]++;
if (j - i + 1 - map[nums.get(i)] > k) {
map[nums.get(i++)]--;
}
ans = Math.max(ans, map[nums.get(j)]);
}
return ans;
}
}

C++

分组 + 双指针:

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 longestEqualSubarray(vector<int>& nums, int k) {
int n = nums.size(), ans = 0;
vector<vector<int>> pos(n + 1);
for (int i = 0; i < n; i++) {
pos[nums[i]].push_back(i);
}
for (auto &ps : pos) {
int left = 0;
for (int right = 0; right < ps.size(); right++) {
while (ps[right] - ps[left] - right + left > k) {
left++;
}
ans = max(ans, right - left + 1);
}
}
return ans;
}
};