zoukankan      html  css  js  c++  java
  • POJ 3069 Saruman's Army (贪心)

    题目大意:直线上有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
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    0430
    hlg1306再遇攻击--射线法判断点是否在多边形内部
    hlg1429凸多边形 二分+叉积
    计算几何
    像个孩子一样
    i am so happy
    hdu3371Connect the Cities---最小生成树kruskal
    hlg1339Touring DIJ+堆优化
    hdu3790最短路问题
    Lucky Goddess
  • 原文地址:https://www.cnblogs.com/alan-W/p/7188033.html
Copyright © 2011-2022 走看看