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
-----------------------------------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define SIZE 1001
int J[SIZE];
int F[SIZE];
double rate[SIZE];
int bigger(int a, double keyRate, double keyJ);
int main()
{
int M, N;
int i, j, tempIndex;
double tempRate,tempJ,tempF;
double total;
while (scanf("%d%d", &M, &N) == 2)
{
total = 0;
if (M == -1 && N == -1)
break;
for (i = 0; i < N; i++)
{
scanf("%d%d", &J[i], &F[i]);
rate[i] = ((double)F[i] / J[i]);
}
//插入排序
for (i = 1; i < N; i++)
{
tempRate = rate[i];
tempJ = J[i];
tempF = F[i];
tempIndex = i;
j = i - 1;
while (j >= 0 && bigger(j, tempRate, tempJ))
{
rate[j + 1] = rate[j];
J[j + 1] = J[j];
F[j + 1] = F[j];
j = j - 1;
}
rate[j + 1] = tempRate;
J[j + 1] = tempJ;
F[j + 1] = tempF;
} for (i = 0; i < N; i++)
{
//判断比率为0,也就是免费送的情况
if (M > F[i] || rate[i] <= 0.0000001)
{
total += J[i];
M -= F[i];
}
else
{
total += (double)M / F[i] * J[i];
break;
}
}
printf("%0.3f\n", total);
}
} int bigger(int a, double keyRate, double keyJ)
{
if (fabs(rate[a] - keyRate) <= 0.000001)
return J[a] < keyJ;
else
{
return rate[a] > keyRate;
}
}

  

ACM1009:FatMouse' Trade的更多相关文章

  1. HDU1009:FatMouse' Trade(初探贪心,wait)

    FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...

  2. Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏

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

  3. FatMouse' Trade(杭电ACM---1009)

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

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

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

  5. Hdu 1009 FatMouse' Trade

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

  6. 题目1433:FatMouse (未解决)

    题目描述: FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse co ...

  7. FatMouse' Trade

    /* problem: FatMouse' Trade this is greedy problem. firstly:we should calculate the average J[i]/F[i ...

  8. HDU1009 FatMouse' Trade

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

  9. FatMouse' Trade -HZNU寒假集训

    FatMouse' Trade FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wa ...

随机推荐

  1. August 30th 2017 Week 35th Wednesday

    A lion does not concern himself with the opinion of sheep. 狮子可不会在意绵羊是怎么想的. As a sheep, you must run ...

  2. poi导出excel出现本工作薄不能再使用其他新字体的解决方法

    最近使用POI处理EXCEL,当处理的单元格太多时,就会出现,本工作薄使用字体过多,不能再使用其他新的字体的是提示. 网上很多方法告诉我,要怎么修改excel文件,但是这个解决不了问题啊,难道让客户去 ...

  3. angular cli

    1. 安装cnpm: npm install -g cnpm --registry=https://registry.npm.taobao.org 2. 安装angular/cli: cnpm ins ...

  4. 【[CQOI2018]解锁屏幕】

    状压这个东西好像没有什么能优化的高级东西,像什么斜率优化,单调队列在状压的优化上都很少见 而最常见的状压优化就是预处理优化了, 这道题就预处理一下所有点对之间连线上的点,之后压成状态就能做到\(O(2 ...

  5. hdu_1009 贪心

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

  6. python之九九乘法表

    for i in range(1,10): print( ) for j in range(1,i+1): print('%d*%d=%d '%(i,j,i*j),end="") ...

  7. 人人开源之代码生成器(renren-generator)

    本篇文章,主要包含三个部分,介绍.代码生成演示.代码分析(不会很深入)等 三个部分足以让你学会使用,实际生产可能遇到的问题不会在这遇到. 代码生成器的作用在于提高开发效率.但是这个代码生成器仍有其局限 ...

  8. Java中使用OpenSSL生成的RSA公私钥

    RSA是什么:RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的 ...

  9. PAT——1062. 最简分数

    一个分数一般写成两个整数相除的形式:N/M,其中M不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正分数 N1/M1 和 N2/M2,要求你按从小到大的顺序列出它们之间分母 ...

  10. SDOI2018一轮NOI培训 题目整理

    \(qwq\)首先,这些题对于我而言--类似于\(emmm\)洪水猛兽 \(\mathcal{Day \ \ 1}\) T1 \(\mathcal{\color{red}{Description}}\ ...