传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1009

FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 93676    Accepted Submission(s): 32566

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
 
Author
CHEN, Yue
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1051 1052 1049 2187 1001 
 
题目意思:
一共有n个房子,每个房子里有老鼠喜欢吃的javabeans,但是每个房间里的javabeans的价格不一样。老鼠用m元,问m元最多可以卖多少javabeans,其中每个房间里的javabeans可以被分割。
 
分析:按照单价排序(价值除以重量),一直选择单价最大的的全部,当不能选择全部的时候,选择一部分
 
code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<memory.h>
#include<math.h>
#define eps 1e-7
using namespace std;
#define max_v 1005
struct node
{
double v,c;
}p[max_v];
bool cmp(node a,node b)
{
return (a.v/a.c)>(b.v/b.c);//单价
}
int main()
{
int n,m;
double sum;
int i;
while(cin>>m>>n)
{
if(n==-&&m==-)
break;
for(i=;i<n;i++)
cin>>p[i].v>>p[i].c;
sort(p,p+n,cmp);
sum=;
for(i=;i<n;i++)
{
if(m>=p[i].c)
{
sum+=p[i].v;
m-=p[i].c;
}else
{
sum+=(m*(p[i].v/p[i].c));
break;
}
}
printf("%0.3lf\n",sum);
}
return ;
}
 
 

HDU 1009 FatMouse' Trade(简单贪心)的更多相关文章

  1. hdu 1009 FatMouse&#39; Trade

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

  2. HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  3. FatMouse&#39; Trade(杭电1009)

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

  4. 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 ...

  5. HDU 1009:FatMouse&#39; Trade(简单贪心)

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

  6. [题解]hdu 1009 FatMouse' Trade(贪心基础题)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  7. HDU 1009 FatMouse' Trade (贪心)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1009 题目大意:肥鼠准备了 磅的猫粮,准备和看管仓库的猫交易,仓库里装有他最喜爱的食物 豆.仓库有 个 ...

  8. HDU 1009 FatMouse' Trade(贪心)

    FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...

  9. hdu 1009:FatMouse' Trade(贪心)

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

随机推荐

  1. poj 1947 树形背包 (删边)

    http://blog.csdn.net/woshi250hua/article/details/7632785 这道题我一开始想的dp[i][j],i是节点,j是删除的点数,dp是最少删边的个数,然 ...

  2. 多版本python如何切换

    一.在命令行中 通过py -x 二.在py文件中 头部字段添加 #!python2 或 #!python3 即可调用相应版本解释器 命令行调用python:py helloworld.py

  3. JS判断浏览器类型,JS判断客户端操作系统

    JS判断浏览器类型 function judge(){ var browser = { versions: function() { var u = navigator.userAgent, app ...

  4. cf1037E. Trips(图论 set)

    题意 题目链接 Sol 倒着考虑!倒着考虑!倒着考虑! 显然,一个能成为答案的子图一定满足,其中任意节点的度数\(>= k\) 那么倒着维护就只用考虑删除操作,如果一个点不合法的话就把它删掉,然 ...

  5. wampserver 点击跳转localhost变0.0.0.0的解决方法!

    最近下载新版本wampserver发现点击项目不会自动添加localhost了,导致访问项目很麻烦. 修改如下 修改wamp根目录下的wampmanager.conf urlAddLocalhost ...

  6. 003客户端负载均衡Ribbon & 短路器Hystrix

    1.POM配置 和普通Spring Boot工程相比,仅仅添加了Eureka.Ribbon.Hystrix依赖和Spring Cloud依赖管理 <dependencies> <!- ...

  7. 【Python】区分List 和String

    区分String和list String can't mutate 每次变更实质上开辟新的资源 List 可变更 警惕指针

  8. swoole 创建UDP服务器

    udp_server.php <?php // 创建Server对象,监听 127.0.0.1:9502端口,类型为SWOOLE_SOCK_UDP $serv = new swoole_serv ...

  9. java音频播放器

    java音频播放器备份,支持wav,mp3 都是摘抄于网络,wav播放,mp3播放 播放wav版本 包: 不需要其他jar包 代码: package com; import javax.sound.s ...

  10. leetcode-wildcard matching-ZZ

    http://yucoding.blogspot.com/2013/02/leetcode-question-123-wildcard-matching.html 几个例子: (1) acbdeabd ...