Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define llj(a,b,c) for (int (a) = (b); (a) < (c); (a)++)
- #define lli(a,b) llj(i,a,b)
- #define lll(i, x) llj(i, 0, x)
- #define fori(x) lll(i, x)
- #define forj(x) lll(j, x)
- #define fork(x) lll(k, x)
- #define all(X) (X).begin(), (X).end()
- #define ever ;;
- #define fe(x, C) for (auto& x : (C))
- #define INF ((int) 1e9+10)
- #define LINF ((ll) 1e18 + 10)
- #define pb push_back
- #define mp make_pair
- #define ms(arr,val) memset(arr , val , sizeof(arr))
- #define rint(x) scanf("%d", &(x))
- #define rlong(x) scanf("%lld", &(x))
- #define nrint(x) int x; rint(x);
- #define nrlong(x) long long x; rlong(x);
- #define rfloat(x) scanf("%lf", &(x))
- #define endl "\n"
- #define trace(X) cout << (X) << endl;
- #define trace2(X,Y) cout << (X) << ' ' << (Y) << endl;
- #define trace3(X,Y,Z) cout << (X) << ' ' << (Y) << ' ' << (Z) << endl;
- #define trace4(X,Y,Z,A) cout << (X) << ' ' << (Y) << ' ' << (Z) << ' ' << (A) << endl;
- #define tp(X) trace2(X.first ,X.second)
- #define vi vector<int>
- #define vvi vector<vector<int> >
- #define pvi(a) fe(thing ,a) cout << thing << ' '; cout << "\n";
- typedef long long ll;
- typedef pair<int,int> pii;
- #define sz(A) ((int)A.size())
- #define x first
- #define y second
- #define read_speed ios::sync_with_stdio(false); cin.tie(0)
- #define LINE cout << endl << "--------------------------" << endl
- /*--------------------------THE END---------------------------------*/
- int n,k;
- vector<int> x;
- int Check(int dist)
- {
- int needed = 0;
- int current = 0;
- while (current < n)
- {
- //cout << current << endl;
- int cityIndex = upper_bound(all(x), x[current] + dist) - x.begin() - 1;
- int lastIndex = upper_bound(all(x), x[cityIndex] + dist) - x.begin() - 1;
- current = lastIndex + 1;
- needed++;
- //trace3(cityIndex, lastIndex, current);
- }
- return needed <= k;
- }
- int BinarySearch()
- {
- int lo = 0, hi = 1e9;
- while (lo < hi)
- {
- int mid = (lo + hi) / 2;
- if (Check(mid))
- {
- hi = mid;
- }
- else
- {
- lo = mid + 1;
- }
- }
- return lo;
- }
- int main()
- {
- // freopen("test.txt", "r", stdin);
- cin >> n >> k;
- int temp;
- fori(n)
- {
- cin >> temp;
- x.push_back(temp);
- }
- sort(all(x));
- cout << BinarySearch() << endl;
- //cout << Check(2) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement