POJ: 2456 Aggressive cows (z max min)

describe

Farmer John builds a long corral consisting of N (2 <= N <= 100,000) compartments numbered x1,...,xN (0 <= xi <= 1,000,000,000) .However ,
John's C (2 <= C <= N) cows don't like this layout, and if several cows are in one compartment, they are about to fight. In order not to let the cows hurt each other. John decides to allocate his own stalls to the cows so that the minimum distance between any two cows is as large as possible, so what is this maximum minimum distance?

 
enter
The first line: two integers N and C separated by spaces The
second line - the N+1th line: the position of xi is indicated respectively
output
Each set of test data outputs an integer, the largest and smallest values ​​that satisfy the meaning of the question, pay attention to line breaks.
sample input
5 3
1
2
8
4
9
Sample output
3

The intention is to express: put C cows into N numbered compartments, so that the minimum difference between the numbers of the compartments where any two cows are located is the largest.

Analysis: This is a minimization maximization problem. First sort the compartment numbers from small to large, the maximum distance will not exceed the difference between the two cows at both ends, and the minimum value is 0. So we can find the minimum value by bisection enumeration. Assuming that the current minimum value is x, if it is judged that the minimum difference is x, you can put down C cows, indicating that the current x is a little small, so let x become larger before making a judgment; if it cannot be put down, it means that the current x is too large , let x become smaller first and then make a judgment. Until a maximum x is found is the final answer.

Now let's do the problem solving steps:

C(d) = The position of the cows can be arranged so that the distance between the two closest cows is not less than d

Then the problem now becomes to find the maximum d that satisfies C(d), and the nearest distance is not less than d, and it can also be said that the distance of all cattle is not less than d;

The judgment on the problem can be solved very easily using the greedy method.

1. Sort the position x of the cowshed

2. Put the first cow into the x0 cowshed

3. If the i-th cow is placed in xj, the i+1-th cow must be placed in the smallest xk that satisfies xj+d<=xk

AC code:

#include<stdio.h>
#include<algorithm>
#define MAX 101000
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int a[MAX];
bool C(int d)
{
    int last=0;
    for(int i = 1 ; i < m ; i++)
    {
        int crt=last+ 1 ;
         while (crt<n&&a[crt]-a[last]<d) /// Only need to compare k-1 times to find the most suitable value d to make k cows, use last&crt to represent The position of a cow and the current cow 
            crt++ ;
         if (crt== n)
             return  false ; /// Maximum n is reached. Explain that the value of d is smaller 
        last = crt;
    }
    return true;
}
intmain ()
{
    scanf("%d%d",&n,&m);
    for(int i = 0 ; i < n ; i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    int st = 0,en=INF,mid;
    while(en-st>1)
    {
         mid=(en+st)/2;
        if(C(mid)==false)
            en=mid;
        else
            st=mid;
    }
    printf( " %d\n " ,(en+st)/ 2 ); /// The question came to my mind because I don't know whether it is en or st 
}
View Code

Another solution:

#include<stdio.h>
#include<algorithm>
#define MAX 101000
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int a[MAX];
bool C(int d)
{
    int sum=0;
    int ans=1;
    for(int i=1;i<n;i++)
    {
        if((sum+a[i]-a[i-1])<d)
        {
            sum+=a[i]-a[i-1];

        }
        else
        {
            sum=0;
            years ++ ;
        }
    }
    if(ans>=m)
        return true;
    return false;
}
intmain ()
{
    int en= -1 ,st= INF;
    scanf("%d%d",&n,&m);
    for(int i = 0 ; i < n ; i++)
    {
        scanf("%d",&a[i]);
    }

    sort(a,a+n);
    en=a[n-1]-a[0];
    for(int i=1;i<n;i++)
    {
        st=min(st,a[i]-a[i-1]);

    }
     while(en-st>1)
     {
         int mid=(en+st)/2;
          if(C(mid))
            st=mid;
          else
            en=mid;
     }
    printf( " %d\n " ,(st+en)/ 2 ); /// The question came to my mind because I don't know whether it is en or st 
}
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324724639&siteId=291194637