题目: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.
题目要求的是最小电量的heater的数量,那我们对于每一个点都处理出他前面有哪个可以让他变热,以及后面最远哪个可以让它变热,最后扫一遍就行了。由于n和r都小于1000,直接暴力。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int late[N];
bool has[N];
int pre[N];
int main()
{
int n, r;
scanf("%d%d", &n, &r);
for (int i = 1; i <= n; i++)
{
scanf("%d", &has[i]);
late[i] = has[i]? i : 0;
if (has[i])
{
pre[i] = i;
for (int j = 1; j < r; j++)
{
if (i - j <= 0)
break;
late[i - j] = i;
}
for (int j = 1; j < r; j++)
{
if (i + j > n)
break;
pre[i + j] = i;
}
}
}
int cnt = 0;
int now = 1;
bool can = 1;
while (now <= n)
{
if (!late[now])
{
if (!pre[now])
{
can = 0;
break;
}
cnt++;
now = pre[now];
now += r;
continue;
}
now = late[now];
cnt++;
now += r;
}
if (!can)
{
puts("-1");
return 0;
}
printf("%d\n", cnt);
return 0;
}