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. linux定时任务:crontab命令

    crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查 ...

  2. Java数字签名算法--RSA

    签名具有的特性: 安全性 抗否认性 数字签名:带有密钥(公钥.私钥)的消息摘要算法(使用私钥进行签名,使用公钥进行验证) 数字签名算法:RSA.DSA.ECDSA 数字签名特性: 验证数据完整性 认证 ...

  3. 7.2 TCP IP的11种状态

    先看TCP IP的10种状态,如下所示: 三次握手: 客户端A端发送SYN,然后进入SYN_SENT状态,服务器B端接收到SYN后,返回一个响应ACK,同时也发送一个SYN,然后B端进入SYN_RCV ...

  4. Spring AOP关于cglib动态代理

    一: Spring AOP的默认代理方式是jdk动态代理,还有另外一种代理方式是cglib代理,简单说前者基于接口,后者基于继承,基本思路是将被代理对象的类作为父类,然后创建子类来进行方法的调用,调用 ...

  5. 隐藏微信小程序剪贴板提示

    wx.setClipboardData 剪贴内容老是有一个Toast提示,很不爽,如何隐藏? 只要你代码写得够快,提示都追不上你. wx.setClipboardData({ data: '女朋友了? ...

  6. Arpa’s obvious problem and Mehrdad’s terrible solution 思维

    There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious ...

  7. CTF-练习平台-Misc之 这么多数据包

    十一.这么多数据包 下载文件后解压,用wireshark打开CTF.pcapng,发现有很多包,快速浏览后发现前面都是攻击机(192.168.116.138)在向目标机(192.168.116.159 ...

  8. hdu 5183 hash表

    BC # 32 1002 题意:给出一个数组 a 和一个数 K ,问是否存在数对( i , j ),使 a i   - a i + 1 +……+ (-1)j - i  a j : 对于这道题,一开始就 ...

  9. 【BZOJ3110】【Zjoi2013】K大数查询 - 2

    之前用权值线段树套区间线段树水过,现在再练习一下整体二分 原题:有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b ...

  10. 龙儿经理嘴上经常说的B树

    国内的数据结构教材一般是按照Knuth定义,即“阶”定义为一个节点的子节点数目的最大值. 对于一棵m阶B-tree,每个结点至多可以拥有m个子结点.各结点的关键字和可以拥有的子结点数都有限制 规定m阶 ...