POJ3069 Saruman's Army【贪心】
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 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
思路:从最左边开始,找r范围内最右边的点,然后在超出r范围的最左边点当做起始点继续寻找。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
int main()
{
int n,r, arr[maxn];
while(scanf("%d%d",&r,&n)!=EOF && n+r!=-2)
{
for(int i=0;i<n;++i)
scanf("%d",&arr[i]);
sort(arr,arr+n);
int i=0,ans=0;
while(i<n)
{
int s=arr[i++];
while(i<n && arr[i]<=s+r)
++i;
int p=arr[i-1];
while(i<n && arr[i]<=p+r)
++i;
++ans;
}
printf("%d\n",ans);
}
return 0;
}
POJ3069 Saruman's Army【贪心】的更多相关文章
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- poj3069 Saruman's Army
http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...
- 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: 18794 Accepted: 9222 D ...
- B - B Saruman's Army(贪心)
在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...
- poj3069 Saruman's Army(贪心)
https://vjudge.net/problem/POJ-3069 弄清楚一点,第一个stone的位置,考虑左右两边都要覆盖R,所以一般情况下不会在左边第一个(除非前两个相距>R). 一开始 ...
- poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...
- Saruman's Army(贪心)
Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep tra ...
- Saruman's Army(POJ3069)
Description Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. ...
随机推荐
- Java三行代码搞定MD5加密
Java三行代码搞定MD5加密 https://www.dexcoder.com/selfly/article/4026 public class MD5Test { public static vo ...
- [Cypress] Create Aliases for DOM Elements in Cypress Tests
We’ll often need to access the same DOM elements multiple times in one test. Your first instinct mig ...
- 虚拟化(四):vsphere高可用功能前提-共享存储搭建
虚拟化(一):虚拟化及vmware产品介绍 虚拟化(二):虚拟化及vmware workstation产品使用 虚拟化(三):vsphere套件的安装注意及使用 虚拟化(四):vsphere高可用功能 ...
- Android 输入管理服务-输入事件到达之后的处理流程
接上一篇博客"Android 输入管理服务启动过程的流程".这两天分析了Android 输入管理服务接收到输入事件之后的处理流程,详细流程例如以下面两图所看到的: 接下图
- 转:java身份证格式强校验
package com.dsh.zealandweb.utils; import java.util.HashSet; import java.util.regex.Pattern; import o ...
- oc46--nonatomic, retain
// // Person.h #import <Foundation/Foundation.h> #import "Room.h" #import "Car. ...
- LA4122
哈夫曼树+搜索 抄了抄代码 先开始不知道怎么限制哈夫曼树,然后看了看代码,是用bfs序来限制.因为每个节点的右子树节点肯定不小于左儿子,同一层也是.所以先搞出bfs序,然后搜索,判断每一层右边是否大于 ...
- Python入门 六、像个 Pythonista
pickle import pickle test_data = ['Save me!',123.456,True] f = file('test.data','w') pickle.dump(tes ...
- scws
SCWS 是 Simple Chinese Word Segmentation 的首字母缩写(即:简易中文分词系统). 这是一套基于词频词典的机械式中文分词引擎,它能将一整段的中文文本基本正确地切分成 ...
- [Apple开发者帐户帮助]五、管理标识符(3)删除应用程序ID
您可以在不再需要时删除App ID.但是,您无法删除上载到App Store Connect的应用程序的显式应用程序ID . 所需角色:帐户持有人或管理员. 在“ 证书”,“标识符和配置文件”中,从左 ...