FatMouse' Trade

Problem 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
 
 
分析:按J[i]/F[i]的价值从大到小贪。
 
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
struct javabean{
int j,f;
double v;
}value[maxn];
bool cmp(javabean a,javabean b){
return a.v>b.v;
}
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)){
if(m==-&&n==-) break;
for(int i=;i<n;i++){
scanf("%d%d",&value[i].j,&value[i].f);
value[i].v=(double)value[i].j/(value[i].f*1.0);
}
sort(value,value+n,cmp);
double sum=0.0;
for(int i=;i<n;i++){
if(m>=value[i].f){
sum+=value[i].j;
m-=value[i].f;
}
else{
sum+=value[i].v*m;
break;
}
}
printf("%.3f\n",sum);
}
return ;
}

HDU-1009(简单贪心)的更多相关文章

  1. hdu 2037简单贪心--活动安排问题

    活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...

  2. hdu 4079简单贪心

    #include<stdio.h> #include<string.h> #define ll __int64 #define N 11000 struct node { in ...

  3. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  4. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  5. Hdu 1009 FatMouse' Trade 2016-05-05 23:02 86人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. HDU 1564 简单博弈 水

    n*n棋盘,初始左上角有一个石头,每次放只能在相邻的四个位置之一,不能操作者输. 如果以初始石头编号为1作为后手,那么对于每次先手胜的情况其最后一步的四周的编号必定是奇数,且此时编号为偶数,而对于一个 ...

  7. CDOJ 1502 string(简单贪心)

    题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...

  8. hdu 1009 FatMouse&#39; Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. ACM_发工资(简单贪心)

    发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...

  10. Hdu 4864(Task 贪心)(Java实现)

    Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...

随机推荐

  1. Android ListView内容变化后的动态刷新

    ListView内容变化后的动态刷新 基本知识点: 1.更新适配器Adapter数据源 2.调用适配器Adapter的刷新方法notifyDataSetChanged() 首先需要定义ListView ...

  2. 【HDOJ】4585 Shaolin

    Set可解,Treap也可解.(1) Treap /* */ #include <iostream> #include <string> #include <map> ...

  3. win8.1右键没有“新建”选项

    # 记事本中新建以下代码,复制粘贴进去,# 将.txt格式改为.reg,双击运行它就行了. Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROO ...

  4. bzoj1042

    首先直接做多重背包肯定会TLE的, 观察这个背包问题有什么特殊性呢 物品种类和重量,价值是一定的,不同的是背包的容量和物品的数量 由于当物品数量没有限制的时候,方案数是可以预处理出来的 所以我们考虑用 ...

  5. BZOJ1345: [Baltic2007]序列问题Sequence

    1345: [Baltic2007]序列问题Sequence Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 556  Solved: 274[Submi ...

  6. 【转】在Ubuntu上下载、编译和安装Android最新源代码

    原文网址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Linux Kernel和Android有一定 ...

  7. Linux停SVN提交时强制写日志

    Linux下SVN提交时强制写日志 SVN默认可以不写注释提交,有时候可能忘记写注释,有的人也没有写注释的习惯,导致翻看history的时候都不知道做了哪些更改,可以依照以下步骤修改SVN配置,强制提 ...

  8. RIDE常用快捷键

    重命名: F2 搜索关键字: F5 执行用例: F8 创建新工程: Ctrl+N 创建新测试套: Ctrl+Shift+F 创建新用例: Ctrl+Shift+T 创建新关键字: Ctrl+Shift ...

  9. 好用的ajax后台框架

    dwz 简单实用的国产jquery Ui框架 http://www.j-ui.com/#_blank

  10. AsyncSocket的使用

    AsyncSocket使用流程 安装AsyncSocket 拷贝AsyncSocket类到项目 使用AsyncSocket set delegate @interface NetWork : NSOb ...