7 条题解

  • 0
    @ 2024-6-2 18:00:26
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e5 + 10;
    int n, m;
    int a[N], l, r, mid, ans;
    bool check(int mid){
    	int sum = 1;
    	int last = a[1];
    	for(int i = 2 ; i <= n ; i++){
    		if(a[i] >= last + mid){
    			sum++;
    			last = a[i];
    		}
    	}
    	return sum >= m;
    }
    int main(){
    	cin >> n >> m;
    	for(int i = 1 ; i <= n ; i++) cin >> a[i];
    	sort(a + 1 , a + n + 1);
    	l = 1;
    	r = a[n];
    	while(l <= r){
    		int mid = l + r >> 1;
    		if(check(mid)){
    			ans = mid;
    			l = mid + 1;
    		}
    		else r = mid - 1;
    	}
    	cout << ans << endl;
    	return 0;
    }
    

    信息

    ID
    357
    时间
    1000ms
    内存
    512MiB
    难度
    7
    标签
    递交数
    413
    已通过
    104
    上传者