您的位置:首页 > 其它

HDU1009 解题报告

2017-07-24 20:23 183 查看

FatMouse' Trade

[b]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 79626    Accepted Submission(s): 27496
[/b]

[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then
b0d9
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.

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

 

[align=left]Sample Output[/align]

13.333
31.500

 

[align=left]Author[/align]
CHEN, Yue
 

[align=left]Source[/align]
ZJCPC2004

 

[align=left]Recommend[/align]
JGShining   |   We have carefully selected several similar problems for you:  1008 1050 1005 1051 1004 



做法:贪心

将输入的每一对数据按照(cat food)/javabean从大到小排列,从多的开始取,一直到猫粮用完

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct Trade{
double j; //Javabean
double f; //cat food
double jf; //每一单位cat food可以得到的Javabean
};
const double INF=2e9;
const int maxn=1000+10;
int N;
double M;  //N行,M猫粮数目
Trade trade[maxn];

void solve();
bool cmp(const Trade &t1,const Trade &t2);

int main()
{
while(cin>>M>>N)
{
if(N==-1&&M==-1) break;
for(int i=0;i<N;i++)
{
scanf("%lf%lf",&trade[i].j,&trade[i].f);
if(trade[i].f==0) trade[i].jf=INF;
else trade[i].jf=trade[i].j/trade[i].f;
}
solve();
}
return 0;
}

bool cmp(const Trade &t1,const Trade &t2)
{
return t1.jf>t2.jf;
}

void solve()
{
sort(trade,trade+N,cmp);
double ans=0;
for(int i=0;i<N;i++)
{
Trade t=trade[i];
if(M>t.f)
{
ans+=t.j;
M-=t.f;
}
else
{
ans+=(M/t.f)*t.j;
break;
}
}
printf("%.3f\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: