Saruman's Army(POJ3069)
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.
- #include<iostream>
- #include<algorithm>
- using namespace std;
- int main()
- {
- int n, r;
- while (cin >> r >> n, n != - && r != -)
- {
- int a[];
- for (int i = ; i < n; i++)
- cin >> a[i];
- sort(a, a + n);
- int i = , ans = ;
- while (i < n)
- {
- int s = a[i++];
- while (i < n&&a[i] <= s + r) i++;
- int p = a[i - ];
- while (i < n&&a[i] <= p + r) i++;
- ans++;
- }
- cout << ans << endl;
- }
- return ;
- }
//这次学习的代码是通过循环控制找到最优位置
Saruman's Army(POJ3069)的更多相关文章
- poj3069 Saruman's Army
http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...
- POJ3069 Saruman's Army【贪心】
Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...
- 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(萨鲁曼军)
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 3069:Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13242 Accepted: 6636 D ...
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- Saruman's Army(贪心)
Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep tra ...
- POJ 3069 Saruman's Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
随机推荐
- 常用的Markdown语法
1.表格 | 左对齐标题 | 右对齐标题 | 居中对齐标题 | | :------| ------: | :------: | | 短文本 | 中等文本 | 稍微长一点的文本 | | 稍微长一点的文本 ...
- windows下flazr对rtmp视频流进行压力测试(批量直播测试)
flazr-0.7-RC2下载地址:百度网盘 提取码:nu05 简述:通过推流软件推送摄像头视频流到nginx流媒体服务器,获取nginx流媒体服务器上的视频流,在windows下使用flazr软件进 ...
- RabbitMQ安装(一)
RabbitMQ官网 http://www.rabbitmq.com 下载地址 http://www.rabbitmq.com/download.html 一 Windows下安装RabbitMq 1 ...
- ORACLE数据库,数据量大,转移数据到备份表语句
INSERT INTO TEMP_BUS_TRAVEL_INFO ( SELECT * FROM BUS_TRAVEL_INFO t ') SELECT COUNT(*) FROM TEMP_BUS_ ...
- BurpSuite使用笔记
参考:Burp Suite 实战指南 Proxy模块 options--> add 设置监听地址,端口 binding 如果是作为本地浏览器代理默认(127.0.0.1:8080)就可以了. 如 ...
- 8.3版本提示未在本地计算机上注册 Microsoft.ACE.OLEDB.12.0 提供程序
这个原因是8.3版本推出了64位程序,但是Access驱动在64位系统上默认是没有安装的,需要下载一个组件安装即可. 下载2010 Access 驱动程序:数据连接组件安装 http://www.ba ...
- 【转】Python之向日志输出中添加上下文信息
[转]Python之向日志输出中添加上下文信息 除了传递给日志记录函数的参数(如msg)外,有时候我们还想在日志输出中包含一些额外的上下文信息.比如,在一个网络应用中,可能希望在日志中记录客户端的特定 ...
- leetcode 307 Range Sum Query
问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ...
- Linux将公网ip映射到局域网ip
测试环境如下: monitor: msc1:公网IP:103.6.164.128 eth0 内网IP:192.168.0.57 eth0内网IP:192.168.0.16 eth1 通过访问monit ...
- matlab积分使用
clc; %清屏 clear; %清除变量 close all; %关闭 syms x; f(x) = x; df = int(f(x),x);%求不定积分 figure; subplot(1,2,1 ...