HDU 1009 FatMouse' Trade

题目地址:点击打开链接

思路:贪心

AC代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Mouse
{
	double a;
	double b;
	double c;
};
bool comp(const Mouse &d1,const Mouse &d2)
{
	return d1.c>d2.c;
}
int main()
{
	int m,n,i;
	double sum;
	vector<Mouse> v;
	Mouse mouse;
	while(cin>>m>>n)
	{
		v.clear();
		if(m==-1&&n==-1)
			break;
		sum=0;
		for(i=0;i<n;i++)
		{
			cin>>mouse.a>>mouse.b;
			mouse.c=mouse.a/mouse.b;
			v.push_back(mouse);
		}
		sort(v.begin(),v.end(),comp);
		for(i=0;i<v.size();i++)
		{
			if(m>=v[i].b)
			{
				sum+=v[i].a;
				m-=v[i].b;
			}
			else
			{
				sum+=m*v[i].c;
				break;
			}
		}
		printf("%.3lf\n",sum);
	}
	return 0;
}


你可能感兴趣的:(HDU 1009 FatMouse' Trade)