题目:
http://poj.org/problem?id=3069
题意:
在一条直线上有n个点,给出这些点的位置,再给出给出一个r,从n个点中选出一些点,以选出的点为中心覆盖半径为r内的其他点(选出的点也被覆盖),问最小选出多少点可以覆盖所有的点。
思路:
贪心算一下。以最左端的点为起点,向右延伸r的距离,找出此距离内最靠右的点,这个最靠右的点就是要选中的点,再以此点为起点,向右延伸r的距离,覆盖掉应该覆盖掉的点。然后以未被覆盖的最左点为起点,重复上述过程即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 10010, INF = 0x3f3f3f3f;
int arr[N];
int work(int st, int r, int n)
{
for(int i = st; i <= n; i++)
if(arr[st] + r < arr[i]) return i-1;
return n;
}
int main()
{
int r, n;
while(scanf("%d%d", &r, &n), r != -1 || n != -1)
{
for(int i = 1; i <= n; i++) scanf("%d", &arr[i]);
sort(arr + 1, arr + 1 + n);
int res = 0, st = 1;
while(st <= n)
{
int tmp = work(st, r, n);
st = work(tmp, r, n) + 1;
res++;
}
printf("%d\n", res);
}
return 0;
}