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. 本地windows下PHP连接远程oracle遇到的诸多问题

    任务目的:本地windows下PHP连接远程服务器下的oracle. 必须必须 确定服务器的数据库版本,如果本地的驱动和对方服务器版本不一致,会导致许多报错. 已知的oracle版本  分为 32位的 ...

  2. linux pc syncy安装问题

    linux pc 上安装syncy遇到的坑 pycurl安装可以指定curl-config,这个是根据自己机器libcurl安装位置确定,不在默认位置时要指定:python setup.py inst ...

  3. python比较两个列表

    两个列表,随机产生4个不相等的数,计算一下,相同位置上的元素相等的个数,用k1表示. b列表中的元素在a列表中,但位置不相同,有多少个,用k2表示. 例如: a=[0, 4, 7, 3]b=[7, 1 ...

  4. C语言初学 计算二元一次方程的问题

    #include<stdio.h> #include<math.h> int main() { double a,b,c,disc,x1,x2; scanf("%lf ...

  5. BZOJ 2115: [Wc2011] Xor

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 2794  Solved: 1184 [Submit][Stat ...

  6. MySQL扩展

    一.使用MySQL特有的函数!        1>到年底还有几天            select datediff('2014-12-31','2014-6-21');//此函数用于计算日期 ...

  7. Altium Designer 覆铜时过孔连接形式的设置——只将过孔连接设置为Direct Connect

    Altium Designer 在PCB覆铜时,所有的过孔和焊盘都是十字连接即Relief Connect连接的,没有像PROTEL 99SE一样只有接地的焊盘才是十字连接而过孔是直接连接的. 如下图 ...

  8. 自制单片机之十一……模数转换IC ADC0809

    我们重在实际制做,太罗嗦的内容我就不说了,只讲些跟制做有关的最精炼的知识. ADC0809是可以将我们要测量的模拟电压信号量转换为数字量从而可以进行存储或显示的一种转换IC.下面是它的管脚图和逻辑图: ...

  9. wan口mac=lan口mac加一,wlan是lan口mac加二

    (1)路由器有两个mac地址,一个用于外网(wan),一个用于内网(wlan和lan): (2)一般路由器上面或者配置路由器的网页上面只标注外网的mac地址: (3)内网的mac地址和外网mac地址一 ...

  10. C 编程调试集

    gcc rw.c rw.c:75:6: warning: conflicting types for ‘process_conn_server’ void process_conn_server(in ...