Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #define FAST_ALLOCATOR_MEMORY 1e8
  2. #include <vector>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include "optimization.h"
  6. //#include <unordered_map>
  7. //#include <cassert>
  8. using namespace std;
  9.  
  10. int points[100'005];
  11. int first_, last_;
  12. int n;
  13. bool is_in(int l, int r) { // [l, r]
  14.    return last_ <= r and last_ >= l;
  15. }
  16.  
  17. bool is(int ans, int k) {
  18.    int cur_beg = first_;
  19.    int cur_end = cur_beg + ans;
  20.    int count = 1;
  21.    if (is_in(cur_beg, cur_end)) return k >= count;
  22.    while (true) {
  23.        cur_beg = *upper_bound(points, points + n, cur_end);
  24.        cur_end = cur_beg + ans;
  25.        count++;
  26.        if (count > k) return false;
  27.        if (is_in(cur_beg, cur_end)) return k >= count;
  28.    }
  29. }
  30. int main() {
  31.    int k;
  32. //    cin >> n >> k;
  33.    n = readInt();
  34.    k = readInt();
  35.    for (int i = 0; i < n; i++) {
  36.        int buf;
  37. //        cin >> buf;
  38.        buf = readInt();
  39.        points[i] = buf;
  40.    }
  41.    sort(points, points + n);
  42.    int right = points[n - 1];
  43.    last_ = right;
  44.    int left = points[0];
  45.    first_ = left;
  46.    int ans_right = right - left + 1;
  47.    int ans_left = 0;
  48.    while (ans_right > ans_left) {
  49.        int ans_mid =  (ans_right + ans_left) / 2;
  50.        if (is(ans_mid, k)) {
  51.            ans_right = ans_mid;
  52.        } else {
  53.            ans_left = ans_mid + 1;
  54.        }
  55.    }
  56.    writeInt(ans_left);
  57.    writeChar('\n');
  58. //    cout << ans_left << endl;
  59.    return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement