POJ 3069 Saruman's Army 贪心

来源:互联网 发布:linux查看文件类型 编辑:程序博客网 时间:2024/05/16 08:08
Saruman's Army
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5897 Accepted: 3009

Description

minions

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 310 20 2010 770 30 1 7 15 20 50-1 -1

Sample Output

24

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

[Submit]   [Go Back]   [Status]   [Discuss]

抽象成一维数轴上的若干个点,标记一个点那么这个点可以辐射到len范围(左右都可以)距离内的点。给出点的影响范围len问如何标记最少的点使得全部的点都被辐射到。

先从最左边的点找到离它最远且能辐射到它的点,然后找到这个点能影响到的最右边的点,这样就找的了一个点然后类推。

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈#include <map>#include <set>#include <queue>#include <cmath>#include <stack>#include <cctype>#include <cstdio>#include <cstring>#include <stdlib.h>#include <iostream>#include <algorithm>#define rd(x) scanf("%d",&x)#define rd2(x,y) scanf("%d%d",&x,&y)#define rds(x) scanf("%s",x)#define rdc(x) scanf("%c",&x)#define ll long long int#define maxn 100005#define mod 1000000007#define INF 0x3f3f3f3f //int 最大值#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)#define MT(x,i) memset(x,i,sizeof(x))#define PI  acos(-1.0)#define E  exp(1)using namespace std;int len,n,cnt,st,ed;int a[maxn];int main(){    while(rd2(len,n)&&(len+n!=-2)){        FOR(i,1,n)rd(a[i]);        sort(a+1,a+1+n);cnt=0;        FOR(i,1,n){            st=a[i];            while(i<=n&&a[i]<=st+len)i++;            ed=a[i-1];            while(i<=n&&a[i]<=ed+len)i++;            i--;            cnt++;        }        printf("%d\n",cnt);    }    return 0;}


0 0
原创粉丝点击