B - 贪心 基础

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

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.
 

Input

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.
 

Output

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.
 

Sample Input

5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
 

Sample Output

13.333
31.500
题目大意:老鼠贿赂猫,要偷吃东西,付出F[i]的猫粮可以吃到J[i]的javabean,同时每一个仓库里面的东西不一定全要,
你可以要任意百分比的东西,同时付出相应百分比的代价,问怎么选才能使老鼠吃到的javabean最多。
思路分析:首先区别于背包问题,背包问题中每一个物品是不能够拆分的,这也是为什么背包问题不能够贪心解决的原因,
用贪心做这道题,很显然,付出最少的代价得到最多的回报的策略是我们应该选择的,因为可以选择任意百分比,也就是说
最后的钱肯定可以花光,本道题的贪心策略就是选择性价比最高的物品优先购买,即value/cost值最大,以这个为标准从大
到小排序,每次把都选择性价比最高的仓库,就构成了正确答案。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
struct nod
{
    double c;
    double v;
    double s;
};
const int maxn=1000+10;
nod java[maxn];
bool cmp(nod a,nod b)
{
    return a.s>b.s;
}
int main()
{
    int  n,i;
    double m,k;
    while(scanf("%lf%d",&m,&n)&&(m!=-1||n!=-1))
    {
        double k=0;
        for(int i=0;i<n;i++)
        {
scanf("%lf%lf",&java[i].v,&java[i].c);
           java[i].s=java[i].v/java[i].c;
        }
        sort(java,java+n,cmp);
     for( i=0;i<=n-1;i++)
     {
         if(m>=java[i].c)
         {
             m-=java[i].c;
             k+=java[i].v;
         }
         else
         {
             k+=(double)m/java[i].c*java[i].v;
             break;
         }
     }
     printf("%.3lf\n",k);
    }
    return 0;
}

hdu 1009 贪心基础题的更多相关文章

  1. [题解]hdu 1009 FatMouse' Trade(贪心基础题)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  2. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  3. 洛谷P5019 铺设道路 题解 模拟/贪心基础题

    题目链接:https://www.luogu.org/problemnew/show/P5019 这道题目是一道模拟题,但是它有一点贪心的思想. 我们假设当前最大的深度是 \(d\) ,那么我们需要把 ...

  4. hdu 2845(dp基础题)

    题意:容易理解. 分析:以后碰到这种类型的题,就要考虑把矩阵先按行来处理,再按列处理.先算出每行能够能够得到的最大值,然后按列处理即可. 代码实现: #include<stdio.h> # ...

  5. hdu 1009 贪心算法

    博主英语不好,看懂个大概,老鼠有M磅猫食.有N个房间,每个房间前有一只猫,房间里有老鼠最喜欢的食品JavaBean,J[i].若要引开猫,必须付出相应的猫食F[i]. 当然这只老鼠没必要每次都付出所有 ...

  6. hdu 1847 博弈基础题 SG函数 或者规律2种方法

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. HDU 6047 贪心思维题

    Maximum Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 1009 FatMouse' Trade (贪心)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1009 题目大意:肥鼠准备了 磅的猫粮,准备和看管仓库的猫交易,仓库里装有他最喜爱的食物 豆.仓库有 个 ...

  9. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

随机推荐

  1. POJ 3630 , HDU 1671 Phone List - from lanshui_Yang

    这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...

  2. 小巧实用的数字加减插件(jquery插件)

    2015-12-04 近期项目需要,我将插件更新了,增加了两个参数,一个参数控制文本框是否支持输入,另一个参数则是新增了一个回调函数,返回文本框内的值.另外对代码局部重构了,优化了一下封装,需要的朋友 ...

  3. javascript学习教程之---如何从一个tab切换到banner幻灯片的转换

    一个简单的tab切换代码: <!doctype html> <html> <head> <meta charset="utf-8"> ...

  4. PHP扩展开发(3)-config.m4

    1. 宏命令 1.1. dnl 注释      1.2. 扩展的工作方式           1.2.1) PHP_ARG_WITH不需要第三方库           1.2.2) PHP_ARG_E ...

  5. Magento资源问题上CDN方案研究

    通过对Magento的了解,发现Magento的资源文件主要分布在media.js.skin三个文件夹里,media文件夹主要包括了系统自带编辑器WYSIWYG Editor 所有编辑器涉及到的资源( ...

  6. PHP优化的总结

    今天看了下PHPBB的相关规范,觉得有很多值得学习之处. 以下就几点PHP的优化做下总结: 1.in_array的用法 避免在大的数组上使用 in_array(),同时避免在循环中对包含20个以上元素 ...

  7. pyqt5通过文本对话框打开文件

    点击按钮,打开文本对话框,找一人文件,打开并显示内容 QFIleDialog                                                              ...

  8. 红帽(Red Hat Linux)下SVN服务器的安装与配置

    转:http://www.cnblogs.com/xd502djj/archive/2011/01/21/1941404.html 第一章 安装 1. 采用源文件编译安装.源文件共两个,为:subve ...

  9. 进程外组件通信之免注册com通信【原创】

    最近在搞进程外组件通信的东西,写了个demo,免注册的,一直没调通,其实就是两个问题卡了好几天,也没找到有用的资料,试了好几天终于才解决,现简单记录下来,免得大家跟我走一样的弯路.下面com端程序名称 ...

  10. Intellij Idea 12 加载weblogic8X的插件

    idea越用越习惯,只到idea12发布后,发现不在支持weblogic8X的server,无奈我们一批单一来源项目的coder. 后发现将idea11安装目录下plugins下的weblogicIn ...