FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 53691 Accepted Submission(s): 17943
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.
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
13.333 31.500
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int j,f;
double one;
}room[1111];
int cmp(node a,node b){
return a.one>b.one;
}
int main(int argc, char *argv[])
{
int n,m,i;
while(cin>>m>>n){
if(n==-1&&m==-1) break;
for(i=0;i<n;i++){
cin>>room[i].j>>room[i].f;
room[i].one=1.0*room[i].j/room[i].f;
}
sort(room,room+n,cmp);
double sum=0;
for(i=0;i<n;i++){
if(m>=room[i].f){
sum+=room[i].j;
m-=room[i].f;
}
else {
sum+=room[i].one*m;
break;
}
}
printf("%.3f\n",sum);
}
return 0;
}