【HDU - 2570】迷瘴 (贪心,水题,排序,卡精度有坑)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/84842317

题干:

通过悬崖的yifenfei,又面临着幽谷的考验—— 
幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身溃烂而死。 
幸好yifenfei早有防备,提前备好了解药材料(各种浓度的万能药水)。现在只需按照配置成不同比例的浓度。 
现已知yifenfei随身携带有n种浓度的万能药水,体积V都相同,浓度则分别为Pi%。并且知道,针对当时幽谷的瘴气情况,只需选择部分或者全部的万能药水,然后配置出浓度不大于 W%的药水即可解毒。 
现在的问题是:如何配置此药,能得到最大体积的当前可用的解药呢? 
特别说明:由于幽谷内设备的限制,只允许把一种已有的药全部混入另一种之中(即:不能出现对一种药只取它的一部分这样的操作)。 

Input

输入数据的第一行是一个整数C,表示测试数据的组数; 
每组测试数据包含2行,首先一行给出三个正整数n,V,W(1<=n,V,W<=100); 
接着一行是n个整数,表示n种药水的浓度Pi%(1<=Pi<=100)。 

Output

对于每组测试数据,请输出一个整数和一个浮点数; 
其中整数表示解药的最大体积,浮点数表示解药的浓度(四舍五入保留2位小数); 
如果不能配出满足要求的的解药,则请输出0 0.00。 

Sample Input

3
1 100 10
100
2 100 24
20 30
3 100 24
20 20 30

Sample Output

0 0.00
100 0.20
300 0.23

解题报告:

   因为要求体积最大,又因为每一个的体积都一样,也就是求数量最多呗。。能多用就多用,,那肯定能先用浓度小的就先用浓度小的啊、、贪心就完事了。(据说直接判断大于小于,,会WA??得用个eps卡精度(但是我没试那样会不会WA))

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
const double eps = 1e-8;
int dp[105][2];
int a[105];
int n,v,w;
int main()
{
	int t;
	cin>>t;
	double ans;
	while(t--) {
		scanf("%d%d%d",&n,&v,&w);
		int tmp;
		for(int i = 1; i<=n; i++) scanf("%d",&a[i]);//,a[i] = tmp*1.0/100;
		sort(a+1,a+n+1);
		ans = a[1]*0.01;
		if(ans > w*0.01) {
			puts("0 0.00");continue;
		}
//		printf("******%lf\n",ans);
		int ansi=1;
		for(int i = 2; i<=n; i++) {
			if((ans*(i-1) + a[i]*0.01)*1.0/i - w*0.01 <= eps ) {
				ans = (ans*(i-1) + a[i]*0.01)*1.0/i;
				ansi = i;
			}
			else break;
		}
		printf("%d %.2f\n",ansi*v,ans);
	}


	return 0 ;
 }

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/84842317
今日推荐