Greedy Mouse
-
描述
-
A fat mouse prepared M pounds of cat food,ready to trade with the cats guarding the warehouse containing his
favorite food:peanut. The warehouse has N rooms.The ith room containsW[i] pounds of peanut and requires
F[i] pounds of cat food. Fatmouse does not have to trade for all the peanut in the room,instead,he may get
W[i]*a% pounds of peanut if he pays F[i]*a% pounds of cat food.The mouse is a stupid mouse,so can you tell
him the maximum amount of peanut he can obtain.
-
输入
- 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 W[i] and F[i] respectively. The test case is terminated by two -1. All integers are not greater than 1000. 输出
- For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of penaut that FatMouse 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<stdio.h>
#include<algorithm>
using namespace std;
struct In
{
int w;
int f;
double v;
}boy[1101];
int cmp(In a,In b)
{
return a.v>b.v;
}
int main()
{
int m,n;
int i,j;
double sum;
while(scanf("%d%d",&m,&n)&&(m!=-1&&n!=-1))
{
for(i=0;i<n;i++)
{
scanf("%d%d",&boy[i].w,&boy[i].f);
boy[i].v=boy[i].w*1.0/boy[i].f;
}
sort(boy,boy+n,cmp);
sum=0;
for(j=0;j<n;j++)
{
if(boy[j].f<=m)
{
m-=boy[j].f;
sum+=boy[j].w;
}
else
{
sum+=boy[j].w*1.0/boy[j].f*m ;
break;
}
}
printf("%.3f\n",sum);
}
return 0;
}
附解题报告:
解题报告格式
解题者 郭月盟
解题时间 20140922 18:45
编程环境 Dev c++;
题目描述
A fat mouse prepared M pounds of cat food,ready to trade with the cats guarding the warehouse containing hisfavorite food:peanut. The warehouse has N rooms.The ith room containsW[i] pounds of peanut and requires F[i] pounds of cat food. Fatmouse does not have to trade for all the peanut in the room,instead,he may get W[i]*a% pounds of peanut if he pays F[i]*a% pounds of cat food.The mouse is a stupid mouse,so can you tell
himthe maximum amount of peanut he can obtain
大致意思就是fat mouse有M猫食,对于每只猫,能保护食物不被老鼠偷的情况是不同的;
输入
The input consists of multipletest cases. Each test case begins with a line containing two non-negativeintegers M and N. Then N lines follow, each contains two non-negative integersW[i] and F[i] respectively. The test case is terminated by two -1. All integersare not greater than 1000.
输出
For each test case, print in asingle line a real number accurate up to 3 decimal places, which is the maximumamount of penaut that FatMouse can obtain.
样例输入
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
样例输出
13.333
31.500
M 代表猫食,N代表猫个数,接下来是每个猫能保护的食物,和要的猫食;
解题思路 :建立结构体,包含1:保护食物,2;需要猫食,3:单位牺牲所得价值,用sort函数按照牺牲猫食所得利益降序排列,遍历结构体中的;如果M>2, sum+=1,m-=2;否则sum+=3*m;
最后输出sum为结果。
难点 :sort 函数,结构体
关键点:贪心策略,sort函数排序
体会:多想多练,对于贪心的题目,找不到贪心策略是无法解题的
备注:
源代码
代码详细注释
/* #include<stdio.h>
#include<algorithm>
using namespace std;
struct In
{
int w;
int f;
doublev;
}boy[1101];//建立结构体
int cmp(In a,In b)//sort排序
{
return a.v>b.v;
}
int main()
{
int m,n;
inti,j;
double sum;
while(scanf("%d%d",&m,&n)&&(m!=-1&&n!=-1))
{
for(i=0;i<n;i++)
{
scanf("%d%d",&boy[i].w,&boy[i].f);
boy[i].v=boy[i].w*1.0/boy[i].f;
}
sort(boy,boy+n,cmp);
sum=0;
for(j=0;j<n;j++)
{
if(boy[j].f<=m) //猫食还够
{
m-=boy[j].f;
sum+=boy[j].w;
}
Else //不够的话
{
sum+=boy[j].w*1.0/boy[j].f*m;
break;
}
}
printf("%.3f\n",sum);
}
return 0;
}