zoukankan      html  css  js  c++  java
  • poj 3069 Saruman's Army

    题意:

    在一条线上,有若干个点,在每个点的r范围内必须有一个被标记的点,问最少标记多少个点。

    思路:

    贪心,在一个未标记点向右找一个远的点为标记点,再在这个点的基础上找一个最近的不能被标记的点,就是下一个需要处理的点。

    坑:

    有lower_bound比较难写Orz。

    代码:

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 using namespace std;
     5 
     6 int x[1005];
     7 
     8 int main()
     9 {
    10     int r,n;
    11 
    12     while (scanf("%d%d",&r,&n))
    13     {
    14         if (r == -1 && n == -1) break;
    15 
    16         for (int i = 0;i < n;i++)
    17             scanf("%d",&x[i]);
    18 
    19         sort(x,x+n);
    20 
    21         n = unique(x,x+n) - x;
    22 
    23         int ans = 0;
    24 
    25         int i = 0;
    26 
    27         while (i < n)
    28         {
    29             int cur = x[i];
    30 
    31             while (i < n && cur + r >= x[i]) i++;
    32 
    33             ans++;
    34 
    35             cur = x[i-1];
    36 
    37             while (i < n && cur + r >= x[i]) i++;
    38         }
    39 
    40         //for (int i = 0;i < n;i++) printf("%d ",x[i]);
    41 
    42         printf("%d
    ",ans);
    43     }
    44 
    45     return 0;
    46 }
  • 相关阅读:
    MongoDB中常用的find
    MongoDB文档的增删改操作
    我的notepad++
    MongoDB入门知识
    Python基础5-常用模块
    Python基础4
    Python基础3(2017-07-20)
    Python基础2(2017-07-18)
    Python基础1(2017-07-16)
    Python简介(2017-07-16)
  • 原文地址:https://www.cnblogs.com/kickit/p/8027186.html
Copyright © 2011-2022 走看看