HDU 2570 迷障 贪心

迷瘴

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4093 Accepted Submission(s): 1402

Problem Description

通过悬崖的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

简单的贪心,先按每瓶药水的浓度从小到大排序,然后用循环拿浓度小的药水配置,直到浓度大于W即可。

这里需要强调的是(可能是对于我,因为我一直在这错):

在判断出每加了一瓶药水之后的浓度小于等于W之前,不要更新浓度! 比如:

for(i=0;i<n;i++)
{
	sum+=p[i];
	if((double)sum/(i+1)<=w)
	{
		pp=(double)sum/(i+1);
		vv+=v;
	}
	else
		break;
}

其中sum表示目前药水的总数,pp表示现在的浓度,在这里要先用if判断一下,确认加上p[i]之后的浓度小于等于W之后才去更新

pp=(double)sum/(i+1);

如果写成下面这样的就错了:

for(i=0;i<n;i++)
{
	sum+=p[i];
	pp=(double)sum/(i+1);
	if(pp<=w)
	{
		vv+=v;
	}
	else
		break;
}

即先更新当前浓度pp,如果先更新了,而pp大于W了,则break,那么pp就是带着错误的浓度退出了循环,那么输出的pp就是错 的。 这里是我犯的错误,希望以次记录,以后不要再犯了。

本题代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

int main()
{
	int i,cas,n,v,w,p[110];
	int vv,sum;
	double pp;
	scanf("%d",&cas);
	while(cas--)
	{
		scanf("%d%d%d",&n,&v,&w);
		for(i=0;i<n;i++)
			scanf("%d",&p[i]);
		sort(p,p+n);
		vv=sum=0;i=0;pp=0;
		if(p[0]>w)
		{
			printf("0 0.00\n");
			continue;
		}
		for(i=0;i<n;i++)
		{
			sum+=p[i];
			if((double)sum/(i+1)<=w)
			{
				pp=(double)sum/(i+1);
				vv+=v;
			}
			else
				break;
		}
		printf("%d %.2f\n",vv,pp/100);
	}
	return 0;
}
时间: 2024-08-26 19:52:06

HDU 2570 迷障 贪心的相关文章

--hdu 2570 迷瘴(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2570 Ac code: #include<stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b) { return *(int *)a-*(int *)b; } int main(void) { int c,n,v,w,iarr[110],i; double sum; scanf("%d",&

hud 2570 迷障(水 贪心)

对给的药水浓度进行排序,对于小于解毒药水浓度的可以直接加上它的体积,对每次混合后的药水浓度进行记录, 大于解毒药水浓度就输出,注意你求得是百分比还是百分数... #include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> using namespace std; int s[10005]; int main() { int a,i,n,m,k; scanf("

HDU OJ 迷障 题目257

 /* 迷瘴 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4842    Accepted Submission(s): 1653 Problem Description 通过悬崖的yifenfei,又面临着幽谷的考验-- 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅.由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体

迷障(杭电2570)

/*迷瘴 通过悬崖的yifenfei,又面临着幽谷的考验-- 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅.由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身溃烂而死. 幸好yifenfei早有防备,提前备好了解药材料(各种浓度的万能药水).现在只需按照配置成不同比例的浓度. 现已知yifenfei随身携带有n种浓度的万能药水,体积V都相同,浓度则分别为Pi%.并且知道,针对当时幽谷的瘴气情况,只需选择部分或者全部的万能药水,然后配置出浓度不大于 W%的药水即可解毒. 现在

HDU 2570:迷瘴

迷瘴 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3819    Accepted Submission(s): 1316 Problem Description 通过悬崖的yifenfei,又面临着幽谷的考验-- 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅.由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

hdu 4864 Task (贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int t; int v; int yy; }; struct node a[100010],b[100010]; bool cmp(node a1,node a2) { if(a1.t==a2.t)//先按时间从大到小 return a1.v>a2.v;//再按水平从大

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1