地址 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. Shell命令的执行优先级

    Shell内置命令.外部命令.别名.函数.保留关键字的优先级 在Shell中,有5种可调用的东西:别名(alias).函数(function).shell保留关键字.shell内置命令.外部命令. 如 ...

  2. ASP.NET MVC5基础 - Global.asax详解

    简介 作用 Global.asax的处理事件 实例说明 总结 简介 Global.asax是一个文本文件,它提供全局可用代码.这些代码包括应用程序的事件处理程序以及会话事件.方法和静态变量.有时该文件 ...

  3. Java时区问题

    Java时区相关 时间格式 UTC是以原子时计时,更加精准,适应现代社会的精确计时.不过一般使用不需要精确到秒时,视为等同.GMT是前世界标准时,UTC是现世界标准时.每年格林尼治天文台会发调时信息, ...

  4. Audit Object Changes 审核对象更改

    Important 重要 The Audit Trail module is not supported by the Entity Framework ORM in the current vers ...

  5. Java中"或"运算与"与"运算快慢的三三两两

    先上结论 模运算比与运算慢20%到30% 这是通过实验的方式得到的结论.因为没有大大可以进行明确指导,所以我以最终运行的结果为准.欢迎指正. 测试代码 @Test public void test10 ...

  6. FlowPortal 6.00c 使用xFormDesigner复制粘贴中文总是乱码

    环境: Windows Server 2016中文版 FlowPortal 6.00C 问题: 使用xFormDesigner 在源码选项卡复制粘贴中文总是乱码. 解决办法: 控制面板---区域--- ...

  7. Python3 中的Number

    Python3 支持 int.float.bool.complex(复数). 在Python 3里,只有一种整数类型 int,表示为长整型,没有 python2 中的 Long. 像大多数语言一样,数 ...

  8. 强大的django-debug-toolbar,django项目性能分析工具

    强大的django-debug-toolbar,django项目性能分析工具 给大家介绍一个用于django中debug模式下查看网站性能等其他信息的插件django-debug-toolbar 首先 ...

  9. C++ explicit关键字,修饰构造函数,ctor

    #include <iostream> // operator Type() 类型操作符重载 // operator int() // operator double() // ... / ...

  10. IT兄弟连 HTML5教程 CSS3揭秘 CSS规则的组成

    CSS和HTML一样都是由W3C制定的标准,本章中介绍的特性和功能还是来源于CSS1和CSS2(CSS2是根据CSS1扩展的).W3C也有新的版本更新,称为CSS3.虽然浏览器已经准备开始实现CSS3 ...