SPOJ 297 minimum Aggressive cows Interval

Source: Internet
Author: User
Tags cmath

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;}
        
       
      
     
    
   
  
 

 

??

 

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.