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 n,r;
int x[1005];
int s,p;
int main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    while(cin >> r >> n && n != -1 && r != -1){
        for(int i = 0; i < n; i++)
            cin >> x[i];
        sort(x,x+n);
        int i = 0,ans = 0;
        while(i < n){
            s = x[i++];
            while(i < n && x[i] <= s + r)
                i++;
            int p = x[i-1];
            while(i < n && x[i] <= p + r)
                i++;
            ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

最新文章

  1. CentOS6.5下搭建NFS文件服务器
  2. Java学习笔记(四)——google java编程风格指南(上)
  3. epoll在socket通信中的应用
  4. UML用户指南--UML图简介
  5. POJ_3273_Monthly_Expense_(二分,最小化最大值)
  6. HDU_1241 Oil Deposits(DFS深搜)
  7. C语言数据输入与输出
  8. php常用的排序算法与二分法查找
  9. UCOS 杂项 笔记
  10. XJOI网上同步测试DAY14 T3
  11. 部分无线终端不响应键盘事件(keydown,keypress,keyup)的解决办法
  12. 1关于script标签属性,注意点,浏览器文档模式,各种数据类型的转化
  13. 一个简单的php站点配置
  14. 翻译一篇关于jedis的文章
  15. Generetor函数与线程之间的思考
  16. java内存结构
  17. 华为oj之字符串分割
  18. chrome恢复默认搜索引擎为Google
  19. 《笨方法学Python》加分题16
  20. 【托业】【新托业TOEIC新题型真题】学习笔记11-题库六-P7

热门文章

  1. selenium选择框
  2. python项目虚拟环境搭建
  3. layer.open 回调函数
  4. XSS 1
  5. lees入门
  6. 后台用map接收数据,报类型转换错误
  7. CSS学习(6)层叠
  8. 每天进步一点点------DE2-70-TV例子说明
  9. TestNG单元测试与使用详解
  10. php和redis怎么实现消息队列