Programming and algorithm (b) algorithm basis "the" fourth Tuesday minute "Aggressive cows 2456

2456:Aggressive cows

Total time limit: 
1000ms
 
Memory Limit: 
65536kB
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?
Entry
* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Export
* Line 1: One integer: the largest minimum distance
Sample input
5 3
1
2
8
4
9
Sample Output
3
prompt
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.

To express the meaning of the questions is: C cattle into N compartments numbered years so as to minimize the difference between any two cows numbered compartments where the maximum.

Analysis: This is a minimum value maximization problem. Number of first compartments ordered from small to large, the maximum distance does not exceed the difference between the two ends of the cows, the minimum value is 0. So we can enumerate the minimum requirements by half. X is assumed that the current minimum value, C can be put down when the cow is judged if the minimum difference is x, x instructions little current, it is determined again let x becomes large; if fit, to indicate the current too x , it becomes smaller and then let x be determined. Until obtaining a maximum of x is the final answer.

#define _CRT_SECURE_NO_WARNINGS 
#include <the iostream> 
#include <cstdio> 
#include <algorithm> the using namespace STD; int A [ 100006 ];
 int N, C;
 BOOL FUNC ( int X) 
{ int TEMP = A [ 0 ];
     int CNT = . 1 ;
     for ( int I = . 1 ; I <N; I ++ ) 
    { // count at a set pitch (for example, four), the position of the discharge barn whether cattle C iF (a [I] - TEMP> = the X-) 
            cnt

 


    
        
         
        {++;
            temp = a[i];
        }
        if (cnt == C)
        {
            return true;
        }
    }
    return false;
}

int main()
{
    int i;
    cin >> N; // room number for cows
    cin >> C; // cows number
    for (i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }
    Sort (A, A +N); 

    int   left, right, MID; 
    left = 0 ; 
    right = A [N - . 1 ] -a [ 0 ];
     // MID = left + (right - left) / 2; 
    the while (left <= right) 
    { // position barn 5 [1, 2, 4, 8, 9], the maximum distance is 8, the pitch of all possible values is from 0 to [8], binary search problem 
        mid = left + ( right - left) / 2 ; // first pitch is 4 to failure, is necessary to reduce the distance 
        IF ((FUNC (MID)))   // if the current pitch down to meet the cow C, that increases the distance to try the situation 
        { // spacing how large? 
            + = MID left . 1 ; / *=. 5 left, [~ 0. 4] Delete, [. 5 ~. 8] SELECT * / 
        } 
        the else 
        { // the narrow pitch 
            right MID = - . 1 ; / * right =. 3, [~. 3. 8] Delete, [~ 0. 3] SELECT * / 
        } 
    } 
    // when the spacing = 3, to meet the condition set up, return true, at this time, left = 3 + 1 = 4 , while (4 <= 3) exit, the actual spacing = 3 4-1 
    the printf ( " D% \ n- " , LEFT - . 1 ); 

    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/focus-z/p/11521900.html