HDU2570(贪心)

题目链接

传送门

解题思路

以第三个样例为例,浓度分别为20 20 30,那么混合后的浓度应该为20 20 23 。先对原有浓度从小到大排序,求混合浓度时要先换算出百分比,然后乘以体积,即b[i] = a[i] / 100.0 * v 。最后循环遍历比较下就可以了。

参考代码

#include <functional>  
#include <algorithm>  
#include <iostream>  
#include <fstream>  
#include <sstream>  
#include <iomanip>  
#include <numeric>  
#include <cstring>  
#include <climits>  
#include <cassert>  
#include <complex>  
#include <cstdio>  
#include <string>  
#include <vector>  
#include <bitset>  
#include <queue>  
#include <stack>  
#include <cmath>  
#include <ctime>  
#include <list>  
#include <set>  
#include <map>  
using namespace std;  

#pragma comment(linker, "/STACK:102400000,102400000")  

typedef long long LL;  
typedef double DB;  
typedef unsigned uint;  
typedef unsigned long long uLL;  

/** Constant List .. **/ //{  

const int MOD = int(1e9)+7;  
const int INF = 0x3f3f3f3f;  
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;  
const DB EPS = 1e-9;  
const DB OO = 1e20;  
const DB PI = acos(-1.0); //M_PI;  
int a[101];  
double b[101];  
int main()  
{  
    #ifdef DoubleQ  
    freopen("in.txt","r",stdin);  
    #endif  
    int T;  
    scanf("%d",&T);  
    while(T--)  
    {  
        int n , v , w;  
        scanf("%d%d%d",&n,&v,&w);  
        double sumt = 0;  
        double sumv =0;  
        for(int i = 0 ; i < n  ; i ++)  
        {  
            scanf("%d",&a[i]);  
        }  
        sort(a , a + n);  
        for(int i = 0  ; i < n ; i ++)  
        {  
            sumt += a[i] / 100.0 * v;  
            sumv += v;  
            b[i] = sumt / (double)sumv;  
        }  
        int cntv = 0;  
        double concer = 0.00;  
        for(int i = 0 ; i < n ; i ++)  
        {  
            if(b[i] * 100 < w)  
            {  
                cntv += v;  
                concer = b[i];  
            }  
        }  
        printf("%d %.2lf\n", cntv , concer);  
    }  
}  

暂无评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

© 2018-2019 惜春令 京ICP备18010644号 网站地图