简单贪心。

从左边开始,找 r 以内最大距离的点,再在该点的右侧找到该点能覆盖的点。如图。

自己的逻辑有些混乱,最后还是参考书上代码。(《挑战程序设计》 P46)

/******************************************
Problem: 3069 User:
Memory: 668K Time: 16MS
Language: G++ Result: Accepted
******************************************/
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std; int a[1005]; int main()
{
int r, n, i;
while (scanf("%d%d", &r, &n) == 2) {
if (r == -1) break;
for (i = 0; i < n; ++i)
scanf("%d", &a[i]);
sort(a, a + n);
int ans;
ans = i = 0;
while(i < n) {
int s = a[i++]; // s is the first point uncovered
while (i < n && a[i] <= s + r) ++i; // s can cover range
int p = a[i - 1]; // p should be covered
while (i < n && a[i] <= p + r) ++i; // p can cover range
++ans; // the point is p
}
printf("%d\n", ans);
}
return 0;
}

  

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. poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》

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

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

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

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

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

  7. poj 3069 Saruman's Army

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

  8. POJ 3069 Saruman's Army (模拟)

    题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...

  9. POJ 3069——Saruman's Army(贪心)

    链接:http://poj.org/problem?id=3069 题解 #include<iostream> #include<algorithm> using namesp ...

随机推荐

  1. Oracle 分析函数之聚集函数(MAX、MIN、AVG和SUM)

    MAX 查找组中表达式的最大值 MAX(COL ) OVER ( [ <partition_by_clause> ] < order_by_clause > )MIN 查找组中 ...

  2. 【jpa】 引用包的问题

    Hibernate使用Annotation(注解)需加                         hibernate-jpa-2.0-api-1.0.0.Final.jar Hibernate3 ...

  3. linux sed 批量替换多个文件中的字符

    格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` linux sed 批量替换多个文件中的字符串 sed -i "s/oldst ...

  4. UI表单

    Monk.UI表单美化插件诞生记!   阅读目录 背景 预览效果 表单组件 普通文本框 多行文本框 复选框 切换滑块 单选框 下拉选择框 数字输入框 时间选择 文件选择 显示文本 按钮 开源地址 背景 ...

  5. CSS3中translate、transform和translation的区别和联系

    translate:移动,transform的一个方法               通过 translate() 方法,元素从其当前位置移动,根据给定的 left(x 坐标) 和 top(y 坐标) ...

  6. 被FBI点名的中国黑客-Lion

    网名:Lion(狮子) 真实姓名:林勇 QQ:21509     简介:红客联盟创始人(该组织在2001年5月的黑客大战中一举成名,会员人数最多时达到6万,很有影响力),现在安氏因特网安全系统(中国) ...

  7. hdu 3487

    splay #include<cstdio> #include<cstring> #include<iostream> #include<algorithm& ...

  8. csuoj 1355: 地雷清除计划

    这是一个非常神奇的题: 感觉像一个模拟搜索: 但是竟然可以用网络流来解决: 直接粘题解把: 如果不能走通的话,必然说明能够从右上角(图外面)沿雷“跳” ,一直可以“跳”左下角(图外面) ,因此建好图之 ...

  9. 定义设置颜色的RGB值的宏

    //定义设置颜色的RGB值的宏 #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha ...

  10. Android-UGallery

    public class UGallery extends Gallery { public UGallery(Context context, AttributeSet attrs) { super ...