hdu 1009 贪心基础题

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;
}

时间: 2024-08-05 19:35:24

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

hdu 4550 贪心 思维题 不错

http://acm.hdu.edu.cn/showproblem.php?pid=4550 想了挺久,然后各种分类 终于AC,如果是现场,对自己没信心的话,估计还是要WA,,,,,,然后搜题解,发现人家都认为是简单题,看来我还是太弱了,牡丹江没有做出来K看来还是自己贪心和思维有问题 d是一个Deque 最朴素的算法是,如果当前的数<=d.front(),那么插入队列的前面,否则插入队列后面,但是有零所以需要单独处理,还是自己多举例找规律 我的策略: 1.记录0的个数zero,最小非零的数的个数

hdu 4803 贪心/思维题

http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上键,最后下键补全,可是例子都过不了..... 题解參考http://www.cnblogs.com/xuesu/p/3967704.html http://www.cnblogs.com/Canon-CSU/p/3451784.html http://blog.csdn.net/keshuai199

HDU 1009贪心

代码如下.需要说明的是,之前一直WA,发现对这题来说,难点不是贪心,是对浮点数的处理,做题经验不足会导致一直不能AC.在代码第43行,用1.0*m*x[i].a/x[i].b就能AC,但是如果直接用x[i].re*m则会WA.其实计算re只是为了排序,在最后计算结果的时候如果还用这个浮点数就会导致多个浮点误差累积,使误差会更大.而直接用m*x[i].a/x[i].b这样做,由于先算的是整数之间的乘法,然后再算一次除法,那么得到浮点数的误差就只有一次,而不是上一种情况的多次误差累积. 1 #inc

HDU 1009 贪心问题

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 46205 Accepted Submission(s): 15482 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guardin

POJ 2376 Cleaning shifts 贪心 基础题

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13042   Accepted: 3373 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

hdu 1009 贪心入门

Mycode: compile error,i don't know why? #include <iostream> #include<algorithm> #include<cstdio> #define maxn 10005 using namespace std; struct room { int j; int f; double val; }; struct room r[3001]; bool compare(struct room a,struct ro

hdu 1009 贪心

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 51935    Accepted Submission(s): 17434 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

HDU 6047 贪心思维题

Maximum Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1797    Accepted Submission(s): 842 Problem Description Steph is extremely obsessed with "sequence problems" that are usuall

ZOJ 1025 Wooden Sticks(贪心 基础题)

题目链接 题意: 机器加工n个木条,每个木条有一个长度和重量.加工第一根木条需要1分钟的准备时间,接下来如果后一根木条的长度和质量都大于等于前一根木条,则不需要准备时间,否则需要1分钟的准备时间,求加工完所有木条最少时间. 比如有5根木条,长度和重量分别是(4,9), (5,2), (2,1), (3,5), (1,4),则需要2分钟就可加工第1分钟加工(1,4), (3,5), (4,9):第2分钟加工 (2,1), (5,2): 思路:将木条按长度从小到大排序,dp[i]记录第i根木条是在什