关闭

POJ 3069

标签: pojACM算法
494人阅读 评论(4) 收藏 举报
分类:

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.

  1. 题意:英语伤不起=-=, 大概就是 给出一个n 一个 r n是一条直线上面有n个点 的坐标(相当于数轴) 在这些点上面放一个灯 给出的r 就是灯的照射范围,让你求出 最少给出多少个灯才能把一条线照满
  2. 思路: 按照贪心的思路,充分的利用r到每个点的区间

import java.util.Arrays;
import java.util.Scanner;


public class Main {
    private static int n ,r,current=0;
    private static int[] array;
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext()) {
        current =0;
        r = scanner.nextInt();
        n = scanner.nextInt();
        if (n==-1&&r==-1) 
        break;
        array = new int[n];
        for (int i = 0; i < array.length; i++) {
        array[i] = scanner.nextInt();
        }
        int i=0;
        Arrays.sort(array);//先对这n个点排序
        while (i<n) {
        int x = array[i++];
        while (i<n&&x+r>=array[i]) //相当于第一个点加上r(照射范围)大于下一个点,这样下一个点就不用放灯了,直接i++,知道不能照到为止
            i++;
        int  p = array[i-1];
        while (i<n&&p+r>=array[i]) //这就是下一个点了  同理以上
            i++;
        current ++;
        }
        System.out.println(current);

    }
    scanner.close();
    }

}
3
0
查看评论

poj3069Saruman's Army(贪心)

题意直线上有一些点。现在能给一部分点加buff,加了buff的点能影响到周围r的范围内的点。问最少要给多少点加buff才能影响到所有的点思路典型的贪心,每次尽量找能影响到当前的最靠右的点来加buff,加完之后继续从加了buff的那个点影响范围之外的第一个点开始继续找代码#include <cs...
  • readlnh
  • readlnh
  • 2016-08-18 00:52
  • 248

poj3069 贪心

题意:直线上有n个点,点i的位置是Xi。从这n个点中选择若干个,给它们加上标记。对每一个点,其距离为R以内的区域里必须有带标记的点(本身为带有标记的点,可以认为与其距离为0的地方有一个带有标记的点)。在满足这个条件的情况下,希望能为尽可能少的点添加标记。求最少要有多少个点被加上标记。 算法...
  • aidway
  • aidway
  • 2016-04-08 22:46
  • 305

POJ-3069-Saruman's Army

这个题是说给你n个点,然后让你标记其中尽可能少的点,使得n个点都处于被标记点左右不超过R的区间内, 思路:贪心~尽量充分利用区间 代码: #include #include #include #include using namespace std; const int maxn=1100; ...
  • z309241990
  • z309241990
  • 2013-12-23 19:26
  • 1523

POJ 3069 Saruman's Army(贪心)

Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5954 Accepted: 3037 Descri...
  • zwj1452267376
  • zwj1452267376
  • 2015-11-17 18:31
  • 433

poj 3069

#include #include int a[1010]; int cmp(int *a,int *b) { return *a-*b; } int main() { int R,n,i,j,k,count;; while(scanf("%d%d",...
  • wlwlxgg
  • wlwlxgg
  • 2014-03-26 18:12
  • 301

POJ 3069

Sarumans Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions...
  • ericxieforever
  • ericxieforever
  • 2008-04-07 23:33
  • 698

poj 3069贪心~

点击打开链接http://poj.org/problem?id=3069#include #include #define M 1010 using namespace std; int a[M]; int main() { int r,n,i,j,k,ans; while(1) { ...
  • Jeremy1149
  • Jeremy1149
  • 2016-05-22 17:56
  • 141

poj 3069 贪心

背景:1Y,但是思考的时候并没有充分思考好思路再写,而是有点含糊的边写边想,这样不好,一定要思路已经十分明确了再写。 思路:从一个没有被覆盖的点开始,在以它为中心半径为r的区域内,至少有一个点标记,那么就选择,距离它最远的点。标记这个点之后,算出下一个没有被标记的点,重复以上操作即可。这里把最后一...
  • jibancanyang
  • jibancanyang
  • 2015-04-10 15:44
  • 480

HDU 3340 Rain in ACStar(线段树+几何)

HDU 3340 Rain in ACStar 题目链接 题意:给定几个多边形(3-5边形),然后中间有一些询问,询问一个区间的总面积 思路:多边形分割为梯形,梯形的面积为上底d1 + 下底d2 乘上 高度 / 2,两个梯形面积累加的话,可以等价为上底下底累加,所以就可以用线段树搞...
  • u011217342
  • u011217342
  • 2014-10-18 15:32
  • 901

POJ 3617 3069 贪心

POJ 3617 3069 贪心两个都是贪心对于3617,每次比较从左看以及从右看的字典序,字典序小的那边直接弹出输出 然后需要注意的是输出格式每行80个字符#include <iostream> #include <cstdio> #include <cstdli...
  • xykaiku
  • xykaiku
  • 2017-03-17 08:38
  • 175
    个人资料
    • 访问:112174次
    • 积分:1387
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:3篇
    • 译文:0篇
    • 评论:32条
    博客专栏
    最新评论