POJ 3069:Saruman's Army
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13242 | Accepted: 6636 |
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的距离内存在被标记的点,求至少要标记多少个点。
思路参考白书46页
#include<stdio.h>
#include<string.h>
#include<algorithm>
const int maxn=1e5+10;
int a[maxn];
int main()
{
int r,n;
while(~scanf("%d%d",&r,&n))
{
if(r==-1&&n==-1) break;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++) scanf("%d",&a[i]);
std::sort(a,a+n);
int sum=0;
int i=0;
while(i<n)
{
int s=a[i++];
while(i<n&&s+r>=a[i]) i++;
int p=a[i-1];
while(i<n&&a[i]<=p+r) i++;
sum++;
}
printf("%d\n",sum);
}
return 0;
}
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(萨鲁曼军)
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 : 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: 6688 Accepted: 3424 De ...
- POJ 3069 Saruman's Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
- Q - Saruman's Army POJ - 3069
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: 18794 Accepted: 9222 D ...
- poj 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
随机推荐
- 《剑指offer》第三十三题(二叉搜索树的后序遍历序列)
// 面试题33:二叉搜索树的后序遍历序列 // 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果. // 如果是则返回true,否则返回false.假设输入的数组的任意两个数字都 ...
- android适配各种分辨率的问题
Android设备屏幕的尺寸是各式各样的,如小米是4英寸的,Xoom平板是10英寸:分辨率也千奇百怪,800×480,960×540等:Android版本的碎片化问题更是萦绕于心,不过在设计应用时可以 ...
- Codeforces D - Ithea Plays With Chtholly
D - Ithea Plays With Chtholly 思路:考虑每个位置最多被替换c/2次 那么折半考虑,如果小于c/2,从左往右替换,大于c/2总右往左替换,只有小于这个数(从左往右)或者大于 ...
- Python requests快速上手
Python requests快速上手 这里参考官方文档,在ide中写了一遍,加深一下印象,定义的函数只是为了方便区分不同的请求方式 #-*-coding:utf-8-*- # Time:2017/1 ...
- Chrome与之驱动对应的版本
看到网上基本没有最新的chromedriver与chrome的对应关系表,便兴起整理了一份如下,希望对大家有用: chromedriver版本 支持的Chrome版本 v2.46 v71-73 v2. ...
- 流氓 2345.com的新动态及解决方法
安装了[电脑公司]的Win7_SP1之后, IE的主页被绑架. 症状是先转到 IE959.com,然后自动跳转到 www.2345.com 网上当然有很多例子了,可是都没有效果. 1. 更改IE设置没 ...
- 4-4 集成测试练习,和测试基础知识(guide)。
Guide指南 18章应用测试指南(简单学习了一下.) (中文版--主要是为了先理解,之前看过英文版受语言影响,怕理解有偏差.) Minitest::Test是ActiveSupport::TestC ...
- php--------使用 isset()判断字符串长度速度比strlen()更快
isset()速度为什么比strlen()更快呢? strlen()函数函数执行起来相当快,因为它不做任何计算,只返回在zval 结构(C的内置数据结构,用于存储PHP变量)中存储的已知字符串长度.但 ...
- Confluence 6 导入 Active Directory 服务器证书 - Windows
为了让你的应用服务器能够信任你的目录服务器.你目录服务器上导出的证书需要导入到你应用服务器的 Java 运行环境中.JDK 存储了信任的证书,这个存储信任证书的文件称为一个 keystore.默认的 ...
- ccf交通规划
一.试题 问题描述 G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统. 建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改 ...