[NOIP模拟][二分]能源

题目描述:
这里写图片描述
样例输入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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值