接金币
排序 + 模拟
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
String s = in.readLine();
// 测试用例中会有空行,加一下判断
if (s.equals("")) s = in.readLine();
int n = Integer.parseInt(s);
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
String[] ss = in.readLine().split(" ");
arr[i][0] = Integer.parseInt(ss[0]);
arr[i][1] = Integer.parseInt(ss[1]);
}
Arrays.sort(arr, (a, b) -> (a[1] - b[1]));
int cur = 0, time = 0;
boolean flag = false;
for (int i = 0; i < n; i++) {
if (Math.abs(cur - arr[i][0]) > arr[i][1] - time) {
flag = true;
break;
}
cur = arr[i][0];
time = arr[i][1];
}
if (flag) {
out.println("Notabletocatch");
} else {
out.println("Abletocatch");
}
}
out.flush();
in.close();
}
}
又是毕业季I
数论题,详细看洛谷题解
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
String[] ss = in.readLine().split(" ");
out.println(Integer.parseInt(ss[0]) / Integer.parseInt(ss[1]));
out.flush();
in.close();
}
}
删数问题
正难则反,将删除 k 位
转换成保留 n - k 位
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
char[] ch = in.readLine().toCharArray();
int k = Integer.parseInt(in.readLine());
int n = ch.length;
StringBuilder sb = new StringBuilder();
int st = -1;
for (int i = 0; i < n - k; i++) {
char c = '9';
for (int j = st + 1; j <= k + i; j++) {
if (ch[j] < c) {
c = ch[j];
st = j;
}
}
sb.append(c);
}
int idx = 0, len = sb.length();
// 去除前导0
while (idx < len && sb.charAt(idx) == '0') idx++;
String ans = sb.substring(idx);
// 判断去除前导0后为空字符串的情况
if (!ans.equals(""))out.println(ans);
else out.print("0");
out.flush();
in.close();
}
}
谈判
贪心
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
int n = Integer.parseInt(in.readLine());
PriorityQueue<Long> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
st.nextToken();
pq.offer((long)st.nval);
}
long ans = 0L;
while (pq.size() > 1) {
long a = pq.poll(), b = pq.poll();
ans += a + b;
pq.offer(a + b);
}
out.println(ans);
out.flush();
in.close();
}
}