一、题面

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. FacadePattern(23种设计模式之一)

    设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...

  2. 简单Factory模式

    #pragma once #include "student.h" #include "Teacher.h" typedef enum _EPersonType ...

  3. .net中对HTTP请求的两种请求:Get和Post的操作

    .net中对HTTP请求的简单操作总结 第一部分,HTTP协议的简单了解 一.           什么是HTTP协议 超文本传输协议 (HTTP-Hypertext transfer protoco ...

  4. jqentitymanage

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflect ...

  5. 树形DP-HDU1561 The more, The Better

    很好的树形DP入门题,看着和选课那道题如出一辙. Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻 ...

  6. 转Delphi中XLSReadWrite控件的使用(3) 读和写Excel

    unit OpExcell; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...

  7. .net core 结合nlog使用Elasticsearch , Logstash, Kibana

    什么是ELK ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件.新增了一个FileBeat,它是一个轻量级的日志收集处理工具 ...

  8. C# LINQ(6)

    目前说了 select group...by where from join on equal 这几个关键字,如果经过练习,熟练使用这几个关键字,大部分的LINQ查询基本都是可以完成的. 今天说一下l ...

  9. C# 接口(2)

    接口的实现方式. 接口的实现分两种: 1 显示实现接口 2 实现接口. 我们前面所得的接口的实现均为实现接口.也就是第二种方式.那么我们来来看第第一种实现方式: interface IHuman { ...

  10. 异常上报功能Bugly简介

    目的:为了能够快速定位到线上版本bug位置,经过比较之后,决定使用腾讯家的Bugly. 1.注册产品 官方文档使用指南 1.1 登录 - 使用 QQ 登录Bugly官网 没有账号就注册,要实名就实名, ...