地址 http://poj.org/problem?id=3069

题解

题目可以考虑贪心 尽可能的根据题意选择靠右边的点

注意

开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一轮

我最开始就是轮次的操作理解错误 结果wa了

ac代码如下

 #include <iostream>
#include <vector>
#include <algorithm> using namespace std; /*
poj3069 题目大意:一个直线上有N个点。点i的距离是Xi。
从这些点中选取若干个加上标记。
要求:对于每个点,与其距离为R的范围内必有做标记的点(包括自身)。
求至少标记多少点才能满足要求。 输入
N=6 R =10
X={1 7 15 20 30 50}
输出
3 Sample Input 0 3
10 20 20
10 7
70 30 1 7 15 20 50
-1 -1
Sample Output 2
4
*/
int nodes[]; int solve(int r,int n)
{
int count = ; int leftNotCover = ;
int currMark = ; while (leftNotCover < n) {
//找到最左边 第一个未覆盖的点
if (leftNotCover != ) {
while (currMark < n && nodes[leftNotCover] - nodes[currMark] <= r) leftNotCover++;
if (leftNotCover >= n) break;
} currMark = leftNotCover;
while (currMark < n && nodes[currMark] - nodes[leftNotCover] <= r) currMark++;
count++;
currMark = currMark - ;
leftNotCover = currMark + ;
} return count;
} int main()
{
int r = ;
int n = ;
while () {
cin >> r >> n;
if (r == - || n == -) return -; for (int i = ; i < n; ++i) {
cin >> nodes[i];
}
sort(nodes, nodes + n);
cout <<solve(r, n) << endl;
} 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. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

  5. POJ 3069——Saruman's Army(贪心)

    链接:http://poj.org/problem?id=3069 题解 #include<iostream> #include<algorithm> using namesp ...

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

    简单贪心. 从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点.如图. 自己的逻辑有些混乱,最后还是参考书上代码.(<挑战程序设计> P46) /*********** ...

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

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

  8. poj 3069 Saruman's Army

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

  9. POJ 3069 Saruman's Army (模拟)

    题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...

随机推荐

  1. koa2使用es7 的装饰器decorator

    本文主要讲述我在做项目中使用装饰器(decorator)来动态加载koa-router的路由的一个基础架构. 目前JavaScript 对decorator 是不支持,但是可以用babel 来编译 既 ...

  2. JS---DOM---设置和获取---标签内容和文本内容

    设置和获取---标签内容和文本内容   总结---设置: 使用innerText主要是设置文本的, 设置标签内容, 是没有标签的效果的 innerHTML是可以设置文本内容 innerHTML主要的作 ...

  3. c#实现两个窗体相互传值

    本文转自:https://mp.csdn.net/postedit/100058721 1.功能展示 有时需要将子界面的内容传递到父界面,方法有好几种.经常用的是通过委托实现.具体的效果如下: ​   ...

  4. maven配置多个镜像

    问题场景 1.国内访问maven默认远程中央镜像特别慢 2.用阿里的镜像替代远程中央镜像 3.大部分jar包都可以在阿里镜像中找到,部分jar包在阿里镜像中没有,需要单独配置镜像 解决方案 setti ...

  5. TCP 三次握手与四次挥手

    TCP是什么      TCP(Transmission Control Protocol 传输控制协议)是一种面向连接(连接导向)的.可靠的. 基于IP的传输层协议.       TCP有6种标示: ...

  6. Python Weekly 419

    文章,教程或讲座 如何用 Dropbox Security 构建用于日志系统的威胁检测和事件响应的工具 https://blogs.dropbox.com/tech/2019/10/how-dropb ...

  7. ETCD:多机上的集群

    原文地址:cluster on multiple machines 总览 启动一个集群静态的要求是每一个集群中的成员需要知道其他成员的位置.在许多情况下,集群成员的IP可能无法提前知道.在这种情况下, ...

  8. Spring Boot可执行Jar包运行原理

    目录 1. 打可执行Jar包 2. 可执行Jar包内部结构 3. JarLauncher 4. 简单总结 5. 远程调试 Spring Boot有一个很方便的功能就是可以将应用打成可执行的Jar.那么 ...

  9. 在IntelliJ IDEA中,Lombok注解@Slf4j找不到log解决方案

    在IntelliJ IDEA中,注解@Slf4j找不到log时,可以安装Lombok插件 File → settings → Plugins, 然后点击"Browse repositorie ...

  10. IDEA激活码

    MNQ043JMTU-eyJsaWNlbnNlSWQiOiJNTlEwNDNKTVRVIiwibGljZW5zZWVOYW1lIjoiR1VPIEJJTiIsImFzc2lnbmVlTmFtZSI6I ...