Saruman's Army

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18794   Accepted: 9222

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.

题意:输入n个点和雷达扫描半径R;问覆盖所有点最少需要多少个雷达

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
int a[];
int n,cnt,r;
int main()
{
while(cin>>r>>n&&r!=-&&n!=-)
{
for(int i=;i<n;i++)
cin>>a[i];
sort(a,a+n);
int i=,cnt=;
while(i<n)//一个标记能覆盖的范围[s+r,p+r],超出这个范围就加一个标记
{
int s=a[i++];//能被覆盖的最左端的点的位置
while(i<n&&a[i]<=s+r)
i++;
int p=a[i-];//标记的位置
while(i<n&&a[i]<=p+r)
i++;
cnt++;
}
cout<<cnt<<endl; }
}

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 (模拟)

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

  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 : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  7. poj 3069 Saruman's Army

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

  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. jquery 分页 Ajax异步

    //使用Ajax异步查询数据 <div class="table-responsive"> <table class="table table-bord ...

  2. 【PAT甲级】1063 Set Similarity (25 分)

    题意: 输入一个正整数N表示集合的个数(<=50),接着输入N行,每行包括一个数字x代表集合的容量(<=10000),接着输入x个非负整数.输入一个正整数Q(<=2000),接着输入 ...

  3. 使用kibana或postman操作Elasticsearch的常用命令

    前言服务器:CentOS-6.7 运行系统:windows10 JDK版本:1.8 Elasticsearch版本:5.6.6 插件:kibana.elasticsearch-head 工具:post ...

  4. 「JSOI2014」矩形并

    「JSOI2014」矩形并 传送门 我们首先考虑怎么算这个期望比较好. 我们不难发现每一个矩形要和 \(n - 1\) 个矩形去交,而总共又有 \(n\) 个矩形,所以我们把矩形两两之间的交全部加起来 ...

  5. (踩过的坑)使用Github Page搭建个人博客

    最近需要搭建一个网站,作为导航网址,但是自己的域名备案还要等上几天,就想着有没有别的办法来搭建一个公网可以访问的网站. Github Page的话是一个github个人主页,完全适合用来搭建普通网站. ...

  6. SSD boot test script for(if)循环

    ;Author : Bing Song ;// ;Usage: modify log drictory according to actual drictory gettime timestr &qu ...

  7. 06. Z字型变换

    题目: 提交01: class Solution { public String convert(String s, int numRows) { int length = 2*numRows-2; ...

  8. C++结构体struct与C语⾔结构体和C++引⽤&与传值的区别

    写再最前面:摘录于柳神的笔记: (1)定义好结构体 stu 之后,使⽤这个结构体类型的时候,C语⾔需要写关键字 struct ,⽽C++⾥⾯可以省 略不写: (2)这个引⽤符号 & 要和C语⾔ ...

  9. Linux 修改/etc/profile 出错 导致所有命令都 command not found 的解决办法

    执行命令 export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin 然后再修改/etc/profile 再执行文件: source /etc/p ...

  10. HIHOcoder编程总结

    [Offer收割]编程练习赛44 对于第一题题目1 : 扫雷游戏,首先要想清楚思路,虽然是暴力算法,但是这八个方向要自己把坐标写正确,不要慌乱,自己写的时候就写错了一个,第二个就是判断的时候,j + ...