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 ...
随机推荐
- mac Axure RP 8 授权码 以及汉化
Koshy wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6xY 汉化包 汉化包链接 密码: upri 汉化步骤 以Win7 ...
- css中三种隐藏方式
1.overflow 溢出隐藏 overflow:hidden 2.display 隐藏不占据原来的文档,即会让出空间 display:black 显示 display:none 隐藏 3.vis ...
- (七)json序列化
在spring boot项目中已经包含有json序列化的框架,具体在包com.fasterxml.jackson.annotation中,建议看看详细源码. 但在项目应用上还是会有一些坑会出现的,举个 ...
- Python基础、条件语句和基本数据类型
1. 第一句python - 后缀名是可以是任意? - 导入模块时,如果不是.py文件 ==> 以后文件后缀名是 .py 2. 两种执行方式 python解释器 py文件路径 python 进入 ...
- python新手之字典增删改查
一.字典的定义 city_list = { 'beijin':"北京",'shanghai':"上海" } print(city_list) 二.字典添加一个元 ...
- 我和Python的Py交易》》》》》》函数
一 函数是什么? 是数学中的函数? Python中 函数是指将一组语句的集合通过一个名字(函数名)封装起来的一段代码.(所以这里的函数是subroutine子程序) 那要函数干嘛.不都是代码吗?只不 ...
- pascal 的字符串操作
1.ord 将字符转为 ascii码 2.chr 将ascii码转为字符 3.trunc 求整数部分 4.random , randomize 5.copy(s,i,l)从s串中截取第i个字符开始后长 ...
- linux下使用汇编语言编写hello world!程序
最近公司需要完成安全方面的测试,随之带来需要更深入地学习攻击方法和漏洞分析的技术,总感觉有点像黑客:),不过不能只知道一些安全测试工具的方法和工具的使用,更需要基础功夫,首先从大学学过的汇编语言(呵呵 ...
- 20145209刘一阳《JAVA程序设计》第1周学习总结
20145209刘一阳<JAVA程序设计>第1周学习总结 本周任务 了解Java基础知识 了解JVM.JRE与JDK,并下载.安装.测试JDK 了解PATH.CLASSPATH.SOURC ...
- c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承
公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...