FatMouse

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1431

解决:641

题目描述:

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.

输入:

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.

输出:

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.

样例输入:
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
样例输出:
13.333
31.500

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <iomanip>
 4 using namespace std;
 5
 6 struct goods
 7 {
 8    int J,F;
 9    double xinjia;
10 }buf[1000];
11
12
13 bool cmp(goods a,goods b)
14 {
15
16     return  a.xinjia<b.xinjia;
17 }
18
19
20 int main()
21 {
22   int m,n,j,f;
23   while(cin>>m)
24   {
25      cin>>n;
26      if(m==-1&&n==-1) break;
27      int i;
28      for(i=0;i<n;i++)
29      {
30        cin>>j>>f;
31        buf[i].J=j;
32        buf[i].F=f;
33        buf[i].xinjia=(double)f/j;
34      }
35      sort(buf,buf+n,cmp);
36
37      double sum=0.0;
38      for(i=0;i<n;i++)
39      {
40         if(buf[i].F<m)
41         {
42            sum=sum+buf[i].J;
43            m=m-buf[i].F;
44         }
45         else
46         {
47            sum=sum+buf[i].J*(double)m/buf[i].F;
48            break;
49         }
50      }
51
52      cout<<fixed<<setprecision(3)<<sum<<endl;
53
54
55   }
56   return 0;
57 }
58
59 /**************************************************************
60     Problem: 1433
61     User: 2009declan
62     Language: C++
63     Result: Accepted
64     Time:10 ms
65     Memory:1536 kb
66 ****************************************************************/
时间: 2024-11-09 08:00:33

FatMouse的相关文章

FatMouse&#39; Trade hdu1009

题目地址: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): 66570    Accepted Submission(s): 22635 Problem Description FatMouse prepared M

[2016-03-28][HDU][1078][FatMouse and Cheese]

时间:2016-03-28 17:40:34 星期一 题目编号:[2016-03-28][HDU][1078][FatMouse and Cheese] #include <algorithm> #include <cstring> #include <cstdio> using namespace std; const int maxn = 100 + 10; int a[maxn][maxn]; int dp[maxn][maxn]; int n ,k; int d

动态规划(DP),类似LIS,FatMouse&#39;s Speed

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1108 解题报告: 1.首先按照weight从小到大排列,weight相同的按照speed从大到小排列; 2.Count[i]表示到第i个老鼠时,所求的最长“速度递减”子序列的长度: 3.path[i]=j是题目的关键,记录在Count[i]=Count[j]时,即最长“速度递减”子序列最后一个老鼠的前一只老鼠的位置 4.递归输出id void output(in

HDU 1009 FatMouse&#39; Trade

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 59851    Accepted Submission(s): 20095 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gu

HDU1078 FatMouse and Cheese 【内存搜索】

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4966    Accepted Submission(s): 2035 Problem Description FatMouse has stored some cheese in a city. The city can be considere

hdu 1078 FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5701    Accepted Submission(s): 2320 Problem Description FatMouse has stored some cheese in a city. The city can be considered as a square grid of

【贪心专题】HDU 1009 FatMouse&#39; Trade (贪心选取)

链接:click here~~ 题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物.有N个房间,每个房间里面都有食物.你可以得到J[i]单位的食物,但你需要付出F[i]单位的的猫食. 计算M磅猫食可以获得最多食物的重量. [解题思路]贪心算法,求最优解.将J[i]/F[i]的值从大到小排列,每次取最大的,局部最优,达到全局最优,从而获得最大值. 代码: // 贪心策略,优先选择投资最大的房间,每选择一次,交换次数依次减少,最后的次数用于价值最小的 //注意精度转化:1.0*(int

贪心/hdu 1009 FatMouse&#39; Trade

题意 有n种物品,每一种需要不同的消费,现在手里有m块钱,求问最多可以买多少 分析 贪心 把每一种物品的价格算出来,然后sort一下,按照价格从便宜到贵排序,能买多少买多少,买买买! Accepted Code 1 /* 2 PROBLEM:hdu1009 3 AUTHER:Nicole Lam 4 MEMO:贪心 5 */ 6 7 #include<cstdio> 8 #include<algorithm> 9 using namespace std; 10 11 12 stru

ZOJ 1109 Language of FatMouse

主要是字符串查找,有多种解法: 算法一:Trie(时间效率最高)(27888KB) #include <stdio.h> #include <stdlib.h> #include <string.h> const int CHAR_LEN = 11; /* 单词长度 */ const int ALPH_LEN = 26; /* 字母个数 */ const int MAX = 22; /* 一行字符串的最大长度 */ const char EXIST = '1'; /*

HDU FatMouse&#39; Trade

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20968    Accepted Submission(s): 6501 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua