Saruman's Army(POJ3069)
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'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's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
- 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: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's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- 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's Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
随机推荐
- Hbase思维导图之调优
- call,apply,bind——js权威指南函数属性和方法章节读书笔记
每个函数(即这两个方法是函数的方法)都包含两个非继承而来的方法: apply()和 call().参数明确,使用call.参数不明确,使用apply,可以遍历数组参数 1,call里面的参数是散开的, ...
- linux switch 跳转到 ”跳转至 case 标号“ 的错误
参考链接: http://blog.csdn.net/qustdjx/article/details/8636489
- 阿里云IoT物联网平台入门教程
参考链接:https://www.geek-workshop.com/thread-37883-1-1.html
- 20165237 2017-2018-2 《Java程序设计》第4周学习总结
20165237 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 1.子类只能有一个父类,父类可以有多个子类. 2.子类继承父类的成员变量和方法. 3.开闭原 ...
- 20165234 预备作业3 Linux安装及学习
一.基于VirtualBox虚拟机安装Ubuntu 这是我首次安装虚拟机,也是首次接触Linux系统.对于下学期要运用的这个操作系统,以及如何在自己的电脑上安装虚拟机,我还是有些期待和好奇的. 我一直 ...
- 围在栅栏中的爱WriteUp(附QWE密码加解密脚本)
题目的链接:http://www.shiyanbar.com/ctf/1917 1.首先题目给出的是摩尔斯电码: 在下面的网站上解密:https://www.cryptool.org/en/cto-c ...
- 20165325 2017-2018-2 《Java程序设计》结对编程_第二周:四则运算
20165325 2017-2018-2 <Java程序设计>结对编程_第二周:四则运算 一.码云链接 FAO项目的码云链接; 1.Git提交日志已经实现一个功能/一个bug修复的注释说明 ...
- 数字图像处理的Matlab实现(2)—MATLAB基础
第2章 MATLAB编程基础 2.1 M-文件 MATLAB中的M-文件可以是简单执行一系列MATLAB语句的源文件,也可以是接收自变量并产生一个或多个输出的函数. M-文件由文本编辑器创建,并以fi ...
- asyncio之asyncio.run
asyncio.run(coro, *, debug=False) python3.7加入,只能作为asyncio的入口函数.