GYM 101933I(贪心大整数)

Posted alphawa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GYM 101933I(贪心大整数)相关的知识,希望对你有一定的参考价值。

我读题有问题呀,题目中到底哪里保证数据一定至少是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));
        }
    }
    
}

以上是关于GYM 101933I(贪心大整数)的主要内容,如果未能解决你的问题,请参考以下文章

hdu 5695 Gym Class 贪心+拓扑

Codeforces.GYM101612E.Equal Numbers(贪心)

2016"百度之星" - 初赛(Astar Round2A)1006 Gym Class(HDU5695)——贪心+拓扑排序

Gym 101201I Postman (贪心)

CodeForces Gym 100685I Innovative Business (贪心)

UVaLive 6588 && Gym 100299I (贪心+构造)