P1080 国王游戏
题解:首先考虑相邻的两个大臣该如何排列。如果 i i i排在 j j j前面需要满足 a i ⋅ p r e b j < a j ⋅ p r e b i \frac{a_i \cdot pre}{b_j} <\frac{a_j \cdot pre}{b_i} bjai⋅pre<biaj⋅pre,显然 a i ⋅ b i < a j ⋅ b j a_i \cdot b_i < a_j \cdot b_j ai⋅bi<aj⋅bj然后因为交换相邻的两个大臣并不会对整个序列的前后有影响,所以我们可以考虑冒泡排序,以 a i ⋅ b i < a j ⋅ b j a_i \cdot b_i < a_j \cdot b_j ai⋅bi<aj⋅bj进行排序,但是明显冒泡太慢了,所以需要快排。最后的枚举一下即可。
import java.util.Scanner;
import java.util.Comparator;
import java.math.BigInteger;
import java.util.Arrays;
class Minister{
private int left,right;
public Minister(int x,int y) {
this.left = x;
this.right = y;
}
public int getLeft() {
return this.left;
}
public int getRight() {
return this.right;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Minister[] ministers = new Minister[n + 1];
for(int i = 0; i <= n; ++i) {
ministers[i] = new Minister(in.nextInt(),in.nextInt());
}
Arrays.sort(ministers, 1, n + 1, new Comparator<Minister>() {
@Override
public int compare(Minister o1, Minister o2) {
return o1.getLeft() * o1.getRight() - o2.getLeft() * o2.getRight();
}
});
BigInteger emper = new BigInteger(ministers[0].getLeft() + "");
BigInteger max = BigInteger.ZERO;
for(int i = 1; i <= n; ++i) {
BigInteger t = emper.divide(new BigInteger(ministers[i].getRight() + ""));
if(t.compareTo(max) > 0) {
max = t;
}
emper = emper.multiply(new BigInteger(ministers[i].getLeft() + ""));
}
System.out.println(max);
in.close();
}
}