题目:
| Saruman's Army| Time Limit: 1000MS | | Memory Limit: 65536K |
| --------------------------- | - | ------------------------ |
| Total Submissions: 8359 | | Accepted: 4257 |DescriptionSaruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effective range of R units, and must be carried by some troop in the army (i.e., palantirs are not allowed to “free float” in mid-air). Help Saruman take control of Middle Earth by determining the minimum number of palantirs needed for Saruman to ensure that each of his minions is within R units of some palantir.InputThe input test file will contain multiple cases. Each test case begins with a single line containing an integer R, the maximum effective range of all palantirs (where 0 ≤ R ≤ 1000), and an integer n, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positionsx1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.OutputFor each test case, print a single integer indicating the minimum number of palantirs needed.Sample Input0 3 10 20 20 10 7 70 30 1 7 15 20 50 -1 -1Sample Output```
2 4
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - | - |
\[[Submit](http://poj.org/submit?problem_id=3069)] \[Go Back] \[[Status](http://poj.org/problemstatus?problem_id=3069)] \[[Discuss](http://poj.org/bbs?problem_id=3069)]
代码:
```cpp
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
while(1)
{
int n,r,a[1010];
scanf("%d %d",&r,&n);
if(r==-1&&n==-1)return 0;
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int i=0,ans=0;
while(i<n)
{
int s=a[i++];//先让s指向第一个点,s是没有被覆盖的最左的点的位置
while(i<n&&a[i]<=s+r)i++;//一直向右前进知道距离s的距离大于r的点
int p=a[i-1];//新加上的标记的点的位置
while(i<n&&a[i]<=p+r)i++;//一直向右前进知道距离P的距离大于r的点
ans++;//记录标记的点的个数
}
printf("%d\n",ans);
}
return 0;
}
PS:还是贪心的题,给了很多点给定一个半径,让选出最小的点的数量覆盖完这半径范围内所有点。