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, …, xnof 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

思路:从最左边开始,找r范围内最右边的点,然后在超出r范围的最左边点当做起始点继续寻找。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
int main()
{
int n,r, arr[maxn];
while(scanf("%d%d",&r,&n)!=EOF && n+r!=-2)
{
for(int i=0;i<n;++i)
scanf("%d",&arr[i]);
sort(arr,arr+n);
int i=0,ans=0;
while(i<n)
{
int s=arr[i++];
while(i<n && arr[i]<=s+r)
++i;
int p=arr[i-1];
while(i<n && arr[i]<=p+r)
++i;
++ans;
}
printf("%d\n",ans);
}
return 0;
}

POJ3069 Saruman's Army【贪心】的更多相关文章

  1. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  2. poj3069 Saruman's Army

    http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...

  3. POJ 3069 Saruman's Army(贪心)

     Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  4. poj 3069 Saruman's Army 贪心模拟

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18794   Accepted: 9222 D ...

  5. B - B Saruman's Army(贪心)

    在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...

  6. poj3069 Saruman's Army(贪心)

    https://vjudge.net/problem/POJ-3069 弄清楚一点,第一个stone的位置,考虑左右两边都要覆盖R,所以一般情况下不会在左边第一个(除非前两个相距>R). 一开始 ...

  7. poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...

  8. Saruman's Army(贪心)

    Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep tra ...

  9. Saruman's Army(POJ3069)

    Description Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. ...

随机推荐

  1. C++对象模型——对象复制语意学 (Object Copy Semantics)(第五章)

    5.3    对象复制语意学 (Object Copy Semantics) 当设计一个 class,并以一个 class object指定给 class object时,有三种选择:     1.什 ...

  2. HDU1698 Just a Hook 【线段树】+【成段更新】+【lazy标记】

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. SQL Server2012 T-SQL基础教程--读书笔记(8 - 10章)

    SQL Server2012 T-SQL基础教程--读书笔记(8 - 10章) 示例数据库:点我 CHAPTER 08 数据修改 8.1 插入数据 8.1.1 INSERT VALUES 语句 8.1 ...

  4. C语言实现字符串拼接

    #include <stdio.h>#include <stdlib.h>#include <string.h> char* str_contact(const c ...

  5. PCB SLOT槽孔数量计算方法,同CAM350孔数一致 实现方法

    最近有好几个写脚本的朋友问我,SLOT槽孔孔的如何计算的,要求孔数与CAM350孔数保持一致. 前几年通过在CAM350里面不断测试,结果是:CAM 350中SLOT槽孔,孔与孔之间最高位,凸位高度值 ...

  6. Necklace of Beads(polya定理)

    http://poj.org/problem?id=1286 题意:求用3种颜色给n个珠子涂色的方案数.polya定理模板题. #include <stdio.h> #include &l ...

  7. A Round Peg in a Ground Hole(圆与凸包)

    http://poj.org/problem?id=1584 题意:判断所给的点能不能形成凸包,并判断所给的圆是否在凸包内. 改了好几天的一个题,今天才发现是输入顺序弄错了,办过的最脑残的事情..sa ...

  8. EditPlus 2:用空格替换制表符

    打开软件点击菜单栏上的Tools(工具),在点击perferences(外观),再点击左边栏的File->Setting & Syntax(文件->设置与符号),再点击右栏的Tab ...

  9. BZOJ 2406 二分+有上下界的网络流判定

    思路: 求出每行的和  sum_row 每列的和   sum_line 二分最后的答案mid S->i  流量[sum_row[i]-mid,sum_row[i]+mid] i->n+j ...

  10. reduce多种方法计算数组中某个值的出现次数

    先来了解下reduce用法 arr.reduce(callback[, initialValue]) callback执行数组中每个值的函数,包含四个参数: accumulator 累计器累计回调的返 ...