码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-4110:圣诞老人的礼物-Santa Clau’s Gifts

时间:2015-03-11 14:33:07      阅读:590      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.Scanner;

// 4110:圣诞老人的礼物-Santa Clau’s Gifts
public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int maxW = input.nextInt();
        int[][] p = new int[n][2];
        double[][] density = new double[n][2];
        
        // 获得输入并计算密度
        for (int i = 0; i < n; i++) {
            p[i][0] = input.nextInt();
            p[i][1] = input.nextInt();
            
            density[i][0] = i;
            density[i][1] = 1.0 * p[i][0] / p[i][1];
        }
        
        // 对密度进行排序
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                double[] swap = new double[2];
                if (density[i][1] < density[j][1]) {
                    swap = density[i];
                    density[i] = density[j];
                    density[j] = swap;
                }
            }
        }
        
        int sumW = 0;
        double value = 0;
        
        for (int i = 0; i < n; i++) {
            int index = (int) density[i][0];
            if (sumW >= maxW) {
                break;
            }
            
            if (p[index][1] + sumW <= maxW) {
                sumW += p[index][1];
                value += p[index][0];
            } else {
                value += ((maxW - sumW) * density[i][1]);
                sumW = maxW;
            }
        }
        
        System.out.println(value);
    }
}

 

POJ-4110:圣诞老人的礼物-Santa Clau’s Gifts

标签:

原文地址:http://www.cnblogs.com/Stone-sqrt3/p/4329553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!