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. 寒假安卓app开发学习记录(6)

    今天把之前学过的内容复习了一遍,然后用了大概一个小时的时间看了看教学视频. 学到的主要内容是如何调试Android程序,以及Android的应用以及打包过程.   调试: 依次点击Run“-”Atta ...

  2. ansible笔记(8):初识ansible playbook

    回顾总结:我们来想象一个工作场景,看看怎样把之前的知识点应用到这个工作场景中.假设,我们想要在192.168.10.2主机上安装nginx并启动,我们可以在ansible控制主机中执行如下3条命令. ...

  3. winform学习(3)窗体事件

    窗体的常用事件 事件可以理解为用户的操作,如点击鼠标键盘 应用程序需要在事件发生时进行响应,因此事件分为: 注册事件:必须为对象注册事件才会被执行(如为某控件添加一个单击的事件) 触发事件:注册后的事 ...

  4. Flask 教程 第二十章:加点JavaScript魔法

    本文翻译自The Flask Mega-Tutorial Part XX: Some JavaScript Magic 这是Flask Mega-Tutorial系列的第二十部分,我将添加一个功能,当 ...

  5. 计算机网络 - TCP_NODELAY 和 TCP_CORK, TCP_NOPUSH

    参考 https://www.cnblogs.com/biyeymyhjob/p/4670502.html https://stackoverflow.com/questions/3761276/wh ...

  6. Java-POJ1000-A+B Problem

  7. 快捷键(二):VSCode

    1,打开命令板:F1或Ctrl+Chift+P 2,新建文件:Ctrl+N 3,文件间切换:Ctrl+Tab 4,新开界面:Ctrl+Shift+N 5,关闭当前窗口:Ctrl+W 6,关闭界面:Ct ...

  8. RAID 5+备份硬盘实验:mdadm

    *独立冗余磁盘阵列---RAID5* RAID5+备份盘: 把硬盘设备的数据奇偶校验信息保存到其他硬盘设备中.  RAID 5磁盘阵列组中数据的奇偶校验信息并不是单独保存到某一块硬盘设备中, 而是存储 ...

  9. Linux 内核内存池

    内核中经常进行内存的分配和释放.为了便于数据的频繁分配和回收,通常建立一个空闲链表——内存池.当不使用的已分配的内存时,将其放入内存池中,而不是直接释放掉. Linux内核提供了slab层来管理内存的 ...

  10. Educational Codeforces Round 70 (Rated for Div. 2) 题解

    比赛链接:https://codeforc.es/contest/1202 A. You Are Given Two Binary Strings... 题意:给出两个二进制数\(f(x)\)和\(f ...