迷瘴 HDU - 2570(贪心)

本题可以用贪心算法来做,将浓度从小到大排序,判断将其加入已选集合后浓度是否大于w,若大于,则不入选。

#include <stdio.h>
#include <algorithm>

using namespace std;

bool cmp(int a, int b)
{
	return a < b;
}

int list[100];
int main()
{
	int c, n, v, w;
	scanf("%d", &c);
	while(c--)
	{
		scanf("%d %d %d", &n, &v, &w);
		for(int i = 0; i < n; i++)
			scanf("%d", &list[i]);
		
		sort(list, list+n, cmp);
		
		int ans = 0;
		int cnt = 0;
		for(int i = 0; i < n; i++)
		{
			int tmp = ans + list[i];
			if(tmp*0.01 / (cnt+1) <= w*0.01)   //如果加入之后,浓度小于w 
			{
				ans += list[i];
				cnt++;
			}
		}
		if(!cnt)
			printf("0 0.00\n");
		else
			printf("%d %.2f\n", cnt*v, ans*0.01/cnt);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/mch2869253130/article/details/86164693