这是一道简单的贪心算法题,其实吧,我对于贪心的理解也没有很深。但是对付这道题来说还是绰绰有余的。但就这道题,我的感触其实是非常深的,因为做过两遍,第二次做以为还是这样简单,复制了一遍代码后发现,时间超限,这时候才意识到,如果后台数据很大的话很容易报错,于是又优化了一下。今天决定写博客纪念一下。
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.
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
好吧好吧,我们看盗版中文:
描述
FatMouse准备了M磅的猫粮,准备与守卫包含他最喜欢的食物JavaBean的仓库的猫交易。
仓库有N个房间。第i个房间包含J [i]磅的JavaBeans并且需要F [i]磅的猫粮。FatMouse不需要为房间里的所有JavaBeans进行交易,相反,如果他支付F [i] *%磅的猫粮,他可能会得到J磅的JavaBeans。这是一个实数。现在他正在为你分配这个功课:告诉他他可以获得的最大JavaBeans数量。
输入
输入包含多个测试用例。每个测试用例以包含两个非负整数M和N的行开始。然后是N行,每行包含两个非负整数J [i]和F [i]。最后一个测试用例后跟两个-1。所有整数不大于1000。
产量
对于每个测试用例,在一行中打印一个实数,最多精确到3个小数位,这是FatMouse可以获得的最大JavaBeans数量。
样本输入
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
样本输出
13.333
31.500
下面是第一次的代码:
这是我初学C语言时做的,很稚嫩,格式也不好,我没用结构体,开了三个数组用来存题目中的数。
用选择法排序笨拙的排了一下。
#include<stdio.h>
int main()
{
int m,n,i,j,k,a[50],b[50],z1,z2;
double c[50],t,s;
while(scanf("%d %d",&m,&n)!=EOF)
{
s=0;
if(m==-1&&n==-1)
break;
for(i=0;i<=n-1;i++)
{
scanf("%d %d",&a[i],&b[i]);
c[i]=(double)a[i]/b[i];
}
for(i=0;i<=n-2;i++)
{
k=i;
for(j=i+1;j<=n-1;j++)
if(c[j]>c[k])
k=j;
t=c[k];c[k]=c[i];c[i]=t;
z1=a[k];a[k]=a[i];a[i]=z1;
z2=b[k];b[k]=b[i];b[i]=z2;
}
for(i=0;i<=n-1;i++)
{
if(m>b[i])
{
m=m-b[i];
s+=a[i];
}
else
{
s+=(double)a[i]*((double)m/b[i]);
break;
}
}
printf("%.3lf\n",s);
}
return 0;
}
我还是简述一下这道题的思路吧,简单的贪心,你手中的食物能够买通几个守卫,从而获得最多的粮食,守卫需要的食物越少,并且他手里的粮食越多,你买通他就越划算,所以这时你需要计算一个比值,就是守卫手中的粮食数与所他需要食物之比,就是代码中 a[i]/b[i],即c[i]。然后将C 数组排序,同时A B数组中对应的信息也需要跟着变化,这也就是为啥我排序的地方很麻烦的原因。其实用结构体来解决就非常简单了。还有一个问题非常重要:问题是可分割的,意思是,你手中的食物如果不能完全买下守卫,也可以买下一部分,就是你给他多少食物,它对应给你多少粮食,这也是为啥用贪心的原因之一,因为问题是可分割的。
下面是优化以后的代码:
这次使用了结构体,以及sort排序,更简洁一些,时间复杂度降低了很多。主要思路还是没有变化的。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n, m;
double ans;
double t;
struct A
{
double J;
double F;
double c;
}mos[2000];
bool cmp (A a, A b)
{
return a.c > b.c;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
ans = 0;
t = (double) n;
memset(mos, 0, sizeof(mos));
if(n==-1 && m==-1)
break;
for(int i=0; i<m; i++)
{
scanf("%lf%lf", &mos[i].J, &mos[i].F);
mos[i].c = mos[i].J / mos[i].F;
}
sort(mos, mos+m, cmp);
for(int i=0; i<m; i++)
{
if( mos[i].F<=t )
{
ans += mos[i].J;
t -= mos[i].F;
}
else
{
ans += t * mos[i].c;
break;
}
}
printf("%.3lf\n", ans);
}
return 0;
}