1001 Discount
按题意计算每种充值方案的折扣,取最值即可。
Code:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static float[] des;
static int[] b;
static float[] c;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int test = in.nextInt();
float[] ans = new float[test];
for (int i = 0; i < test; i++) {
int n=in.nextInt();
des=null;
des = new float[n];
b = new int[n];
c = new float[n];
for (int j = 0; j < n; j++) {
b[j]=in.nextInt();
c[j]=in.nextFloat();
des[j]=(1-c[j])/(b[j]+(1-c[j]));
}
Arrays.sort(des);
ans[i]=des[des.length-1];
}
for (float x:ans) {
String result = String.format("%.5f", x);
System.out.println(result);
}
}
}
1002 Game
Alice 交换以后有一半概率翻倍,一般概率减半;
因为x是大于0并且小于或等于1的,所以当 p > 1 时,Alice 拿到的一定是2x的那部分,所以一定不换;
然后样例给了拿到1也会交换,那么拿到小于1的肯定也会交换。
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int test = in.nextInt();
double[] p = new double[test];
String[] ans = new String[test];
for (int i = 0; i < test; i++) {
p[i]=in.nextDouble();
if(p[i]<=1) {
ans[i]="Yes";
}else {
ans[i]="No";
}
}
for(String x:ans) {
System.out.println(x);
}
}
}
1003 Permutation
当m≥⌊n/2⌋时,一定能变成 n…1,这时逆序对数最大。否则我们依次交换 1 和 n,2 和 n-1,…,一共交换 m 对。
举个例子:令n=5,m=2
1…n 的顺序排列 | 逆序对数目 |
---|---|
1 2 3 4 5 | 0 |
5 2 3 4 1 | (n-1)+(n-2) |
5 4 3 2 1 | (n-1)+(n-2)+(n-3+(n-4)) |
那么可以推出逆序对数目最大公式:m[i]*(2*n[i]-1-2*m[i])
,并且当m[i]大于n[i]/2时令其为n[i]/2;
然后枚举所有方案,依次求出逆序对最大数目
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int test = in.nextInt();
long[] n = new long[test];
long[] m = new long[test];
long[] ans = new long[test];
for (int i = 0; i < test; i++) {
n[i] = in.nextLong();
m[i] = in.nextLong();
if (m[i] > n[i] / 2) {
m[i]=n[i]/2;
ans[i] = m[i] * (2 * n[i] - 1 - 2 * m[i]);
} else {
ans[i] = m[i] * (2 * n[i] - 1 - 2 * m[i]);
}
}
for(long x:ans) {
System.out.println(x);
}
}
}
1004 Intersection
如果不考虑撞车,那么所有车一定是走左右向靠下的车道最优。假设所有车都按这个方案走,现在考虑撞车的情况,撞车只会发生在十字路口的右下角,在这种情况下,靠外的车要走靠上的车道,相应的时间会加一,此题得解。
Code:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int[] x;
static int[] y;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int test = in.nextInt();
int[] ans = new int[test];
for (int i = 0; i < test; i++) {
int n=in.nextInt();
x=null;
y=null;
x = new int[n];
y = new int[n];
for (int j = 0; j < n; j++) {
x[j]=in.nextInt();
y[j]=in.nextInt();
}
Arrays.sort(y);
if(Arrays.binarySearch(x, 2)<0) {
ans[i]=y[y.length-1]+1;
}else {
ans[i]=y[y.length-1]+2;
}
}
for(int x:ans) {
System.out.println(x);
}
}
}