SPOJ 297 minimum Aggressive cows Interval
For a given number of numbers in ascending order, select the number of c to minimize the interval between any two adjacent numbers. Returns the maximum minimum interval.
If the minimum value is the maximum value, we recommend that you use binary instead. Obviously, this question can be divided into two parts. First, let's think like this. If the smallest number in a certain number of c is not the first number, we will replace it with the first number, so that the interval between the first number and the second number will not decrease, so the minimum interval will not decrease. Therefore, when selecting a number, we must select the first one (although it is not the only choice, but it is not better than selecting the first one to increase the minimum interval ). In this way, we can determine whether the current interval x meets the question. The determination method is very simple, because the Split points have fixed the interval value, that is, the interval between each two numbers must be at least x. From the first number, the second number is the number closest to the first number and the interval is not less than x. The third number is the number closest to the second number and the interval is not less than x, and so on, until the number cannot be obtained. If the number is not less than c, the interval value x is acceptable.
#include
#include
#include #include
#include
#include
#include
#include
#include
#include
using namespace std;const int MAX = 100005;int n, c, a[MAX];bool judge(int x){int cnt = 1, temp = a[0];for(int i = 0; i < n; i++){if(a[i] - temp >= x){cnt++;temp = a[i];}}return cnt >= c;}void input(){ scanf(%d%d, &n, &c); for(int i = 0; i < n; i++) scanf(%d, a+i);}void solve(){ sort(a, a + n); int l = 0, r = a[n - 1] - a[0], mid; while(l < r) { mid = (l + r)/2; if(judge(mid)) l = mid + 1; else r = mid; } if(!judge(l)) l--; printf(%d, l);}int main(){ int T;scanf(%d, &T);while(T--) {input();solve();}return 0;}
??