POJ 2456 Aggressive cows "two points"

Source: Internet
Author: User

Aggressive cows

Title Link: http://poj.org/problem?id=2456

Test instructions: There are n positions, (2 <= n <= 100,000), to place the C bull (2 <= c <= N) in these n positions, if the minimum distance between the cow and the cow is maximum, the maximum possible minimum distance is obtained.

Analysis: Obviously is a problem to maximize the minimum value, it is easy to find the monotony of the problem, set the maximum possible minimum distance is ans,ans∈ (0, (pos[n-1]-pos[0])/(C-1)); N positions are sorted first, then in intervals (0, (pos[n-1 ]-pos[0])/(C-1)) to search for answers in two points.

#include <queue> #include <cstdio> #include <string> #include <cstring> #include <iostream > #include <algorithm>using namespace std;const int maxn = 100000+5;const int INF = 1000000000+5;int N,C,POS[MAXN] ;//determine if the minimum distance is set to X, can be filled in n position C cow, here is a bit greedy meaning bool judge (int x) {    int cnt = 1,prepos = Pos[0];    for (int i = 1,j;i < n;i++)    {        if (pos[i]-prepos<x) continue;        cnt++; Prepos=pos[i];    }    return CNT >= C;} void Search () {    int l,r,mid,ans;    L = 0;r = (Pos[n-1]-pos[0])/(C-1);    while (r-l>=1)    {        mid = (l+r)/2;        if (judge (mid))        {            ans = mid;            L = mid+1;        }        else R = Mid;    }    printf ("%d\n", ans);} int main () {    //freopen ("input.in", "R", stdin);    while (~SCANF ("%d%d", &n,&c)    } {for        (int i = 0;i < n;i++) scanf ("%d", &pos[i]);        Sort (pos,pos+n);        Search ();    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2456 Aggressive cows "two points"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.