FatMouse' Trade
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 30 Accepted Submission(s) : 16
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
Output
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 <cstdio>
struct cat
{
int f,j;
double p;
}a[1005];
int comp(const void *a,const void *b)
{
return(*(cat *)a).p<(*(cat *)b).p?1:-1;
}
using namespace std;
int main(int argc, char *argv[])
{
int n,m,i;
double num;
while(cin>>m>>n)
{
if(m==-1&&n==-1) break;
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].j,&a[i].f);
a[i].p=1.0*a[i].j/a[i].f;
}
qsort(a,n,sizeof(a[0]),comp);
num=0;
for(i=0;i<n;i++)
{
if(m>=a[i].f) {num+=a[i].j;m-=a[i].f;}
else
{
double x;
x=1.0*a[i].j*m/a[i].f;
num+=x;
break;
}
}
printf("%.3f\n",num);
}
return 0;
}