简单贪心。

从左边开始,找 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. centos7和windows7双系统安装

    前些天安装了双系统(centos7+win7),其实网上关于这类的教程很多,这篇日志也只是针对本人安装过程中遇到的一些问题进行说明.我是按照先安装win7再安装centos7的顺序. 1.关于分区: ...

  2. mysql基本内容学习过程

    mysql数据库的基本操作: , 数据库的登录:mysql -u 用户名(root) -p密码 -P (端口) -h服务器名(本地表示:127.0.0.1) . 更改数据库显示:mysql -u ro ...

  3. HDU 4540 威威猫系列故事——打地鼠(DP)

    点我看题目 题意 :中文题,不详述. 思路 : 状态转移方程 dp[ i ][ j ] = dp[i-1][k] + fabs(a[ i ][ j ]-a[i-1][k]) ; dp[i][j]代表的 ...

  4. PHP unlink() 函数

    定义和用法 unlink() 函数删除文件. 若成功,则返回 true,失败则返回 false. 语法 unlink(filename,context) 参数 描述 filename 必需.规定要删除 ...

  5. UVA 515 King

    差分约束系统的第一个题目,看了落花大神的博客后,对差分约束有了一定了解,关键在于建图,然后就是判断是否存在负权回路. 关于差分约束系统的解释详见维基百科:http://zh.wikipedia.org ...

  6. GetActiveView 返回 NULL 为 MDI 框架窗口

    blog 在 MDI 应用程序中,MDI 主框架窗口(CMDIFrameWnd) 不具有与其相关联的视图.相反,每个单独的子窗口(CMDIChildWnd)具有与之关联的一个或多个视图.因此,对 MD ...

  7. BZOJ2843: 极地旅行社

    2843: 极地旅行社 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 90  Solved: 56[Submit][Status] Descripti ...

  8. keychain中我的证书与证书, p12与pem, apns, 推送

    如果在A电脑上生成 的apns, 到B电脑上导入此文件时, 在 "我的证书"中找不到, 要去"证书"才能找到, 因为 创建时的csr不是本机生成的. 那这样的话 ...

  9. "is not on any development teams " Xcode的账号错误问题

    "***@*** is not on any development teams " Xcode现在在"Accounts"中时不时会出现这个提示. 但其他按钮都 ...

  10. js中test,exec和match方法

    test test 返回 Boolean,查找对应的字符串中是否存在模式. var str = "1a1b1c";var reg = new RegExp("1.&quo ...