File size: 1,032 Bytes
c4b0eef |
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 74 75 76 77 78 79 80 81 82 83 |
#include <bits/stdc++.h>
using namespace std;
int stalls[100100];
int t,n,c,aux;
bool colocar(int key, int dist)
{
bool ans= false;
int vacas=1;
int pos= stalls[0];
for (int i = 1; i < n; ++i)
{
if(stalls[i]-pos>=dist)
{
pos= stalls[i];
vacas++;
if (vacas==key)
{
ans= true;
break;
}
}
}
return ans;
}
int BinarySearch(int hi,int lo, int key)
{
int mid,res=0;
while (hi>lo)
{
mid = lo + (hi-lo)/2;
if (colocar(key,mid))
{
lo= mid+1;
if (mid>res)
{
res=mid;
}
}
else
{
hi = mid;
}
}
return res; // lo is close to the border between no and yes
}
int main()
{
int max=0;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&c);
memset(stalls,0,n);
for (int i = 0; i < n; ++i)
{
scanf("%d",&aux);
stalls[i]= aux;
if (aux>max)
{
max= aux;
}
}
sort(stalls,stalls+n);
printf("%d\n",BinarySearch(max,0,c) );
}
return 0;
}
|