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, …, xnof 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范围内最右边的点,然后在超出r范围的最左边点当做起始点继续寻找。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
int main()
{
int n,r, arr[maxn];
while(scanf("%d%d",&r,&n)!=EOF && n+r!=-2)
{
for(int i=0;i<n;++i)
scanf("%d",&arr[i]);
sort(arr,arr+n);
int i=0,ans=0;
while(i<n)
{
int s=arr[i++];
while(i<n && arr[i]<=s+r)
++i;
int p=arr[i-1];
while(i<n && arr[i]<=p+r)
++i;
++ans;
}
printf("%d\n",ans);
}
return 0;
}

POJ3069 Saruman's Army【贪心】的更多相关文章

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

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

  2. poj3069 Saruman's Army

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

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

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

  4. poj 3069 Saruman's Army 贪心模拟

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

  5. B - B Saruman's Army(贪心)

    在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...

  6. poj3069 Saruman's Army(贪心)

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

  7. poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...

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

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

随机推荐

  1. HDU 5225

    求逆序的个数.首先处理出对n个数它所有排列的逆序的个数.然后,按位枚举,当枚举的数少于当前位,逆序数的个数就是前面确定的数对于后面数的逆序总数+后面k个数的排列的逆序总数. 1Y. #include& ...

  2. [React Storybook] Get started with Storybook for React

    Storybook is a UI component development environment for React, Vue, and Angular. With that, you can ...

  3. matlab中怎样加入凝视

    1)方法一 选中你要加凝视的内容,然后选择工具菜单"text|comment"就能够了,假设要把凝视变为语句,相同选中要转变的语句,然后用鼠标选择"text|uncomm ...

  4. python统计ES存储空间占用的代码

    import os from os.path import join, getsize def get_dir_size(dir, suffix_filter=None): size = 0L if ...

  5. 91. ExtJS获取父子、兄弟容器元素方法

    转自:https://blog.csdn.net/u014745818/article/details/44957341 1 1.当前对象的父对象(上级对象) this.ownerCt: 2.当前对象 ...

  6. 关于类和对象的进一步讨论之析构函数 C++

    析构函数也是一个特殊的成员函数.它的作用与构造函数相反.它的名字是在类名的前面加一个“~”符号.在C++中“~”是位取反运算符.当对象的生命结束时,会自动执行解析函数.以下几种情况会执行析构函数: 1 ...

  7. Python 39 数据库的数据类型

    一:整型 为什么需要 数据分类? 1.为了描述事物更加准确 2.描述起来更方便 3.节省内存空间 例:1 a 你     utf8 下 5个字节 1 a b c   unicode 6个字节 mysq ...

  8. 前端布局神器 display:flex

    前端布局神器display:flex 一直使用flex布局,屡试不爽,但是总是记不住一些属性,这里写来记录一下.   2009年,W3C提出了一种新的方案--Flex布局,可以简便.完整.响应式地实现 ...

  9. iOS开发之判断手机号和邮箱 正则表达式

    #pragma mark --判断手机号合法性 + (BOOL)checkPhone:(NSString *)phoneNumber { NSString *regex = @"^((13[ ...

  10. NPM 国内镜像使用方法

    npm官方站点: http://www.npmjs.org/ 本文使用国内镜像地址: http://www.cnpmjs.org/ 搜索镜像:https://npm.taobao.org/ 具体方法: ...