首页 > 其他 > 详细

GYM 101933I(贪心、大整数)

时间:2019-05-12 01:04:44      阅读:155      评论:0      收藏:0      [点我收藏+]

我读题有问题呀,题目中到底哪里保证数据一定至少是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(贪心、大整数)

原文:https://www.cnblogs.com/AlphaWA/p/10850780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!