poj-3065
来源:互联网 发布:读地理地图软件 编辑:程序博客网 时间:2024/06/07 08:53
Saruman's Army
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5711 Accepted: 2927
Description
Saruman 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.
Input
The 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 positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.
Output
For each test case, print a single integer indicating the minimum number of palantirs needed.
Sample Input
0 3
10 20 20
10 7
70 30 1 7 15 20 50
-1 -1
Sample Output
2
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5711 Accepted: 2927
Description
Saruman 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.
Input
The 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 positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.
Output
For each test case, print a single integer indicating the minimum number of palantirs needed.
Sample Input
0 3
10 20 20
10 7
70 30 1 7 15 20 50
-1 -1
Sample Output
2
4
题目来源:http://poj.org/problem?id=3069
考察点:贪心算法
题目大意:给你n个数,从这n个数中选取最少的数,做上标记,使对每一个数,在它距离为r的范围内都有被标记的数.求最少选取几个数.
解题思路:将n个数从小到大排序,然后从最左边的点开始(记为a点),寻找一个距a点r范围内最远的点,那么该点就是因该标记的点,记为b,因为它的覆盖面最大,满足最优情况,剩下的点也这样处理.寻找下一个点时,跳过(b-r,b+r).
代码如下:
#include<stdio.h>#include<algorithm>using namespace std;int x[1010];int ans;int r,n;int main(){ int i,j,y;while(scanf("%d%d",&r,&n)){if(n==-1&&r==-1)break;for(i=0;i<n;i++)scanf("%d",&x[i]);sort(x,x+n);ans=0;int t,p;int flag=0;for(i=0;i<n;){ for(j=i+1;j<n;j++) { if(x[i]+r<x[j])break; } for(p=j;p<n;p++) { if(x[j-1]+r<x[p]) break; } ans++; i=p; }printf("%d\n",ans);}return 0;}
0 0
- poj-3065
- POJ 3065 Stargates 笔记
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Android学习实践:7.使用布局文件进行事件绑定
- 使用Lambda与不使用Lambda表达式
- char *str 与char str[] 的区别
- 新电脑安装系统时提示 File:\Boot\BCD Status:0xc000000e 错误解决方案
- EventBus使用详解(一)——初步使用EventBus
- poj-3065
- Redis 缓存 + Spring 的集成示例
- VMWare安装Mac OS X
- Android中自定义弹窗提醒控件
- 正在腾飞的教育界,你了解多少?
- PAT(甲级)1022
- Struts2的action配置方法
- A. Kefa and First Steps
- mysql中You can't specify target table for update in FROM clause错误 转