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,BaseAdapter实现列表页

    参考: 1.讲解很详细: blog.csdn.net/psuaije/article/details/7447391 总结: 代码:

  2. Default Web Site

    win7上设置默认website的物理路径

  3. Flash正则例子

    var jack:String = "zhuhai"; var arr:Array = ["23", "4", "67" ...

  4. zoj 3805 Machine

    Machine Time Limit: 2 Seconds      Memory Limit: 65536 KB In a typical assembly line, machines are c ...

  5. MVC和Web API 过滤器Filter [转]

    ASP.NET MVC 支持以下类型的操作筛选器: ·        授权筛选器.这些筛选器用于实现IAuthorizationFilter和做出关于是否执行操作方法(如执行身份验证或验证请求的属性) ...

  6. python爬虫学习(1)__抓取煎蛋图片

    #coding=utf-8 #python_demo 爬取煎蛋妹子图在本地文件夹 import requests import threading import time import os from ...

  7. CF 294C(Java大数做计数)

    题目链接:http://codeforces.com/contest/294/problem/C 代码: import java.util.*; import java.math.*; public ...

  8. HTTPS、SSL与数字证书介绍

    在互联网安全通信方式上,目前用的最多的就是https配合ssl和数字证书来保证传输和认证安全了.本文追本溯源围绕这个模式谈一谈. 名词解释 HTTPS:在HTTP(超文本传输协议)基础上提出的一种安全 ...

  9. Maven : 将Jar安装到本地仓库和Jar上传到私服 转

    http://blog.csdn.net/we_shell/article/details/49819221 Jar的maven配置 <dependency><groupId> ...

  10. Office在线预览及PDF在线预览的实现方式史上最全大集合

    Office在线预览及PDF在线预览的实现方式大集合 一.服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览 微软方:利用Office2007以上版本的一个PDF插件SaveAsPD ...