Advertisement
Emiliatan

c575

May 29th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. /* c575             */
  2. /* AC (11ms, 636KB) */
  3. #include <cstdio>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. typedef long long int64;
  10.  
  11. vector<int64> point;
  12.  
  13. bool solve(int r, int N, int K)
  14. {
  15.     int cnt = 0, Range;
  16.     for(auto it = point.begin(); it != point.end();)
  17.     {
  18.         Range = *it + r;
  19.         ++cnt;
  20.         if(cnt > K)
  21.             return false;
  22.         if(Range >= point[N - 1] && cnt <= K)
  23.             return true;
  24.         while(Range >= *it)
  25.             ++it;
  26.     }
  27.     return false;
  28. }
  29. int main()
  30. {
  31.     for(int N, K, result; ~scanf("%d %d", &N, &K) && N && K;)
  32.     {
  33.         point.resize(N);
  34.         for(int i = 0; i < N && scanf("%lld", &point[i++]););
  35.         sort(point.begin(), point.end());
  36.  
  37.         int R = point[N - 1] - point[0], L = 1;
  38.         if(K == 1)
  39.         {
  40.             printf("%d\n", R);
  41.             continue;
  42.         }
  43.         else
  44.             while(L < R)
  45.             {
  46.                 int M = (R + L) >> 1;
  47.                 if(solve(M, N, K))
  48.                     R = M;
  49.                 else
  50.                     L = M + 1;
  51.             }
  52.         printf("%d\n", R);
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement