1. 程式人生 > >迷瘴 HDU - 2570(貪心)

迷瘴 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; }