P1842,想使用二分查找来得到答案,但是全都RE了,求助
查看原帖
P1842,想使用二分查找来得到答案,但是全都RE了,求助
1362701
Sensory楼主2024/9/22 11:59
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5+9;

int a[N];
int n,c;
//vector <int> x;

int f(int mid){
	int res = 0,pre = -1e9;
	for(int i=1;i<=n;++i){
		if(a[i]-pre>=mid){
			res++;
			pre = a[i];
		}
	}
	return res;
}

int solve(){
	cin>>n>>c;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	int l=0,r = 1e9+9;
	
	while (l+1!=r){
		int mid = (l+r)>>1;
		if (f(mid)>=c) l = mid;
		else r=mid;
	}
	cout<<l<<"\n";
}


int main (){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _ = 1;
	while (_--)solve();
	return 0; 
}

代码如上

qwq

2024/9/22 11:59
加载中...