直线上有N个点。点i的位置是Xi。从这N个点中选择若干个,给它们加上标记。对每一个点,其距离为R以内的区域里必须又带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点)。在满足这个条件的情况下,希望能为尽可能少的点添加标记。请问至少要有多少点被加上标记?

#include "iostream"
#include "algorithm"
using namespace std; const int MAX_N = 1000;
int N=6, R=10;
int X[MAX_N] = {1,7,15,20,30,50}; void solve() {
sort(X,X+N);
int i = 0, ans = 0;
while (i<N)
{
//S为最左侧没有被覆盖的点
int S = X[i++];
//一直向右前进直到距离S的距离大于R的点
while (i<N && X[i]<=S+R) i++;
//P是新加上标记的点的位置
int P = X[i-1];
//一直向右前进找到距离P的距离大于R的点
while (i<N && X[i]<=P+R) i++;
ans++;
}
cout << ans << endl;
} int main() {
solve();
system("pause");
return 0;
}#include "iostream"
#include "algorithm"
using namespace std; const int MAX_N = 1000;
int N=6, R=10;
int X[MAX_N] = {1,7,15,20,30,50}; void solve() {
sort(X,X+N);
int i = 0, ans = 0;
while (i<N)
{
//S为最左侧没有被覆盖的点
int S = X[i++];
//一直向右前进直到距离S的距离大于R的点
while (i<N && X[i]<=S+R) i++;
//P是新加上标记的点的位置
int P = X[i-1];
//一直向右前进找到距离P的距离大于R的点
while (i<N && X[i]<=P+R) i++;
ans++;
}
cout << ans << endl;
} int main() {
solve();
system("pause");
return 0;
}

Saruman's Army (POJ 3069)的更多相关文章

  1. Q - Saruman's Army POJ - 3069

    Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...

  2. 贪心-Saruman‘s Army POJ - 3069

    万恶之源 目录 题意 思路 贪心的原则是什么呢? 错解 正解 代码实现 书上的代码 我的代码 比较一下 问题 题意 给定若干个点的坐标,与范围R.每个点可以选择是否标记,标记后这个点的左右范围R内的所 ...

  3. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

  4. POJ 3069 Saruman's Army(贪心)

     Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  5. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  6. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  7. POJ 3069:Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13242   Accepted: 6636 D ...

  8. poj 3069 Saruman's Army(贪心)

    Saruman's Army Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  9. POJ 3069 Saruman&#39;s Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6688   Accepted: 3424 De ...

随机推荐

  1. Spring中Ioc容器的注入方式

    1 通过setter方法注入 bean类: package com.test; public class UserServiceImplement implements IUserService { ...

  2. hadoop 常用命令

    hdfs dfs -mkdir -p /usr/input/hot hdfs dfs -ls / hdfs dfs -ls /usr/input hdfd dfs -cat /usr/ouput/ho ...

  3. Java编程思想-第四章练习题

    练习1:写一个程序,打印从1到100的值 public class Print1To100{ public static void main(String args[]){ for(int i = 1 ...

  4. javascript 中 nodeValue 、value 、text 的区别

     nodeValue: 属性设置或者返回某节点的值: 也可以改变某个文本节点的值, node.nodeValue eg: 如何获取p元素里面的文本内容 <p id="demo" ...

  5. java关键字-transient

    java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持. Java的serialization提供了一种持久化对象实例的机制.当持久化对象时,可能有 ...

  6. [转]Android Volley完全解析(四),带你从源码的角度理解Volley

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17656437 经过前三篇文章的学习,Volley的用法我们已经掌握的差不多了,但是 ...

  7. Zend Framework学习日记(2)--HelloWorld篇(转)

    Zend Framework学习日记(2)--HelloWorld篇 这一篇主要演示如何用zf命令行工具建立一个基于Zend Framework框架的工程,也是我初学Zend Framework的小练 ...

  8. Electron开发环境部署

    Electron开发环境部署 安装node.js 可以从node.js官方网站上获取安装包,并进行安装,安装完可以通过 ndoe -v 指令进行版本查看. 本文的开发环境为node.js 4.4.5. ...

  9. 自定义控件 带描边的TextView

    使用 public class MainActivity extends Activity {     @Override     protected void onCreate(Bundle sav ...

  10. MVC文件上传 - 使用Request.Files上传多个文件

    控制器 1: using System; 2: using System.Collections.Generic; 3: using System.IO; 4: using System.Linq; ...