Skip to content

Instantly share code, notes, and snippets.

@bouk
Created June 21, 2013 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouk/5832485 to your computer and use it in GitHub Desktop.
Save bouk/5832485 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int H, k;
vector<int> houses;
bool possible(int w)
{
for(int i = 0; i < H; i++)
{
int left = k - 1;
int start = houses[i];
int end = houses[i] + w * 2;
for(int h = (i + 1) % H; h != i; h = (h + 1) % H)
{
//printf("%d\n", pos);
if(start <= houses[h] && houses[h] <= end || ((end > 1000000) && houses[h] <= (end % 1000000)))
{
continue;
}
else
{
// printf("%d %d %d %d\n", i, end , w, houses[h]);
left--;
// printf("%d\n", left);
start = houses[h];
end = houses[h] + w * 2;
}
}
if(left >= 0)
{
return true;
}
}
return false;
}
int main()
{
scanf("%d", &H);
houses.resize(H);
for(int i = 0; i < H; i++)
{
scanf("%d", &houses[i]);
}
sort(houses.begin(), houses.end());
scanf("%d", &k);
int low = 0;
int high = 500000;
while(low < high)
{
int center = (low + high) / 2;
if(possible(center))
{
high = center;
}
else
{
low = center + 1;
}
}
printf("%d\n", high);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment