poj 3069 Saruman's Army (贪心)
简单贪心。
从左边开始,找 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 (贪心)的更多相关文章
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- POJ 3069 Saruman's Army(贪心)
Saruman's Army Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3069 Saruman's Army 贪心模拟
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18794 Accepted: 9222 D ...
- poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- poj 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
- POJ 3069 Saruman's Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
- POJ 3069——Saruman's Army(贪心)
链接:http://poj.org/problem?id=3069 题解 #include<iostream> #include<algorithm> using namesp ...
随机推荐
- NGUI系列教程三
接下来我们再来看Progress Bar和Slider,对比参数我们可以发现,Progress Bar和slider的明显区别在于slider多一个Thumb选项,这里的Thumb就是我们拖动的时候点 ...
- Tesseract——OCR图像识别 入门篇
Tesseract——OCR图像识别 入门篇 最近给了我一个任务,让我研究图像识别,从我们项目的screenshot中识别文字信息,so我开始了学习,与大家分享下. 我看到目前OCR技术有很多,最主要 ...
- failed with: java.lang.NullPointerException
failed with: java.lang.NullPointerException 需要在nutch的配置文件 'conf/nutch-site.xml'. 里设置如下,不然就报上面的错误了. 当 ...
- codeforces 392A Blocked Points
我的方式是用暴力的方法找到每一行每一列的边界点: 但是有大神直接用一个公式解决了:floor(n*sqrt(2))*4: 想了很久还是不理解,求各路大神指点! #include<iostream ...
- RadioGroup 的 RadioButton 选择改变字体颜色和背景颜色
RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...
- PHP strip_tags() 函数
定义和用法 strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签. 语法 strip_tags(string,allow) 参数 描述 string 必需.规定要检查的字符串. ...
- configure脚本参数介绍
configure脚本有大量的命令行选项. 下面对每一个选项进行简略的介绍: --cache-file=FILE'configure' 会在你的系统上测试存在的特性(或者bug!).为了加速随后进行的 ...
- easyui源码翻译1.32--ComboTree(树形下拉框)
前言 扩展自$.fn.combo.defaults和$.fn.tree.defaults.使用$.fn.combotree.defaults重写默认值对象.下载该插件翻译源码 树形下拉框结合选择控件和 ...
- delphi xe3的helper语法 good
在C#中有一个很有用的helper保留字,它可以让我们对已有的类添加额外功能,当时就在想delphi有这个保留字就好了,这样许多控件就不需要继承重写了.后来delphi 果然有了这个语法,到delph ...
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...