Wednesday, February 12, 2020

POJ.2456 Aggressive cows

1.Problem
http://poj.org/problem?id=2456

2.Idea
Binary Search

3.Source
 int N, M;  
 double x[100005];  
 bool C(int d)  
 {  
      int last = 0;  
      for (int i = 1; i < M; i++) {  
           int crt = last + 1;  
           while (crt < N && x[crt] - x[last] < d) {  
                crt++;  
           }  
           if (crt == N) return false;  
           last = crt;  
      }  
      return true;  
 }  
 void solve()  
 {  
      sort(x, x + N);  
      int lb = 0, ub = INF;  
      while (ub - lb > 1) {  
           int mid = (lb + ub) / 2;  
           if (C(mid)) lb = mid;  
           else ub = mid;  
      }  
      printf("%d\n", lb);  
 }  
 int main()  
 {  
      cin >> N >> M;  
      for (int i = 0; i < N; i++) cin >> x[i];  
      solve();  
      return 0;  
 }  

No comments:

Post a Comment