Saruman's Army
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13242   Accepted: 6636

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的距离内存在被标记的点,求至少要标记多少个点。

思路参考白书46页

#include<stdio.h>
#include<string.h>
#include<algorithm>
const int maxn=1e5+10;
int a[maxn];
int main()
{
int r,n;
while(~scanf("%d%d",&r,&n))
{
if(r==-1&&n==-1) break;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++) scanf("%d",&a[i]);
std::sort(a,a+n);
int sum=0;
int i=0;
while(i<n)
{
int s=a[i++];
while(i<n&&s+r>=a[i]) i++;
int p=a[i-1];
while(i<n&&a[i]<=p+r) i++;
sum++;
}
printf("%d\n",sum);
}
return 0;
}

POJ 3069:Saruman's Army的更多相关文章

  1. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  2. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

  3. POJ 3069 Saruman's Army(贪心)

     Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  4. poj 3069 Saruman's Army(贪心)

    Saruman's Army Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  5. POJ 3069 Saruman&#39;s Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6688   Accepted: 3424 De ...

  6. POJ 3069 Saruman's Army (模拟)

    题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...

  7. Q - Saruman's Army POJ - 3069

    Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...

  8. poj 3069 Saruman's Army 贪心模拟

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18794   Accepted: 9222 D ...

  9. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

随机推荐

  1. docker运行python3.6+flask小记

    docker新手. 目的:开发运行多个web api项目的时候,方便开发,兼顾部署. 方案大概这样: 1 隔离:系统运行在容器里,1容器只起1进程,代替在host起多个venv或虚拟机: 2调试和备份 ...

  2. ESXi时间同步

    ●建立NTP服务器 1,修改下面的注册表键把值设置为1.注册表键:HKEY_LOCAL_MACHINE\SYSTEM\ CurrentControlSet\services\W32Time\TimeP ...

  3. URAL 1658 Sum of Digits

    URAL 1658 思路: dp+记录路径 状态:dp[i][j]表示s1为i,s2为j的最小位数 初始状态:dp[0][0]=0 状态转移:dp[i][j]=min(dp[i-k][j-k*k]+1 ...

  4. CoderForce 148D-Bag of mice (概率DP求概率)

    题目大意:美女与野兽在玩画鸽子的游戏.鸽子在用黑布遮住的笼子里,白色的有w只,黑色的有b只,每次拿出一只作画,谁先画到白色的鸽子谁就赢.美女首先画,因为野兽太丑,它每次画的时候都会吓跑一只鸽子,所有出 ...

  5. WebSocket教程(二)

    运行环境:jdk8 tomcat8 无须其他jar包. package com.reach.socketController; import java.io.IOException; import j ...

  6. Is your JDeveloper Slow? - It shouldn't be!(转)

    我的Jdeveloper随便点一个AM,code的显示速度和手指的反应速度跟不上,真的是着急,忍了好久,找到以下教程. 经过考虑,仅仅只是在jdev.conf(jdevbin/jdev/bin/jde ...

  7. 使用dbms_output.put_line打印异常所在的行

    dbms_output.put_line(dbms_utility.format_error_stack); dbms_output.put_line(dbms_utility.format_call ...

  8. @XmlAccessorType @XmlType 详解

    例子: @XmlAccessorType(XmlAccessType.FIELD) String name=”CY”; } public static void main(String[] args) ...

  9. Error: [ng:areq] Argument 'LoginCtrl' is not a function, got undefined

  10. 68. Text Justification *HARD*

    Given an array of words and a length L, format the text such that each line has exactly L characters ...