Codeforces Round #143 (Div. 2) C. To Add or Not to Add

校赛时,数据比较水我水过了,这个地方必须优化。

二分是个不错选择,但是整数区间的二分一直是我头大的一个地方。

因为二分的区间我一般选不好。

第一次写,我的判断条件是一个个去累加,看是否超过k...

果断TLE掉,先预处理前缀和。

WA在第13组数据,纠结很久。。。。。

好吧 数组开小了。

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define pi acos(-1.0)
typedef long long ll;
int n;
ll k;
ll a[100010],s[100010];
bool ok(int len,int x){
    ll res=0;
    res=a[x]*(ll)len-(s[x]-s[x-len]);
    if(res<=k) return true;
    else return false;
}
int sol(int x){
    int l=0,r=x,m;
    while(l<r){
        m=(l+r+1)/2;
        if(ok(m,x))
            l=m;
        else
            r=m-1;
    }
    return l;
}
int main(){
    ll val;
    while(~scanf("%d%I64d",&n,&k)){
        for(int i=1;i<=n;i++){
            scanf("%I64d",&a[i]);
        }
        sort(a+1,a+n+1);
        for(int i=1;i<=n;i++)
            s[i]=s[i-1]+a[i];
        int ans=0;
        for(int i=n;i>=1;i--){
            int t=sol(i);
            if(t>=ans){
                ans=t;
                val=a[i];
            }
        }
        printf("%d %I64d\n",ans,val);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值