[二分-二分最大化最小值]poj-2456

原创 2018年04月16日 21:08:03
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18456 Accepted: 8786

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.

题意:

有n间牛舍和m头牛,第i号牛舍在xi的位置上,要求将牛放到牛舍中,且最大化任意两头牛的距离.

题解:

二分这个最大距离,

对于小的mid来说能放下的牛一定最多,于是在能放下n头牛的基础下不断增加left,最后的最优解为right

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 1e5+10;
const int inf = 1e9+10;
vector<int> arr;
int n,c;
/*bool check(int mid) {
    int cnt = 1;
    vector<int>::iterator it = arr.begin();
    while(it != arr.end()) {
        it = lower_bound(arr.begin(),arr.end(),mid+*it);
        if(it != arr.end()) cnt++;
    }
    return cnt >= c;
}*/
bool check(int mid) {
    int cnt = 1,i = 1,it = 0;
    while(i < n) {
        if(arr[i] < arr[it] + mid) i++;
        else it = i,cnt++,i++;
    }
    return cnt >= c;
}
int main()
{
    while(~scanf("%d%d",&n,&c)) {
        for(int i=0;i<n;i++){
            int x;scanf("%d",&x);
            arr.push_back(x);
        }
        sort(arr.begin(),arr.end());
        int right = inf,left = 0;
        while(left <= right) {
            int mid = (right + left) / 2;
            if(check(mid)) left = mid + 1;
            else right = mid - 1;
        }
        printf("%d\n",right);
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/79966445

POJ 3273 二分求最大化最小值

Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20086 Accep...
  • qq_22902423
  • qq_22902423
  • 2016-01-28 16:12:26
  • 2299

POJ 2456 Aggressive cows(二分答案,最大化最小值)

题目链接 题意:给出n个牛舍坐标(在一直线上),然后有m个牛要入住,现在问相邻牛之间的最小距离最大是几 解答:二分答案,函数ok(d)检查距离是d合法与否 #include #include//#...
  • u013167299
  • u013167299
  • 2015-11-21 16:35:57
  • 497

poj 2456 二分法 最大化最小值

题目:http://poj.org/problem?id=2456 重新练习下二分法,发现还是手速不够 从这道题学到一下几点: 1、线性分几段的方法,看我的Judge()代码; 2、二分的wh...
  • u011026968
  • u011026968
  • 2014-05-10 21:44:48
  • 1139

POJ 2456 Aggressive cows(二分查找 最大化最小值)

题意:给出n个牛棚的位置,选择其中的m个给牛住,使得牛之间的距离的最小值最大。 首先我们可以知道距离的最小值一定产生于相邻的牛之间,所以要保证相邻牛之间的距离大于等于这个最小值d。所以把牛棚的位置...
  • secfly
  • secfly
  • 2016-03-25 16:25:55
  • 377

二分法求最大最小值

代码如下 #include #include #include using namespace std; void max_min(vectorv,int left,int right,int &m...
  • qq_30138291
  • qq_30138291
  • 2017-07-19 09:40:49
  • 225

POJ 2456 Aggressive cows(最小值最大化+二分)

http://poj.org/problem?id=2456题意:把C头牛放到N个带有编号的隔间里,使得任意两头牛所在的隔间编号的最小差值最大。例如样例排完序后变成1 2 4 8 9,那么1位置放一头...
  • Codeblocksm
  • Codeblocksm
  • 2015-11-14 17:19:37
  • 258

二分法典型应用(二)最大化最小值 POJ2456 Aggressive cows

问题描述: Farmer John has built a new long barn, with N (2
  • karry_zzj
  • karry_zzj
  • 2017-04-18 00:04:53
  • 482

poj2456(贪心+二分)

题目链接:poj2456 /* 题意: 有n个牛栏,选m个放进牛,相当于一条线段上有 n 个点,选取 m 个点, 使得相邻点之间的最小距离值最大 思路:贪心+二分 二分枚举相邻两牛的...
  • u010728156
  • u010728156
  • 2014-04-03 19:30:45
  • 879

最大化平均值(二分搜索)

有n个物品的重量和价值分别是w[i]和v[i]。从中选出k个物品使得单位重量的价值最大。 限制条件: 1 1 输入    n=3    k=2    (w,v)={(2,2),(5,3),(2,1)}...
  • m0_37846371
  • m0_37846371
  • 2017-06-16 12:47:38
  • 222
收藏助手
不良信息举报
您举报文章:[二分-二分最大化最小值]poj-2456
举报原因:
原因补充:

(最多只允许输入30个字)