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. Linux驱动开发 -- 打开dev_dbg() 【转】

    转自:http://blog.chinaunix.net/uid-20672559-id-3383042.html linux设备驱动调试,我们在内核中看到内核使用dev_dbg来控制输出信息,这个函 ...

  2. PHP笔记随笔

    1.CSS控制页面文字不能复制: body{-webkit-user-select:none;}   2.[php过滤汉字和非汉字] $sc="aaad....##--__i汉字过滤&quo ...

  3. RAC例子

    我个人非常推崇ReactiveCocoa,它就像中国的太极,太极生两仪,两仪生四象,四象生八卦,八卦生万物.ReactiveCocoa是一个高度抽象的编程框架,它真的很抽象,初看你不知道它是要干嘛的, ...

  4. symfony中twig的流程控制if,for用法

    流程控制 if 语句 if语句在twig中的作用如同if语句在PHP中的一样.1.你可以通过下面这个简单的例子来判断表达式的结果是否正确. {% if online == false %} <p ...

  5. oracle-odu小试牛刀--恢复drop表的数据

         现在进入oracle12c时代:普遍用的oracle版本为10g以上.在oracle10g之后提供了一个回收的机制.所以恢复drop表的数据以及表很容易.当然需要打开回收机制以及是归档模式. ...

  6. 6.shap以及selector的使用

    功能:相当于自定义一个模板 首先,要新建一个drawble文件夹 然后,再新建一个XML文件 在<shap></shap>中写内容 <corners/>圆角 < ...

  7. c# UDP通信

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  9. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  10. 周赛-Expression 分类: 比赛 2015-08-02 09:35 3人阅读 评论(0) 收藏

    A. Expression time limit per test1 second memory limit per test256 megabytes inputstandard input out ...