POJ-3069
Saruman's Army
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10994 Accepted: 5555 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 -1Sample Output
2
4Hint
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范围内必须要有一个标记。
贪心,从最左边开始找r内最远的点。
AC代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; int a[]; int main(){
ios::sync_with_stdio(false);
int r,n;
while(cin>>r>>n&&r>=&&n>=){
memset(a,,sizeof(a));
for(int i=;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
int temp=,ans=;
while(temp<n){
int s=a[temp++];
while(temp<n&&a[temp]<=s+r){
temp++;
}
int cnt=a[temp-];
while(temp<n&&a[temp]<=cnt+r){
temp++;
}
ans++;
}
cout<<ans<<endl;
}
return ;
}
POJ-3069的更多相关文章
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- POJ 3069 Saruman's Army(贪心)
Saruman's Army Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- 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(贪心)
链接: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) /*********** ...
- poj 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
- Greedy:Saruman's Army(POJ 3069)
2015-09-06 萨鲁曼军队 问题大意:萨鲁曼白想要让他的军队从sengard到Helm’s Deep,为了跟踪他的军队,他在军队中放置了魔法石(军队是一条线),魔法石可以看到前后距离为R的距离, ...
- Saruman's Army (POJ 3069)
直线上有N个点.点i的位置是Xi.从这N个点中选择若干个,给它们加上标记.对每一个点,其距离为R以内的区域里必须又带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点).在 ...
- POJ 3069:Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13242 Accepted: 6636 D ...
随机推荐
- 提高Interface Builder高效工作的8个技巧
本文转载至 http://www.cocoachina.com/ios/20141106/10151.html iOS开发Interface Builder 本文译自:8 Tips for worki ...
- sql server单引号和双引号的区别
--当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔--当 SET QUOTED_IDENTIFIER 为 OFF(默认值)时,表达式中的文 ...
- 为什么需要onRoute函数?
为什么需要onRoute函数? · Tencent/wepy Wiki https://github.com/Tencent/wepy/wiki/%E4%B8%BA%E4%BB%80%E4%B9%88 ...
- Generalised Policy Iteration With Monte-Carlo Evaluation
http://www0.cs.ucl.ac.uk/staff/d.silver/web/Teaching_files/control.pdf
- 2-phase-commit 3-phase-commit
2 prepare commit rollback acknowledge
- java中final与static的使用场景
final Java关键词final有“无法改变”的含义,主要用于修饰非抽象类.方法或者变量.使用时注意: final类不能被继承,没有子类,final类中的方法默认是final的. final方法不 ...
- Dijkstra的堆优化
先附上一个例题:P3371 [模板]单源最短路径 一眼扫去,最短路... spfa可行,但是今天的主题是Dijkstra: #include<iostream> #include<a ...
- 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---4
字符串的例子: 1.字符串排序: 应用范围:准备花名册,建立索引以及很多情况下都会用刀字符串的排序.这个程序的主要工具就是strcmp(). 算法:读一个字符串数组,对它们进行排序并输出. #incl ...
- EF异常捕捉
try{// 写数据库}catch (DbEntityValidationException dbEx){ }
- vsftpd虚拟用户【公司系统部分享】
一,安装相关工具包 #yum -y install pam vsftpd db4 db4-utils -- pam 是用来提供身份验证的 -- vsftpd 是ftp服务的主程序 -- db4支持文件 ...