Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define FAST_ALLOCATOR_MEMORY 1e8
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include "optimization.h"
- //#include <unordered_map>
- //#include <cassert>
- using namespace std;
- int points[100'005];
- int first_, last_;
- int n;
- bool is_in(int l, int r) { // [l, r]
- return last_ <= r and last_ >= l;
- }
- bool is(int ans, int k) {
- int cur_beg = first_;
- int cur_end = cur_beg + ans;
- int count = 1;
- if (is_in(cur_beg, cur_end)) return k >= count;
- while (true) {
- cur_beg = *upper_bound(points, points + n, cur_end);
- cur_end = cur_beg + ans;
- count++;
- if (count > k) return false;
- if (is_in(cur_beg, cur_end)) return k >= count;
- }
- }
- int main() {
- int k;
- // cin >> n >> k;
- n = readInt();
- k = readInt();
- for (int i = 0; i < n; i++) {
- int buf;
- // cin >> buf;
- buf = readInt();
- points[i] = buf;
- }
- sort(points, points + n);
- int right = points[n - 1];
- last_ = right;
- int left = points[0];
- first_ = left;
- int ans_right = right - left + 1;
- int ans_left = 0;
- while (ans_right > ans_left) {
- int ans_mid = (ans_right + ans_left) / 2;
- if (is(ans_mid, k)) {
- ans_right = ans_mid;
- } else {
- ans_left = ans_mid + 1;
- }
- }
- writeInt(ans_left);
- writeChar('\n');
- // cout << ans_left << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement