poj 3069 Saruman's Army 贪心模拟
Saruman's Army
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18794 | Accepted: 9222 |
Description
Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effective range of R units, and must be carried by some troop in the army (i.e., palantirs are not allowed to “free float” in mid-air). Help Saruman take control of Middle Earth by determining the minimum number of palantirs needed for Saruman to ensure that each of his minions is within R units of some palantir.
Input
The input test file will contain multiple cases. Each test case begins with a single line containing an integer R, the maximum effective range of all palantirs (where 0 ≤ R ≤ 1000), and an integer n, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.
Output
For each test case, print a single integer indicating the minimum number of palantirs needed.
Sample Input
0 3
10 20 20
10 7
70 30 1 7 15 20 50
-1 -1
Sample Output
2
4
Hint
In the first test case, Saruman may place a palantir at positions 10 and 20. Here, note that a single palantir with range 0 can cover both of the troops at position 20.
In the second test case, Saruman can place palantirs at position 7 (covering troops at 1, 7, and 15), position 20 (covering positions 20 and 30), position 50, and position 70. Here, note that palantirs must be distributed among troops and are not allowed to “free float.” Thus, Saruman cannot place a palantir at position 60 to cover the troops at positions 50 and 70.
题意:输入n个点和雷达扫描半径R;问覆盖所有点最少需要多少个雷达
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
int a[];
int n,cnt,r;
int main()
{
while(cin>>r>>n&&r!=-&&n!=-)
{
for(int i=;i<n;i++)
cin>>a[i];
sort(a,a+n);
int i=,cnt=;
while(i<n)//一个标记能覆盖的范围[s+r,p+r],超出这个范围就加一个标记
{
int s=a[i++];//能被覆盖的最左端的点的位置
while(i<n&&a[i]<=s+r)
i++;
int p=a[i-];//标记的位置
while(i<n&&a[i]<=p+r)
i++;
cnt++;
}
cout<<cnt<<endl; }
}
poj 3069 Saruman's Army 贪心模拟的更多相关文章
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- POJ 3069 Saruman's Army(贪心)
Saruman's Army Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 3069 Saruman's Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
- poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- poj 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
- poj 3069 Saruman's Army (贪心)
简单贪心. 从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点.如图. 自己的逻辑有些混乱,最后还是参考书上代码.(<挑战程序设计> P46) /*********** ...
- POJ 3069——Saruman's Army(贪心)
链接:http://poj.org/problem?id=3069 题解 #include<iostream> #include<algorithm> using namesp ...
随机推荐
- Redis 简易消息队列
为了保持程序的高效,建议使用ProtoBuf. Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48, ...
- Style 继承
在应用某个主题后,想在此基础上自定义新的样式,可以使用如下方式继承样式. <Style x:Key="ListViewItemStyle" TargetType=" ...
- S32K144之时钟配置
一般来说,时钟精度.稳定性取决于所采用的时钟源,就MCU S32K来说如内部振荡器SIRC,FIRC,128KLPO,外部晶振等,跟所使用的外设(FTM, LPIT,LPT,RTC等)和哪一路输出时钟 ...
- mysql字符串相关函数(并与sql server对比)
https://blog.csdn.net/zhengxiuchen86/article/details/81220779 1.判断子串substr在字符串str中出现的位置 例子:查询']'在‘OP ...
- Oracle Parallel使用方法
一. 并行查询 并行查询允许将一个sql select语句划分为多个较小的查询,每个部分的查询并发地运行,然后将各个部分的结果组合起来,提供最终的结果,多用于全表扫描,索引全扫描等,大表的扫描和连接. ...
- 小程序使用scroll-view横向滑动时,flex布局失效问题
最近在完善以前项目,类目增多,需要进行横向滑动 实现方法1 可以在外盒子scroll-view使用white-space: nowrap来禁止子盒子换行,子盒子使用display: inline-bl ...
- webdriver-js操作滚动条
webdriver-js操作滚动条 1. webdriver高级应用-js操作滚动条 1.滑动页面的滚动条到页面最下面 2.滑动页面的滚动条到页面的某个元素 3.滑动页面的滚动条向下移动某个 ...
- Java基础知识笔记第四章:类和对象
编程语言的几个发展阶段 面向机器语言 面向过程语言 面向对象语言:封装.继承.多态 类 类声明 class Person{ ....... } class 植物{ ....... } 类体 类使用 ...
- python学习第一课
第一课: 1.不要使用来路不明的软件 2.下载杀毒软件 3.不懂技术的人在技术人面前会显得愈发无知 4.python无所不能 需要掌握的知识: 1.python基本语法 2.文件处理 3.函数 4.模 ...
- SwiftStack 因战略转变而裁员
导读 销售团队在前段圣诞节来临前面临裁减的糟糕处境.企业云存储公司SwiftStack进行了裁员,人数不详,公司规模因此缩小. IT外媒The Register获悉,这家公司裁掉了大概一半的人员,但总 ...