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 ...
随机推荐
- jsp中导入导出excel,ssh框架
导入Excel:jsp中 <form action="user_importTradingMoney" enctype="multipart/form-data&q ...
- Java 截取反斜杠--java使用split拆分特殊字符
Java 截取反斜杠 replaceAll和split (“\”) 问题解决办法 xxx.split("\\") 显然得不到想要的结果 正确方法 xxx.split("\ ...
- mac 环境下使用virtual box 虚拟机(win7)与主机之间互相ping通
首先选择virtual box设置网络连接方式为网桥 混杂模式设置为全部允许 如下图: 进入虚拟机把虚拟机IP设置和主机在一个网段.如主机是192.168.1.100虚拟机可以设置为192.168.1 ...
- java并发编程--Runnable Callable及Future
1.Runnable Runnable是个接口,使用很简单: 1. 实现该接口并重写run方法 2. 利用该类的对象创建线程 3. 线程启动时就会自动调用该对象的run方法 通常在开发中结合Execu ...
- 高速排序-c++(分别用数组和容器实现)
/********************************************************************** *版权全部 (C)2014, cheng yang. * ...
- sql server里一些常用的查询
查看表的创建和更改时间: select * from sys.tables 查询数据库的创建时间: select * from sys.databases where name in ('数据 ...
- vb的LINQ实现
vb实现LINQ非常简单的例子: Dim numbers() As Integer = {1, 2, 3, 4, 5, 6, 7} Dim allNumbers = From number In nu ...
- Ajax客户登陆验证
服务器端操作方便之处我就不吹了,地球人都知道,它最烦莫过于页面刷新,头都被刷晕了,而且他在刷新的时候,还触发服务器端的事件,现在Ajax的出现,他们的结合是发展的必然! 一.介绍一下Aj ...
- NHibernate之映射文件配置说明(转载1)
源博客:http://www.cnblogs.com/kissdodog/archive/2013/02/21/2919886.html 1. hibernate-mapping 这个元素包括以下可选 ...
- (转)asp.net动态设置标题title 关键字keywords 描述descrtptions
方法一 if (!IsPostBack){//Page title网页标题Page.Title = “我的网站标题”;//须将网页head标签设成服务器控件模式,即<head runat=&qu ...