您的位置:首页 > 其它

老人实在是饿了(贪心)

2014-05-09 23:45 127 查看

http://acm.hdu.edu.cn/showproblem.php?pid=2187

自己的第一道,贪心的题。虽然是水题,但是还是纪念一下。

Problem Description

对于幸存的灾民来说,最急待解决的显然是温饱问题,救灾部队一边在组织人员全力打通交通,一边在组织采购粮食。现在假设下拨了一定数量的救灾经费要去市场采购大米(散装)。如果市场有m种大米,各种大米的单价和重量已知,请问,为了满足更多灾民的需求,最多能采购多少重量的大米呢?

Input

输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(0<n<=1000,0<m<=1000),分别表示经费的金额和大米的种类,然后是m行数据,每行包含2个整数p和h(1<=p<=25,1<=h<=100),分别表示单价和对应大米的重量。

Output

对于每组测试数据,请输出能够购买大米的最多重量(你可以假设经费买不光所有的大米)。
每个实例的输出占一行,保留2位小数。

Sample Input

1

7 2

3 3

4 4

Sample Output

2.33

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cctype>
#define INF 0x3f3f3f3f
#define maxn 100001

using namespace std;

struct rice{
int price;
int weight;
};

rice s[maxn];

bool cmp(rice a,rice b)
{
if(a.price==b.price)
return a.weight<a.weight;
else
return a.price<b.price;
}

int main()
{
int t;
int n,m;
cin>>t;
while(t--)
{while(scanf("%d%d",&n,&m)!=EOF)
{
double sum=0;

for(int i=0;i<m;i++)
scanf("%d%d",&s[i].price,&s[i].weight);

sort(s,s+m,cmp);
for(int i=0;i<m;i++)
{
if(n-s[i].price*s[i].weight>0)
{
sum+=s[i].weight;
n-=s[i].price*s[i].weight;
}
else
{
sum+=n*(1.0/s[i].price);
break;
}
}
printf("%.2f\n",sum);
}
}

return 0;
}


贪心:顾名思意,尽可能的索取,想着全部夺过来。只为目的,其他的都忽略。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: