Saruman's Army (POJ 3069)
直线上有N个点。点i的位置是Xi。从这N个点中选择若干个,给它们加上标记。对每一个点,其距离为R以内的区域里必须又带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点)。在满足这个条件的情况下,希望能为尽可能少的点添加标记。请问至少要有多少点被加上标记?
#include "iostream"
#include "algorithm"
using namespace std; const int MAX_N = 1000;
int N=6, R=10;
int X[MAX_N] = {1,7,15,20,30,50}; void solve() {
sort(X,X+N);
int i = 0, ans = 0;
while (i<N)
{
//S为最左侧没有被覆盖的点
int S = X[i++];
//一直向右前进直到距离S的距离大于R的点
while (i<N && X[i]<=S+R) i++;
//P是新加上标记的点的位置
int P = X[i-1];
//一直向右前进找到距离P的距离大于R的点
while (i<N && X[i]<=P+R) i++;
ans++;
}
cout << ans << endl;
} int main() {
solve();
system("pause");
return 0;
}#include "iostream"
#include "algorithm"
using namespace std; const int MAX_N = 1000;
int N=6, R=10;
int X[MAX_N] = {1,7,15,20,30,50}; void solve() {
sort(X,X+N);
int i = 0, ans = 0;
while (i<N)
{
//S为最左侧没有被覆盖的点
int S = X[i++];
//一直向右前进直到距离S的距离大于R的点
while (i<N && X[i]<=S+R) i++;
//P是新加上标记的点的位置
int P = X[i-1];
//一直向右前进找到距离P的距离大于R的点
while (i<N && X[i]<=P+R) i++;
ans++;
}
cout << ans << endl;
} int main() {
solve();
system("pause");
return 0;
}
Saruman's Army (POJ 3069)的更多相关文章
- Q - Saruman's Army POJ - 3069
Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...
- 贪心-Saruman‘s Army POJ - 3069
万恶之源 目录 题意 思路 贪心的原则是什么呢? 错解 正解 代码实现 书上的代码 我的代码 比较一下 问题 题意 给定若干个点的坐标,与范围R.每个点可以选择是否标记,标记后这个点的左右范围R内的所 ...
- 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:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- 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: 65536K Total Submissions: 8477 Accepted: 4317 De ...
- POJ 3069:Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13242 Accepted: 6636 D ...
- 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: 6688 Accepted: 3424 De ...
随机推荐
- Java中4种权限的理解
1. 包访问权限 (1)包的理解:将一组相关的.有意义的类文件组织在一起(即相应的.java文件放在一个文件夹下)就构成了包或者类库.(每个类文件的开头都包含一个所属包的声明“package pac ...
- C#_Test
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Plus ...
- Java 实现Md5算法
package other; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/* * ...
- Xcode Build Setting Reference
https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/ ...
- 美丽的for循环语句
美丽的for循环语句 题目:用for循环语句实现四个三角形不同的形状. 图案: ---------------第一个三角形图形形状----------------**********第二个三 ...
- ETL-Career RoadMap
RoadMap: 1.Tester:sql的单体或批处理测试: 2. Application Developer 2.1 批处理手动工具(如何使用.如何调度批处理.如何生成批处理脚本): 2.2 批处 ...
- mac os使用lsusb命令和连接未知的Android设备
今天在mac上连接一个android设备发现连不上,adb devices看不到设备.于是想用lsusb命令看下,结果发现Mac居然没有这个命令,于是网上搜了下.发现了以下的命令system_prof ...
- 数据库(SQLITE3函数总结): sqlite3_open, sqlite3_exec, slite3_close,sqlite3_prepare_v2,sqlite3_column_text,
Sqlite3 的确非常好用.小巧.速度快.近期研究它,有一些收获,这里把我对 sqlite3 的研究列出来,以备忘记. 导入SQLLite library并引入头文件. libsqlite3.dyl ...
- MAVEN Scope使用
在Maven的依赖管理中,经常会用到依赖的scope设置.这里整理下各种scope的使用场景和说明,以及在使用中的实践心得.Scope的使用场景和说明1.compile编译范围,默认scope,在工程 ...
- HTML与CSS入门——第五章 使用文本块和列表
知识点: 1.在页面上对齐文本的方法 2.三种HTML列表的使用方法 3.在列表中放置列表的方法 5.1 在页面上对齐文本: 父元素内子元素文本的居中:在控制父元素的text-align:center ...