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.
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n, r;
while (cin >> r >> n, n != - && r != -)
{
int a[];
for (int i = ; i < n; i++)
cin >> a[i];
sort(a, a + n);
int i = , ans = ;
while (i < n)
{
int s = a[i++];
while (i < n&&a[i] <= s + r) i++;
int p = a[i - ];
while (i < n&&a[i] <= p + r) i++;
ans++;
}
cout << ans << endl;
}
return ;
}
//这次学习的代码是通过循环控制找到最优位置
Saruman's Army(POJ3069)的更多相关文章
-
poj3069 Saruman&#39;s Army
http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...
-
POJ3069 Saruman's Army【贪心】
Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...
-
poj 3069 Saruman&#39;s Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
-
POJ 3069 Saruman&#39;s Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
-
POJ 3069 Saruman&#39;s Army(贪心)
Saruman's Army Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
-
POJ 3069:Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13242 Accepted: 6636 D ...
-
poj 3069 Saruman&#39;s Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
-
Saruman&#39;s Army(贪心)
Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep tra ...
-
POJ 3069 Saruman&#39;s Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
随机推荐
-
windows下利用virtual 安装 flask
出处: https://segmentfault.com/a/1190000002450878 本文介绍Windows下如何从零开始搭建Python + Flask开发环境. 安装Python 2.7 ...
- https协议
-
Java虚拟机类载入顺序
Java虚拟机在载入类的时候.先初始化父类静态变量.再初始化子类静态变量.然后载入父类,最后载入子类 public class Parent { static{ System.out.println( ...
-
SQL Sever数据库中 T-sql语句的使用(增、删、改、查)
SQL中的增.删.改.查 一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert ...
-
使用vlc打开usb摄像头
打开vlc播放器 可以打开网络串流的方式打开摄像头,但只支持第一个摄像头 这一串地址拼凑方法看下面,下面可以选择摄像头 为什么只支持第一个摄像头可以参考下一篇使用Vlc.DotNet打开摄像头并截图 ...
-
jmeter 启动报错:not able to find java executable or version
1 运行cmd输入:java -version 查看 提示没有命令 2 查看环境变量path 3 执行 %JAVA_HOME% 提示找不到文件件 4 修改 文件夹名称 5 运行cmd java -ve ...
-
python基础类型—元祖
元组 被称为只读列表,即数据可以被查询,但不能被修改,所以,字符串的切片操作同样适用于元组. 例:(1,2,3)("a","b","c") ...
-
Kafka 0.8 如何创建topic
1. 操作命令 bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions ...
-
批量 ping 测试脚本(IP 扫描)
是否会使用 vpn 工作,已经成为魔法师和麻瓜之间最重要的区分.使用 vpn 工作,也产生了其它一些奇奇怪怪的问题,比如,选择 vpn 服务器. 你要测试哪个 vpn 离你最近. 所以,就有了下面的脚 ...
-
saltstack之sls文件配置
state模块 state模块会调用它的sls函数处理一个以.sls结尾的配置文件. sls文件是YAML语言来进行描述的 缩进 不要使用`tab`因为tab是四个空格,而YAML是使用两个空格 冒号 ...