POJ 3069 Saruman's Army
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6688 | Accepted: 3424 |
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个点,从这n个点中选择若干个,给他们加上标记。
对每个点,其距离为r以内的区域里必须有带标记的点(自己本身带有标记的点,能够觉得与其距离为0的地方有一个带有标记的点)。在满足这个条件的情况下,希望能为尽可能少的点加入标记,请问至少要有多少点被加上标记
#include <cstdio>
#include <algorithm>
using namespace std; int r, n;
int x[1001]; void solve()
{
sort(x, x + n);
int i = 0, ans = 0;
while (i < n){
int s = x[i++]; //s是没有被覆盖的最左的点的位置
while (i < n && x[i] <= s + r) //一直向右前进直到距s的距离大于r的点
i++;
int p = x[i - 1]; //p是新加上标点的点的位置
while (i < n && x[i] <= p + r) //一直向右前进直到距p的距离大于r的点
i++;
ans++;
}
printf("%d\n", ans);
} int main()
{
while (scanf("%d%d", &r, &n) != EOF){
if (r == -1 && n == -1)
break;
for (int i = 0; i < n; i++){
scanf("%d", &x[i]);
}
solve();
}
return 0;
}
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: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
- 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(贪心)
链接: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 贪心模拟
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18794 Accepted: 9222 D ...
随机推荐
- CentOS7.5安装Mysql5.7.22
一 下载 下载地址:https://www.mysql.com/downloads/ 1 点击第一个链接地址,进入MySQL官方网站,单击“Downloads”下载Tab页,进入下载界面 2 找到Co ...
- git与pycharm合并,珠联璧合
前段时间提交代码都是各种手写push push push,好蠢,今天尝试了一下ide直接提交,爽的一匹,做个总结. 首先github帐号肯定要有. 看图,设置好帐号啥的 再设置git路径,这个应该自动 ...
- jquery自定义插件-参数化配置多级菜单导航栏插件
1 自定义菜单导航栏插件的必要性 看图说话,下面是利用自定义的菜单导航栏插件simpleMenu创建的网站导航示例: 插件默认提供的是如上图的导航栏样式,即一二级菜单为横向分布:三四级菜单为纵向分布. ...
- Python全栈开发之2、运算符与基本数据结构
运算符 一.算数元算: 读者可以跟着我按照下面的代码重新写一遍,其中需要注意的是,使用除的话,在python3中可以直接使用,结果是4没有任何问题,但是在python2中使用的话,则不行,比如 9/2 ...
- ASP.NET MVC 3和Razor中的@helper
ASP.NET MVC 3支持一项名为“Razor”的新视图引擎选项(除了继续支持/加强现有的.aspx视图引擎外).当编写一个视图模板时,Razor将所需的字符和击键数减少到最小,并保证一个快速.通 ...
- 在phpWeChat中生成公众号 jssdk 各个参数(PHP)
<?php //jsapipara $jsapipara=array(); $jsapipara['appid']=WECHAT_APPID; $jsapipara['noncestr']=cr ...
- bound和unbound方法,类的绑定和非绑定是什么
作者:灵剑链接:https://www.zhihu.com/question/41006598/answer/148994582来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 「COCI2016/2017 Contest #2」Bruza
「COCI2016/2017 Contest #2」Bruza 解题思路 : 首先对于任意时刻 \(i\) ,硬币一定移动到了深度为 \(i\) 的节点,所以第 \(i\) 时刻 Danel 一定染掉 ...
- Linux下对拍程序
在程序对应文件夹下存为.sh文件 在终端命令中进入相应文件夹,用 sh XXX.sh 调用 while true; do ./datamaker>tmp.in ./baoli<tmp.in ...
- 利用SharpZipLib对字符串进行压缩和解压缩
添加对ICSharpCode.SharpZipLib的引用. using ICSharpCode.SharpZipLib.BZip2; /// <summary> /// 压缩 /// & ...