POJ 3069 Saruman's Army(贪心)

Saruman's Army

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14330   Accepted: 7199

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.

Source

Stanford Local 2006

题意:给定 n 个点,第 i 个点的坐标是 xi . 现在需要对其中的一些点加上标记。 要求是 对于每一个点,其距离为R 的区域范围内必须有带有标记的点。问最少需要标记多少个点。

贪心思路,因为有一个范围R,我们可以先排序,标记一个起始位置,然后从起始位置开始寻找第一个与它的距离 大于 R 的点 (假设为 i),随后就可以对 起始位置 到 i - 2 进行标记 (只能标记到 i - 2,也就是说 i - 1 这个点不标记), 然后从 i 到 n 选择满足 与 i - 1 这个点距离小于等于R 的点进行标记,标记完后 更新起始位置。   最后整个数组  未标记的点的数量  就是 answer

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)

using namespace std;

int a[1500];
int vis[1500];

int main()
{
    int R;
    while(~scanf("%d",&R)){
        INT(n);
        if(n == -1 && R == -1) break;
        rep(i,0,n) scanf("%d",&a[i]);
        sort(a,a + n); a[n] = 0x3f3f3f3f;
        clr(vis,0);
        int flag = 0;
        int begi = 0;
        rep(i,0,n + 1){
            if(vis[i]) continue;
            if(a[i] - a[begi] > R){
                flag = 1;
            }
            if(flag){
                int cnt = i - 2;
                //debug(cnt);
                //debug(begi);
                while(cnt >= begi)
                    vis[cnt --] = 1;
                cnt = i;
                while(cnt < n && a[cnt] - a[i - 1] <= R)
                    vis[cnt ++] = 1;
                begi = cnt;
                flag = 0;
            }
        }
        int ans = 0;
        rep(i,0,n) if(!vis[i]) ++ ans;
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/82466778