CodeForces Round #515 Div.3 B. Heaters
http://codeforces.com/contest/1066/problem/B
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
代码:
#include <bits/stdc++.h>
using namespace std; int N, R;
int pos[1010]; int main() {
scanf("%d%d", &N, &R);
for(int i = 0; i < N; i ++)
scanf("%d", &pos[i]); vector<int> T;
int st = -1;
for(int i = N - 1; i >= 0; i --) {
if(pos[i] == 1 && i < R) {
st = i;
break;
}
} if(st == -1) {
printf("-1\n");
return 0;
} if(N - st <= R) {
printf("1\n");
return 0;
} T.push_back(st); while(true) {
// cout << "!!!" << endl;
bool flag = false;
for(int i = N - 1; i >= st + 1; i --) {
if(i - st + 1 <= 2 * R && pos[i]) {
st = i;
flag = true;
break;
}
}
if(!flag) {
break;
} T.push_back(st); if(N - st <= R) {
printf("%d\n", T.size());
return 0;
}
} printf("-1\n"); return 0;
}
CodeForces Round #515 Div.3 B. Heaters的更多相关文章
- Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】
任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...
- Codeforces Round #515 (Div. 3) B. Heaters (贪心)
题意:有\(n\)个桩子,\(1\)表示该位置有一个火炉,可以使两边距离为\(r\)的范围照亮,问最少使用多少炉子使得所有范围都被照亮. 题解:贪心,首先我们从\(r\)位置开始向左找,如果找到了就记 ...
- 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 #515 (Div. 3) E. Binary Numbers AND Sum
E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...
- CodeForces Round #515 Div.3 D. Boxes Packing
http://codeforces.com/contest/1066/problem/D Maksim has nn objects and mm boxes, each box has size e ...
- CodeForces Round #515 Div.3 C. Books Queries
http://codeforces.com/contest/1066/problem/C You have got a shelf and want to put some books on it. ...
- CodeForces Round #515 Div.3 A. Vova and Train
http://codeforces.com/contest/1066/problem/A Vova plans to go to the conference by train. Initially, ...
随机推荐
- java 的http请求方式:HttpURLConnection和HttpClient
1.要了解一些概念性的东西,比如Http的协议以及协议头等一些东东 2.HttpURLConnection一般步骤:创建URL对象==>获取URL的HttpURLConnection对象实例== ...
- V2EX 神回复 #1
"抠图"用英文怎么说 今天突然被"抠图"这个单词给难住了," image segmentation "," image cut & ...
- NYOJ-255-C小加 之 随机数
原题链接 C小加 之 随机数 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 ACM队的“C小加”同学想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用 ...
- java模式
模式(Pattern) 模式(Pattern)的概念最早由建筑大师Christopher Alexander于二十世纪七十年代提出,应用于建筑领域,八十年代中期由Ward Cunningham和Ken ...
- 【BZOJ1216】[HNOI2003] 操作系统(堆+模拟)
点此看题面 大致题意: 有\(n\)个任务,每个任务有4个属性:编号.到达时间.执行时间和优先级.每个单位时间,会执行一个优先级最高(若有多个优先级最高的,就先执行到达时间较早的)的任务,请你按完成的 ...
- Python实现购物小程序
一.需求 1.登录 { ‘xxx1’:{'passwd':'123','role':1,'moeny':10000,"carts":['mac']}, 'xxx1':{'passw ...
- HTML 5.1 的 14 个新特性(含使用案例)
HTML5 属于万维网联盟 (W3C), 这个组织为整个网络界提供了标准,如此形成的协议可在全世界通行.在 2016 年 11 月, W3C 对长期行使的 HTML 5 标准进行了更新,它是2年内的第 ...
- java设计模式——建造者模式
一. 定义与类型 定义:将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示 用户只需制定需要建造的类型就可以得到它们,建造过程以及细节不需要知道 类型:创建型 建造者模式与工厂模 ...
- 操作系统(2)_进程管理_李善平ppt
所有程序都有CPU和io这两部分,即使没有用户输入也有输出. CPU最好特别忙,io空闲无所谓. 程序/数据/状态 三个维度来看进程. 等待的资源可能是io资源或者通信资源(别的进程的答复). 一个进 ...
- UOJ#386. 【UNR #3】鸽子固定器(链表)
题意 题目链接 为了固定S**p*鸽鸽,whx和zzt来到鸽具商店选购鸽子固定器. 鸽具商店有 nn 个不同大小的固定器,现在可以选择至多 mm 个来固定S**p*鸽鸽.每个固定器有大小 sisi 和 ...