http://poj.org/problem?id=3069

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 这道题讲的是白巫师要带领他的军队从一个地方到另外一个地方,走直线,但是为了监控他的军队嘞,他就用一种石头放在其中一个人身上,然后这个石头有r米的监控范围,问至少需要
多少石头(大概就是这个意思,我英语也不好啊[大哭])
然后就开始想代码,我想的是,中间一个人拿石头,那就可以监控两边的人啊,所以从第一个开始,一直向后……

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int n,r,a[1010];
    while(scanf("%d%d",&r,&n)==2&&n!=-1&&r!=-1)
    {
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);

sort(a,a+n);
        int sum=0,i=0;
        while(i<n)
        {
            int x=a[i];
            i++;
            while(i<n && a[i]-x<=r) i++;//这个是判断距离0处有r的点,因为有个i++,所以i=过了r距离之后的第一个i
            int p=a[i-1];//所以这个就是拿着石头的人
            while(i<n && a[i]-p<=r) i++;//这个就是右边有r的距离
            sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}

poj3069 Saruman's Army的更多相关文章

  1. POJ3069 Saruman's Army【贪心】

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

  2. poj3069 Saruman's Army(贪心)

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

  3. Saruman's Army(POJ3069)

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

  4. poj 3069 Saruman's Army

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

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

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

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

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

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

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

随机推荐

  1. NGUI 层级关系控制

    NGUI元素的遮挡情况是不依赖空间关系,所以在NGUI上添加特效有时候特别蛋疼,特别是美术同学还要依赖空间关系来控制特效效果,那先看看看NGUI的层级是怎么处理的,不过下面的描述都是针对单个相机下的P ...

  2. Linux系统glibc库版本信息查看

    原文链接:http://www.jbxue.com/LINUXjishu/29946.html 1. CentOS /lib/i386-linux-gnu/libc.so. 或 rpm -qi gli ...

  3. GCC编译C代码

    C程序的编译过程       常用文件的后缀名: gcc编译c代码 1.gcc 常用编译选项: 2.gcc编译方法 testc.c: #include <stdio.h> int main ...

  4. 【Git学习笔记】初始化Git仓库和版本回退

    学习地址 http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 什么是版本库呢?版本库又 ...

  5. Shell 重定向

    一直没搞懂 &> 和 <& 是表示什么意思. 今天自己总结一下,希望自己能理解它的真正含义. 重定向标准输入输出,切记 “1” 和 “>”之间没有空格 $ > ...

  6. 关于smarty的一些个人笔记

    注释为{注释} 注意下面代码中<%extends file="路径"%>和<%widget name="路径"%>这两个路径中的区别 c ...

  7. 在四合院里写code是什么体验(非拉仇恨)

    我工作的地点在一个幽静的四合院,后海边上.每次出了地铁还要走长长的十分钟的胡同,经过恬静的老人,嬉戏的小孩,还有就是长年不缺的游客.很多次,在夕阳将下未下时五六点的时候,小胡同里老人拿着小马扎,一群小 ...

  8. 使用udev实现显示器的热插拔和usb的自动挂载

    udev:用来监听硬件设备是否发生改变,并可以给硬件设备命名 ,也可以在硬件发生改变之后执行脚本 使用udev检测显示器是否发生变化,然后执行脚本,解决linux显示器热插拔问题 先补充一点: [ro ...

  9. 关于 update别名 与update select

    正确写法:  update  别名 set 别名点字段 =xxxx UPDATE a SET a.StandardID = (SELECT b.StandardID FROM SurgeryMappi ...

  10. Python之路----------time模块

    时间模块是常用的模块 一.time模块 import time print(time.clock())#返回处理器时间,3.3开始已经屏蔽. print(time.altzone)#返回与UTC时间差 ...