Codeforces Round #739 (Div. 3)
# | 题目 | 分数 | 是否AC |
---|---|---|---|
A | Dislike of Threes | 800 | ✅ |
B | Who’s Opposite? | 800 | ✅ |
C | Infinity Table | 800 | ✅ |
D | Make a Power of Two | 1300 | ✅ |
E | Polycarp and String Transformation | 1800 | ❌ |
F1 | Nearest Beautiful Number (easy version) | 1900 | ✅ |
F2 | Nearest Beautiful Number (hard version) | 2100 | ✅ |
A. Dislike of Threes
题目类型
题意
分析
时间复杂度
O ( 1000 ) O( 1000 ) O(1000)
代码
static int[] TB = new int[10010];
public static void solve() throws IOException {
pw.println(TB[nextInt()]);
}
public static void makeTB() {
int num = 1;
for (int i = 1; i < TB.length; i++) {
while (num % 10 == 3 || num % 3 == 0) num++;
TB[i] = num++;
}
}
B. Who’s Opposite?
题目类型
题意
分析
时间复杂度
O ( 1 ) O( 1 ) O(1)
代码
public static void solve() throws IOException {
int a = nextInt();
int b = nextInt();
int c = nextInt();
int sum = Math.abs(a - b) * 2;
if (sum < a || sum < b || sum < c) {
pw.println(-1);
return ;
}
if (c > sum / 2) pw.println(c - sum / 2);
else pw.println(c + sum / 2);
}
C. Infinity Table
题目类型
题意
分析
时间复杂度
O ( ) O( ) O()
代码
public static void solve() throws IOException {
long k = nextLong();
if (k == 1) {
pw.println(1 + " " + 1);
return;
}
for (int i = 100001; i >= 0; i--) {
if ((i & 1) == 0) continue;
long re = getSum(i);
if (re < k) {
int y = (i + 3) / 2;
long t = k - re;
if (t <= y) pw.println(t + " " + y);
else pw.println(y + " " + (2 * y - t));
return ;
}
}
}
public static long getSum(long x) {
return (x + 1L) / 2L * (1L + x) / 2L;
}
D. Make a Power of Two
题目类型
题意
分析
时间复杂度
O ( ) O( ) O()
代码
public static void solve() throws IOException {
char[] c = String.valueOf(nextInt()).toCharArray();
int ans = Integer.MAX_VALUE;
for (int i = 0; i < 63; i++) ans = Math.min(ans, change(String.valueOf(1L << i).toCharArray(), c));
pw.println(ans);
}
public static int change(char[] s, char[] num) {
int cnt = 0;
for (char c : num) if (cnt < s.length && s[cnt] == c) cnt++;
return num.length - cnt + s.length - cnt;
}
E. Polycarp and String Transformation
题目类型
题意
分析
时间复杂度
O ( ) O( ) O()
代码
这里粘贴代码
F1. Nearest Beautiful Number (easy version)
题目类型
题意
分析
时间复杂度
O ( 10 ∗ 10 ∗ 10 ∗ 2 10 ) O( 10 * 10 * 10 * 2^{10} ) O(10∗10∗10∗210)
代码
static TreeSet<Long> set1 = new TreeSet<>();
static TreeSet<Long> set2 = new TreeSet<>();
public static void solve() throws IOException {
long n = nextLong();
int k = nextInt();
if (k == 1) {
pw.println(set1.ceiling(n));
} else {
pw.println(set2.ceiling(n));
}
}
public static void makeTB() {
for (int i = 0; i < 10; i++) {
StringBuilder s = new StringBuilder();
for (int j = 0; j < 10; j++) {
s.append((char) ('0' + i));
set1.add(getNum(s));
set2.add(getNum(s));
for (int k = 0; k < 10; k++) dp(s, (char) ('0' + k), 0);
}
}
}
public static void dp(StringBuilder s, char k, int step) {
if (step == s.length()) return ;
StringBuilder s1 = new StringBuilder(s);
s1.replace(step, step + 1, String.valueOf(k));
set2.add(getNum(s1));
dp(s1, k, step + 1);
StringBuilder s2 = new StringBuilder(s);
dp(s2, k, step + 1);
}
public static long getNum(StringBuilder s) {
return Long.parseLong(String.valueOf(s));
}
F2. Nearest Beautiful Number (hard version)
题目类型
题意
分析
时间复杂度
O ( 10 ∗ 10 ) O( 10 * 10 ) O(10∗10)
代码
public static void solve() throws IOException {
int n = nextInt();
int k = nextInt();
char[] num = String.valueOf(n).toCharArray();
TreeSet<Integer> set = new TreeSet<>();
for (char i : num) set.add(i - '0');
if (set.size() <= k) {
pw.println(n);
return ;
}
set.clear();
for (char i : num) {
if (set.size() == k - 1) break;
set.add(i - '0');
}
long ans = Long.MAX_VALUE;
for (int i = 0; i < 10; i++) {
TreeSet<Integer> set1 = new TreeSet<>(set);
set1.add(i);
ans = Math.min(ans, fun(set1, num, n, 0));
}
pw.println(ans);
}
public static long fun(TreeSet<Integer> set, char[] num, int n, int step) {
if (step == num.length) return Long.MAX_VALUE;
long res = Long.MAX_VALUE;
char[] ans = new char[num.length];
System.arraycopy(num, 0, ans, 0, step);
if (set.higher(num[step] - '0') != null) {
ans[step] = (char) (set.higher(num[step] - '0') + '0');
for (int i = step + 1; i < num.length; i++) ans[i] = (char) (set.first() + '0');
long re = Long.parseLong(String.valueOf(ans));
if (re >= n) res = Math.min(res, re);
}
if (set.contains(num[step] - '0')) res = Math.min(res, fun(set, num, n, step + 1));
return res;
}