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;
    }


你可能感兴趣的:(NOJ [1141] Remove Water)