二分 Codeforces Beta Round #62 ——B. 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 xunits 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个电池,电池里的电量不同,将这些电池里的电量相互转移直到全部相同,每次转移会浪费掉x%(1<=x<=99)的电量,问最后各个电池里的电量是多少。

不论最后如何转移最后剩余的电量肯定会再电量的最小值和最大值之间。所以我们会想到二分这个最终电量,直到l和r的差值小到可以忽略。

#include <bits/stdc++.h>
#define exp 1e-7
using namespace std;
int n,k;
double l=1005,r=-1,a[10105];

int judge(double mid){
	double sum1=0,sum2=0;
	for(int i=0;i<n;i++){
		if(a[i]<mid){
			sum1=sum1+(mid-a[i])/(1.0-k/100.0);
		}
		else{
			sum2=sum2+a[i]-mid;
		}
	}
	if(sum1>sum2)
		return 0;
	else
		return 1;
}

int main()
{
	while(scanf("%d%d",&n,&k)!=EOF){
		
		for(int i=0;i<n;i++){
			scanf("%lf",&a[i]);
			if(a[i]>r)
				r=a[i];
			if(a[i]<l)
				l=a[i];
		}
		double mid=(l+r)/2.0;
		while(l+exp<=r){
			mid=(l+r)/2.0;
			if(judge(mid))
				l=mid;
			else
				r=mid;
		}
		printf("%.9lf\n",mid);
	}
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值