POJ2456 Aggressive cows

Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13993 Accepted: 6775

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

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.

Source



————————————————————————————————————

题目的意思是给出n个坐标,选出n个位置使得最小的距离最大

思路:二分加验证


#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstring>
using namespace std;
#define LL long long

int n,k;
int a[100005];

bool ok(int xx)
{
    int cnt=1;
    int x=a[0];
    for(int i=1;i<n;i++)
    {
        if(a[i]-x>=xx)
        {
            cnt++;
            x=a[i];
        }
    }
    if(cnt>=k)
        return 1;
    else
        return 0;


}

int main()
{
   while(~scanf("%d%d",&n,&k))
   {
       for(int i=0;i<n;i++)
       {
           scanf("%d",&a[i]);
       }
       sort(a,a+n);
       int l=1,r=1e9;
       int ans=-1;
       while(l<=r)
       {
           int mid=(l+r)/2;
           if(ok(mid))
           {
               l=mid+1;
               ans=mid;
           }
           else
            r=mid-1;
       }
       printf("%d\n",ans);

   }

    return 0;
}




### 关于 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、付费专栏及课程。

余额充值