-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAGGRCOW.cpp
More file actions
73 lines (72 loc) · 1.24 KB
/
AGGRCOW.cpp
File metadata and controls
73 lines (72 loc) · 1.24 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
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
#define test int t;scanf("%d",&t);while(t--)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define pii pair<int,long int>
#define ll long long int
#define pli pair<ll ,int>
using namespace std;
ll stalls[100007];
int n,c;
int binarySearch(int point)
{
int start=1,end=n,mid,last=-1;
while(start<=end)
{
mid=(start+end)/2;
if(stalls[mid]>point)
{
last=mid;
end=mid-1;
}
else if(stalls[mid]<point)
start=mid+1;
else
return mid;
}
return last;
}
bool check(ll d)
{
//printf(" IN CHECK FOR VALUE : %d\n",d);
ll nextPoint=stalls[1]+d,cows=c-1,i;
while(cows!=0)
{
i=binarySearch(nextPoint);
//printf(" <%lld , %lld >,",nextPoint,stalls[i]);
if(i!=-1)
{
cows--;
nextPoint=stalls[i]+d;
}
else
return false;
}
return true;
}
int main()
{
test
{
scanf("%d%d",&n,&c);
loop(i,1,n+1)
scanf("%lld",&stalls[i]);
sort(stalls,stalls+n);
ll start=1,end=stalls[n]-stalls[1],mid,ans;
//printf("start = %lld end = %lld\n",start,end);
while(start<=end)
{
mid=(start+end)/2;
//printf("Checking for mid = %lld\n",mid);
if(check(mid))
{
// printf("true for diff = %lld\n",mid);
ans=mid;
start=mid+1;
}
else
end=mid-1;
}
printf("%lld\n",ans);
}
return 0;
}