简单贪心。

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

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

  1. /******************************************
  2. Problem: 3069 User:
  3. Memory: 668K Time: 16MS
  4. Language: G++ Result: Accepted
  5. ******************************************/
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. int a[1005];
  13.  
  14. int main()
  15. {
  16. int r, n, i;
  17. while (scanf("%d%d", &r, &n) == 2) {
  18. if (r == -1) break;
  19. for (i = 0; i < n; ++i)
  20. scanf("%d", &a[i]);
  21. sort(a, a + n);
  22. int ans;
  23. ans = i = 0;
  24. while(i < n) {
  25. int s = a[i++]; // s is the first point uncovered
  26. while (i < n && a[i] <= s + r) ++i; // s can cover range
  27. int p = a[i - 1]; // p should be covered
  28. while (i < n && a[i] <= p + r) ++i; // p can cover range
  29. ++ans; // the point is p
  30. }
  31. printf("%d\n", ans);
  32. }
  33. return 0;
  34. }

  

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. Sublime Text 3之Package Control 安装

    1.通过快捷键 ctrl+` 或者 View > Show Console 打开控制台,然后粘贴以下安装代码: import urllib.request,os; pf = 'Package C ...

  2. .NET 轻量级 ORM 框架 - Dapper 介绍

    Dapper简单介绍: Dapper is a single file you can drop in to your project that will extend your IDbConnect ...

  3. 转载:STM32之中断与事件---中断与事件的区别

    这张图是一条外部中断线或外部事件线的示意图,图中信号线上划有一条斜线,旁边标志19字样的注释,表示这样的线路共有19套.图中的蓝色虚线箭头,标出了外部中断信号的传输路径,首先外部信号从编号1的芯片管脚 ...

  4. 网络请求 post 的接受请求头的代理方法

    接收响应头 content-Length content-Type - (void)connection:(NSURLConnection *)connection didReceiveRespons ...

  5. Automotive Security的一些资料和心得(8):Hardware Security Module (HSM)

    1. Introduction - 保护软件的安全性措施,作为值得信赖的安全锚,- 安全地生成,存储和处理安全性关键材料屏蔽任何潜在的恶意软件,?- 通过运用有效的限制硬件篡改攻击的可能性篡改保护措施 ...

  6. C++ 数据类型及相关问题 及输出精度控制

    1.有哪些数据类型? 2.数据类型在不同的编译器会有不同的位宽,如何得知? 使用如下命令: cout<<sizeof(int)<<endl; cout<<sizeo ...

  7. 安卓接入ShareSDK问题

    平台图标错乱原因  导出的jar包 包括了 style.class R$attr.class MainActivity.class R$color.class R$drawable.class R$s ...

  8. java Date和String转换总结

    java.util.Date和String类型的转换是非常常用的,现在总结一下: 1. Date转换为String //Date --->String DateFormat dft = new ...

  9. Codeforces Round #238 (Div. 1)

    感觉这场题目有种似曾相识感觉,C题还没看,日后补上.一定要坚持做下去. A Unusual Product 题意: 给定一个n*n的01矩阵,3种操作, 1 i 将第i行翻转 2 i 将第i列翻转 3 ...

  10. 转载爱哥自定义View系列--Paint详解

    上图是paint中的各种set方法 这些属性大多我们都可以见名知意,很好理解,即便如此,哥还是带大家过一遍逐个剖析其用法,其中会不定穿插各种绘图类比如Canvas.Xfermode.ColorFilt ...