CSU-ACM2019寒假训练2-D - Maximize the minimum

这个题目就是最小值最大化的问题(也就是说能去的最小值的最大值)~

题目

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

Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.

日常翻译:
农夫约翰建造了一个新的长谷仓,N(2 <= N <= 100,000)摊位。 档位沿x1,…,xN(0 <= xi <= 1,000,000,000)的位置沿直线定位。

他的C(2 <= C <= N)奶牛不喜欢这种谷仓布局,一旦进入摊位就会变得咄咄逼人。 为了防止奶牛互相伤害,FJ希望将奶牛分配到畜栏,以使其中任何两头奶牛之间的最小距离尽可能大。 最小的最小距离是多少?
输入
*第1行:两个以空格分隔的整数:N和C.

*行2…N + 1:行i + 1包含整数停顿位置xi
产量
*第1行:一个整数:最大的最小距离
样本输入
5 3
1
2
8
4
9
样本输出
3
题示
输出详情:
FJ可以将他的3头奶牛放在位置1,4和8的摊位上,最小距离为3。
建议使用大量输入数据scanf。

思路:其实就是二分加贪心,暴力枚举,首先再输入的时候找到最远的距离,然后以此二分,猜测一个值是否满足,若满足,则将距离在加大一点,不行就减小。而判断的方法则是现将一头牛放在第一个柱子,将此距离带入判断距离这个最远的柱子放第二头牛,以此类推,最后判断进行可以满足的柱子数量,如果C-1次操作(C头牛之间的间距)中到达了给的N的柱子的数量,说明,距离不满足了,这个距离不可以放的下所有的牛,反之则可以~~
(好好想想二分得到的是最小值)
代码如下:

#include
#include
using namespace std;
const int mx=1e5+10;
const int ma=1e9+10;
int s[mx],N,C;
int check(int a)
{
	int last=0;
	for(int i=1;iend)     //输入时找到最大值
	  end=s[i];
	  if(s[i]1)//这个自己想想,加入最后剩下两个相邻的,该用什么条件才能跳出循环
	{
	 int mid=(begin+end)/2;
	 if(check(mid))   //距离满足,将距离增大
	 begin=mid;
	 else
	 end=mid;      //距离不满足,距离减小
    }
    printf("%d\n",begin);
    return 0;
}

你可能感兴趣的:(训练题目)