HDU-2570-迷瘴

链接:https://vjudge.net/problem/HDU-2570

题意:

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

思路:

贪心。

浓度混合,从小的挨个往大的混,如果某一时刻,混过之后浓度超限,则此时刻就是答案。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
using namespace std;

typedef long long LL;

const int MAXN = 100 + 10;

int c[MAXN];

int main()
{
    int t, n, v, w;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d%d", &n, &v, &w);
        for (int i = 1;i <= n;i++)
            scanf("%d", &c[i]);
        sort(c + 1, c + 1 + n);
        double ans = 0;
        int cnt = 0;
        for (int i = 1;i <= n;i++)
        {
            if (ans * cnt + v * c[i] <= w * (cnt + v))
            {
                ans = (ans * cnt + v * c[i]) / (cnt + v);
                cnt += v;
            }
        }
        printf("%d %.2lf
", cnt, ans / 100.0);
    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/YDDDD/p/10624010.html