您的位置:首页 > 其它

CodeForces 68B Energy exchange (二分查找)

2018-01-21 11:24 525 查看
B. Energy exchange

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Examples

input
3 50
4 2 1


output
2.000000000


input
2 90
1 11


output
1.909090909


[题意]

要求n个数值均等,  但是从高向低传输,过程中有损失  损失为 (x*k) /100     A->B  A失去X  B得到X*(1-k/100)

问 最终 均等后 最大是多少.   

    [思路]

最小为 l  最大 为 r   设 中间 最终可能值 为  mid=(l+r)/2      比mid 小的 为sum1  比mid 大的为sum2 

需要sum1  失去sum2   若 sum2*损失量 < 需要量  说明    mid取值过大  需要变小

反之需要变大

1e-6

[代码实现]

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed);cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FI(n) IO::read(n)
#define Be IO::begin()

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=1e5+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

namespace IO {
const int MT = 5e7;
char buf[MT]; int c,sz;
void begin(){
c = 0;
sz = fread(buf, 1, MT, stdin);//一次性输入
}
template<class T>
inline bool read(T &t){
while( c < sz && buf[c] != '-' && ( buf[c]<'0' || buf[c] >'9')) c++;
if( c>=sz) return false;
bool flag = 0; if( buf[c]== '-') flag = 1,c++;
for( t=0; c<=sz && '0' <=buf[c] && buf[c] <= '9'; c++ ) t= t*10 + buf[c]-'0';
if(flag) t=-t;
return true;
}
}
ll inv[maxn*2];
inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
inline ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
inline ll inv2(ll b){return qpow(b,MOD-2);}

long double a[MAXN];
int main()
{

SHUT;
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);

long double l=a[1];
long double r=a
;
while(r-l>1e-6)
{
long double mid=(l+r)/2.0;
long double sum1=0,sum2=0;
for(int i=1;i<=n;i++)
{
if(a[i]<mid)
sum1+= mid-a[i];
if(a[i]>mid)
sum2+=a[i]-mid;
}
//cout<<sum1<<" "<<sum2<<endl;
if(sum2*((100-k)/100.0)>sum1)
l=mid;
else
r=mid;
}
cout<<l<<endl;
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: