C - FatMouse' Trade

FatMouse‘ Trade

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

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

首先这是一道贪心算法,当f/j越大时代表这样越有利,所以进行排序;

但也有值得注意的几组数据(来自杭电的大神)

此题除了要满足例子以外,还要满足一些条件才能真正算ac:
0 1
1 0
1.000

1 0
0.000

5 4
10000 5
2000 2
100 0
300 0
10400.000

数据类型用double,就这样
 1 #include<stdio.h>
 2 int j[1010],f[1010];
 3 double a[1010];
 4 void paixu(double a[],int k[],int f[],int n)
 5 {
 6     int i=0,j=n-1,h=k[0],m=f[0];
 7     double  t=a[0];
 8     if(n>1){
 9         while(i<j)
10         {
11             for(;i<j;j--)
12             if(a[j]>t){
13                 a[i]=a[j];
14                 f[i]=f[j];
15                 k[i]=k[j];
16                 i++;
17                 break;
18             }
19             for(;i<j;i++)
20             {
21                 if(a[i]<t){
22                     a[j]=a[i];
23                     f[j]=f[i];
24                     k[j]=k[i];
25                     j--;
26                     break;
27                 }
28             }
29         }
30         a[i]=t;
31         f[i]=m;
32         k[i]=h;
33         paixu(a,k,f,i);
34         paixu(a+i+1,k+i+1,f+i+1,n-i-1);
35     }
36 }
37 void hanshu(int m,int n)
38 {
39     int i;
40     double  sum=0;
41     for(i=0;i<n;i++)
42     {
43         if(m>=f[i]){
44             sum=sum+j[i];
45             m=m-f[i];
46         }
47         else{
48             sum=sum+1.0*m/f[i]*j[i];
49             break;
50         }
51     }
52     printf("%0.3f\n",sum);
53 }
54 int main()
55 {
56     int n,m,i,t;
57     while(1)
58     {scanf("%d%d",&m,&n);
59     if(m==-1&&n==-1)break;
60     for(i=0;i<n;i++)
61         {scanf("%d%d",&j[i],&f[i]);
62         if(f[i]!=0)a[i]=1.0*j[i]/f[i];
63         else a[i]=1020;
64         }
65         paixu(a,j,f,n);
66         hanshu(m,n);
67         }
68     return 0;
69
70 }

C - FatMouse' Trade

时间: 2024-12-14 17:02:45

C - FatMouse' Trade的相关文章

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

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

【贪心专题】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

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

杭电 1009 FatMouse&#39; Trade

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

HDU1009 FatMouse&#39; Trade

题目大意: 一直老鼠想吃咖啡豆,这些咖啡豆由一只猫来看守,它需要用它有的猫粮来换. 读入两个整数m,n分别代表老鼠有的猫粮的总数,和放着咖啡豆的房间数.接下来n行,每行两个数j[i],f[i],分别代表这个房间中的总的咖啡豆的数量和需要的猫粮数,求出这时老鼠可获得的最大的猫粮数,知道接收到-1,-1结束处理. 解题思路: 典型的贪心问题中的分数背包问题.由于可以取得房间中的咖啡豆的任意部分,所以转化为分数背包为题,求出每个房间的咖啡豆的单价,我是求出了单位重量的猫粮可以换取的咖啡豆,这样我觉得可

杭电oj 1009 FatMouse&#39; Trade

Tips:本题采用贪心算法,类似于背包问题,关键在于读入数据之后,将数据按 J[i]/F[i] 从大到小排列即可. 1 /**本程序主要采用贪心算法思想,类似于背包问题*/ 2 #include<stdio.h> 3 #include<string.h> 4 int main() 5 { 6 int M,N; 7 while(scanf("%d %d",&M,&N)) 8 { 9 if(M == -1 && N == -1) 10

(hdu step 1.3.1)FatMouse&#39; Trade(在收入需要一定的付出的情况下求最大收入)

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

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪心)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 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