我读题有问题呀,题目中到底哪里保证数据一定至少是2倍关系了Orz……然后既然题意就是保证了那贪心一下即可,因为如果当前这个大的不选,那剩下一堆逐渐小于上一代的1/2的,凑起来都不如这个大的,更别说答案了。
/**
*
* @author alphawa
*/
import java.util.*;
import java.math.BigInteger;
public class Solution {
public static class alpha {
String name;
BigInteger val;
}
public static class XYYcmp implements Comparator {
public int compare(Object A, Object B) {
alpha x = (alpha)A;
alpha y = (alpha)B;
return x.val.compareTo(y.val) < 0 ? 1 : -1;
}
}
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int n = read.nextInt();
BigInteger V = read.nextBigInteger();
alpha[] a = new alpha[n];
for (int i = 0; i < n; i++) {
a[i] = new alpha();
a[i].name = read.next();
a[i].val = read.nextBigInteger();
}
Arrays.sort(a, new XYYcmp());
Vector<String> Ans = new Vector<String>();
for (int i = 0; i < n; i++) {
if (a[i].val.compareTo(V) <= 0) {
V = V.subtract(a[i].val);
Ans.add(a[i].name);
}
}
if (V.compareTo(BigInteger.valueOf(0)) != 0) {
System.out.println(0);
} else {
System.out.println(Ans.size());
for (int i = 0; i < Ans.size(); i++)
System.out.println(Ans.elementAt(i));
}
}
}
原文地址:https://www.cnblogs.com/AlphaWA/p/10850780.html
时间: 2024-10-15 03:46:16