FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33106 Accepted Submission(s): 10731
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
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
friend bool operator< (node n1,node n2) //在这个分函数定义中“operator<”是固定的名称,用来定义优先级如何比较,不能改用其他名字
{
double a,b;
a=n1.a*1.0/n1.b;
b=n2.a*1.0/n2.b;
return a<b; //这里是来定义优先级如何比较,是从大到小还是别的,这里定义的是从大到小
}
int a,b;
};
int main()
{
int m,n,i;
double max;
while(scanf("%d%d",&m,&n)>0)
{
if(m==-1&&n==-1) break;max=0.0;
priority_queue<node> qi; //在main函数中定义优先队列
node s[10000];
for(i=1;i<=n;i++)
scanf("%d%d",&s[i].a,&s[i].b);
for(i=1;i<=n;i++)
qi.push(s[i]); //将元素压入到队列中
for(i=1;i<=n;i++)
{
if(qi.top().b<=m)
{
m-=qi.top().b; //取出最顶端元素,也就是最优结果
max+=qi.top().a;
}
else
{
double j;
j=m*1.0/qi.top().b;m=0;
max+=qi.top().a*j;
}
if(m==0) break;
qi.pop(); //去掉顶端
}
printf("%.3lf\n",max);
}
return 0;
}