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
  • 相关阅读:
    xplan.sql(本脚本获取执行计划显示执行顺序)
    闪回查询(SELECT AS OF)
    闪回事务查询
    闪回版本查询
    闪回数据库
    shell循环语句
    前后端交互json字符串
    element vuex 语音播报
    highcharts中数据列点击事件
    highcharts为X轴标签添加链接
  • 原文地址:https://www.cnblogs.com/alan-W/p/7188033.html
Copyright © 2011-2022 走看看