POJ 3069 Saruman's Army(萨鲁曼军)

Time Limit: 1000MS   Memory Limit: 65536K

【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.

白袍萨鲁曼必须带领着他的军队从艾辛格径直前往圣盔谷。为了有序行军,萨鲁曼在军队中分发被称作真知晶球的远见石。每个真知晶球的最大有效范围为R个单位,并且必须由军队中的一些部队携带(即真知晶球不允许中途变动)。

通过使用最少的真知晶球来确保萨鲁曼的每个仆从都在晶球附近的R个单位内,助他掌控中土世界。

【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.

输入文件有多组测试样例。

每组测试数据第一行,包括一个整数R(0 ≤ R ≤ 1000),表示真知晶球的最大有效范围,还有一个整数n(1 ≤ n ≤ 1000),表示萨鲁曼军的部队数量。

下一行有n个整数,x1, …,xn(0 ≤ xi ≤ 1000)表示每个部队的位置。

R = n = −1表示输入结束。

【Output】

【输出】

For each test case, print a single integer indicating the minimum number of palantirs needed.

对于每个测试样例,输出一个整数表示最少需要使用的真知晶球数量。

【Sample Input - 输入样例】

【Sample Output - 输出样例】

0 3

10 20 20

10 7

70 30 1 7 15 20 50

-1 -1

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.

对于第一个测试样例,萨鲁曼可以在位置10和20放置真知晶球。这样,范围为0的真知晶球可以覆盖同在位置20的部队。

对于第二个测试样例,萨鲁曼可以放置在位置7(覆盖部队1,7和15),位置20(覆盖20和30),位置50,还有位置70。这里要注意,真知晶球必须放在部队中并且不允许变动。因此,萨鲁曼不能在位置60放置真知晶球来覆盖在位置50和70的部队。

【题解】

  贪心法,一开始还在纠结应该往前还是往后……

  然后看了看提示的第二个测试样例……从当前位置往后贪就好了。

【代码 C++】

 #include<cstdio>
#include <cstring>
#include <algorithm>
int main(){
int r, n, i, j, data[], opt;
while (scanf("%d%d", &r, &n)){
if (r + n < ) break;
memset(data, , sizeof(data));
for (i = ; i < n; ++i) scanf("%d", &data[i]);
std::sort(data, data + n);
for (i = opt = ; i < n; ++opt){
for (j = i; data[j] <= data[i] + r; ++j);
for (i = --j; data[i] <= data[j] + r; ++i);
}
printf("%d\n", opt);
}
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: 8477   Accepted: 4317 De ...

  4. poj 3069 Saruman's Army(贪心)

    Saruman's Army Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  5. POJ 3069 Saruman's Army (模拟)

    题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...

  6. poj 3069 Saruman's Army 贪心模拟

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18794   Accepted: 9222 D ...

  7. POJ 3069——Saruman's Army(贪心)

    链接:http://poj.org/problem?id=3069 题解 #include<iostream> #include<algorithm> using namesp ...

  8. poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...

  9. poj 3069 Saruman's Army (贪心)

    简单贪心. 从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点.如图. 自己的逻辑有些混乱,最后还是参考书上代码.(<挑战程序设计> P46) /*********** ...

随机推荐

  1. Jquery中$(document).ready()与传统JavaScript中的window.onload方法的区别(2016/8/3)

    Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间       ...

  2. Hive报错之java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonFactory

    一.问题: 在使用Hive0.11进行select查询的时候报: hive,),site from zhifu; Total MapReduce jobs Launching Job out In o ...

  3. Nagios监控磁盘

    1.查看check_disk脚本 [oracle@rhel5 ~]$ /usr/local/nagios/libexec/check_disk --h check_disk v1.) Copyrigh ...

  4. ORACLE--分区表数据清理

    由于分区表数据增加:没做清除操作:导致表空间告急.需要清理很久之前的数据:释放空间.步骤如下 一,查看哪个表占的空间 SELECT t.segment_name, SUM(t.bytes / 1024 ...

  5. AngularJS 用 Interceptors 来统一处理 HTTP 请求和响应

    Web 开发中,除了数据操作之外,最频繁的就是发起和处理各种 HTTP 请求了,加上 HTTP 请求又是异步的,如果在每个请求中来单独捕获各种常规错误,处理各类自定义错误,那将会有大量的功能类似的代码 ...

  6. Python repr() 或str() 函数(转)

    Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数.函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式(如果没有等价的语法,则会发生 ...

  7. csdn在线编程里面的一个排列组合题

    是csdn在线编程里面的一个问题 回文字符串是指从左到右和从右到左相同的字符串,现给定一个仅由小写字母组成的字符串,你可以把它的字母重新排列,以形成不同的回文字符串. 输入:非空仅由小写字母组成的字符 ...

  8. 滑雪 分类: POJ 2015-07-23 19:48 9人阅读 评论(0) 收藏

    滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 83276 Accepted: 31159 Description Mich ...

  9. csharp通过dll调用opencv函数,图片作为参数

    [blog 项目实战派]csharp通过dll调用opencv函数,图片作为参数          ​一直想做着方面的研究,但是因为这个方面的知识过于小众,也是由于自己找资料的能力比较弱,知道今天才找 ...

  10. zend studio 注释快捷键

    使用zend studio编写程序时,我们经常要做一些注释.zend studio为我们提供了行注释和块注释的快捷键ctrl+slash和ctrl+shift+slash,开始不明白什么是slash, ...