Codeforces Round #738 (Div. 2)
# | 题目 | 分数 | 是否AC |
---|---|---|---|
A | Mocha and Math | ✅ | |
B | Mocha and Red and Blue | ✅ | |
C | Mocha and Hiking | ✅ | |
D1 | Mocha and Diana (Easy Version) | ✅ | |
D2 | Mocha and Diana (Hard Version) | ❌ | |
E | Mocha and Stars | ❌ |
A. Mocha and Math
题目类型
题意
分析
时间复杂度
O ( n ) O( n ) O(n)
代码
public static void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
int[] b = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
int ans = a[0];
for (int i = 1; i < n; i++) ans &= a[i];
pw.println(ans);
}
B. Mocha and Red and Blue
题目类型
题意
分析
时间复杂度
O ( n ) O( n ) O(n)
代码
public static void solve() throws IOException {
int n = nextInt();
char[] s = next().toCharArray();
int cnt = 0;
for (int i = 0; i < n; i++) if (s[i] == '?') cnt++;
if (cnt == n) {
for (int i = 0; i < n; i++) pw.print((i & 1) == 0 ? 'B' : 'R');
pw.println();
return;
}
for (int i = 0; i < n; i++) {
if (s[i] != '?') {
for (int j = i; j >= 0; j--) if (s[j] == '?') s[j] = s[j + 1] == 'B' ? 'R' : 'B';
for (int j = i; j < n; j++) if (s[j] == '?') s[j] = s[j - 1] == 'B' ? 'R' : 'B';
}
}
pw.println(s);
}
C. Mocha and Hiking
题目类型
题意
分析
时间复杂度
O ( n ) O( n ) O(n)
代码
public static void solve() throws IOException {
int n = nextInt();
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextInt();
if (a[n] == 0) {
for (int i = 1; i <= n + 1; i++) pw.print(i + " ");
pw.println();
return ;
}
if (a[1] == 1) {
pw.print((n + 1) + " ");
for (int i = 1; i <= n; i++) pw.print(i + " ");
pw.println();
return ;
}
for (int i = 1; i < n; i++) if (a[i] == 0 && a[i + 1] == 1) {
for (int j = 1; j <= i; j++) pw.print(j + " ");
pw.print((n + 1) + " ");
for (int j = i + 1; j <= n; j++) pw.print(j + " ");
pw.println();
return ;
}
pw.println();
}
D1. Mocha and Diana (Easy Version)
题目类型
题意
分析
时间复杂度
O ( n 2 log n ) O( n^2 \log n ) O(n2logn)
代码
public static void solve() throws IOException {
int n = nextInt();
int m1 = nextInt();
int m2 = nextInt();
int[] fa1 = new int[n + 1];
int[] fa2 = new int[n + 1];
init(fa1, fa2);
while (m1-- > 0) unite(fa1, nextInt(), nextInt());
while (m2-- > 0) unite(fa2, nextInt(), nextInt());
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (!same(fa1, i, j) && !same(fa2, i, j)) {
ans++;
unite(fa1, i, j);
unite(fa2, i, j);
pw.println(i + " " + j);
}
}
}
System.out.println(ans);
}
public static int find(int[] fa, int x) {
return fa[x] == x ? x : (fa[x] = find(fa, fa[x]));
}
public static void unite(int[] fa, int x, int y) {
x = find(fa, x);
y = find(fa, y);
if (x == y) return ;
fa[x] = y;
}
public static boolean same(int[] fa, int x, int y) {
return find(fa, x) == find(fa, y);
}
public static void init(int[] fa1, int[] fa2) {
for (int i = 0; i < fa1.length; i++) {
fa1[i] = i;
fa2[i] = i;
}
}