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, ...
 
随机推荐
- sql常识
			
1.UNION与UNION ALL的区别UNION去重且排序UNION ALL不去重不排序2.sql语句or与union all的执行效率比较:union all>union> in &g ...
 - Linux进程的虚拟存储器知识点
			
http://blog.csdn.net/yxccc_914/article/details/52665713 用libreoffice画表真时有点蛋疼,效率很低.. 深入理解计算机系统->虚拟 ...
 - fold - 折叠输入行, 使其适合指定的宽度
			
总览 (SYNOPSIS) ../src/fold [OPTION]... [FILE]... 描述 (DESCRIPTION) 折叠(wrap) 每个 文件 FILE 中 的 输入行 (缺省为 标准 ...
 - HP刀片服务器安装系统步骤
			
原文链接 HP刀片服务器安装系统步骤: HP刀片使用一个叫OA(Onboard Administrator)的管理平台统一进行管理,具有很高的集成性,同时使用起来也很简单方便,通过IE浏览器输入IP地 ...
 - 织梦dedecms出现系统基本参数空白或显示Call to undefined function make_hash()
			
织梦dedecms出现系统基本参数空白或显示Call to undefined function make_hash() 最新的织梦版本(2018-01-09)修改了include文件夹中的commo ...
 - Cookie 与 Session 的区别
			
Cookie与Session的区别 cookie的简介 cookie是Web服务器保存在客户端的一系列文本信息 cookie的作用 对特定对象的追踪 统计网页浏览次数 简化登录 安全性能:容易信息泄露 ...
 - 讲说问题:|和||的区别以及&和&&的区别。2、Java中的数据类型分几类?基本数据类型有哪些?
			
|和||的区别以及&和&&的区别. |或 为或运算 判断为逻辑或 || 为短路或 只有逻辑判断 当左侧为真不再继续判断 &与 为与运算 判断为逻辑与 && ...
 - java链接MySQL数据库时使用com.mysql.jdbc.Connection的包会出红线问题 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
			
package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connecti ...
 - PAT 乙级 1015
			
题目 题目地址:PAT 乙级 1015 题解 常规题,难点在于理清楚排序规则,通过比较简洁的方式进行编码: 在这里我选择使用vector进行存储,并使用sort方法排序,因为本题不是简单按照大小排序, ...
 - (79)zabbix key总是not supported的解决方法
			
zabbix定义好key之后,总是会出现Not supported,看到这个问题,大家不用着急,问题其实很容易解决,首先鼠标点击当前key的大红叉上,会显示出报错内容. 常见的有: 1. zabbix ...