【二分】PKU 2456 Aggressive cows

【二分】PKU 2456 Aggressive cows
#include  < iostream >
#include 
< algorithm >
#include 
< vector >
#include 
< string >
using   namespace  std;
#define  MAXSIZE 100010
int  x[MAXSIZE];
int  c,n;
bool  check( int  dis)
{
    
int  pre = 0 ,count = 1 ;
    
for ( int  i = 1 ;i < n;i ++ )
        
if (x[i] - x[pre] >= dis)
        {
            count
++ ;
            
if (count >= c)  return   1 ;
            pre
= i;
        }
    
return   0 ;
}
int  binary_search()
{
    
int  low = 0 ,up = x[n - 1 ] - x[ 0 ],mid;
    
while (low < up)
    {
        
// mid=low+(up-low)/2;
        mid = (low + up + 1 ) / 2 ;
        
if (check(mid))
            low
= mid;
        
else  up = mid - 1 ;
    }
    
return  low;
}
int  main()
{
    scanf(
" %d%d " , & n, & c);
    
for ( int  i = 0 ;i < n;i ++ )
        scanf(
" %d " , & x[i]);
    sort(x,x
+ n);
    printf(
" %d\n " ,binary_search());
}
http://acm.pku.edu.cn/JudgeOnline/problem?id=2456
注意二分时mid的取值

你可能感兴趣的:(【二分】PKU 2456 Aggressive cows)