POJ_3069 Saruman's Army 【贪心】
一、题面
二、题意分析
我的理解是,可以在每个点设置一个监测点,能够监测到范围R内的所有其他点,那么问给出N个点的一维位置,需要在其中挑多少个监测点把所有点都监测到。
贪心解决:
1.先排序。
2.考虑第一个点,因为每个点是必须要监测的,那么第一个点需要被监测到,它可以是监测点,也可以是被监测点。贪心的想,为了能监测更多的点,让第一个点作为被监测的点,且是监测范围内最边界上的点。
3.现在需要考虑,当第一个点往右探测R范围时,最后一个点将是需要设置的监测点。
4.以这个监测点再往右扫R范围。
5.完成了一个监测点及其范围内点的覆盖。后面的点类似处理。
三、AC代码
#include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; const int MAXN = 1e3;
int Data[MAXN+], N, R; int solve()
{
int i = , ans = , cur;
while(i < N)
{
cur = Data[i++];
while(i < N && Data[i] <= cur+R)
i++;
cur = Data[i-];
while(i < N && Data[i] <= cur + R)
i++;
ans++;
}
return ans;
} int main()
{
freopen("input.txt", "r", stdin);
while(scanf("%d %d", &R, &N) != EOF && R != -)
{
for(int i = ; i < N; i++)
scanf("%d", &Data[i]);
sort(Data, Data+N);
printf("%d\n", solve() );
}
return ;
}
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 ...
- B - B Saruman's Army(贪心)
在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...
- poj 3069 Saruman's Army 贪心 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3069 题解 题目可以考虑贪心 尽可能的根据题意选择靠右边的点 注意 开始无标记点 寻找左侧第一个没覆盖的点 再来推算既可能靠右的标记点为一 ...
- Saruman's Army(贪心)
Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep tra ...
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- POJ3069 Saruman's Army【贪心】
Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep tra ...
- poj 3069 Saruman's Army
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8477 Accepted: 4317 De ...
随机推荐
- Codeforces 1109E 线段树
思路及博客:https://www.cnblogs.com/uid001/p/10507346.html 代码: #include <bits/stdc++.h> #define LL l ...
- Python05 函数
待更新... 2018-4-16 09:00:30
- OS线程模型
线程模型 N对1 内核线程 映射 用户进程, 用户进程里可以启多个线程 1对1 内核线程和用户线程 1对1 Linux采用这种方式 N对M 用户线程被抽象为更轻量的线程, 内核线程和轻量的线程对应 进 ...
- mybatis 框架 的应用之四(一对一 与 一对多)
lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&u ...
- ROS naviagtion analysis: costmap_2d--StaticLayer
博客转载自:https://blog.csdn.net/u013158492/article/details/50493246 从UML中能够看到,StaticLayer主要是在实现Layer层要求实 ...
- c语言学习笔记 break语句
比如 for() { for() { break; } } 那个break语句只是跳出它所在的那个for循环,不会把最外面的for循环都跳出去.
- python pipelines 用法
http://zacstewart.com/2014/08/05/pipelines-of-featureunions-of-pipelines.html http://blog.csdn.net/m ...
- PC/APP/H5三端测试的相同与不同
随着手机应用的不断状态,同一款产品的移动端应用市场占相较PC端也越来越大,那么app与PC端针对这些产品的测试有什么相同与不同之处呢?总结如下: 首先谈一谈相同之处: 一,针对同一个系统功能的测试,三 ...
- web api 设置允许跨域,并设置预检请求时间
<httpProtocol> <customHeaders> <!--响应类型 (值为逗号分隔的一个字符串,表明服务器支持的所有跨域请求的方法)--> <ad ...
- 关于MultiDataTrigger和MultiTrigger的一些注意事项
他俩有着相同的语法. 都是在conditions中编写触发条件. 因为都是同一个触发类. 在conditions中有Property和Binding这两个属性.那么这两个可以同时使用吗?当然是不可以的 ...