Skip to content

Commit 4bfc7ca

Browse files
committed
abc290 a~d & abc291 a~e
1 parent c2c695c commit 4bfc7ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+702
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package c290;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Scanner;
5+
6+
public class Abc290_a {
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
9+
int n = scanner.nextInt();
10+
int m = scanner.nextInt();
11+
int[] a = new int[n];
12+
for (int i = 0; i < n; i++) {
13+
a[i] = scanner.nextInt();
14+
}
15+
int sum = 0;
16+
for (int i = 0; i < m; i++) {
17+
int bi = scanner.nextInt();
18+
sum += a[bi - 1];
19+
}
20+
System.out.println(sum);
21+
}
22+
}
23+
/*
24+
A - Contest Result
25+
https://atcoder.jp/contests/abc290/tasks/abc290_a
26+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package c290;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Scanner;
5+
6+
public class Abc290_b {
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
9+
int n = scanner.nextInt();
10+
int k = scanner.nextInt();
11+
String s = scanner.next();
12+
char[] chars = s.toCharArray();
13+
14+
for (int i = 0; i < n; i++) {
15+
if (k == 0) {
16+
System.out.println(s.substring(0, i) + "x".repeat(n - i));
17+
return;
18+
}
19+
if (chars[i] == 'o') {
20+
k--;
21+
}
22+
}
23+
System.out.println(s);
24+
}
25+
}
26+
/*
27+
B - Qual B
28+
https://atcoder.jp/contests/abc290/tasks/abc290_b
29+
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package c290;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.HashSet;
5+
import java.util.Scanner;
6+
import java.util.Set;
7+
import java.util.TreeSet;
8+
9+
public class Abc290_c {
10+
public static void main(String[] args) {
11+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
12+
int n = scanner.nextInt();
13+
int k = scanner.nextInt();
14+
int[] a = new int[n];
15+
for (int i = 0; i < n; i++) {
16+
a[i] = scanner.nextInt();
17+
}
18+
TreeSet<Integer> treeSet = new TreeSet<>();
19+
for (int x : a) {
20+
treeSet.add(x);
21+
}
22+
Set<Integer> set = new HashSet<>();
23+
if (treeSet.size() > k) {
24+
for (int i = 0; i < k; i++) {
25+
set.add(treeSet.pollFirst());
26+
}
27+
} else {
28+
set.addAll(treeSet);
29+
}
30+
31+
int sz = set.size();
32+
for (int i = 0; i <= sz; i++) {
33+
if (!set.contains(i)) {
34+
System.out.println(i);
35+
return;
36+
}
37+
}
38+
}
39+
}
40+
/*
41+
C - Max MEX
42+
https://atcoder.jp/contests/abc290/tasks/abc290_c
43+
44+
这里,对于序列 X,我们定义 MEX(X)为唯一的非负整数 m,满足以下条件:
45+
X 中包含 0≤i<m 的每一个整数 i。
46+
m 不包含在 X 中。
47+
相似题目: 2003. 每棵子树内缺失的最小基因值
48+
https://leetcode.cn/problems/smallest-missing-genetic-value-in-each-subtree/
49+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package c290;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Scanner;
5+
6+
public class Abc290_d {
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
9+
int t = scanner.nextInt();
10+
while (t-- > 0) {
11+
int n = scanner.nextInt();
12+
int d = scanner.nextInt();
13+
int k = scanner.nextInt();
14+
15+
k--;
16+
int a = n / getGCD(n, d);
17+
long res = (long) d * k % n + k / a;
18+
System.out.println(res);
19+
}
20+
}
21+
22+
private static int getGCD(int num1, int num2) {
23+
if (num1 == 0) {
24+
return num2;
25+
}
26+
return getGCD(num2 % num1, num1);
27+
}
28+
}
29+
/*
30+
D - Marking
31+
https://atcoder.jp/contests/abc290/tasks/abc290_d
32+
33+
为了解决这个问题,你需要知道(或通过实验等弄清楚)以下事实:
34+
设 A 和 B 为正整数,g=gcd(A, B),设 A=ag, B=bg。则整数 0, BmodA, 2BmodA, ..., (a-1)BmodA 包含 0, g, 2g, ..., (a-1)g(即 g 在 [0, a-1] 所有倍数)每个恰好一次。
35+
例如,如果 A=12, B=9,其中 g=3, a=4, b=3,则 0, BmodA, 2BmodA, 3BmodA 分别为 0, 9, 6, 3,其中 g 在 0 到 A 之间的所有倍数各出现一次。
36+
我们如何利用这个事实来解决原来的问题呢?首先,设 g 是 N 和 D 的最大公约数,设 N=gn, D=gd。
37+
由于 0, DmodN, 2DmodN, ..., (n−1)DmodN 是不相同的,所以第 i 个(1≤i≤n) 个需要标记的方格为 (i−1)DmodN。
38+
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package c291;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Scanner;
5+
6+
public class Abc291_a {
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
9+
String s = scanner.next();
10+
for (int i = 1; i <= s.length(); i++) {
11+
char ch = s.charAt(i - 1);
12+
if (Character.isUpperCase(ch)) {
13+
System.out.println(i);
14+
return;
15+
}
16+
}
17+
}
18+
}
19+
/*
20+
A - camel Case
21+
https://atcoder.jp/contests/abc291/tasks/abc291_a
22+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package c291;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Arrays;
5+
import java.util.Scanner;
6+
7+
public class Abc291_b {
8+
public static void main(String[] args) {
9+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
10+
int n = scanner.nextInt();
11+
int[] x = new int[n * 5];
12+
for (int i = 0; i < n * 5; i++) {
13+
x[i] = scanner.nextInt();
14+
}
15+
Arrays.sort(x);
16+
17+
long sum = 0L;
18+
for (int i = n; i < n * 4; i++) {
19+
sum += x[i];
20+
}
21+
double res = sum / 3.0 / n;
22+
System.out.println(res);
23+
}
24+
}
25+
/*
26+
B - Trimmed Mean
27+
https://atcoder.jp/contests/abc291/tasks/abc291_b
28+
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package c291;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.HashSet;
5+
import java.util.Scanner;
6+
import java.util.Set;
7+
8+
public class Abc291_c {
9+
public static void main(String[] args) {
10+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
11+
int n = scanner.nextInt();
12+
String s = scanner.next();
13+
14+
int x = 0, y = 0;
15+
Set<Long> set = new HashSet<>();
16+
set.add(0L);
17+
for (char ch : s.toCharArray()) {
18+
if (ch == 'R') {
19+
x++;
20+
} else if (ch == 'L') {
21+
x--;
22+
} else if (ch == 'U') {
23+
y++;
24+
} else {
25+
y--;
26+
}
27+
long val = x * 200005L + y;
28+
if (set.contains(val)) {
29+
System.out.println("Yes");
30+
return;
31+
} else {
32+
set.add(val);
33+
}
34+
}
35+
System.out.println("No");
36+
}
37+
}
38+
/*
39+
C - LRUD Instructions 2
40+
https://atcoder.jp/contests/abc291/tasks/abc291_c
41+
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package c291;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Scanner;
7+
8+
public class Abc291_d {
9+
private static final int MOD = 998244353;
10+
private static int[] a, b;
11+
static Map<Integer, Map<Integer, Integer>> memo;
12+
13+
public static void main(String[] args) {
14+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
15+
int n = scanner.nextInt();
16+
a = new int[n];
17+
b = new int[n];
18+
for (int i = 0; i < n; i++) {
19+
a[i] = scanner.nextInt();
20+
b[i] = scanner.nextInt();
21+
}
22+
23+
memo = new HashMap<>();
24+
int res = dfs(0, -1);
25+
System.out.println(res);
26+
}
27+
28+
private static int dfs(int i, int fa) {
29+
if (i == a.length) {
30+
return 1;
31+
}
32+
if (memo.containsKey(i) && memo.get(i).containsKey(fa)) {
33+
return memo.get(i).get(fa);
34+
}
35+
int res = 0;
36+
if (a[i] != fa) {
37+
res = (res + dfs(i + 1, a[i])) % MOD;
38+
}
39+
if (b[i] != fa) {
40+
res = (res + dfs(i + 1, b[i])) % MOD;
41+
}
42+
memo.computeIfAbsent(i, key -> new HashMap<>()).put(fa, res);
43+
return res;
44+
}
45+
}
46+
/*
47+
D - Flip Cards
48+
https://atcoder.jp/contests/abc291/tasks/abc291_d
49+
50+
记忆化搜索
51+
*/
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package c291;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Queue;
11+
import java.util.Scanner;
12+
import java.util.stream.Collectors;
13+
14+
public class Abc291_e {
15+
public static void main(String[] args) {
16+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
17+
int n = scanner.nextInt();
18+
int m = scanner.nextInt();
19+
int[][] xys = new int[m][2];
20+
for (int i = 0; i < m; i++) {
21+
xys[i][0] = scanner.nextInt();
22+
xys[i][1] = scanner.nextInt();
23+
}
24+
System.out.println(solve(n, xys));
25+
}
26+
27+
private static String solve(int n, int[][] xys) {
28+
// 建图
29+
Map<Integer, List<Integer>> adj = new HashMap<>();
30+
int[] deg = new int[n];
31+
for (int[] xy : xys) {
32+
int u = xy[0] - 1, v = xy[1] - 1;
33+
adj.computeIfAbsent(u, key -> new ArrayList<>()).add(v);
34+
deg[v]++;
35+
}
36+
37+
// 拓扑序需要唯一
38+
Queue<Integer> queue = new LinkedList<>();
39+
for (int i = 0; i < n; i++) {
40+
if (deg[i] == 0) {
41+
queue.add(i);
42+
}
43+
}
44+
if (queue.size() != 1) {
45+
return "No";
46+
}
47+
List<Integer> topo = new ArrayList<>();
48+
while (!queue.isEmpty()) {
49+
int sz = queue.size();
50+
int u = queue.remove();
51+
topo.add(u);
52+
for (int v : adj.getOrDefault(u, new ArrayList<>())) {
53+
deg[v]--;
54+
if (deg[v] == 0) {
55+
queue.add(v);
56+
}
57+
}
58+
// 删一个只能加一个
59+
if (queue.size() > sz) {
60+
return "No";
61+
}
62+
}
63+
if (topo.size() != n) {
64+
return "No";
65+
}
66+
67+
int x = 1;
68+
int[] ans = new int[n];
69+
for (int id : topo) {
70+
ans[id] = x++;
71+
}
72+
return "Yes" + System.lineSeparator()
73+
+ Arrays.stream(ans).mapToObj(String::valueOf).collect(Collectors.joining(" "));
74+
}
75+
}
76+
/*
77+
E - Find Permutation
78+
https://atcoder.jp/contests/abc291/tasks/abc291_e
79+
80+
唯一拓扑序
81+
*/

0 commit comments

Comments
 (0)