POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军)
Time Limit: 1000MS Memory Limit: 65536K
|
【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. |
白袍萨鲁曼必须带领着他的军队从艾辛格径直前往圣盔谷。为了有序行军,萨鲁曼在军队中分发被称作真知晶球的远见石。每个真知晶球的最大有效范围为R个单位,并且必须由军队中的一些部队携带(即真知晶球不允许中途变动)。 通过使用最少的真知晶球来确保萨鲁曼的每个仆从都在晶球附近的R个单位内,助他掌控中土世界。 |
|
【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. |
输入文件有多组测试样例。 每组测试数据第一行,包括一个整数R(0 ≤ R ≤ 1000),表示真知晶球的最大有效范围,还有一个整数n(1 ≤ n ≤ 1000),表示萨鲁曼军的部队数量。 下一行有n个整数,x1, …,xn(0 ≤ xi ≤ 1000)表示每个部队的位置。 R = n = −1表示输入结束。 |
|
【Output】 |
【输出】 |
|
For each test case, print a single integer indicating the minimum number of palantirs needed. |
对于每个测试样例,输出一个整数表示最少需要使用的真知晶球数量。 |
|
【Sample Input - 输入样例】 |
【Sample Output - 输出样例】 |
|
0 3 10 20 20 10 7 70 30 1 7 15 20 50 -1 -1 |
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. |
对于第一个测试样例,萨鲁曼可以在位置10和20放置真知晶球。这样,范围为0的真知晶球可以覆盖同在位置20的部队。 对于第二个测试样例,萨鲁曼可以放置在位置7(覆盖部队1,7和15),位置20(覆盖20和30),位置50,还有位置70。这里要注意,真知晶球必须放在部队中并且不允许变动。因此,萨鲁曼不能在位置60放置真知晶球来覆盖在位置50和70的部队。 |
【题解】
贪心法,一开始还在纠结应该往前还是往后……
然后看了看提示的第二个测试样例……从当前位置往后贪就好了。
【代码 C++】
#include<cstdio>
#include <cstring>
#include <algorithm>
int main(){
int r, n, i, j, data[], opt;
while (scanf("%d%d", &r, &n)){
if (r + n < ) break;
memset(data, , sizeof(data));
for (i = ; i < n; ++i) scanf("%d", &data[i]);
std::sort(data, data + n);
for (i = opt = ; i < n; ++opt){
for (j = i; data[j] <= data[i] + r; ++j);
for (i = --j; data[i] <= data[j] + r; ++i);
}
printf("%d\n", opt);
}
return ;
}
POJ 3069 Saruman's Army(萨鲁曼军)的更多相关文章
- 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 贪心模拟
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18794 Accepted: 9222 D ...
- 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 (贪心)
简单贪心. 从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点.如图. 自己的逻辑有些混乱,最后还是参考书上代码.(<挑战程序设计> P46) /*********** ...
随机推荐
- xxxx
http://www.cnblogs.com/zhuojun/p/5747999.html
- USB 设备类协议入门【转】
本文转载自:http://www.cnblogs.com/xidongs/archive/2011/09/26/2191616.html 一.应用场合 USB HID类是比较大的一个类,HID类设备属 ...
- 使用 ViewPager 和 RadioGroup 封装的一个导航控件
import android.animation.ObjectAnimator; import android.content.Context; import android.graphics.dra ...
- eclipse项目中启动项目无法载入类
在eclipse 项目中,当载入jar包后,加载里面的包,可以找到此类,但是编译运行的时候报错java.lang.ClassNotFoundException: 1,路径名未写正确: 2,配置出错; ...
- css改变背景透明度【转】
兼容主流浏览器的CSS透明代码: .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0. ...
- NIOS II开发备忘录
大概有一年没做NIOS II的开发了,回想上一次做NIOS II还是去年做毕业设计的时候.那时候做的是基于SOPC的频率特性测试仪,我大约花了2个月的时间,从无到有的学习了NIOS II开发.学习过N ...
- codeigniter db操作方法
链接数据库 ——- $this->load->database();//手动连接数据库 //连接多数据库 $DB1 = $this->load->database(‘group ...
- 通达OA 免狗迁移到公网 的另类解决办法
1,通达OA 发布到公网 ,要真正的 Anywhere2,正版通达OA,有加密狗在本地机器上 ,通达必须检测有狗才可以运行3,阿里云服务器 (你想往上插加密狗都没地方的说..汗)4,本地ISP 不提 ...
- 关于IllegalMonitorStateException异常
关于IllegalMonitorStateException异常: api中的解释 另请参见: Object.notify(), Object.notifyAll(), Object.wait(), ...
- python项目练习3:万能的XML
1.目的 如何用XML来表示多种数据,以及如何使用适合XML或SAX的简单API来处理XML文本.目标是通过一个描述各种网页和目录的XML文件生成一个完整的网站. 注:有关XML的描述参见http:/ ...