Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- const int maxN = 100010;
- long long dist[maxN];
- long long n,c;
- bool yes (long long d) {
- long long cow = c;
- long long prev_loc = dist[1];
- --cow;
- vector<int> loc;
- loc.push_back(dist[1]);
- for (int i = 2; i <= n; ++i) {
- if (dist[i] - prev_loc >= d) {
- loc.push_back(dist[i]);
- --cow;
- prev_loc = dist[i];
- }
- if (cow == 0) {
- return true;
- }
- }
- return false;
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int T;
- cin >> T;
- for (int test_case = 1; test_case <= T; ++test_case) {
- cin >> n >> c;
- for (int i = 1; i <= n; ++i) cin >> dist[i];
- sort (dist + 1,dist + n + 1);
- long long res = 0;
- long long l = 1,r = dist[n];
- while (l <= r) {
- long long mid = l + (r - l) / 2;
- if (yes(mid)) {
- l = mid + 1;
- res = mid;
- } else {
- r = mid - 1;
- }
- }
- cout << res << '\n';
- //~ cout << dist[1] << ' ';
- //~ --c;
- //~ long long prev_loc = dist[1];
- //~ for (int i = 1; i <= n; ++i) {
- //~ if (dist[i] - prev_loc >= res) {
- //~ cout << dist[i] << ' ';
- //~ --c;
- //~ }
- //~ if (c == 0) break;
- //~ }
- }
- //~ cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
- }
- Problem Link : https://www.spoj.com/problems/AGGRCOW/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement