poj 3069 Saruman's Army

poj 3069 Saruman's Army 贪心

题目链接:

http://poj.org/problem?id=3069

思路:

数组从小到大排序。从最左边的点开始,看他能够达到范围内最远的电视哪一个,找到了计数器加1,相当于在此点建立了一个标记。然后从此点继续向右延伸,看能够包括到的最远的点是哪一个。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int maxn = 1005;
int main() {
    int n,r,x[maxn],index,cnt;
    while(~scanf("%d %d",&r,&n)&&n!=-1&&r!=-1) {
        for(int i=0;i<n;++i) scanf("%d",&x[i]);
        sort(x,x+n);
        cnt=index=0;
        while(index<n) {
            int temp1=x[index++];
            while(index<n&&x[index]<=temp1+r) index++;
            int temp2=x[index-1];
            while(index<n&&x[index]<=temp2+r) index++;
            cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}
posted @ 2017-11-29 21:15  lemonsbiscuit  阅读(142)  评论(0编辑  收藏  举报