地址 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. winform实现自定义折叠面板控件

    代码文件:https://github.com/Caijt/CollapsePanel 最近在学习做winform,想实现一个系统导航菜单,系统菜单以模块进行分组,菜单是树型结构. 效果类似旧版QQ的 ...

  2. k8s的yaml说明

    理解k8s里的几个概念 Kubernetes 通过各种 Controller 来管理 Pod 的生命周期.为了满足不同业务场景,Kubernetes 开发了 Deployment.ReplicaSet ...

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

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

  4. Dynamics 365利用Web API对视图进行查询

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  5. Android8.1 SystemUI源码分析之 电池时钟刷新

    SystemUI源码分析相关文章 Android8.1 SystemUI源码分析之 Notification流程 分析之前再贴一下 StatusBar 相关类图 电池图标刷新 从上篇的分析得到电池图标 ...

  6. Linux笔记16 使用Vsftpd服务传输文件;使用Samba或NFS实现文件共享。

    FTP协议有下面两种工作模式. 1.主动模式:FTP服务器主动向客户端发起连接请求. 2.被动模式:FTP服务器等待客户端发起连接请求(FTP的默认工作模式).Vsftpd服务程序vsftpd作为更加 ...

  7. CodeForces - 460C(二分+差分)

    题意 https://vjudge.net/problem/CodeForces-460C 一个长度为 n 的序列 a ,你有 m 次操作的机会,每次操作是将其中连续的 w 个元素增加 1 .最大化最 ...

  8. VS2017初学者如何打开右侧的解决方案资源管理器

  9. git 分布式控制版本管理器(上)

    git的作用: 1.更方便的存储版本 2.恢复之前的版本 3.更方便的对比 4.协同合作 下载地址git官网: https://git-scm.com/ 可自选自己电脑的操作系统 安装: 一路next ...

  10. web安全技术--XSS和CSRF

    Xss一般是脚本代码,主要是JS的,但是也有AS和VBS的. 主要分为反射型,存储型,DOM型三个大类. 一般来讲在手工测试的时候先要考虑的地方就是哪里有输入那里有输出. 然后是进行敏感字符测试,通常 ...