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"