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 n,r;
int x[1005];
int s,p;
int main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    while(cin >> r >> n && n != -1 && r != -1){
        for(int i = 0; i < n; i++)
            cin >> x[i];
        sort(x,x+n);
        int i = 0,ans = 0;
        while(i < n){
            s = x[i++];
            while(i < n && x[i] <= s + r)
                i++;
            int p = x[i-1];
            while(i < n && x[i] <= p + r)
                i++;
            ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

Q - Saruman's Army POJ - 3069的更多相关文章

  1. Saruman's Army (POJ 3069)

    直线上有N个点.点i的位置是Xi.从这N个点中选择若干个,给它们加上标记.对每一个点,其距离为R以内的区域里必须又带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点).在 ...

  2. 贪心-Saruman‘s Army POJ - 3069

    万恶之源 目录 题意 思路 贪心的原则是什么呢? 错解 正解 代码实现 书上的代码 我的代码 比较一下 问题 题意 给定若干个点的坐标,与范围R.每个点可以选择是否标记,标记后这个点的左右范围R内的所 ...

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

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

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

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

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

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

  6. poj 3069 Saruman's Army

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

  7. POJ 3069:Saruman's Army

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

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

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

  9. POJ 3069 Saruman&#39;s Army

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

随机推荐

  1. php设计模式之工厂方法实例代码

    实现不修改原代码,扩展新功能 <?php header("Content-type:text/html;charset=utf-8"); /** * db接口 * 实现连接数 ...

  2. Go_sql注入

    我们任何时候都不应该自己拼接SQL语句! sqlInjectDemo("xxx' or 1=1#") sqlInjectDemo("xxx' union select * ...

  3. Navicat图形更改表结构的时,设置外键时出现1452错误

    原文地址:http://www.mamicode.com/info-detail-1296600.html 提示1452错误,如下图所示. 然后百度了一下,得到了一个靠谱的答案: 这是因为表设置了外键 ...

  4. tomcat、nginx、apache、tengine都是什么,及其作用

      Tomcat的功能职责:Tomcat运行在JVM之上,它和HTTP服务器一样,绑定IP地址并监听TCP端口,同时还包含以下指责: • 管理Servlet程序的生命周期• 将URL映射到指定的Ser ...

  5. grep Or And 操作

    grep or 操作符 使用 \| 如果不使用grep命令的任何选项,可以通过使用 '|' 来分割多个pattern,以此实现OR的操作. grep 'pattern1\|pattern2' file ...

  6. python正则非贪婪模式

    上一篇python正则匹配次数大家应该也发现了,除了?其他匹配次数规则都是尽可能多的匹配 那如果只想匹配1次怎么办呢,这就是正则中非贪婪模式的概念了 原理就是利用?与其他匹配次数规则进行组合 单个匹配 ...

  7. 6_14 Abbott的复仇(UVa816)<图的最短路BFS>

    1999次世界总决赛的比赛包括一个骰子迷宫问题.在这个问题被写的时候,法官们无法发现骰子迷宫概念的原始来源.不久之后的比赛,但是,罗伯特先生雅培,无数的迷宫和对作者的创造者主题,联系大赛评委,自称是骰 ...

  8. ASP.NET的 Razor引擎和JavaScript是一种什么关系

    Razor能做的JS大部分能做,不是全部.比如说,如果你用了Entity Frame一类的ORM的时候,Razor可以直接绑定数据库数据,但JS就不可能直接访问数据库——必须通过其他web servi ...

  9. C/S编程

    https://blog.csdn.net/antony1776/article/details/73717666 实现C/S程序,加上登录注册聊天等功能. 然后要做个协议的样子出来. 比如说注册功能 ...

  10. CDH安装时,无法纳管全部的节点的一个bug

      问题描述: 使用CDH 5.2版本安装时,agent节点有12个.按照安装说明,在各个节点启动cm-agent之后,发现只有6个节点能被纳管.其它的节点总是无法加入纳管中. 在确认防火墙已经关闭后 ...