HDU1009_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
Author
CHEN, Yue
Source
ZJCPC2004
题目大意:有N个房间,每一个房间存有FatMouse喜欢吃的食物。可是每一个房间
的食物都须要用对应的猫粮去换。
FatMouse 有M磅的猫粮,为它最多能换到多
少的食物。
思路:贪心方法。用结构体存每间房间的食物量和所需猫粮量。
按食物的单位价格(
即食物/猫粮的大小)进行排列,每次选单位价格最小的购买,知道M磅猫粮用完
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct warehouse
{
double j;
double f;
}a[1100];
bool cmp(warehouse a,warehouse b)
{
return a.f/a.g < b.f/b.g;
}
int main()
{
int N;
double M;
while(~scanf("%lf%d",&M,&N)&& (M!=-1||N!=-1))
{
memset(a,0,sizeof(a));
for(int i = 0; i < N; i++)
{
scanf("%lf%lf",&a[i].j,&a[i].f);
}
sort(a,a+N,cmp);
double sum = 0;
for(int i = 0; i < N; i++)
{
if(M <= 0.000001)
break;
if(M >= a[i].f)
{
sum += a[i].j;
M -= a[i].f;
}
else
{
sum += M*a[i].j/a[i].f;
M = 0;
}
}
printf("%.3lf\n",sum);
}
return 0;
}
HDU1009_FatMouse' Trade【贪心】【水题】的更多相关文章
- LightOJ 1166 Old Sorting 置换群 或 贪心 水题
LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : ...
- DP+贪心水题合集_C++
本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本次是最后一篇免费的考试题解,以后的考试题目以及题解将会以付费的方式阅读,题目质量可以拿本次作为参考 本来半个月前就已经搞得差不多了,然后 ...
- 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)
http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...
- Gym 101873K - You Are Fired - [贪心水题]
题目链接:http://codeforces.com/gym/101873/problem/K 题意: 现在给出 $n(1 \le n \le 1e4)$ 个员工,最多可以裁员 $k$ 人,名字为 $ ...
- HDU 4334——Trouble——————【贪心&水题】
Trouble Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 2111 ACM 贪心 水题
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2111 题意:知道背包容量和物品单价.体积.问能买到的最大价值? 注意:单价指的是单位体积的价格 思路:先把 ...
- Ride to Office(贪心水题)
[题目链接] http://noi.openjudge.cn/ch0406/2404/ [算法] 一开始zz了,先按时间排序然后如果速度超过当前男主速度,且在男主到达目的地前超过男主则最终男主和这个人 ...
- hdu 1051:Wooden Sticks(水题,贪心)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 【UOJ#82】【UR #7】水题生成器(贪心)
[UOJ#82][UR #7]水题生成器(贪心) 题面 UOJ 题解 把\(n!\)的所有约数搜出来,这个个数不会很多. 然后从大往小能选则选就好了. #include<iostream> ...
随机推荐
- hihoCoder #1349 Nature Numbers
题目大意 考虑自然数构成的序列 $a$:$01234567891011\dots$,序列下标从 $0$ 开始,即 $a_0 =0, a_1 = 1$ . 求 $a_n$($0\le n\le 10^{ ...
- python基于SQLAlchemy的DBtools
新版,只创建一次线程池 # -*- coding: utf-8 -*- from sqlalchemy import create_engine from sqlalchemy.orm import ...
- uva 12723 概率dp
Dudu is a very starving possum. He currently stands in the first shelf of a fridge. This fridge isco ...
- hdu 6021 MG loves string
MG loves string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others ...
- Nginx配置https双向认证
1. 前期的准备工作: 安装openssl和nginx的https模块 cd ~/ mkdir ssl cd ssl mkdir demoCA cd demoCA mkdir newcert ...
- 应用css3制作loading效果
参考秒味课堂 代码发出来备忘 html <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- Opus 和 AAC 声音编码格式
Opus编码器 是一个有损声音编码的格式,由互联网工程任务组(IETF)近来开发,适用于网络上的实时声音传输,标准格式为RFC 6716.Opus 格式是一个开放格式,使用上没有任何专利或限制. Op ...
- linux信号-------初涉
一.信号的本质 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.在软件层次上是对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是进程 ...
- POJ 1797 Heavy Transportation SPFA变形
原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- luogu U10783 名字被和谐了
链接 https://www.luogu.org/problem/show?pid=U10783 题目背景 众所周知,我们称g是a的约数,当且仅当g是正数且a mod g = 0. 众所周知,若g既是 ...