poj 3069 Saruman's Army
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8477 | Accepted: 4317 |
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.
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.
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.
简单的贪心。
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[];
int main()
{
int r,n;
while(~scanf("%d%d",&r,&n)&&r>=&&n>-)
{
for(int i=;i<n;i++)
{
scanf("%d",a+i);
}
sort(a,a+n);
int i=,s=;
int ans=;
while(i<n)
{
s=a[i++];
while(s+r>=a[i]&&i<n) i++;
if(i>n) break;
int p=a[i-];
ans++;
while(p+r>=a[i]&&i<n) i++; }
cout<<ans<<endl;
}
}
poj 3069 Saruman's Army的更多相关文章
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- 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 : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- POJ 3069 Saruman's Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
- poj 3069 Saruman's Army 贪心模拟
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18794 Accepted: 9222 D ...
- POJ 3069——Saruman's Army(贪心)
链接:http://poj.org/problem?id=3069 题解 #include<iostream> #include<algorithm> using namesp ...
- poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...
- poj 3069 Saruman's Army (贪心)
简单贪心. 从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点.如图. 自己的逻辑有些混乱,最后还是参考书上代码.(<挑战程序设计> P46) /*********** ...
随机推荐
- Android开发笔记之《Window下安装Ubuntu双系统,Grub无法显示Window选项》
解决方法是: 在terminal里面输入: sudo update-grub 会找到windows的grub 重启电脑就可以了.
- A:手把手教Wordpress仿站(基础)
安装源码 需要服务器有php环境(PHP,Mysql,Apeach/Ngnax) 我用的主机宝(环境一键安装工具) 打开后台突然出现这种情况 Briefly unavailable for sch ...
- 【09-23】js原型继承学习笔记
js原型继承学习笔记 function funcA(){ this.a="prototype a"; } var b=new funcA(); b.a="object a ...
- Ubuntu 12.4 Apache2 安装教程
一.更新操作系统 sudo apt-get update && sudo apt-get upgrade 二.安装Apache及依赖 sudo apt-get install apac ...
- JavaScript学习链接
Js中this的用法:http://www.cnblogs.com/RitaRichard/archive/2011/10/14/2212161.html JavaScript\ActionScrip ...
- PHP面向对象(OOP)编程入门教程
面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行PHP的高级编程,对于提高 PHP编程能力和规划好Web开发构架都是非常有意义的.下面我们就 ...
- jquery_DOM笔记
回头补充知识: jquery事件复习: bind() 用于绑定多个事件,当某一个节点需要进行多项处理的时候使用 .使用方式 $(select).bind({event:function(),event ...
- Asp.Net Core--简单的授权
翻译如下: 在MVC中授权通过控制AuthorizeAttribute属性及其各种参数.在最简单的应用AuthorizeAttribute属性控制器或行动限制访问控制器或操作任何身份验证的用户. 例如 ...
- Ajax方法提交整个表单的信息
<pre>$.ajax({ cache: true, type: "POST", ...
- HTML页面meta标签内容详解
所有的浏览器都支持meta标签,用于提供页面相关信息,信息都是以名(http-equiv和name标示)/值(content标示)对的形式现实. 属性content,用于定义http-equiv(定义 ...