forked from ShivamDubey7/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathAGGRCOW.cpp
72 lines (60 loc) · 1.28 KB
/
AGGRCOW.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define vi vector<int>
#define pii pair<int, int>
#define all(c) c.begin(), c.end()
#define pb push_back
#define f first
#define s second
#define int long long
#define fo(i, n) for (int i = 0; i < n; i++)
#define IOS ios::sync_with_stdio(0);
vector<int> arr(100100);
int n,c;
bool check(int x){
int sum =0;int count = 1;
int left =0;
for(int i =1; i<n;i++){
sum += arr[i]-arr[i-1];
if(sum >= x)
{
count++;
sum=0;
}
}
return (count >= c);
}
void solve();
signed main()
{
IOS
cin.tie(0);
cout.tie(0);
int _t;cin >> _t;while (_t--) solve();
}
void solve(){
cin>>n>>c;
arr.resize(n);
for(int i = 0; i<n;i++){
cin>>arr[i];
}
sort(arr.begin(),arr.end());
int hi=0; int lo =0 ;
for(int i =1; i<n;i++){
hi =max(arr[i]-arr[i-1],hi);
lo = min(arr[i]-arr[i-1],lo);
}
int ans =lo;
while(hi>= lo){
int mid =lo + (hi-lo)/2;
if(check(mid)){
lo =mid+1;
ans = mid;
}
else{
hi = mid-1;
}
}
cout<<ans<<endl;
}