码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3069 Saruman's Army (贪心)

时间:2017-07-15 23:54:04      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:stream   sar   div   names   iostream   isp   main   alt   long   

题目大意:直线上有N个点,点i的位置是Xi,从这N个点中选取若干,给他们加上标记,对每一个点,其距离为R以内的区域内必须有被标记的点。求至少需要多少个点被标记。

 

题目思路:设最左边的点:点p的坐标为x,那么离其距离为R的点的坐标为(x+R),我们应该标记的点应为坐标最接近且小于等于(x+R)的点p,则此时【x,p+R】范围内点点均被标记。依次进行下去,直到包含所有点为止。

技术分享

 

技术分享
#include<stdio.h>
#include<queue>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define LL long long
#define MOD 100000007
#define MAXSIZE 10005

using namespace std;

int p[MAXSIZE];

int main()
{
    int r,n;
    while(scanf("%d%d",&r,&n),r!=-1 && n!=-1)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&p[i]);
        sort(p,p+n);
        int ans = 0;
        int pos1=0,pos2=0;
        while(pos1 < n)
        {
            int index = p[pos1] + r;
            pos2 = pos1;
            while(p[pos2] <= index)
                pos2++;
            ans++;
            index = p[pos2-1] + r;
            pos1 = pos2;
            while(p[pos1] <= index)
                pos1++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

POJ 3069 Saruman's Army (贪心)

标签:stream   sar   div   names   iostream   isp   main   alt   long   

原文地址:http://www.cnblogs.com/alan-W/p/7188033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!