poj 3069 Saruman's Army 贪心模拟
Saruman's Army
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18794 | Accepted: 9222 |
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个点和雷达扫描半径R;问覆盖所有点最少需要多少个雷达
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
int a[];
int n,cnt,r;
int main()
{
while(cin>>r>>n&&r!=-&&n!=-)
{
for(int i=;i<n;i++)
cin>>a[i];
sort(a,a+n);
int i=,cnt=;
while(i<n)//一个标记能覆盖的范围[s+r,p+r],超出这个范围就加一个标记
{
int s=a[i++];//能被覆盖的最左端的点的位置
while(i<n&&a[i]<=s+r)
i++;
int p=a[i-];//标记的位置
while(i<n&&a[i]<=p+r)
i++;
cnt++;
}
cout<<cnt<<endl; }
}
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 (模拟)
题目连接 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 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...
- 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 : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- 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 (贪心)
简单贪心. 从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点.如图. 自己的逻辑有些混乱,最后还是参考书上代码.(<挑战程序设计> P46) /*********** ...
- POJ 3069——Saruman's Army(贪心)
链接:http://poj.org/problem?id=3069 题解 #include<iostream> #include<algorithm> using namesp ...
随机推荐
- Python与线性代数——Numpy中的matrix()和array()的区别
Numpy中matrix必须是2维的,但是 numpy中array可以是多维的(1D,2D,3D····ND).matrix是array的一个小的分支,包含于array.所以matrix 拥有arra ...
- [原]SVN代码管理
1.SVN工程结构 branches zhangsan lisi wangwu tags project-release trunk project-trunk branches:下为trunk的分支 ...
- LinkList(链表)
code: #include <stdio.h> #include <time.h> #include <conio.h> #include <stdlib. ...
- 无法获得锁/var/lib/dpkg/lock - open(11.资源暂时不可用)
E:无法获得锁/var/lib/dpkg/lock - open(11.资源暂时不可用) E:无法锁定资源目录(/var/lib/dpkg)是否有其他进程正占用它? 解决方案: sudo rm /v ...
- SqlCacheDependency 缓存数据库依赖
启用SQL SERVER 通知 aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d N ...
- 【协作式原创】自己动手写docker之run代码解析
linux预备知识 urfave cli预备知识 准备工作 阿里云抢占式实例:centos7.4 每次实例释放后都要重新安装go wget https://dl.google.com/go/go1.1 ...
- IVM sdk command
1.load sdk shell ./auto_load_user.sh 2.查看all端口状态 IVM:0>ifcs show devport 3.查看个别端口状态 IVM:0>ifcs ...
- 外部IC寄存器的位定义
宏定义法:直接就是常量操作 方法1:用#define来定义,方便省事,缺点:系统不做检查 方法2:用enum来定义,可以像#define一样定义常量,同时系统做检查.既可以定义某个位也可以定义几个位的 ...
- Eclipse中java代码注释变成乱码的问题
今天在查看曾经写过的代码时发生了一件很是让人头疼的事: 我写的所有注释全部都变成了了乱码,曾经刚入门时也是经常遇到类似的问题,解决起来很快,每天可能都会在工作空间里看到,但是随着时间的推移,写代码的规 ...
- Codeforces 1304D. Shortest and Longest LIS
根据题目,我们可以找最短的LIS和最长的LIS,找最短LIS时,可以将每一个increase序列分成一组,从左到右将最大的还未选择的数字填写进去,不同组之间一定不会存在s[i]<s[j]的情况, ...