CodeForces - 1066B Heaters (水题,模拟+二分)

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. ifn=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.

Input

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.

Output

Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.

Examples

Input

6 2
0 1 1 0 0 1

Output

3

Input

5 3
1 0 0 0 1

Output

2

Input

5 10
0 0 0 0 0

Output

-1

Input

10 3
0 0 1 1 0 1 0 0 0 1

Output

3

Note

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 1010warms up elements [8;10][8;10] so the answer is 33.

 

      数值为1的地方可以点燃它并影响【i-r+1,i+r-1】的区域,问最少点燃几个地方就能影响所有位置。不能的话就输出-1;

      预处理后使用二分查找,每次查找最大的能够影响目前位置的点在哪里。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int n, m[maxn], t;
int sot[maxn], cnt;
int main() {
	while (~scanf("%d%d", &n, &t)) {
		cnt = 1;
		for (int i = 1; i <= n; i++) {
			int tmp;
			scanf("%d", &tmp);
			if (tmp){
				sot[cnt++] = i - t + 1;
			}
		}
		int now = 1, spot = 1, sum = 0;
		while (now <= n) {
			int pos = upper_bound(sot + 1, sot + cnt, now) - sot;
		//	cout << pos <<" "<<now<< endl;
			if (pos == 1 || sot[pos - 1] + 2*t - 2 < now) {
				spot = 0; break;
			}
			sum++;
			now = sot[pos - 1] + 2 * t - 1;
		}
		if (spot)
			cout << sum << endl;
		else
			cout << -1 << endl;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vova STC32系列单片机上位机进行PID(比例积分微分)控制的调试,通常涉及到以下几个步骤: 1. **PID算法理解**: PID控制器由比例(P)、积分(I)和微分(D)三个部分组成,用于实时调整系统的输出,以稳定给定的输入。 2. **硬件连接**: 首先,确保你的STC32单片机和上位机(如PC或嵌入式开发板)之间有正确连接。可能需要串口通信(例如UART),并通过编程将PID参数传输到单片机。 3. **PID库或函数**: 使用STC32的C语言库或者自定义函数实现PID算法。这可能包括设置PID系数(Kp, Ki, Kd)、计算输出值、以及更新系统状态等。 4. **初始化和配置**: 初始化PID控制器,设定死区、积分限幅和微分滤波时间常数等参数,以避免积分发散和过快的微分响应。 5. **数据采集与反馈**: 单片机需要从传感器读取实际测量值,并将此值作为PID算法的输入。 6. **单片机编程**: 编写单片机代码,调用PID函数,根据反馈值计算出控制输出,并驱动执行器。 7. **调试过程**: - 在上位机软件中,你可以使用串口监视器观察通信数据,确认PID的输入输出是否正常。 - 调整PID参数,观察系统的动态响应是否改善,是否存在振荡或过度调整。 - 如果有问题,检查硬件连接、代码逻辑和参数设置。 8. **性能优化**: 可能需要通过实验或理论分析来调整PID参数,直到达到满意的控制精度和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值