E - Energy exchange (二分)

It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by units.

Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.

Input

First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers.

Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).

Output

Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy.

The absolute or relative error in the answer should not exceed 10 - 6.

Example

Input

3 50
4 2 1

Output

2.000000000

Input

2 90
1 11

Output

1.909090909

题意:n个蓄电池,各个电池之间可以传输电能,但是会有损耗,求使得所有蓄电池的电量相同的电量值

例如 实例一

(4-2)*(1-50%)==1,加给1,使得 全部变成2

说实话,一开始没啥思路,后来感觉k%的损耗没法办,

于是采用极限法即,k==0时

aver==sum/n,k==100%时

aver==min{x[n]}

而aver却无法求,于是想到二分

即l==min{x[n]},r==sum/n

换句话说从k==0到k==100%不断二分,直到求出k的值等于(近似)输入的k

控制二分的条件我用的是     

多于aver的蓄电池的多出的部分*转化率(x-x*k/100) 与    低于aver的蓄电池的所需的能量

之间的关系

如果  供给  大于  所需   说明输入的k对于当前的k来说太小了

k要增加,于是aver要减小,即r==aver

反之   l==aver

代码如下

#include <stdio.h>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;

int main()
{

    int sum=0;
    int a[20000],n,k,i,j;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]),
        sum+=a[i];
    double l,r;
    r=sum*1./n;
    l=0.;
    double aver=(r+l)/2;
    while(r-l>1e-11)
    {
        double lost,get;
        lost=get=0.;
        for(i=0;i<n;i++)
        {
            if(aver<(double)a[i])
                lost+=((double)a[i]-aver)*(1.-k/100.);
            else
                get+=aver-(double)a[i];
        }
        if(lost>get)
            l=aver;
        else
            r=aver;
        aver=(r+l)/2;
    }
    printf("%.9lf\n",l);

    return 0;
}

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值