Angry Cows(Silver)

Angry Cows(Silver)

https://ac.nowcoder.com/acm/problem/24017

做法:二分

思路:

  • 1.先找出二分所需要的边界
  • 2.每次判断这个点是否成立,可以用upper_bound模拟。如果能模拟完所有点则成立,否则不成立。

代码

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=5e4+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);

int x[N],ans;
int n,k;

bool check(int mid){
    int res=0,p=0;
    _for(i,k) {
        p=upper_bound(x,x+n,x[p]+mid*2)-x;
        if(p==n) return true;
    }
    return false; 
} 

void solve(){
    cin>>n>>k;
    _for(i,n) cin>>x[i];
    sort(x,x+n);
    int l=0,r=x[n-1]-x[0];
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid)){
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    cout<<ans<<"\n"; 
}


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
#ifdef DEBUG
    freopen("F:/laji/1.in", "r", stdin);
//    freopen("F:/laji/2.out", "w", stdout);
#endif
//    int t;cin>>t;while(t--)
    solve();
    return 0;
}
牛客每日一题 文章被收录于专栏

全部评论

相关推荐

LZStarV:冲就好了,就算真的是字节也冲,面评脏了大不了等三四个月就淡了,而且等到那个时候实力进步了选择还多,何必拘泥于字节
点赞 评论 收藏
分享
头像 会员标识
12-16 14:02
浙江大学 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务