一、题面

POJ3069

二、题意分析

我的理解是,可以在每个点设置一个监测点,能够监测到范围R内的所有其他点,那么问给出N个点的一维位置,需要在其中挑多少个监测点把所有点都监测到。

贪心解决:

  1.先排序。

  2.考虑第一个点,因为每个点是必须要监测的,那么第一个点需要被监测到,它可以是监测点,也可以是被监测点。贪心的想,为了能监测更多的点,让第一个点作为被监测的点,且是监测范围内最边界上的点。

  3.现在需要考虑,当第一个点往右探测R范围时,最后一个点将是需要设置的监测点。

  4.以这个监测点再往右扫R范围。

  5.完成了一个监测点及其范围内点的覆盖。后面的点类似处理。

三、AC代码

 #include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; const int MAXN = 1e3;
int Data[MAXN+], N, R; int solve()
{
int i = , ans = , cur;
while(i < N)
{
cur = Data[i++];
while(i < N && Data[i] <= cur+R)
i++;
cur = Data[i-];
while(i < N && Data[i] <= cur + R)
i++;
ans++;
}
return ans;
} int main()
{
freopen("input.txt", "r", stdin);
while(scanf("%d %d", &R, &N) != EOF && R != -)
{
for(int i = ; i < N; i++)
scanf("%d", &Data[i]);
sort(Data, Data+N);
printf("%d\n", solve() );
}
return ;
}

POJ_3069 Saruman's Army 【贪心】的更多相关文章

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

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

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

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

  3. poj 3069 Saruman's Army 贪心模拟

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

  4. B - B Saruman's Army(贪心)

    在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...

  5. poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...

  6. Saruman's Army(贪心)

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

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

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

  8. POJ3069 Saruman's Army【贪心】

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

  9. poj 3069 Saruman's Army

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

随机推荐

  1. JAVA环境安装配置

    dk1.6 64位是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序. jdk1.6 64位安装教程 jdk1.6 64位JDK的安装路径:D:\Program Fi ...

  2. 在CentOS6.x下安装Compiz——桌面立方体,特效种种

    很多人貌似认为compiz必须要emerland,但事实上,没这个必要. compiz+gnome,实用,而又华丽,是个不错的选择. compiz需要显卡驱动,一般情况下不成问题(别忘了这是很新的ce ...

  3. dataframe 用法总结

    http://pda.readthedocs.io/en/latest/chp5.html data = [] 列表初始化 data = (,) data = {} 字典初始化 data = pd.D ...

  4. 第十九课 pluginlib&Nodelet

    把rgb摄像头的数据转换为laser的时候使用了Nodelet. pluginlib(插件库) 在ros中有一个plugin的包,下面是一个ROS Plugin Registration的例子 上面包 ...

  5. backquote

    character (`) A backquote or backtick. echo 'date' date echo `date` 2015年 07月 03日 星期五 16:11:13 CST j ...

  6. ios7适配--uitableviewcell选中效果

    ios7 UITableViewCell selectionStyle won't go back to blue up vote6down votefavorite 2 Xcode 5.0, iOS ...

  7. leetcode N-Queens I && N-Queens II

    第一个的代码: #include<iostream> #include<vector> using namespace std; bool isLegal(int i, int ...

  8. angular 事件绑定

    <button (click)="onClick($event)">点我</button> import { Component, OnInit } fro ...

  9. 手动给kvm虚机挂载lvm卷

    1.查看计算节点上虚机挂载的卷 [root@xgto01n010243186070 ~]# virsh domblklist instance- Target Source ------------- ...

  10. 查看linux操作系统的版本等信息

    1.查看操作系统是32位还是64位的命令: (1)getconf LONG_BIT (2)uname -a (3)uname -m (4)arch (5)file /sbin/init 2.查看操作系 ...