FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 59382 Accepted Submission(s): 19929
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.
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
Recommend
JGShining
题意:老鼠要拿猫粮和猫换豆,交换的规则是 F[i]*a% <==> J[i]*a%, 求老鼠可得到的最多的猫粮数目。
思路:简单贪心,将每个仓库J[i] 和F[i] 按比值降序排序,优先选比值最高的来交换,如果最后一个仓库的无法全部交换,则能交换多少就交换多少(m剩下的数量*a% 来交换 J[k] * a%)。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;
const int INF = 0x7fffffff;
struct Node
{
int x, y;
double bi;
};
bool cmp(Node a, Node b)
{
return a.bi > b.bi;
}
Node a[1000000];
int main()
{
int m, n;
while(cin >> m >> n)
{
if(m == -1 && n == -1)
break;
for(int i = 0; i < n; ++i)
{
cin >> a[i].x >> a[i].y;
a[i].bi = (double)a[i].x / (double)a[i].y;
}
sort(a, a+n, cmp);
/*for(int i = 0; i < n; ++i)
cout << a[i].bi << endl;*/
double cnt = 0;
double sum = 0;
for(int i = 0; i < n; ++i)
{
if(m - a[i].y >= 0)
{
m -= a[i].y;
sum += a[i].x;
}
else
{
double tbi = (double)m/a[i].y;
sum += (tbi*a[i].x);
break;
}
}
printf("%.3lf\n", sum);
}
return 0;
}