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. Ubuntu下安装Qt4.5(包括X86和ARM版本)

    条件:TQ2440开发板,虚拟机安装的Ubuntu10.04,安装好天嵌自带的GCC交叉编译器参考:http://blog.csdn.net/newnewman80/article/details/6 ...

  2. ASPX在Debug模式下直接link原始CSS而非Bundle后的CSS

    由于对CSS做了bundle, debug的时候修改原始css文件不会直接反映到浏览器中, 效率较低. 于是我想在debug的时候直接link原始CSS. 很简单. <% #if DEBUG % ...

  3. index 辨别字符在字符串中的位置

    namespace index{    class Program    {        static void Main(string[] args)        {            wh ...

  4. SIFT算法:DoG尺度空间生产

    SIFT算法:DoG尺度空间生产  SIFT算法:KeyPoint找寻.定位与优化 SIFT算法:确定特征点方向  SIFT算法:特征描述子 目录: 1.高斯尺度空间(GSS - Gauss Scal ...

  5. Linux 修改默认文件关联打开程序

    从总体上讲 /etc/gnome/defaults.list 保存了全局的打开方式-/.local/share/applications/mimeapps.list 保存了个人的打开方式当这两个文件不 ...

  6. linux设置LD_LIBRARY_PATH变量

    在 Linux 下,如果你写好了自己的动态链接库,需要在其它程序里调用,则需要让这些程序能找到这些动态链接库.如果设置不对,会出现类似如下的错误: test: error while loading ...

  7. Centos6 编译安装局域网NTP服务器

    一.条件及环境: 1.CentOS:我用的是最新的CentOS 6.5 64位版,此教程应该也适用于RedHat 6及CentOS 6系列所有系统: 2.编译所需环境: 1 # yum -y inst ...

  8. Spark Repl过程分析(源码)

  9. 设置button 不可被点击

    mGetCode.setEnabled(false);//不可被点击 mGetCode.setEnabled(true);//可被点击 bt.setClickable(true);//设置点击为tru ...

  10. JAVA基础英语单词表(下)

    quantity                     / 'kwɔntiti /                    量,数量 query                             ...