题目描述:
样例输入1:
3 50
4 2 1
样例输出1:
2.000000
样例输入2:
2 90
1 11
样例输出2:
1.909091
数据规模:
对于100%的数据:1≤ n≤10000, 0≤k≤ 99, 0≤ai ≤1000
题目分析:
二分答案。对平均能量进行二分,上界可以为第一次的平均数(因为有损耗必然会小于这个值),下界为0。特别需要注意的是精度问题。
附代码:
#include<iostream>
#include<cstring>
#include<string>
#include<ctime>
#include<queue>
#include<iomanip>
#include<cctype>
#include<cmath>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
const double eps=1e-8;
int n,k,a[10100];
double tot,p,aver;
int readint()
{
char ch;int i=0,f=1;
for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
if(ch=='-')
{
f=-1;
ch=getchar();
}
for(;ch>='0'&&ch<='9';ch=getchar())
i=(i<<3)+(i<<1)+ch-'0';
return i*f;
}
bool check(double x)//检查是否满足
{
double tot1=0,tot2=0,tots=0;
for(int i=1;i<=n;i++)
{
if(x-a[i]<eps)
tot1+=a[i]-x;//大于此时平均数的总和,能够转移的总和
else
tots+=x-a[i];//小于的之和,需要的量
}
tot2=tot1*p;//可成功转移的(除掉损耗)
if(tots-tot2<eps)
return true;
else return false;
}
double find()//二分
{
double l=0,r=aver,mid;
while(l-r<eps)
{
mid=(l+r)/2;
if(check(mid)==true)
l=mid+0.0000001;
else
r=mid-0.0000001;
}
return r;
}
int main()
{
freopen("energy.in","r",stdin);
freopen("energy.out","w",stdout);
n=readint();k=readint();
for(int i=1;i<=n;i++)
a[i]=readint();
p=(100-k)*1.0/100;
for(int i=1;i<=n;i++)
tot+=a[i];
aver=tot/n;
sort(a+1,a+n+1);
double ans=find();
printf("%0.6f",ans);
return 0;
}