Saruman's Army

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 2
Problem 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
<span lang="en-us"><p>The input test file will contain multiple cases. Each test case begins with a single line containing an integer <i>R</i>, the maximum effective range of all palantirs (where 0 ≤ <i>R</i> ≤ 1000), and an integer <i>n</i>, the number of troops in Saruman’s army (where 1 ≤ <i>n</i> ≤ 1000). The next line contains n integers, indicating the positions <i>x</i><sub>1</sub>, …, <i>x<sub>n</sub></i> of each troop (where 0 ≤ <i>x<sub>i</sub></i> ≤ 1000). The end-of-file is marked by a test case with <i>R</i> = <i>n</i> = −1.</p></span>
 
Output
<p>For each test case, print a single integer indicating the minimum number of palantirs needed.</p>
 
Sample Input
0 3 10 20 20 10 7 70 30 1 7 15 20 50 -1 -1
 
Sample Output
2 4
 
题目大意:数轴上有些点,每个点可以放个什么鬼东西,可以覆盖R范围,问最少需要多少个这个东西
题目分析:是挑战那本书上的题,从左往右扫,找圆心位置即可
 #include <iostream>
#include<cstring>
#include <string>
#include <algorithm>
using namespace std;
bool cmp(int x, int y)
{
return x < y;
}
int main()
{
int r, n;
int a[];
while (cin >> r >> n)
{
if (r == - && n == -) break;
int i;
for (i = ; i <= n; i++)
{
cin >> a[i];
}
sort(a + , a + n + , cmp);
int num = ;
int x;
i =;
while (i <= n)
{
x = a[i++];
while (i <= n && a[i] <= x + r) i++;//一直向有前进直到距s的距离大于r的点
int p = a[i - ];//p是新加上标记的点的位置
while (i <= n && a[i] <= p + r) i++;//一直向有前进直到距p的距离大于r的点
num++;
}
cout << num << endl;
}
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: 18794   Accepted: 9222 D ...

  4. poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...

  5. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

  6. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  7. POJ 3069 Saruman's Army (模拟)

    题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...

  8. poj 3069 Saruman's Army (贪心)

    简单贪心. 从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点.如图. 自己的逻辑有些混乱,最后还是参考书上代码.(<挑战程序设计> P46) /*********** ...

  9. POJ 3069——Saruman's Army(贪心)

    链接:http://poj.org/problem?id=3069 题解 #include<iostream> #include<algorithm> using namesp ...

随机推荐

  1. python __new__ __init__

    写过python类的都会知道__init__,可能也了解__new__.我之前也了解__new__,但只做的它发生在__init__之前.其他的就比较模糊了 今天在学习单例模式时,看到有人用__new ...

  2. MyEclipse教程:使用UML创建模块库——第一部分(二)

    MyEclipse 在线购买低至75折!火爆开抢>> [MyEclipse最新版下载] UML1建模文件存储在建模库中,建模可用于生成Java代码,或者可以从代码中生成模型.本教程将向您展 ...

  3. jquery 中事件

    jQuery 事件 - submit() 方法 定义和用法 当提交表单时,会发生 submit 事件. 该事件只适用于表单元素. submit() 方法触发 submit 事件,或规定当发生 subm ...

  4. Python mode_+

    f = open("葫芦小金刚", mode="r+", encoding="utf-8") content = f.read(2) # 顺 ...

  5. OC基础:getter和setter,@public @protected @private 分类: ios学习 OC 2015-06-15 19:23 22人阅读 评论(0) 收藏

    @public 1.公开的,公共的,可以在类的内部和外部访问. 2.类的内部:实例变量名 3.类的外部:对象->实例变量名 @protected 1.受保护的,只能在本类和子类中可以访问 2.类 ...

  6. qt5 交叉编译

    qtbase/mkspecs/linux-arm-gnueabi-g++/qmake.conf 添加 QMAKE_LFLAGS += -Wl,-rpath-link,$$[QT_SYSROOT]/us ...

  7. SpringMVC开发小结

    1. 自动封装返回对象为JSON 1).在spring配置文件中添加如下配置: <mvc:annotation-driven> <mvc:message-converters> ...

  8. 《DSP using MATLAB》Problem 4.1

    用到的z变换的计算公式: 代码: %% ------------------------------------------------------------------------ %% Outp ...

  9. Tomcat:Several ports are already in use问题

    Several ports (8005, 8080, 8009) required by Tomcat v6.0 Server at localhost are already in use. The ...

  10. 2019第1周日-MQ选型要点

    用消息中间件犹如小马过河,选择合适的才最重要,这需要贴合自身的业务需求,技术服务于业务.具体在选择上可从下面功能.性能.可靠性和可用性.运维管理.社区和生态.团队技术栈等维度来进行筛选. 具体技术选型 ...