10、销售月饼的最大利润

题目描述

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需

求量,请你计算可以获得的最大收益是多少。


注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、

72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼,获得

 72 + 45/2 = 94.5(亿元)。

输入描述:

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类数、以及不超过500(以万吨为单位)的正整数

D表示市场最大需求量。随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个正数表示每种月饼的总售价(以亿

元为单位)。数字间以空格分隔。

输出描述:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。

输入例子:

3 20

18 15 10

75 72 45

输出例子:

94.50

java1.8

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
 
public class Main {
 
     
    public static void main(String[] args) throws Exception {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        // 第一行获取月饼种类和总需求量
        String[] nds = input.readLine().split(" ");
        int N = Integer.parseInt(nds[0]);
        int D = Integer.parseInt(nds[1]);
         
        Product[] pro = new Product[N];
        // 第二行获取每种月饼库存
        String[] scores = input.readLine().split(" ");
        // 第三行获取每种月饼的总价值
        String[] prices = input.readLine().split(" ");
        double score;
        double price;
        double subPrice;
        for(int i=0;i<N;i++){
            score = Integer.parseInt(scores[i]);
            price = Integer.parseInt(prices[i]);
            subPrice=price/score;
            pro[i] = new Product(score, price, subPrice);
        }
        // 按单价降序排列
        Arrays.sort(pro);
        double sellPrice = 0;
        for(int i=0;i<N;i++){
            if(pro[i].scoreNums <= D){
                sellPrice+=pro[i].totalPrice;
                D -= pro[i].getScoreNums();
            }else{
                sellPrice+=pro[i].subPrice*D;
                break;
            }
        }
        System.out.printf("%.2f",sellPrice);
    }
      
    static class Product implements Comparable<Product>{
          
        private double scoreNums;
          
        private double totalPrice;
          
        private double subPrice;
         
  
        public Product(double scoreNums, double totalPrice, double subPrice) {
            super();
            this.scoreNums = scoreNums;
            this.totalPrice = totalPrice;
            this.subPrice = subPrice;
        }
 
        public double getScoreNums() {
            return scoreNums;
        }
  
        public void setScoreNums(double scoreNums) {
            this.scoreNums = scoreNums;
        }
  
        public double getTotalPrice() {
            return totalPrice;
        }
  
        public void setTotalPrice(double totalPrice) {
            this.totalPrice = totalPrice;
        }
  
        public double getSubPrice() {
            return subPrice;
        }
  
        public void setSubPrice(double subPrice) {
            this.subPrice = subPrice;
        }
 
        @Override
        public int compareTo(Product o) {
            if(subPrice > o.subPrice) return -1;
            else if(subPrice < o.subPrice) return 1;
            return 0;
        }
          
    }
}

测试用例范围内最大运行耗时30ms,最大占用内存10440KB

猜你喜欢

转载自blog.csdn.net/fuyuwei2015/article/details/82469608
今日推荐