1 solutions

  • 1
    @ 2022-1-5 16:21:36

    二分就完事了,这里我用的模板

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e5+7;
    int w[N];
    int main(){
    	int n,maxx=0,m;
    	cin>>n>>m;
    	for(int i=0;i<n;i++){
    		scanf("%d",&w[i]);
    		if(w[i]>maxx) maxx=w[i];
    	}
    	sort(w,w+n);//一定要排序
    	int left=0,right=maxx,mid=0;
    	while(left<=right){
    		int ans=0;
    		mid=(left+right+1)>>1;
    		int now=w[0];
    		for(int i=1;i<n;i++){
    			if(w[i]-now>=mid){
    			ans++;
    			now=w[i];	
    			} 
    		}
    		if(ans<m-1) right=mid-1;//m要减一,因为这里ans找的是两点之间的距离符合标准的数目
    		else left=mid+1;
    	}
    	cout<<left-1;//模板
    	return 0;
    }
    
    • 1

    Information

    ID
    166
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    7
    Tags
    # Submissions
    109
    Accepted
    25
    Uploaded By