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

作者

Ligh0x74

发布于

2023-08-21

更新于

2023-08-21

许可协议

评论