POJ 3069:Saruman's Army
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13242 | Accepted: 6636 |
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
Hint
In the first test case, Saruman may place a palantir at positions 10 and 20. Here, note that a single palantir with range 0 can cover both of the troops at position 20.
In the second test case, Saruman can place palantirs at position 7 (covering troops at 1, 7, and 15), position 20 (covering positions 20 and 30), position 50, and position 70. Here, note that palantirs must be distributed among troops and are not allowed to “free float.” Thus, Saruman cannot place a palantir at position 60 to cover the troops at positions 50 and 70.
题意:N个点,每个点对应一个位置,要求选出一些点进行标记,使距离这些点R的距离内存在被标记的点,求至少要标记多少个点。
思路参考白书46页
#include<stdio.h>
#include<string.h>
#include<algorithm>
const int maxn=1e5+10;
int a[maxn];
int main()
{
int r,n;
while(~scanf("%d%d",&r,&n))
{
if(r==-1&&n==-1) break;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++) scanf("%d",&a[i]);
std::sort(a,a+n);
int sum=0;
int i=0;
while(i<n)
{
int s=a[i++];
while(i<n&&s+r>=a[i]) i++;
int p=a[i-1];
while(i<n&&a[i]<=p+r) i++;
sum++;
}
printf("%d\n",sum);
}
return 0;
}
POJ 3069:Saruman's Army的更多相关文章
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- POJ 3069 Saruman's Army(贪心)
Saruman's Army Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- POJ 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6688 Accepted: 3424 De ...
- POJ 3069 Saruman's Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
- Q - Saruman's Army POJ - 3069
Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...
- poj 3069 Saruman's Army 贪心模拟
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18794 Accepted: 9222 D ...
- poj 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
随机推荐
- 《剑指offer》第三十九题(数组中出现次数超过一半的数字)
// 面试题39:数组中出现次数超过一半的数字 // 题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例 // 如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, ...
- js 中的几个假值
1. 使用场景 if分支语句 / 短路语句while循环语句for里的第二个语句 2. 6个假值 (都属于 原始类型数据的一部分内容,非原始类型即对象都是真值,如:对象.数组.正则.函数 . ...
- 使用SFTP工具相关问题
1.使用SFTP工具,填写ip,端口都正确但是连接不上? 答:请统一使用 filezilla工具进行连接,环境搭建使用该工具进行测试和使用. 2.使用SFTP工具访 ...
- resin中关于url rewrite来传递jsessionid的问题
最近两天在项目中碰到,一个很奇怪的问题.同一个账号多次切换登录时,会出现这个账号的信息在session中找不到,虽然可以登录成功,但是之后这个用户信息好像没有保存到session中一样,或者是被改变了 ...
- json-rpc和restful
json-rpc是一种动作 restful 是一种资源 RPC 所谓的远程过程调用 (面向方法) SOA 所谓的面向服务的架构(面向消息) REST 所谓的 Representational stat ...
- 关于angular5的惰性加载报错问题
之前为了测试一个模块优化问题,于是用angular-cli快速搭建了个ng5的脚手架demo,在应用惰性加载功能的时候发现浏览器报错如下: ERROR Error: Uncaught (in prom ...
- Python mysql-常用对象
2017-09-08 13:14:14 db = pymysql.connect(host,user,passwaord,db,chartset),charset=utf8,可以避免中文的乱码 con ...
- English trip -- Review Unit6 Time 时间
It's at seven o'clock 整点 7点整 It's at half past seven or It's seven-thirty7点30 It's at seven fi ...
- Confluence 6 使用 LDAP 授权连接一个内部目录 - 用户 Schema 设置
请注意:这部分仅在拷贝用户登录(Copy User on Login)功能被启用后可见. 其他用户 DN(Additional User DN) 这个值被用在进行用户查找和载入的时候来针对 base ...
- 二叉树—-1(No.9HN省赛小题)
题目: 1013: Prototypes analyze 时间限制: 1 Sec 内存限制: 128 MB提交: 6 解决: 4[提交][状态][讨论版] 题目描述 ALpha Ceiling M ...