-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaggresive_cow.cpp
72 lines (63 loc) · 1.51 KB
/
aggresive_cow.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
/*
author:nakul bharti
date:2018-08-09 12:39:55.
spoj
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios::sync_with_stdio(false); // fast i/o
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
ll n,c;
cin>>n>>c;
ll a[n]; // all stall position
for(ll i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n); //sorting the stall position
ll beg = 0; //the minimum value difference possible in stall (keep in mind that min. diff. is our answer)
ll en = a[n-1]; ////the max value difference possible in stall
ll p = c;
ll ans = -1; //ans will contain our answer
while(beg<en) // simple binary search
{
ll mid = (beg+en)/2;
ll k=0;ll f=0;
for(ll i = 1;i<n;i++)
{
if(a[i]-a[k]>=mid)
{
c--;
k = i;
}
if(c==1)
{
f=1;
}
}
if(f==0)
{
en = mid;
}
else{
if(ans<mid)
ans = mid;
beg = mid+1;
}
c = p;
if(beg==en)
{
cout<<ans<<endl;
break;
}
}
}
}