标签:
二分.
最大化最小值.
C(x)表示使两头牛之间最小距离为x,问题转化为求满足C(x)的x的最大值.可知x<=(总长)/(c-1).那么1~(总长)/(c-1)二分求解即可.
1 #include<cstdio> 2 #include<algorithm> 3 using std :: sort; 4 5 const int maxn=100005; 6 int n,c; 7 int a[maxn]; 8 9 void init() 10 { 11 scanf("%d%d",&n,&c); 12 for(int i=1;i<=n;i++) scanf("%d",&a[i]); 13 sort(a+1,a+n+1); 14 } 15 16 bool judge(int x) 17 { 18 int last=1; 19 for(int i=2;i<=c;i++) 20 { 21 int now=last+1; 22 while(now<=n&&(a[now]-a[last]<x)) now++; 23 if(now>n) return false; 24 last=now; 25 } 26 return true; 27 } 28 29 30 int bsearch(int x,int y) 31 { 32 while(x<y) 33 { 34 int m=x+(y-x+1)/2; 35 if(judge(m)) x=m; 36 else y=m-1; 37 } 38 return x; 39 } 40 41 void solve() 42 { 43 int INF=(a[n]-a[1])/(c-1); 44 printf("%d\n",bsearch(1,INF)); 45 } 46 47 int main() 48 { 49 freopen("cow.in","r",stdin); 50 freopen("cow.out","w",stdout); 51 init(); 52 solve(); 53 fclose(stdin); 54 fclose(stdout); 55 return 0; 56 }
标签:
原文地址:http://www.cnblogs.com/Sunnie69/p/5423173.html