Saruman's Army(贪心)

时间:2023-02-21 13:09:46

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(贪心)的更多相关文章

  1. POJ 3617 Best Cow Line &vert;&vert;POJ 3069 Saruman&&num;39&semi;s Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  2. POJ 3069 Saruman&&num;39&semi;s Army&lpar;贪心&rpar;

     Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  3. poj 3069 Saruman&&num;39&semi;s Army 贪心模拟

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18794   Accepted: 9222 D ...

  4. B - B Saruman&&num;39&semi;s Army&lpar;贪心&rpar;

    在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...

  5. poj 3069 Saruman&&num;39&semi;s Army 贪心 题解《挑战程序设计竞赛》

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

  6. poj 3069 Saruman&&num;39&semi;s Army(贪心)

    Saruman's Army Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  7. POJ3069 Saruman's Army【贪心】

    Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...

  8. poj 3069 Saruman&&num;39&semi;s Army

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

  9. poj3069 Saruman&&num;39&semi;s Army

    http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...

随机推荐

  1. Centos 6&period;5 安装ELK

    安装java 查看java安装路径 修改环境变量 添加如下内容: export JAVA_HOME=/usr/java/jdk1.8.0_91 export PATH=$JAVA_HOME/bin:$ ...

  2. lseek函数

    所有打开的文件都有一个当前文件偏移量(current file offset),以下简称为 cfo.cfo 通常是一个非负整数,用于表明文件开始处到文件当前位置的字节数.读写操作通常开始于 cfo,并 ...

  3. -&lowbar;-&num;QUOTA&lowbar;EXCEEDED&lowbar;ERR&colon; DOM Exception 22

    iOS的Safari在无痕模式下,sessionStorage操作产生异常,报错QUOTA_EXCEEDED_ERR: DOM Exception 22. html5 localStorage err ...

  4. go 0000

    1,函数不能比较 函数默认都是值传递,  除了  map   值, slice (切片)  channel  (管道)   interface 默认以这4个默认 引用传递 两个 defer      ...

  5. HihoCoder 1511&colon; 树的方差&lpar;prufer序&rpar;

    题意 对于一棵 \(n\) 个点的带标号无根树,设 \(d[i]\) 为点 \(i\) 的度数,定义一棵树的方差为数组 \(d[1..n]\) 的方差. 给定 \(n\) ,求所有带标号的 \(n\) ...

  6. Confluence 6 为外部用户管理获得支持

    本页面描述了如果你在配置外部用户管理的时候遇到了问题,如何向 Atlassian 支持项目组寻求帮助.外部用户目录挂你包括 Active Directory,其他 LDAP 服务器,Atlassian ...

  7. effective c&plus;&plus; 笔记 &lpar;9-12&rpar;

    //---------------------------15/03/29---------------------------- //#9    绝不在构造和析构过程中调头virtual函数 { / ...

  8. PAT 1055 The World&&num;39&semi;s Richest&lbrack;排序&rsqb;&lbrack;如何不超时&rsqb;

    1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based o ...

  9. BZOJ 1091--切割多边形&lpar;几何&amp&semi;枚举&rpar;

    1091: [SCOI2003]切割多边形 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 356  Solved: 157[Submit][Status ...

  10. Sqlserver 存储过程 返回-6

    存储过程中没有返回 手动返回-6的代码,但是一直接收到-6返回值. 经最后研究发现,是粗心导致的,Insert插入数据时在非空字段插入了Null值导致出现异常,所以返回了-6. 所以说在事务中,可能会 ...