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 withR = 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.

题目意思:Saruman的军队有n个人,为了控制住他的军队,他将一个装着放在所给样例数的一些士兵身上,给出装置的控制范围r,要求出最少需要多少个装置就能完全控制住他的军队。

解题思路:这是一道贪心题,贪心策略是从第一个士兵开始向后面遍历,找到满足距离小于r的最右边的点,这一个点一定会作为一个装着的放置的位置,然后从这个位置开始找到右边的最小的不能被覆盖到的位置作为下一次的起点....循环下去,直到所有的点都被覆盖到。

上代码:

 #include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,r,count,i,k,p;
int a[];
while(scanf("%d%d",&r,&n)!=EOF)
{
if(r==-&&n==-)
{
break;
}
for(i=; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
i=;
count=;
while(i<n)
{
k=a[i]+r;
while(i<n&&a[i]<=k)///找到满足距离小于r的最右边的点
{
i++;
}
p=a[i-]+r;///确定的人
while(i<n&&a[i]<=p)
{
i++;
}
count++;
}
printf("%d\n",count);
}
return ;
}

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. POJ 3069 Saruman's Army(贪心)

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

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

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

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

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

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

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

  6. poj 3069 Saruman's Army(贪心)

    Saruman's Army Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  7. POJ3069 Saruman's Army【贪心】

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

  8. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  9. poj3069 Saruman's Army

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

随机推荐

  1. 谷歌浏览器添加flash白名单

    69以前的版本: 打开 chrome://settings/content/flash 上图中应该有一个"添加"选项 , 依次输入: *.]com [*.]net [*.]org ...

  2. Eclipse报The default workspace'xxxxx' is in use or cannot be created Pl

    原因:出现这种情况一般是workspace的配置文件中出现了.lock文件(workspace/.metadata/.lock),锁定了workspace.把.lock文件删除即可.如果该文件不能删除 ...

  3. Android编译命令

    目录 说在前面 编译流程 编译指令 代码编译 代码检索 其他指令 说在前面 从最开始接触Android系统开始,每次进行代码编译都需要网上搜索编译指令.后来大致熟悉了Android的编译体系,加深了对 ...

  4. ACM1003:Max Sum

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

  5. C# set 跟 get

    可以在类里面 private string name; public string Name { get { return name; } set { name = value; } }

  6. linux (ubuntu)安装pycharm

    1. 下载 http://www.jetbrains.com/pycharm/download/ 选择Linux Tab,选择下载免费的Community Edition. 2. 安装PyCharm ...

  7. vuetify.js框架 下拉框数据改变DOM原数据未清除

    今天遇到一个奇怪的bug 需求很简单,就是将“引擎能力”下拉框选中的值作为筛选条件传入到“样本类型”下拉框中,默认“样本类型”下拉框显示所有样本类型 看图: 如图所示,功能很简单. 其实还是对vuet ...

  8. CakePHP Model中( 获取Session)使用Component的方法

    有时候我们需要在Model中使用Session,大家知道CakePHP把操作Session的方法封装为了一个Component, 在Model中正常读取Session的方法: 在 "app_ ...

  9. autocomplete.jquery 点击或进入默认显示所有结果

    注意使用的是autocomplete.jquery,官网地址是:https://github.com/devbridge/jQuery-Autocomplete.而不是JqueryUI的autocom ...

  10. eclipse插件SCON的SConscript文件和头文件以及C文件包含路径

    1. 本次的头文件路径\Hi2110-B657SP3-SDK\src_release_657SP3\src\lib\onenet\public,以此例子作为研究,本次开发使用eclipse,用到SCO ...