【二分+正序对】51Nod 1711 平均数

题面在这里

考虑二分答案,其实就是求平均值大于等于mid的区间个数

写出来就是 airl+1mid

所以每一项减去mid,写成前缀和就是 SrSl10

然后就是求正序对了

示例程序:

#include<cstdio>
typedef long long ll;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int red(){
    int res=0,f=1;char ch=nc();
    while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();}
    while ('0'<=ch&&ch<='9') res=res*10+ch-48,ch=nc();
    return res*f;
}

const int maxn=100005;
const double eps=1e-5;
int n,a[maxn];
double L,R,mid,ans,s[maxn],c[maxn];
ll res,k;
void ms(int L,int R){
    if (L>=R) return;
    int mid=L+R>>1;
    ms(L,mid);ms(mid+1,R);
    int i=L,j=mid+1;
    for (int k=L;k<=R;k++) c[k]=s[k];
    for (int k=L;k<=R;k++)
     if (i<=mid&&(j>R||c[i]<=c[j])) s[k]=c[i++];else s[k]=c[j++],res-=mid-i+1;
}
//  平均值大于mid的区间个数是否大于K
bool check(){
    s[0]=0;
    for (int i=1;i<=n;i++) s[i]=s[i-1]+a[i]-mid;
    res=(ll)n*(n+1)/2;
    ms(0,n);
    return res>=k;
}
int main(){
    scanf("%d%lld",&n,&k);
    for (int i=1;i<=n;i++) a[i]=red();
    L=1,R=1e5;
    while (R-L>=eps){
        mid=(L+R)/2;
        if (check()) ans=mid,L=mid;else R=mid;
    }
    printf("%.4lf",ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值