poj 3069 Saruman's Army

原创 2018年04月16日 21:58:35
Saruman's Army
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13094 Accepted: 6568

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<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
#define N 1005
typedef long long LL;
int x[N];

int main(){
    int n, r;
    while(scanf("%d%d", &r, &n)!=EOF && n!=-1 && r!=-1){
        for(int i = 0; i < n; i++){
            scanf("%d", &x[i]);
        }
        sort(x, x+n);
        int cnt = 0;
        int s = 0;
        int flag = 0;
        for(int i = 0; i < n;){
            while(i<n){  //找到要标记的点
                if((x[i]-x[s]) <= r) i++;
                else
                    break;
            }
            flag = i-1; //当前被标记的点
            while(i<n){  //找到以flag点为半径画的弧,交右侧坐标轴外围的第一个点
                if((x[i]-x[flag] <= r)) i++;
                else
                    break;
            }
            s = i; //标记这个外围点
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guihaiyuan123/article/details/79967323

POJ 3069 Saruman's Army(贪心)

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

POJ 3069 - Saruman's Army(贪心)

Description Saruman the White must lead his army along a straight path from Isengard to Helm’s De...
  • u013534690
  • u013534690
  • 2015-02-10 08:52:32
  • 388

Poj 3069 Saruman's Army【贪心】

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5742   Accepted: 2...
  • liuke19950717
  • liuke19950717
  • 2015-10-01 22:57:45
  • 1046

poj3069Saruman's Army(贪心)

题意直线上有一些点。现在能给一部分点加buff,加了buff的点能影响到周围r的范围内的点。问最少要给多少点加buff才能影响到所有的点思路典型的贪心,每次尽量找能影响到当前的最靠右的点来加buff,...
  • readlnh
  • readlnh
  • 2016-08-18 00:52:40
  • 265

POJ3069 贪心法 覆盖问题

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6197   Accepted: 3...
  • zcj5027
  • zcj5027
  • 2016-01-25 21:48:35
  • 370

POJ3069 Saruman's Army

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4214   ...
  • u012846486
  • u012846486
  • 2014-10-30 11:31:32
  • 840

POJ 3069 Saruman's Army

题目: http://poj.org/problem?id=3069 题解: 1.选择起始点,从最左边的点开始,R距离内要有标记点,标记点为R距离内最右侧的点。 2.标记点R距离内的点都已...
  • u013444268
  • u013444268
  • 2014-02-18 01:46:54
  • 494

POJ Saruman's Army 3069

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4520   ...
  • ydd97
  • ydd97
  • 2015-01-28 21:27:35
  • 515

poj 3069 Saruman's army

Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep tra...
  • b2utyyomi
  • b2utyyomi
  • 2016-04-11 20:24:22
  • 92

Saruman's Army (POJ 3069)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11486  ...
  • qq1045553189
  • qq1045553189
  • 2017-10-26 14:27:05
  • 33
收藏助手
不良信息举报
您举报文章:poj 3069 Saruman's Army
举报原因:
原因补充:

(最多只允许输入30个字)