贪心--FatMouse' Trade(结构…
来源:互联网 发布:怎么让淘宝号快速升3心 编辑:程序博客网 时间:2024/05/23 23:42
FatMouse prepared M pounds of cat food, ready to trade with thecats guarding the warehouse containing his favorite food,JavaBean.
The warehouse has N rooms. The i-th room contains Jipounds of JavaBeans and requires Fipounds of cat food. FatMouse does not have to trade for all theJavaBeans in the room, instead, he may get Ji* a% pounds of JavaBeans if he pays Fi* a% pounds of cat food. Here a is a real number. Now he isassigning this homework to you: tell him the maximum amount ofJavaBeans he can obtain.
The warehouse has N rooms. The i-th room contains Jipounds of JavaBeans and requires Fipounds of cat food. FatMouse does not have to trade for all theJavaBeans in the room, instead, he may get Ji* a% pounds of JavaBeans if he pays Fi* a% pounds of cat food. Here a is a real number. Now he isassigning this homework to you: tell him the maximum amount ofJavaBeans he can obtain.
- Input
- The input consists of multiple test cases. Each test casebegins with a line containing two non-negative integers M and N.Then N lines follow, each contains two non-negative integersJiand Firespectively. The last test case is followed by two -1's. Allintegers are not greater than 1000.
- Output
- For each test case, print in a single line a real numberaccurate up to 3 decimal places, which is the maximum amount ofJavaBeans that FatMouse can obtain.
- Sample Input
5 37 24 35 220 325 1824 1515 10-1 -1
- Sample Output
13.33331.500
一只老鼠有M磅的猫粮,另外有一只猫控制了老鼠的N个房间,这些房间里面放了老鼠爱吃的绿豆,给出每个房间的绿豆数量,和这个房间的绿豆所需要的猫粮数,现在要求老鼠用这M磅的猫粮最多能换到多少它爱吃的绿豆?
贪心题,由于所有的绿豆都是一样的,所以如果老鼠想要换到最多的绿豆,便可以换猫控制的房间里面最便宜的绿豆,也就是说先换取单位数量的绿豆所需要最少的猫粮的房间里的绿豆,这样就可以保证换到的绿豆是最多的。具体实现可以用一个结构体,里面保存每个房间里面有的绿豆的数量和换取这个房间的绿豆时所需要的猫粮的数量和换取这个房间的 单位重量的绿豆所需要的猫粮数(以下简称单价),然后再按照单价升序给这些结构体排一次序,这时就可以从最便宜的绿豆开始换了。
#include
#include
#include
using namespace std;
struct Room//struct必须在cmp前
{
int J;
int F;
double JF;
}room[10005];//3个数相关联
bool cmp(const Room a, const Room b)
{
return a.JF > b.JF; //>为升序,<为降序
}
int main()
{
int M, N;
while (cin >> M >> N&&(N!=-1)&&(M!=-1))
{
for (int i = 0; i < N;i++)
{
cin >> room[i].J >> room[i].F;
room[i].JF = (double)room[i].J / room[i].F;
}
sort(room, room + N, cmp); //结构体排序(若数列从1开始,则需在起末位置各+1
double ans=0;
for (int i = 0; i < N; i++) //挨个房间换豆子直到不够换某一个房间所有豆子,按比例换并退出
{
if ((M - room[i].F) >= 0)
{
ans += room[i].J;
M = M - room[i].F;
}
else
{
ans += M*room[i].JF;
break;
}
}
printf("%.3lf\n", ans);
}
return 0;
}
0 0
- 贪心--FatMouse' Trade(结构…
- 【贪心】 hdu1009 FatMouse' Trade
- ZOJ FatMouse' Trade 贪心
- 贪心之FatMouse' Trade
- hdu1009 FatMouse' Trade(贪心)
- FatMouse' Trade hdu+贪心
- HDU1009 FatMouse' Trade贪心
- HDU1009 FatMouse' Trade 【贪心】
- HDU1009 FatMouse' Trade 贪心
- FatMouse' Trade--贪心
- FatMouse' Trade 贪心
- 贪心初步-FatMouse' Trade
- 【贪心】FatMouse' Trade
- 贪心 FatMouse' Trade
- hdu1009 FatMouse' Trade(贪心)
- [贪心] HDU1009 FatMouse' Trade
- 贪心&FatMouse' Trade
- 1009 FatMouse' Trade 贪心
- DFS--Prime Ring Problem
- DFS--Red and Black
- 二分&&贪心--Block Towers
- 记忆化搜索(DFS+动态规划)--滑雪
- Parencodings
- 贪心--FatMouse' Trade(结构…
- The Third Cup is Free
- 贪心--Doing Homework again
- 解决vs2013scanf不可用
- 贪心-- Task
- Linux查找(find)指令的使用
- Pell数列
- dp--爬楼梯
- dp-- 数字三角