题目1433:FatMouse(贪心算法)

46 篇文章 0 订阅
46 篇文章 0 订阅




时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1383

解决:615

题目描述:

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain. 

输入:

The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.

输出:

For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.


题目大意:老鼠准备了M 磅食物用来跟猫做交易,交易在i 个房子里面进行, 而每个房子又有限额 J 和 F, 老鼠最多付出 F 磅食物可以得到J 那么多的回报。 结合下面第一个输入来看

5 3 老鼠准备了5 单位食物在三个房子里跟猫做交易

7 2 第一个房子老鼠可以用2 单位食物换7 那么多的 JavaBean(老鼠最爱得食物)

4 3 第二个房子老鼠可以用3 单位食物换4 那么多的JavaBean

5 2 第三个房子老鼠可以用2 单位食物换5 那么多的JavaBean

最后如果食物不够换可以按照百分比换取。例如老鼠在第三个房子中5单位猫食只剩下1 单位了,那么它可以换 5*1/2 JavaBean

输出是老鼠最多可以得到多少JavaBean,也就是说可以用最优的房间换取前进,就能拿到最多的食物。


贪心算法的理解:

贪心算法就是找当前最优的解,而这些最优的解累积起来到全局便成为全局最优的解。在该问题中老鼠看哪个房子可以换得性价比最高的食物,也就是J/F,然后依据J/F对房间进行降序排序最后从高性价比的房间开始换食物,知道食物换完。其实就是你有M元前,商店里有N件商品,每件商品价值为J[i], 商品单价是F[i]元,问你怎么买可以用这M元买商品使商品总价值最大。并且商品的价值可以拆分,即(J[i]*M/F[i])注,M每买一件商品都会减少。

样例输入:
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
样例输出:
13.333
31.500
 
   
import java.util.Scanner;
 
public class Main{
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
         
        while( scanner.hasNext() ){
            double m = scanner.nextDouble();
            int n = scanner.nextInt();
             
            if(m<0 || n<0){
                break;
            }
             
            Goods goodses[] = new Goods[n];
            for (int i = 0; i < n; i++) {
                goodses[i] = new Goods();
                goodses[i].setJ(scanner.nextDouble());
                goodses[i].setF(scanner.nextDouble());
                goodses[i].caculateS();
            }
             
            quickSortGoodsByS(0, n-1, goodses);
             
            int index = 0;
            double result = 0.0000;
            while(m>0 && index<n){
                if(m >= goodses[index].getF()){
                    result += goodses[index].getJ();
                    m -= goodses[index].getF();
                }else if(m < goodses[index].getF()){
                    result += goodses[index].getJ()*m/goodses[index].getF();
                    m = 0;
                }
                index ++;
                 
            }
             
            System.out.println(String.format("%.3f", result));
             
        }
         
         
    }
     
    private static void quickSortGoodsByS(int low, int high, Goods goodses[]) {
        double key = goodses[low].getS();
        int begin = low;
        int end = high;
         
        while(low < high){
            while(goodses[low].getS() >= key && low < high){
                low ++;
            }
            while(goodses[high].getS() < key && low < high){
                high--;
            }
             
            if(low < high){
                Goods temp = goodses[low];
                goodses[low] =goodses[high];
                goodses[high] = temp;
            }
             
        }
         
         
        if(goodses[low].getS() > key){
            Goods temp = goodses[low];
            goodses[low] =goodses[begin];
            goodses[begin] = temp;
        }
         
        if(begin < low - 1){
            quickSortGoodsByS(begin, low-1, goodses);
        }
        if(end > low){
            quickSortGoodsByS(low, end, goodses);
        }
    }
     
    public static void greedHomeWork(Goods goodses[]){
         
    }
    public static class Goods implements Comparable<Goods>{
        private double j;
        private double f;
        private double s;
         
        public void caculateS(){
            this.s = j/f;
        }
 
        public double getJ() {
            return j;
        }
 
        public void setJ(double j) {
            this.j = j;
        }
 
        public double getF() {
            return f;
        }
 
        public void setF(double f) {
            this.f = f;
        }
 
        public double getS() {
            return s;
        }
 
        public void setS(double s) {
            this.s = s;
        }
 
        @Override
        public int compareTo(Goods o) {
            if(this.s > o.s){
                return 1;
            }else if(this.s < o.s){
                return -1;
            }
            return 0;
        }
         
         
    }
 
}
 
/**************************************************************
    Problem: 1433
    User: yihukurama
    Language: Java
    Result: Accepted
    Time:590 ms
    Memory:42876 kb
****************************************************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值