地址 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. django-channels的部署(supervisor+daphne+nginx)

    项目中需要一个聊天室的功能,所以需要websocket通信,选择了使用channels模块,主要记录下channels部署的配置和一些坑. 原项目是通过nginx+uwsgi部署的,这里我没做任何改动 ...

  2. vue - Error: Can't resolve '@/assets/img/github.svg (vue-cli3.0,无法解析.svg图片,已解决)

    用vue脚手架(vue-cli3.0)生成的目录,无法解析.svg图片的问题 <img src="@/assets/img/github.svg" alt="git ...

  3. RSA加解密&RSA加验签详解

    RSA 加密算法是目前最有影响力的 公钥加密算法,并且被普遍认为是目前 最优秀的公钥方案 之一.RSA 是第一个能同时用于 加密 和 数字签名 的算法,它能够 抵抗 到目前为止已知的 所有密码攻击,已 ...

  4. win10,7 80端口被占用的检测和解决方法

    这是一篇关于检测windows7和windows10系统“80端口”被哪个程序占用和怎么解决的小教程.此处用windows10作为范例 1.得到占用该端口的PID ①打开运行(快捷键win+R)(也可 ...

  5. 12c新特性 在线操作数据文件

    我们都知道,oracle pre-12c之前,若是想要把一个数据文件改名或者迁移, 必须在归档模式下先把这个数据文件offline之后, 然后进行OS上的copy或者rename 操作, 最后在sql ...

  6. 手把手教你安装Virtualbox,安装并运行虚拟机

    一.安装VirtualBox. 官网:https://www.virtualbox.org/wiki/Downloads 首先,进入官网下载页面,单击Windows hosts 链接(图中红色方框), ...

  7. 31.Java基础_日期/日期格式/日历类

    Date类 Date对象构造方法 Date对象常用方法 import java.util.*; public class test { public static void main(String[] ...

  8. Less(6)

    1.先判断注入类型 (1)首先看到要求,要求传一个ID参数,并且要求是数字型的:?id=1 (2)再输入?id=1' (3)再输入?id=1 and 1=1 (4)再输入?id=1 and 1=2 ( ...

  9. xamarin调试android部署到模拟器错误记录:Deployment failed Mono.AndroidTools.InstallFailedException: Unexpected install output: Error: Could not access the Package Manager. Is the system running?

    问题记录: 1.生成 ok. 2.昨天也是能部署到模拟器的. 但是今天部署的时候就报了这样的一个错误 Deployment failed Mono.AndroidTools.InstallFailed ...

  10. Vue.js学习笔记--菜鸟搭建一个企业级vue的项目

    vue.js新手搭建一个企业项目,从0开始 前置条件: node.npm请先安装配置好 下面开始: npm 下载vue-cli脚手架工具  确认安装成功看到版本号: 初始化项目,选用webpack(p ...