题意如下:
直线上有N个点,第i点的位置为Xi。从这N个点中选择若干个,给他们加行标记。对每一个点,其距离为R以内的区域里必须带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点)。在满足这个条件的情况下,希望能为尽可能少的点添加标记。请问至少需要为多少个点添加上标记?
AC代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=10001;
int R,n,a[maxn];
int main()
{
while(cin>>R>>n,R+n!=-2)
{
a[0]=0;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);//按照位置进行排序
int ans=0;
for(int i=1;i<=n;)
{
int next_pos=i+1;
while(a[i]+R>=a[next_pos]&&next_pos<=n)
next_pos++;//找到以第i个点为中心能覆盖的点的位置的后面一个位置。
//cout<<"从左到右不能覆盖的第一个点的位置:"<<next_pos<<endl;
i=next_pos-1;
while(a[i]+R>=a[next_pos]&&next_pos<=n)
next_pos++;
i=next_pos;
//cout<<"从右到右不能覆盖的第一个点的位置:"<<next_pos<<endl;
ans++;
}
cout<<ans<<endl;
}
return 0;
}