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.

 #include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n, r;
while (cin >> r >> n, n != - && r != -)
{
int a[];
for (int i = ; i < n; i++)
cin >> a[i];
sort(a, a + n);
int i = , ans = ;
while (i < n)
{
int s = a[i++];
while (i < n&&a[i] <= s + r) i++;
int p = a[i - ];
while (i < n&&a[i] <= p + r) i++;
ans++;
}
cout << ans << endl;
}
return ;
}

//这次学习的代码是通过循环控制找到最优位置

Saruman's Army(POJ3069)的更多相关文章

  1. poj3069 Saruman's Army

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

  2. POJ3069 Saruman's Army【贪心】

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

  3. poj 3069 Saruman's Army

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

  4. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

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

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

  6. POJ 3069:Saruman's Army

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

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

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

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

    题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...

随机推荐

  1. shell实现带颜色输出的进度条

    1.基础版 #!/bin/bash b='' ;$i<=;i+=)) do printf "执行进度 :[%-50s]%d%%\r" $b $i sleep 0.001 b= ...

  2. python 的基础 学习第十天函数的初始

    1,什么是函数,函数就是封装一个功能. 怎么定义函数. # def my_len():#def 是关键字,定义一个一个函数.#my_len():就是函数名,必须和关键字加一个空格,后面加括号和冒号.d ...

  3. 不指定虚拟路径的前提下通过http访问pdf、图片等文件

    通常我们通过http访问图片或者pdf的时候都是将文件上传到指定文件夹下面,然后通过配置虚拟路径来访问指定的资源. 在不配置虚拟路径的情况下,我们通过获取到response的outpurstream, ...

  4. oracle查看未提交事务

    SELECT s.sid, s.serial#, s.event, a.sql_text, a.sql_fulltext, s.username, s.status, s.machine, s.ter ...

  5. MyBatis-进阶1

    接入门的实例,我们知道MyBatis可以使用注解和配置文件实现接口和sql语句的绑定. 那么一个接口方法同时使用注解和xml配置会怎么样. @Select("select * from us ...

  6. ipconfig命令查ip的时候给别人看有危险吗

    知识源:Unit 4: Networking 1 4.1 Networking 1 The Routing of a Packet  网址:https://www.baidu.com/link?url ...

  7. Linux下查询文件的md5,sha1值

    验证下载下来的文件包是不是一致 ··· 验证md5值 #md5sum filename 验证shal值 #sha1sum filename ···

  8. Linux运行时I/O设备的电源管理框架【转】

    转自:https://www.cnblogs.com/coryxie/archive/2013/03/01/2951243.html 本文介绍Linux运行时I/O设备的电源管理框架.属于Linux内 ...

  9. Zynq-Linux移植学习笔记之27UIO机制响应外部中断实现【转】

    转自:https://blog.csdn.net/zhaoxinfan/article/details/80285150 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...

  10. Windows PowerShell 入門(5)-制御構文

    Windows PowerShellにおける制御構文について学びます.数ある制御構文の中でもSwitch文は.他の言語に比べ豊富な機能が用意されています. 対象読者 Windows PowerShel ...