一、题面

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. selenium3加载浏览器

    浏览器禁用更新: 因为selenium对浏览器的支持是有限制的.当浏览器更新到最新版本时,需要下载支持最新版本的插件.有时候selenium还没有更新到支持最新版本的插件,但本地已经更新到最新版本了. ...

  2. C++——STL容器

    序列式容器:vector,list,deque:stack,queue(容器适配器),heap,priority_queue,slist 关联式容器:(底层都是红黑树)set,map,multiset ...

  3. Opencv图像变成灰度图像、取反图像

    #include <iostream>#include <opencv2/opencv.hpp> using namespace cv;using namespace std; ...

  4. sql去除重复记录 且保留id最小的 没用

    第一步:查询重复记录   SELECT * FROM TableName   WHERE RepeatFiled IN (   SELECT RepeatFiled   FROM TableName ...

  5. Tomcat与Web.xml配置

    1.编码配置 <Connector acceptCount=”100″ connectionTimeout=”20000″ disableUploadTimeout=”true” enableL ...

  6. 2014-4-2解决无法访问github和google的问题

    github是个好地方,但是上不去就蛋疼了. 今天github上不去,果断f12下,看下network,发现里面好多请求都是指向 github.global.ssl.fastly.net这个域名的,然 ...

  7. 使用Filter对POST和GET方式的请求参数的进行统一解码

    一.过滤器 二.自定义request类

  8. java學習書

    轉載 成为Java顶尖程序员 ,看这11本书就够了 以下是我推荐给Java开发者们的一些值得一看的好书.但是这些书里面并没有Java基础.Java教程之类的书,不是我不推荐,而是离我自己学习 Java ...

  9. linux centos 宝塔主机控制面板安装和安全狗安装过程记录

    linux 宝塔控制面板 安装过程yum install -y wget && wget -O install.sh http://103.224.251.79:5880/instal ...

  10. <id name="ID"> <generator class="assigned" /> </id>

    <generator>表示一个主键的生成机制.即具体通过何种方式来生成.生成方式包括:increment:生成long, short或者int类型的主键,不能在cluster环境下使用.适 ...