2021 ICPC Asia Taiwan Online Programming Contest 题解
六题
·
A. Olympic Ranking
按照题意对三个数进行排序就行,我是用的一个int数组来排序,int数组的最后一个参数是初始位置。
把国家名字按照输入顺序存储,和上面int[3]是一样的,便于int排序后找到国家名。
踩坑:输入的时候先输入三个int,再输入带空格的string会读不进来,不知道为什么,所以只能直接读一整行然后split了
自定义排序
/**
* @author :Changersh
* @date : 2023/4/29 13:04
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int T, N = 310, n;
private static String[] s = new String[N];
private static Integer[][] a;
public static void main(String[] args) {
n = sc.nextInt();
a = new Integer[n][4];
for (int i = 0; i < n; i++) {
String[] sp = sc.nextLine().split(" ");
a[i][0] = Integer.parseInt(sp[0]);
a[i][1] = Integer.parseInt(sp[1]);
a[i][2] = Integer.parseInt(sp[2]);
a[i][3] = i;
s[i] = "";
for (int j = 3; j < sp.length - 1; j++) {
s[i] += sp[j] + " ";
}
s[i] += sp[sp.length - 1];
}
Arrays.sort(a, new Comparator<Integer[]>() {
@Override
public int compare(Integer[] x, Integer[] y) {
if (!x[0].equals(y[0])) return y[0] - x[0];
if (!x[1].equals(y[1])) return y[1] - x[1];
return y[2] - x[2];
}
});
pw.println(s[a[0][3]]);
pw.close();
}
}
B. Aliquot Sum
给了8秒,暴力求就行
sum一般初始化为0,答案有 1 的时候 i 一般也从 1 开始,别改。
约数和
/**
* @author :Changersh
* @date : 2023/5/1 8:59
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int T, n, sum;
public static void main(String[] args) {
T = sc.nextInt();
while (T-- > 0) solve();
pw.close();
}
private static void solve() {
n = sc.nextInt();
sum = 0;
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
sum += i;
if (n / i != i) sum += (n / i);
}
}
sum -= n;
if (sum > n) pw.println("abundant");
else if (sum < n) pw.println("deficient");
else pw.println("perfect");
}
}
C. A Sorting Problem
由题意得,求逆序对
求逆序对
/**
* @author :Changersh
* @date : 2023/4/29 13:32
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int N = 100005, n;
private static int[] a, cnt;
private static long ans = 0;
public static void main(String[] args) {
n = sc.nextInt();
a = new int[n];
cnt = new int[n];
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
merge_sort(0, n - 1);
pw.println(ans);
pw.close();
}
private static void merge_sort(int l, int r) {
if (l >= r) return;
int mid = l + ((r - l) >> 1);
merge_sort(l, mid);
merge_sort(mid + 1, r);
// merge
int i = l, j = mid + 1, p = l;
while (i <= mid && j <= r) {
if (a[i] <= a[j]) cnt[p++] = a[i++];
else {
ans += mid - i + 1;
cnt[p++] = a[j++];
}
}
while (i <= mid) cnt[p++] = a[i++];
while (j <= r) cnt[p++] = a[j++];
for (int k = l; k <= r; k++) a[k] = cnt[k];
}
}
D. Drunk Passenger
额,猜吧
数学
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer r = new StreamTokenizer(re);
PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
r.nextToken();
int n = (int)r.nval;
double ans = 1;
if (n != 2)
ans = 1.0/(n-1) + (double)(n-2)/(n-1)/2 ;
pr.println(ans);
pr.flush();
}
}
E. Eatcoin
强烈建议用python,试了一下10^100都没超,太厉害了,不用考虑溢出了
1 ^ 5 + 2 ^ 5 + 3 ^ 5+......+ n ^ 5 = (2 * (n + 1) ^ 3 * n * n * n - (n + 1) ^ 2 * n * n) // 12
然后二分天数即可
二分+公式
s = input().split(" ")
p = int(s[0])
q = int(s[1])
x = 0
y = 0
sum = 0
for i in range(1, 20000):
sum += p
sum -= q * (i ** 5)
if (sum <= 0):
x += p
break
x = sum
print(x)
l = 1
r = 10 ** 100
ans = 0
while l <= r:
mid = l + (r - l) // 2
sum = q * (mid * mid) * (mid + 1) * (mid + 1) * (2 * mid * mid + 2 * mid - 1) // 12 - p * mid + x
if (sum >= 10**99):
r = mid - 1
ans = mid
else:
l = mid + 1
print(ans)
J. JavaScript
队友写的,我没写
看了一下,是个模拟,给你两个字符串,包含字母或者数字。
如果包含字母就不能转化成数字了,结果是 NaN。
两个都是纯数字组成的情况下,才能计算,计算 (x - y)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer r = new StreamTokenizer(re);
PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
String[] num = re.readLine().split(" ");
boolean flag1 = false,flag2 = false;
for (int i = 0; i < num[0].length(); i++) {
if(num[0].charAt(i) > '9' || num[0].charAt(i) < '0'){
flag1 = true;
break;
}
}
for (int i = 0; i < num[1].length(); i++) {
if(num[1].charAt(i) > '9' || num[1].charAt(i) < '0'){
flag2 = true;
break;
}
}
if( flag1 && flag1){
pr.println("NAN");
}
else {
pr.println(Integer.parseInt(num[0]) - Integer.parseInt(num[1]));
}
pr.flush();
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)