-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.cpp
More file actions
60 lines (58 loc) · 1.12 KB
/
base.cpp
File metadata and controls
60 lines (58 loc) · 1.12 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>
#define ll long long
ll pos[50005],n,k;
using namespace std;
bool check(ll len)
{
ll cnt(0);
for(ll i=0;i<n;i++)
{
cnt++;
if(cnt>k)
return false;
if((pos[n-1]<=pos[i]+len)&&(cnt<=k))
return true;
ll bou = pos[i]+len;
while((i+1)<n&&(pos[i+1]<=bou))
i++;
}
return false;
}
int main()
{
int t;
ll l,r;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&k);
for(int i=0;i<n;i++)
{
scanf("%lld",&pos[i]);
}
sort(pos,pos+n);
if(k==1)
{
printf("%lld\n",pos[n-1]-pos[0]);
continue;
}
l=1;
r = (pos[n-1]-pos[0])/k+1;
while(l<r)
{
ll mid = (l +r)/2;
if(check(mid))
{
r=mid;
}
else
{
l = mid+1;
}
if(l==r)
break;
}
printf("%lld\n",r);
}
return 0;
}