Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】
任意门:http://codeforces.com/contest/1066/problem/B
1 second
256 megabytes
standard input
standard output
Vova's house is an array consisting of nn elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The ii-th element of the array is 11 if there is a heater in the position ii, otherwise the ii-th element of the array is 00.
Each heater has a value rr (rr is the same for all heaters). This value means that the heater at the position pospos can warm up all the elements in range [pos−r+1;pos+r−1][pos−r+1;pos+r−1].
Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.
Vova's target is to warm up the whole house (all the elements of the array), i.e. if n=6n=6, r=2r=2 and heaters are at positions 22 and 55, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first 33 elements will be warmed up by the first heater and the last 33 elements will be warmed up by the second heater).
Initially, all the heaters are off.
But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.
Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
The first line of the input contains two integers nn and rr (1≤n,r≤10001≤n,r≤1000) — the number of elements in the array and the value of heaters.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1) — the Vova's house description.
Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
6 2
0 1 1 0 0 1
3
5 3
1 0 0 0 1
2
5 10
0 0 0 0 0
-1
10 3
0 0 1 1 0 1 0 0 0 1
3
In the first example the heater at the position 22 warms up elements [1;3][1;3], the heater at the position 33 warms up elements [2,4][2,4] and the heater at the position 66 warms up elements [5;6][5;6] so the answer is 33.
In the second example the heater at the position 11 warms up elements [1;3][1;3] and the heater at the position 55 warms up elements [3;5][3;5] so the answer is 22.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position 33 warms up elements [1;5][1;5], the heater at the position 66 warms up elements [4;8][4;8] and the heater at the position 1010 warms up elements [8;10][8;10] so the answer is 33.
题意概括:
N 个房间,标记为 1 的房间有加热器,加热范围 【 i-r+1, i+r-1】;求使用最少的加热器加热所有房间。
解题思路:
这个贪心并不难想,顺序遍历,尽可能的扩展右端点。
但wa了好多发,主要是区间连接的细节问题。
ans_ed 为已覆盖集合的最右端的下一个。
看代码吧,心好凉。
AC code:
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1e3+; int a[MAXN], l[MAXN], r[MAXN], cnt;
int ans_ed, now_st, now_ed; //当前已选集合能覆盖的最右端的下一个!!!下标!!!,当前结点能覆盖的左端,当前结点能覆盖的右端
int pre_index, now_index; //前一个可加的下标,当前可加的下标
int N, R;
int ans;
bool chch; int main()
{
scanf("%d%d", &N, &R);
bool flag = true;
cnt = ;
for(int i = ; i <= N; i++){
scanf("%d", &a[i]);
if(a[i] == ){
flag = false;
//nxt[++cnt] = i;
cnt++;
l[cnt] = i-R+; //当前可加热的左端
r[cnt] = i+R-; //当前可加热的右端
}
}
//see see
//for(int i = 1; i <= cnt; i++) printf("%d ", nxt[i]);
//puts("");
if(flag) printf("-1\n"); //没有加热器
else{
chch = false;
ans = ;
ans_ed = ; //答案最右端初始化为1
int k = ; //从头开始顺序遍历加热器
bool book = false;
do{
if(l[k] < ans_ed) //当前加热器k可连接前一个加热区,说明该点可用
{
book = true; //标记有保留考虑项
pre_index = k; //记录当前下标
k++; //讨论下一个,如果下一个可加,则当前这个不加
if(k == cnt+){ //如果当前这个为最后一个
if(ans_ed < N+){ //当前未全覆盖
ans++;
ans_ed = r[k-]+; //注意加1
}
}
continue;
} if(book){ //说明前一个可选,但是未选
if(l[k] > ans_ed){ //当前这个不可以加,说明上一个要加
now_index = pre_index;
}
else now_index = k; //当前这个可加
ans++;
ans_ed = r[now_index]+;
if(now_index == pre_index) --k;
book = false;
}
else if(l[k] > ans_ed) //当前加热器不能连接前一个加热区,说明出现断层,但是是顺序遍历下来的,说明无解
{
chch = true;break;
}
else{ //无缝连接,选!
ans++; //答案加一
ans_ed = r[k]+; //更新集合最右端
}
k++; //讨论下一个;
}while(k <= cnt);
if(ans_ed <= N || ans == ) chch = true;
if(chch) printf("-1\n");
else printf("%d\n", ans);
}
return ;
}
Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】的更多相关文章
- Codeforces Round #515 (Div. 3) B. Heaters (贪心)
题意:有\(n\)个桩子,\(1\)表示该位置有一个火炉,可以使两边距离为\(r\)的范围照亮,问最少使用多少炉子使得所有范围都被照亮. 题解:贪心,首先我们从\(r\)位置开始向左找,如果找到了就记 ...
- CodeForces Round #515 Div.3 B. Heaters
http://codeforces.com/contest/1066/problem/B Vova's house is an array consisting of nn elements (yea ...
- Codeforces Round #515 (Div. 3)
Codeforces Round #515 (Div. 3) #include<bits/stdc++.h> #include<iostream> #include<cs ...
- B. Heaters ( Codeforces Round #515 (Div. 3) )
题解:对于每个点 i 来说,从 j = i + r - 1 开始往前找,如果找到一个 a [ j ] 是 1 ,那么就把它选上,但是我们需要判断交界处,也就是如果前面选的那个可以让这个点变温暖,就不用 ...
- Codeforces Round #515 (Div. 3) 解题报告(A~E)
题目链接:http://codeforces.com/contest/1066 1066 A. Vova and Train 题意:Vova想坐火车从1点到L点,在路上v的整数倍的点上分布着灯笼,而在 ...
- Codeforces Round #202 (Div. 1) A. Mafia 贪心
A. Mafia Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
随机推荐
- oracle 基础知识(九)----SQL解析
一,解析过程 二,硬解析,软解析,软软解析 01,硬解析 将SQL语句通过监听器发送到Oracle时, 会触发一个Server process生成,来对该客户进程服务.Server process得到 ...
- cotex_A7/A9:
A7与A9的比较:A7计算性能(DMIPS)不如A9(编号越大计算能力越强),但A7功耗更先进,作为A15的协处理器.A8是单核.
- zookeeper在windows下的伪集群模式
参考:zookeeper在windows下的伪集群模式 踩到的坑: 注意windows下路径需要使用\ dataDir=D:\Program Files\Java\zookeeper-3.4.10-c ...
- jQuery遍历祖先元素:parentsUntil
Description: Get the ancestors of each element in the current set of matched elements, up to but not ...
- 1、java线程模型
要了解多线程,先需要把java线程模型搞清楚,否则有时候很难理清楚一个问题. 硬件多线程: 物理机硬件的并发问题跟jvm中的情况有不少相似之处,物理机的并发处理方案对于虚拟机也有相当大的参考意义.在买 ...
- SQL脚本整理系列一 表分区
表分区的目的: 1.把历史数据放到另外一个表里面 可以提高查询效率 当然如果经常查询历史数据和新数据的合并结果集这样做就大大的不好了 2.通过把一个表放到不同的文件,不同的文件再存储到不同的磁盘列阵中 ...
- CSS3动画属性animation的基本用法
CSS3新增了一个令人心动的属性:animation,尽管利用animation做出来的动画没有flash或者javascript做出的动画流畅绚丽,但是从代码量和浏览器性能上有着明显的优点. ani ...
- Node.js之Buffer
JavaScript 语言自身只有字符串数据类型,没有二进制数据类型.但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门存 ...
- RestTemplate请求出现401错误
最近遇到一个请求API接口总是报401 Unauthorized错误,起初是认为这个是平台返回的,后来用Postman请求,发现平台其实返回的是一串json,里面带有一些权限验证失败的消息,但到我们代 ...
- (三)HTML中的列表标签、框架集及表单标签
一.HTML的列表标签 在网页中,经常可以看到,有的内容排列如同word里面的项目编号,这就是HTML的无序排列和有序排列起到的作用.. HTML之无序排列:<ul></ul> ...