一、题面

POJ3069

二、题意分析

我的理解是,可以在每个点设置一个监测点,能够监测到范围R内的所有其他点,那么问给出N个点的一维位置,需要在其中挑多少个监测点把所有点都监测到。

贪心解决:

  1.先排序。

  2.考虑第一个点,因为每个点是必须要监测的,那么第一个点需要被监测到,它可以是监测点,也可以是被监测点。贪心的想,为了能监测更多的点,让第一个点作为被监测的点,且是监测范围内最边界上的点。

  3.现在需要考虑,当第一个点往右探测R范围时,最后一个点将是需要设置的监测点。

  4.以这个监测点再往右扫R范围。

  5.完成了一个监测点及其范围内点的覆盖。后面的点类似处理。

三、AC代码

 #include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; const int MAXN = 1e3;
int Data[MAXN+], N, R; int solve()
{
int i = , ans = , cur;
while(i < N)
{
cur = Data[i++];
while(i < N && Data[i] <= cur+R)
i++;
cur = Data[i-];
while(i < N && Data[i] <= cur + R)
i++;
ans++;
}
return ans;
} int main()
{
freopen("input.txt", "r", stdin);
while(scanf("%d %d", &R, &N) != EOF && R != -)
{
for(int i = ; i < N; i++)
scanf("%d", &Data[i]);
sort(Data, Data+N);
printf("%d\n", solve() );
}
return ;
}

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

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

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

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

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

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

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

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

  6. Saruman's Army(贪心)

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

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

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

  8. POJ3069 Saruman's Army【贪心】

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

  9. poj 3069 Saruman's Army

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

随机推荐

  1. 代码查看import的类是出自哪个jar包的方法(转)

    import java.security.ProtectionDomain; import java.security.CodeSource; public static void main(Stri ...

  2. bootstrap中的data-dismiss属性

    <button type="button" class="btn default" data-dismiss="modal">关 ...

  3. HTML5样式、链接和表格

    -------------------siwuxie095 HTML5 样式 1.标签 <style> 标签:样式定义 <link> 标签:资源引用 2.属性 rel:用于指定 ...

  4. mask rcnn训练自己的数据集参考文章(推荐)

    最近用Mask_RCNN训练模型,下面几篇文章提供了不少帮助,汇总出来,方便以后查找,并向几位博主老师表示感谢 https://blog.csdn.net/qq_29462849/article/de ...

  5. 496. Next Greater Element I 另一个数组中对应的更大元素

    [抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...

  6. Makefile 编写规则 - 1

    Makefilen内容 1. 显示规则:显示规则说明了,如何生成一个或多个目标.这是由Makefile指出要生成的文件和文件依赖的文件.2. 隐晦规则:基于Makefile的自动推导功能3. 变量的定 ...

  7. C#中特殊的string类型

                                                                                  string C#有string关键字,在翻 ...

  8. 求数列中第K大的数

    原创 利用到快速排序的思想,快速排序思想:https://www.cnblogs.com/chiweiming/p/9188984.html array代表存放数列的数组,K代表第K大的数,mid代表 ...

  9. javascript高级程序设计读书笔记----函数表达式

    定义函数两种方式: 1.函数声明 function sayHi(){ alert("Hi"); } sayHi();//调用函数 2.函数表达式 var sayHi = funct ...

  10. delphi 指针 认识

    delphi 指针分为类型指针和无类型指针: 类型指针分为PChar.PInteger.PString等. 无类型指针Pointer. PPChar/PP...为指针的指针 @和Addr一样,为获取变 ...