POJ2456Aggressive cows-(二分判定)

Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don‘t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Input

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

* Line 1: One integer: the largest minimum distance

Sample Input

5 3
1
2
8
4
9

Sample Output

3翻译:有n间牛房在一条直线上,距离分别是xi,有c只牛,牛在一起容易打架,让牛尽可能在距离比较远的牛房。要安置c只牛,求这个距离最大能是多少?白书上的二分题目,书上讲得一点都不人性化,能用人话说清楚的非要用鬼话,代码也是不够简洁,看了半个小时都不懂,一搜题解,秒懂,记录一下代码。
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<math.h>
 6 #include<string>
 7 #define ll long long
 8 #define inf 0x3f3f3f3f
 9 using namespace std;
10 int n,m;
11 int a[100086];
12
13 bool check(int d)
14 {
15     int num=1,now=a[0];
16     for(int i=1;i<n;i++)
17     {
18         if(a[i]-now>=d)
19             now=a[i],num++;///如果a[i]号牛棚距离上一个的距离>=d,更新牛棚位置,并且增加安置的牛数,大于等于m时候就是可行解
20         if(num>=m)
21             return true;
22     }
23     return false;
24 }
25
26 int main()
27 {
28     while(scanf("%d %d",&n,&m)!=EOF)
29     {
30         for(int i=0;i<n;i++)
31             scanf("%d",&a[i]);
32         sort(a,a+n);
33         int l=1,r=a[n-1],ans=0;
34         while(l<=r)
35         {
36             int mid=(l+r)/2;
37             if(check(mid))
38             {
39                 ans=mid;///不断更新可行解,找到最大值, 即最大化 可行解。
40                 l=mid+1;
41             }
42             else
43                 r=mid-1;
44         }
45         printf("%d\n",ans);
46     }
47     return 0;
48 }
 

原文地址:https://www.cnblogs.com/shoulinniao/p/10597777.html

时间: 2024-10-21 09:45:29

POJ2456Aggressive cows-(二分判定)的相关文章

二分判定 覆盖问题 BZOJ 1052

1 //二分判定 覆盖问题 BZOJ 1052 2 // 首先确定一个最小矩阵包围所有点,则最优正方形的一个角一定与矩形一个角重合. 3 // 然后枚举每个角,再解决子问题 4 5 #include <bits/stdc++.h> 6 using namespace std; 7 #define LL long long 8 typedef pair<int,int> pii; 9 const int inf = 1e9; 10 const int MOD =5010; 11 co

POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定)

POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定) ACM 题目地址: POJ 2942 - Knights of the Round Table 题意: 有N个骑士,给出某些骑士之间的仇恨关系,骑士们开会时会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件: 任意相互憎恨的两个骑士不能相邻 开会人数为大于2的奇数 若某个骑士任何会议都不能参加,那么就必须将他踢出,给出骑士之间的仇恨关系,问最少需要踢出多少个骑士? 分析: 把

poj 2456 Aggressive cows(二分)

// n点中选c点放下c头牛 是的n-1段距离中的最短距离最大 ,求这个最大的最短距离 //假设当前的最小值为x,如果判断出最小差值为x时可以放下C头牛, //就先让x变大再判断:如果放不下,说明当前的x太大了, //就先让x变小然后再进行判断.直到求出一个最大的x就是最终的答案. # include <algorithm> # include <string.h> # include <stdio.h> using namespace std; int n,c,i,a

【二分查找】POJ2456-Aggressive cows

[题目大意] 有N间牛舍和M头牛,告诉你每个牛舍的位置,求出两头牛之间最小距离的最大值. [思路] 二分判断两头牛之间的最小距离d,通过贪心法进行验证. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=100000+5

Lost Cows(线段树+二分判定)

4835: [Usaco2003 Open]Lost Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 17[Submit][Status][Web Board] Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judg ment, they visi

POJ 2456 Aggressive cows (二分 基础)

Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...

【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定

2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2371  Solved: 1143[Submit][Status][Discuss] Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是小X的生日,小 W 想送一个数给他作为生日礼物.当然他不能送一个小X讨厌

POJ 2456 Aggressive cows(二分答案)

Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,x

SPOJ - AGGRCOW Aggressive cows(二分枚举最优解)

题意:给出n个位置,需要将m头牛放在这些位置中,使得所有相邻两头牛间的最小距离最大,求最大的最小距离: 思路:二分枚举最优解.先将所有位置排序,从最大总距离枚举到0,若满足差值大于等于枚举值的位置个数大于等于m,则当前枚举值为最优解. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int t,n,m; int num[500100]; int a[500100];