-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaggressive_cows.cpp
More file actions
60 lines (54 loc) · 1.22 KB
/
Copy pathaggressive_cows.cpp
File metadata and controls
60 lines (54 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
const int N = 100005;
bool check(int a[], int n, int dis, int k) {
int i, placed = 1, prev = a[0];
for (i = 1; i < n; i++) {
if (a[i] - prev >= dis) {
placed++;
prev = a[i];
}
}
return placed >= k;
}
int32_t main()
{
ios_base:: sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t; cin >> t; while (t--)
{
int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
cin >> n >> m;
int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int lf = 1, rt = a[n - 1];
while (lf <= rt) {
int mid = (lf + rt) / 2;
if (check(a, n, mid, m)) {
ans = mid;
lf = mid + 1;
}
else {
rt = mid - 1;
}
}
cout << ans;
}
}