Saruman's Army(贪心)
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, …, xnof each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case withR = 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.
题目意思:Saruman的军队有n个人,为了控制住他的军队,他将一个装着放在所给样例数的一些士兵身上,给出装置的控制范围r,要求出最少需要多少个装置就能完全控制住他的军队。
解题思路:这是一道贪心题,贪心策略是从第一个士兵开始向后面遍历,找到满足距离小于r的最右边的点,这一个点一定会作为一个装着的放置的位置,然后从这个位置开始找到右边的最小的不能被覆盖到的位置作为下一次的起点....循环下去,直到所有的点都被覆盖到。
上代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,r,count,i,k,p;
int a[];
while(scanf("%d%d",&r,&n)!=EOF)
{
if(r==-&&n==-)
{
break;
}
for(i=; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
i=;
count=;
while(i<n)
{
k=a[i]+r;
while(i<n&&a[i]<=k)///找到满足距离小于r的最右边的点
{
i++;
}
p=a[i-]+r;///确定的人
while(i<n&&a[i]<=p)
{
i++;
}
count++;
}
printf("%d\n",count);
}
return ;
}
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(贪心)
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: 18794 Accepted: 9222 D ...
- B - B Saruman's Army(贪心)
在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...
- poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- 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 ...
- poj3069 Saruman's Army
http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...
随机推荐
- 在控制台中操作MYSQL数据库步骤以及一些小问题
一直用Navicat来对MySQL数据库进行操作,今天突然想试试用DOS控制台来操作,特记录自己第一次使用经历,若有错误之处,还望大佬们指点. 首先打开控制台,win+R键,输入cmd,确定 输入my ...
- MySQL慢日志实践
慢日志查询作用 慢日志查询的主要功能就是,记录sql语句中超过设定的时间阈值的查询语句.例如,一条查询sql语句,我们设置的阈值为1s,当这条查询语句的执行时间超过了1s,则将被写入到慢查询配置的日志 ...
- Mysql 关于处理NULL值的相关函数和操作符
操作符 <=> NULL-safe equal. This operator performs an equality comparison like the = operator, bu ...
- 用PHP读取Excel、CSV文件
PHP读取excel.csv文件的库有很多,但用的比较多的有: PHPOffice/PHPExcel.PHPOffice/PhpSpreadsheet,现在PHPExcel已经不再维护了,最新的一次提 ...
- linux操作之软件安装(二)(源码安装)
源码安装 linux上的软件大部分都是c语言开发的 , 那么安装需要gcc编译程序才可以进行源码安装. yum install -y gcc #先安装gcc 安装源码需要三个步骤 1) ./confi ...
- AS 3.1 多library合并打包成aar的正确方式(fat-aar)
前言 主要参考fat-aar来合并打包. 但是这个fat-aar很久没维护了,如果直接使用它会有很多问题.由于对gradle脚本也不是太熟,就只能顺着它的意思,将gradle降级成2.2.3的版本. ...
- 北京Uber优步司机奖励政策(9月21日~9月27日)
用户组:优步北京人民优步A组(适用于9月21日-9月27日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不 ...
- 抽样分布(2) t分布
定义 t分布 设X ~ N(0,1),Y ~ χ2(n),且X,Y相互独立,则称随机变量 服从自由度为n的t分布(学生氏分布) 记为 t~t(n),其概率密度为 由于tn(x)是偶函数,其图形关于y轴 ...
- imageNamed和imageWithContextOfFile的区别?哪个性能高
imageNamed性能高 1.用imageNamed的方式加载时,图片使用完毕后缓存到内存中,内存消耗多,加载速度快.即使生成的对象被 autoReleasePool释放了,这份缓存也不释放,如果图 ...
- Qt官方开发环境生成的exe发布方式
本来想自己写一个打包程序的文章了,但是我发现了宝贝,在这里,大神写的比我牛逼的多了,这里做一下搬运工 一是为了方便大家 二是为了以后方便自己找 原文链接:http://tieba.baidu.com/ ...