NOJ [1141] Remove Water

  • 问题描述
  • There are a few bowls on your desktop.There is some water in each bowl.Initially,each bowl of water are not the same.Then you take the kettle begin to add water in the bowl(you can not add water in the bowl which have the most water),to make the bowl of water same as the water in the most water bowl,but every time after you add a water bowl,

    all bowls of water will evaporate half water.Your task is to make all bowls of water are the same,thinking how to add water can make the minimum evaporation of water.



  • 输入
  • Input until EOF.
    There is a positive integer N(1<N<20) in the first line.
    Next line follows N integers.Every integer is less than 50.
  • 输出
  • For each test,you should output the minimum evaporation of water.Keep six decimal places.

    本题要求寻找最小蒸发水量,每次加水后,所有碗里的水都会蒸发一半,
    下面比较两种极端的情况,
    1.每次往水最少的碗里加水
    2.每次往水次多的碗里加水
    中间的那些碗蒸发水量一样,设为X
    设最多的碗里有水M,次多的有N,最少的有m,
    按1:单次蒸发量=(2*M+X)/2+N/2;
    按2:单次蒸发量=(2*M+X)/2+m/2;
    那么显然第二种情况蒸发水更少
    同理可以去比较每次往水次多的碗里加水和往其他碗里加水的情况
    可以得到,在水次多的碗里加水,蒸发量最小
    所以,本题就是贪心

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    
    int cmp(const double a,const double b)
    {
      return a>b;
    }
    
    int main()
    {
       int n;
       while(~scanf("%d",&n))
       {
         double water[21];
         int i,j;
         double W=0;
         for(i=0;i<n;i++)
          scanf("%lf",&water[i]);
        int k=1;
        while(1)
        {
          sort(water,water+n,cmp);
          if(water[0]==water[n-1])
              break;
          else
            {
              water[k++]=water[0];
              for(i=0;i<n;i++)
              {
                 water[i]/=2;
                 W+=water[i];
              }
            }
        }
        printf("%f\n",W);
       }
       return 0;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值