poj 2456 Aggressive cows(二分)

解决如何在长直线上分配牛棚,使任意两头牛之间的最小距离最大化。采用二分查找法,通过设定判断条件实现最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 

看了题解才弄懂怎么做...漫漫进阶路...小白仍需努力啊...

(参考了 http://blog.youkuaiyun.com/qq_34374664/article/details/53366987)

一点小小的启发(吧...)

1.二分的灵活性 (其实看懂题目之后我都没想出来怎么用二分

2.最小值的最大化 最大值的最小化(看到英文的时候其实有点蒙...)

3.一点小小的问题 就是最后为什么输出的是mid呢...其实还没有想通

思路什么的其实都在注释上面了 就懒得再写一次了

#include<iostream>
#include<cstdio>
#include <cstring> 
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + 5,inf=1e9+5;
int n, c, num[MAXN];
bool judge(int dis)//判断最小距离为dis的情况下能不能把c头牛都安顿好
{
	int last = 0;//第1头牛安顿在0的位置
	int cur = last + 1;//看看能不能把下一头牛安顿在last的下一个棚
	for (int i = 1; i < c; i++)
	{
		while (cur < n && num[cur] - num[last] < dis)
			cur++;//移动呀移动~~
		
		if (cur == n)return false;//还没安排好所有的牛呢 就到末尾了
		last = cur;
	}
	return true;
}
int main()
{
	while (~scanf_s("%d%d", &n, &c))
	{
		for (int i = 0; i < n; i++)
			scanf_s("%d", &num[i]);//每个棚子的距离
		sort(num, num + n);
		//二分找最大的min值
		int min = 0, max = num[n-1] ;
		int mid = (min + max) / 2;
		while (max - min > 1)
		{
			mid = (min + max) / 2;
			if (judge(mid) == true)min = mid;
			else max = mid;
		}
		cout << min << endl;//这里为什么是输出min 还是不太明白
	}
}



### 关于 POJ 2456 的问题分析 POJ 平台上的题目通常涉及算法设计、数据结构应用以及复杂度优化等问题。虽然未提供具体关于 POJ 2456 题目的直接描述,但从其他类似题目的解决方法可以推测其可能的解决方案。 #### 差分约束系统的应用 如果 POJ 2456 类似于差分约束系统的问题,则可以通过构建图模型并使用 SPFA 算法来寻找最长路径[^3]。在此类问题中,条件 \(A - B \geq X\) 和 \(A - B \leq X\) 可转化为节点间的边权关系,并通过引入超级源点确保整个图连通性。这种方法适用于一系列不等式约束下的变量取值范围判定问题。 #### 动态规划 vs 常规解法对比 针对某些特定类型的题目,动态规划 (Dynamic Programming, DP) 提供了一种高效解决问题的方式,尤其当子问题具有重叠性质时。然而,在实际实现过程中需要注意状态定义合理性及转移方程准确性[^2]。相比之下,传统方法可能会因为重复计算而导致时间效率低下。 以下是基于上述理论的一个简单伪代码框架用于处理此类问题: ```python from collections import deque def spfa(n, edges): dist = [-float('inf')] * n in_queue = [False] * n queue = deque() # 初始化超级源点连接所有顶点 super_source = n new_edges = [(super_source, i, 0) for i in range(n)] all_edges = edges + new_edges dist[super_source] = 0 queue.append(super_source) in_queue[super_source] = True while queue: u = queue.popleft() in_queue[u] = False for v, w in adj_list[u]: if dist[v] < dist[u] + w: dist[v] = dist[u] + w if not in_queue[v]: queue.append(v) in_queue[v] = True # 判断是否有正环存在 if len(queue) > n: return "Positive cycle exists" return max(dist[:n]) # 构建邻接表表示图结构 adj_list = [[] for _ in range(n+1)] for a,b,w in edges: adj_list[a].append((b, w)) ``` 此代码片段展示了如何运用队列维护待访问结点集合并通过松弛操作更新最短距离数组 `dist` 。同时加入了对可能出现的正向循环检测机制。 ### 结论 综上所述,解答 POJ 2456 或相似编号的问题需依据具体需求选取合适的技术手段。无论是采用差分约束还是动态规划策略,都应注重细节把控以提升程序性能表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值