• HDU-2570 迷瘴 (贪心)


    题目:

    通过悬崖的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就加入,否则结束判断。

    This is Code:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     int C, med[150], n, v;
     7     double w;
     8     scanf("%d", &C);
     9     while(C--){
    10         memset(med, 0, sizeof(med));
    11         scanf("%d%d%lf", &n, &v, &w);
    12         for (int i = 0; i < n; ++i) scanf("%d", &med[i]);
    13         sort(med, med+n);
    14         int ans = 0, sum = 0;
    15         double temp, ansp = 0;
    16         for (int i = 0; i < n; ++i, ++ans){
    17             temp = double(sum+med[i])/(double)(ans+1);
    18             if (temp <= w) sum += med[i];
    19             else break;
    20             ansp = temp;
    21         }
    22         printf("%d %.2f
    ", ans*v, ansp/100.0);
    23     }
    24 
    25     return 0;
    26 }
  • 相关阅读:
    LeetCode——Generate Parentheses
    LeetCode——Best Time to Buy and Sell Stock IV
    LeetCode——Best Time to Buy and Sell Stock III
    LeetCode——Best Time to Buy and Sell Stock
    LeetCode——Find Minimum in Rotated Sorted Array
    Mahout实现基于用户的协同过滤算法
    使用Java对文件进行解压缩
    LeetCode——Convert Sorted Array to Binary Search Tree
    LeetCode——Missing Number
    LeetCode——Integer to Roman
  • 原文地址:https://www.cnblogs.com/robin1998/p/6359132.html
Copyright © 2020-2023  润新知