FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33735 Accepted Submission(s): 10987
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<algorithm>
#include<cstdio>
using namespace std;
struct a
{
int j;
int f;
double r;
}s[1010];
bool cmp(struct a m,struct a n)
{
return m.r>n.r;
}
int main()
{
int m,n,i;
while(cin>>m>>n)
{
if(m==-1 && n==-1) break;
for(i=0;i<n;i++)
{
cin>>s[i].j>>s[i].f;
if(s[i].f==0)
{
s[i].r=1010;
}
else
{
s[i].r=(double)s[i].j/s[i].f;
}
}
sort(s,s+n,cmp);
int k=0;
double g=0;
while(m && k<n)
{
if(m>=s[k].f)
{
g+=s[k].j;
m-=s[k].f;
k++;
}
else
{
g+=s[k].r*m;
m=0;
}
//if(k==n) break;
}
printf("%.3lf\n",g);
}
}