• 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
  • 相关阅读:
    代码规范总结
    git记住提交密码的技巧
    php foreach遍历
    flight学习笔记
    the resource is not on the build path of a php project
    Google安装postman插件
    PHP开发框架CodeIgniter
    eclipse中php项目开发的环境配置说明
    MyBatis入门篇
    mybatis学习(十二)——mybatis逆向工程
  • 原文地址:https://www.cnblogs.com/alan-W/p/7188033.html
Copyright © 2020-2023  润新知