A Three Threes (打表
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n == 1) System.out.println(1);
if (n == 2) System.out.println(22);
if (n == 3) System.out.println(333);
if (n == 4) System.out.println(4444);
if (n == 5) System.out.println(55555);
if (n == 6) System.out.println(666666);
if (n == 7) System.out.println(7777777);
if (n == 8) System.out.println(88888888);
if (n == 9) System.out.println(999999999);
}
}
B Pentagon (判断是否是同类型的边
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s12 = sc.next();
String t12 = sc.next();
boolean fs12 = false; // 相邻是true
boolean ft12 = false; // 相邻是true
if (s12.charAt(1) == s12.charAt(0) + 1 || s12.charAt(1) == 'E' && s12.charAt(0) == 'A' || s12.charAt(1) == 'A' && s12.charAt(0) == 'E' || s12.charAt(0) == s12.charAt(1) + 1) fs12 = true;
if (t12.charAt(1) == t12.charAt(0) + 1 || t12.charAt(1) == 'E' && t12.charAt(0) == 'A' || t12.charAt(1) == 'A' && t12.charAt(0) == 'E' || t12.charAt(0) == t12.charAt(1) + 1) ft12 = true;
if (fs12 == ft12) System.out.println("Yes");
else System.out.println("No");
}
}
C Repunit Trio (三重循环暴力枚举
import java.util.*;
class Main {
static long[] arr = new long[1000010];
static long[] base = {1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111, 11111111111L, 111111111111L};
static long[] ans = new long[1000010];
static int idx = 0;
static int idxa = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < base.length; i ++ )
for (int j = 0; j < base.length; j++)
for (int k = 0; k < base.length; k++)
arr[idx++] = base[i] + base[j] + base[k];
Arrays.sort(arr, 0, idx);
// for (int i = 0; i < 10; i ++ ) System.out.print(arr[i] + " ");
for (int i = 0; i < idx; i ++ ) {
if (i == 0) ans[idxa ++ ] = arr[i];
else if (arr[i] != arr[i - 1]) ans[idxa ++ ] = arr[i];
}
// System.out.println(idxa);
// for (int i = 0; i < 20; i ++ ) System.out.print(ans[i] + " ");
System.out.println(ans[n - 1]);
}
}
D Erase Leaves (找出节点1的n - 1个子树的最小节点和
import java.util.*;
class Main {
static int[] h, e, ne, arr;
static int idx, idxarr;
static int n;
static int ans = 0;
private static void init() {
h = new int[2 * n + 100]; e = new int[2 * n + 100]; ne = new int[2 * n + 100];
arr = new int[2 * n + 100];
Arrays.fill(h, -1);
}
private static void add(int a, int b) {
e[idx] = b; ne[idx] = h[a]; h[a] = idx ++ ;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 找ver1的其中n - 1棵树的最小总和
n = sc.nextInt();
init();
for (int i = 1; i <= n - 1; i ++ ) {
int u = sc.nextInt(), v = sc.nextInt();
add(u, v);
add(v, u);
}
int d1 = 0;
for (int i = h[1]; i != -1; i = ne[i] ) {
int j = e[i];
arr[idxarr ++ ] = dfs(j, 1);
d1 ++ ;
}
Arrays.sort(arr, 0, idxarr);
// System.out.println("debug -> d1 : " + d1);
for (int i = 0; i < d1 - 1; i ++ ) ans += arr[i];
System.out.println(ans + 1);
}
public static int dfs(int u, int fa) {
int ret = 1;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (j == fa) continue;
ret += dfs(j, u);
}
return ret;
}
}
AtCoder 333. E Takahashi Quest (贪心 + 栈维护
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
/**
* 思路 : 贪心
* 策略 : 当我需要用伤害为x[i]的药水的时候优先使用理我最近的一天的那瓶伤害为x[i]的药水
* 实现 : 利用栈维护
* */
int[] t = new int[n + 10], x = new int[n + 10];
boolean[] f = new boolean[n + 10]; // 代表第i天的药水是否有用, false代表有用, true代表无用, 其中有用(false)有两种含义 -> 一种是打怪日, 一种是需要捡起来以后用
boolean[] at = new boolean[n + 10]; // 查看第i天是否是打怪日
for (int i = 1; i <= n; i ++ ) {
t[i] = sc.nextInt();
x[i] = sc.nextInt();
}
ArrayList<Stack<Integer>> ast = new ArrayList<>(); // ArrayList放的伤害为x[i]的药水的栈, 栈里放的是该次x[i]药水出现的日期
for (int i = 0; i < n + 10; i ++ ) ast.add(new Stack<>()); // 初始化
for (int i = 1; i <= n; i ++ ) {
if (t[i] == 1) ast.get(x[i]).push(i); // 第i天的药水先捡起来放到x[i]的栈里
else {
if (ast.get(x[i]).isEmpty()) { // 如果需要用到伤害为x[i]的药水但是栈是空的, 说明打败不了该天的怪物, 输出-1后结束程序
System.out.println(-1);
return ;
}
ast.get(x[i]).pop(); // 贪心策略 : 用离我最近的一天的一瓶
at[i] = true; // 第i天是打怪日
}
}
for (int i = 0; i < ast.size(); i ++ ) { // 剩下捡了的药水就是没必要捡起来的药水, 清空每一个栈, 标记f为true代表无用
Stack<Integer> st = ast.get(i);
while (!st.isEmpty()) {
int ti = st.pop();
f[ti] = true; // 第ti天的药水没用
}
}
int ans = 0;
int now = 0;
for (int i = 1; i <= n; i ++ ) {
if (!f[i] && !at[i]) now ++ ; // 不是打怪日, 且第i天药水显示有用, 则代表第i天需要捡起来
if (at[i]) now -- ; // 打怪日需要用一瓶库存
ans = Math.max(ans, now);
// System.out.println("debug -> now : " + now + " ans : " + ans);
}
System.out.println(ans);
for (int i = 1; i <= n; i ++ ) {
if (at[i]) continue; // 打怪日无需输出
if (!f[i]) System.out.print(1 + " ");
else System.out.print(0 + " ");
}
}
}
// 写了SpecialJudge
// 1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 0 0 1 0
// 1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0
// 1 1 1 0 0 1 0 1
// 0 0 1 1 1 0 1 1