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. JS实现复制网页内容自动加入版权内容代码和原文链接

    JS实现复制网页内容自动加入版权内容代码和原文链接 实现代码:在body内放入如下代码即可: <script type="text/javascript"> var S ...

  2. 【原创】Nexus搭建Maven私服

    前言: 公司一般都有个自己的私服来管理各种jar包,原因大概有这么3个,分别是: 1.有的公司不能访问外网,只能通过私服来管理jar包和插件: 2.公司网速比较慢,通过公司的私服来获取jar包比较快: ...

  3. ecshop搜索出现相关商品的效果滑动下拉效果

    ecshop搜索栏效果如下 所需要的样式我们可以复制到style.css里: /*搜索滑动效果*/ .Menu { position:absolute; top:30px; left:7px; wid ...

  4. Unix/Linux编程实践教程(二:socket、多线程、进程间通信)

    同一接口不同的数据源: 协同进程: fdopen以文件描述符为参数: fopen和popen: 为了实现popen,必须在子进程中调用sh,因为只有shell本身即/bin/sh可以运行任意shell ...

  5. hdwiki 框架简介

    虽然HDwiki是一个开源的wiki系统,并且代码简洁易懂,但如果想在系统上做做进一步开发还需要对框架有一个整体的认识.熟悉了HDwiki的框架以后完全可以独立出来做其他功能的开发,当做一个开源的PH ...

  6. IE6下 input 背景图滚动问题及标签规范

    ie6 背景图滚动问题: <title>ie6下input背景图滚动问题</title> <style> .box{ height:20px; width:300p ...

  7. Wall(凸包POJ 1113)

    Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32360 Accepted: 10969 Description On ...

  8. 实现multbandblend

    一.首先实现 laplacian金字塔的分割和重构 #include "stdafx.h" #include <iostream> #include <vecto ...

  9. NYOJ(21),BFS,三个水杯

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=21 BFS判环,vis标记状态即可. #include <stdio.h> # ...

  10. Selenium Grid 简易安装

    转载自:http://blog.csdn.net/xifeijian/article/details/17057659