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. JMeter 将上一个请求的结果作为下一个请求的参数——使用正则提取器(转载)

    在接口测试和压力测试过程中,经常会将几个流程串联起来才能测试.如:我要进行获取用户信息接口测试,我就要先登录成功后,才能获取用户信息.所以,我就要首先要登录,获得我的登录凭证(tokenId或tick ...

  2. MySQL运行状态show status详解

    状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...

  3. 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...

  4. jQuery 一句代码返回顶部

    兼容各大主流浏览器,jQuery返回顶部,一句代码搞定 <a class="top" href="javascript:;" style="po ...

  5. responsive tables

    以上内容原本是整理为ppt格式的,贴过来格式有点乱,请见谅. 其他responsive tables参考: http://gergeo.se/RWD-Table-Patterns/ 3种类型的代码参考 ...

  6. Arduino.h

    #ifndef Arduino_h #define Arduino_h #include <stdlib.h> #include <stdbool.h> #include &l ...

  7. test for cvx library in matlab - windows

    Download the zip file of cvx http://cvxr.com/cvx/download/ by downloading cvx-w64.zip Require a lice ...

  8. SQLSERVER2012 Audit (审核)功能

    数据库表结构和数据有时会被无意或者恶意,或者需要追踪最近的数据结构变更记录,以往必须通过日志查询,SQL Server2008开始提供了 审核(Audit )功能,SQL2012有所升级,利用它可以实 ...

  9. 使用clearInterval清除计时循环时,最后一次循环还是会执行解决办法

    原代码: var interv=setInterval(function(){ alert("setInterval执行"); },2000) clearInterval(inte ...

  10. 如何整合最新的Flex sdk和Air sdk。

    使用Flex来开发air应用,如果想使用最新sdk的话,配置起来还真是一个麻烦事儿. Flex捐给apache维护了,air还是adobe自己维护,那么就得分别到这两边下载最新的sdk然后自己整合起来 ...