description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of employees.
The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.
-----Output-----
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
-----Examples-----
Input
5
DDRRR
Output
D
Input
6
DDRRRR
Output
R
-----Note-----
Consider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
|
n = int(input())
s = input()
t = []
d = r = 0
for i in s:
t.append(i)
while len(t) != 1:
for i in range(len(t)):
if t[i] == "R" and d == 0:
r += 1
elif t[i] == "D" and r == 0:
d += 1
elif t[i] == "R" and d != 0:
d -= 1
t[i] = 0
else:
r -= 1
t[i] = 0
k = []
for i in t:
if i != 0:
k.append(i)
t = k[:]
k.sort()
if len(k) != 1:
if k[0] == k[len(k) - 1]:
break
print(t[0])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
|
There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of employees.
The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.
-----Output-----
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
-----Examples-----
Input
5
DDRRR
Output
D
Input
6
DDRRRR
Output
R
-----Note-----
Consider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
|
n = input()
a = input()
cnt = 0
while len(set(a)) == 2:
new_a = []
for i in range(len(a)):
if a[i] == "D":
if cnt >= 0:
new_a.append(a[i])
cnt += 1
else:
if cnt <= 0:
new_a.append(a[i])
cnt -= 1
a = new_a
print(a[0])
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of employees.
The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.
-----Output-----
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
-----Examples-----
Input
5
DDRRR
Output
D
Input
6
DDRRRR
Output
R
-----Note-----
Consider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
|
n = int(input())
raby = [x for x in input()]
numD = 0
numR = 0
flag = True
while flag:
cR = 0
cD = 0
for ind in range(len(raby)):
if raby[ind] == "D":
cD += 1
if numR == 0:
numD += 1
else:
raby[ind] = "N"
numR -= 1
if raby[ind] == "R":
cR += 1
if numD == 0:
numR += 1
else:
raby[ind] = "N"
numD -= 1
if cD == 0 or cR == 0:
flag = False
if numD == 0:
print("R")
else:
print("D")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of employees.
The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.
-----Output-----
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
-----Examples-----
Input
5
DDRRR
Output
D
Input
6
DDRRRR
Output
R
-----Note-----
Consider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
|
n = int(input())
s = input()
D, R = s.count("D"), s.count("R")
d, r = 0, 0
p = [1] * n
ans = 0
i = 0
while ans == 0:
if p[i]:
if s[i] == "D":
if d > 0:
d = d - 1
p[i] = 0
elif R > 0:
R -= 1
r = r + 1
else:
ans = "D"
break
elif r > 0:
r = r - 1
p[i] = 0
elif D > 0:
D -= 1
d = d + 1
else:
ans = "R"
break
i = (i + 1) % n
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of employees.
The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.
-----Output-----
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
-----Examples-----
Input
5
DDRRR
Output
D
Input
6
DDRRRR
Output
R
-----Note-----
Consider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
|
n = int(input())
s = list(input())
r, d = 0, 0
while True:
t = 0
for i in range(n):
if s[i] == "D":
if d > 0:
s[i] = "0"
d -= 1
t += 1
else:
r += 1
if s[i] == "R":
if r > 0:
s[i] = "0"
r -= 1
t += 1
else:
d += 1
if t == 0:
for i in range(n):
if s[i] != "0":
print(s[i])
exit()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of employees.
The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.
-----Output-----
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
-----Examples-----
Input
5
DDRRR
Output
D
Input
6
DDRRRR
Output
R
-----Note-----
Consider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
|
n = input()
a = input()
num = 0
while num < len(a):
b = "-"
num = 0
aa = ""
for i in a:
if b != i:
if num > 0:
num -= 1
else:
num = 1
b = i
aa += i
else:
num += 1
aa += i
a = ""
for i in aa:
if i != b and num > 0:
num -= 1
else:
a += i
print(a[0])
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = list(map(int, input().split()))
a = list(map(int, input().split()))
answer = 0
last_o = -r
used_o = -r
for i in range(n):
if a[i] == 1:
if i - used_o > 2 * (r - 1):
last_o = i
used_o = i
answer += 1
else:
last_o = i
elif i - used_o > 2 * (r - 1):
if i - last_o > 2 * (r - 1):
answer = -1
break
else:
used_o = last_o
answer += 1
if n - used_o > r:
if n - last_o > r:
answer = -1
else:
answer += 1
elif n - used_o > r:
answer += 1
print(answer)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
import sys
from itertools import accumulate
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
def list3d(a, b, c, d):
return [[([d] * c) for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**18
MOD = 10**9 + 7
N, r = MAP()
A = [0] + LIST()
B = []
for i, a in enumerate(A):
if a:
B.append(i)
M = len(B)
dp = list2d(M + 1, M + 1, -INF)
dp[0][0] = 0
for i, b in enumerate(B):
for j in range(M + 1):
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
if dp[i][j] >= b - r:
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], b + r - 1)
ans = INF
for j in range(M + 1):
if dp[M][j] >= N:
ans = j
break
if ans == INF:
print(-1)
else:
print(ans)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
def oleft(i):
for j in range(min(i + r - 1, n - 1), max(-1, i - r), -1):
if a[j]:
return j + r
return 10**6
def oright(i):
for j in range(max(0, i - r + 1), min(i + r, n)):
if a[j]:
return j - r
return -(10**6)
mi, ma = 0, n - 1
s = 0
while mi <= ma:
mi = oleft(mi)
s += mi - r != ma + r
if mi > ma or mi == 10**6:
break
ma = oright(ma)
if ma == -(10**6):
mi = -ma
break
s += mi - r != ma + r
if mi == 10**6:
print(-1)
else:
print(s)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def heaters(n, r, arr):
ultimo = -1
con = 0
n = n - 1
while ultimo < n:
inicio = -1
encontrado = False
mid = max(-1, ultimo - r + 1)
for i in range(n, mid, -1):
if arr[i] == 1:
if i - r <= ultimo:
if i + r - 1 > inicio:
inicio = i
encontrado = True
break
if not encontrado:
return -1
con = con + 1
ultimo = inicio + r - 1
return con
n, r = [int(c) for c in input().split()]
arr = [int(c) for c in input().split()]
print(heaters(n, r, arr))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
data = list(map(int, input().split()))
last = 0
ans = 0
while last < n:
save = -1
for i in range(max(0, last - r + 1), min(n, last + r)):
if data[i] == 1:
save = i
if save == -1:
ans = -1
break
else:
ans += 1
last = save + r
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
ans = last = 0
while True:
pos = -1
for i in range(last + r - 1, last - r, -1):
if i < 0 or i >= n:
continue
if a[i] == 1:
pos = i
break
if pos == -1:
print(-1)
exit()
ans += 1
last = pos + r
if last >= n:
break
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(lambda c: bool(int(c)), input().split()))
c = []
for i in range(n):
if a[i]:
c.append((max(i - r + 1, 0), min(i + r - 1, n - 1)))
if len(c) == 0:
print(-1)
else:
key = not (c[0][0] == 0 and c[-1][1] == n - 1)
for i in range(len(c) - 1):
if c[i][1] < c[i + 1][0] - 1:
key = True
break
if key:
print(-1)
else:
i = 0
while i < len(c) and c[i][0] == 0:
i += 1
s = [c[i - 1]]
for i in range(i, len(c)):
if c[i][0] - 1 > s[-1][1]:
s.append(c[i - 1])
if s[-1][1] != n - 1:
s.append(0)
print(len(s))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def ans(n, r, a):
a.insert(0, 0)
p, l, pl, cnt = 0, 0, 0, 0
for i in range(1, n + 1):
if i == p + r:
if a[i] == 1:
cnt += 1
pl = i
p = i + r - 1
elif l > pl:
cnt += 1
pl = i
p = l + r - 1
else:
return -1
if a[i] == 1:
l = i
if l == 0:
return -1
if n > p:
if l > n - r:
cnt += 1
else:
return -1
return cnt
n, r = map(int, input().split())
a = [int(i) for i in input().split()]
print(ans(n, r, a))
|
FUNC_DEF EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
mb = True
fin = [(0) for i in range(n)]
ans = 0
for i in range(n):
if fin[i] == 0:
for pos in range(min(i + r - 1, n - 1), max(i - r + 1, 0) - 1, -1):
if a[pos] == 1:
ans += 1
for c in range(max(pos - r + 1, 0), min(pos + r - 1, n - 1) + 1):
fin[c] = 1
i = min(i + r - 1, n - 1) + 1
break
for i in fin:
if i == 0:
print(-1)
exit()
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
b = [0] * n
cnt = 0
for i in range(n):
if a[i] == 1:
cnt += 1
le = max(0, i - r + 1)
ri = min(n, i + r)
for j in range(le, ri):
b[j] += 1
for i in b:
if i == 0:
print(-1)
quit()
for i in range(n):
if a[i] == 1:
fl = True
le = max(0, i - r + 1)
ri = min(n, i + r)
for j in range(le, ri):
if b[j] == 1:
fl = False
break
if fl == True:
cnt -= 1
for j in range(le, ri):
b[j] -= 1
print(cnt)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, k = map(int, input().split())
arr = list(map(int, input().split()))
new = [-1] * 1010
for i in range(len(arr)):
if arr[i] == 0:
continue
else:
for j in range(max(0, i - k + 1), min(n - 1, i + k - 1) + 1):
new[j] = max(new[j], i)
pos = 0
ans = 0
while pos < n:
if new[pos] == -1:
ans = -1
break
ans += 1
pos = new[pos] + k
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
ind = []
for i in range(len(a)):
if a[i] == 1:
ind.append(i)
ans = [0] * len(a)
for i in ind:
for j in range(max(i - r, -1) + 1, min(i + r, len(a))):
ans[j] += 1
if 0 in ans:
print(-1)
else:
c = len(ind)
for i in ind:
tag = False
for j in range(max(i - r, -1) + 1, min(i + r, len(a))):
if ans[j] == 1:
tag = True
ans[j] -= 1
if tag:
for j in range(max(i - r, -1) + 1, min(i + r, len(a))):
ans[j] += 1
else:
c -= 1
print(c)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
a = input().split()
n, r = int(a[0]), int(a[1])
heaters = input().split()
def heat():
indexes_1 = []
for i in range(len(heaters)):
if heaters[i] == "1":
indexes_1.append(i)
if len(indexes_1) == 0:
return -1
elif indexes_1[0] - r + 1 > 0:
return -1
elif indexes_1[-1] + r < len(heaters):
return -1
def find_start():
finding_index = r - 1
found_index = None
for i in range(len(indexes_1)):
if indexes_1[i] <= finding_index:
found_index = i
else:
break
return found_index
first = find_start()
count = 1
while first < len(indexes_1):
if indexes_1[first] + r >= len(heaters):
break
finding_index = indexes_1[first] + 2 * (r - 1) + 1
second = first + 1
found_index = None
while second < len(indexes_1):
if indexes_1[second] <= finding_index:
found_index = indexes_1[second]
second += 1
else:
break
if found_index is None:
return -1
else:
first = second - 1
count += 1
return count
print(heat())
|
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NONE WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NONE RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def compute(n, r, array):
num_heaters = sum(array)
if num_heaters == 0:
return -1
heater_pos = set()
array_zeroed = [0] * n
for i, e in enumerate(array):
if e == 1:
heater_pos.add(i)
for j in range(max(0, i - r + 1), min(i + r, n)):
array_zeroed[j] += 1
for elm in array_zeroed:
if elm == 0:
return -1
heaters_on = 0
house = array_zeroed.copy()
for h in heater_pos:
for j in range(h - r + 1, h + r):
if 0 <= j < n:
house[j] -= 1
for e in house:
if e == 0:
heaters_on += 1
for j in range(h - r + 1, h + r):
if 0 <= j < n:
house[j] += 1
break
return heaters_on
def main():
n, r = [int(c) for c in input().split()]
array = [int(c) for c in input().split()]
res = compute(n, r, array)
print(res)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF NUMBER VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF NUMBER VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
from sys import stdin
input = stdin.readline
n, r = map(int, input().split())
arr = [int(x) for x in input().split()]
dp = [0] * n
for i in range(n):
if arr[i] == 1:
s = min(i + r - 1, n - 1)
e = max(i - r + 1, 0)
while s >= e and dp[s] == 0:
dp[s] = 1
s = s - 1
if dp.count(0) > 0:
print(-1)
exit()
dp = []
for i in range(n):
if arr[i] == 1:
rr = min(i + r - 1, n - 1)
ll = max(0, i - r + 1)
dp.append((ll, rr))
l = len(dp)
ans = []
i = 0
while i < l:
if dp[i][0] > 0:
break
i = i + 1
ans.append(dp[i - 1])
s = i
for i in range(s, l):
if i == l - 1:
if ans[-1][1] < n - 1:
ans.append(dp[i])
elif dp[i + 1][0] - ans[-1][1] >= 2:
ans.append(dp[i])
print(len(ans))
|
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
heaters = list(map(int, input().split()))
count = 0
position = 0
last_seen_heater = -1
while position < n:
index = last_seen_heater + 1
while index < position + r and index < n:
if heaters[index] == 1:
last_seen_heater = index
index += 1
new_pos = position
if last_seen_heater >= 0:
new_pos = last_seen_heater + r
if new_pos != position:
position = new_pos
count += 1
else:
count = -1
break
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
from sys import stdin, stdout
def get():
return stdin.readline().strip()
def getf(sp=" "):
return [int(i) for i in get().split(sp)]
def put(a, end="\n"):
stdout.write(str(a) + end)
def putf(a, sep=" ", end="\n"):
stdout.write(sep.join([str(i) for i in a]) + end)
def main():
n, r = getf()
a = getf()
b = []
for i in range(n):
if a[i] == 1:
b.append([i - r + 1, i + r - 1])
b.sort()
lst = 0
ans = 0
x = n * n
i = 0
b += [[n + n, n]]
k = len(b)
while i < k:
if lst >= n:
break
if b[i][0] <= lst:
x = i
i += 1
else:
if x >= k:
pass
elif b[x][0] <= lst:
lst = b[x][1] + 1
ans += 1
else:
i += 1
x = -1
if lst >= n:
put(ans)
else:
put(-1)
main()
|
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_DEF STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR LIST LIST BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def get_max(x, y):
if x > y:
return x
else:
return y
def get_min(x, y):
if x < y:
return x
else:
return y
seg = []
n, r = map(int, input().split())
ar = list(map(int, input().split()))
for i in range(len(ar)):
if ar[i] == 0:
continue
seg.append((get_max(0, i - r + 1), -get_min(i + r - 1, n - 1)))
seg.sort()
if len(seg) == 0:
print(-1)
elif seg[0][0] != 0:
print(-1)
else:
R, tmpR = -seg[0][1], -1
hasil = int(1)
for i in range(1, len(seg)):
if seg[i][0] > R + 1:
R = tmpR
hasil = hasil + 1
tmpR = -1
if seg[i][0] > R + 1:
hasil = -1
break
if -seg[i][1] > R and -seg[i][1] > tmpR:
tmpR = -seg[i][1]
if tmpR != -1:
R = tmpR
hasil = hasil + 1
if R != n - 1:
hasil = -1
print(hasil)
|
FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def mi():
return map(int, input().split())
n, r = mi()
a = list(mi())
def fun(n, a, r):
vis = [0] * n
i = 0
cnt = 0
for j in range(min(r, n)):
if a[j] == 1:
i = j
while i < n:
if vis[i] == 1 and vis[min(n, end + r) - 1] == 1:
break
if a[i] == 1:
cnt += 1
j = max(0, i - (r - 1))
end = min(n, i + r)
next = 0
while j < end:
vis[j] = 1
j += 1
next = 0
for k in range(i + 1, min(n, end + r)):
if a[k] == 1:
next = k
if next:
i = next
continue
i += 1
if 0 in vis:
return -1
else:
return cnt
print(max(fun(n, a, r), fun(n, a[::-1], r)))
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER IF NUMBER VAR RETURN NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
enabled = [0] * n
ans = 0
i = 0
breaked = False
while i < n:
j = i + r - 1
heater = -1
while j >= i - r + 1:
if j >= 0 and j < n and a[j] == 1:
heater = j
break
j -= 1
if heater == -1:
breaked = True
break
else:
i = heater + r
ans += 1
if not breaked:
print(ans)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = [int(i) for i in input().split()]
j = 1
cur = 1
ans = 0
best_cur = 0
while cur <= n + 1 and j <= n:
if cur - r + 1 <= j and cur <= n:
if a[cur - 1] == 1:
best_cur = cur
elif best_cur != 0:
j = best_cur + r
ans += 1
best_cur = 0
cur -= 1
else:
ans = -1
break
cur += 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
arr = [int(x) for x in input().split()]
c = 0
r = min(n, r)
f = False
i = 0
area = 0
while i < n:
if area >= n:
print(c)
break
if c == 0:
for k in range(r - 1, -1, -1):
if arr[i + k]:
area = i + k + r
i += k + 1
c += 1
break
else:
print(-1)
break
else:
for k in range(min(n - 1, (r - 1) * 2), -1, -1):
if i + k >= n:
f = True
continue
if arr[i + k]:
area = i + k + r
i += k + 1
c += 1
break
else:
print(c if area >= n else -1)
break
else:
print(c if c != 0 else -1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
x = r - 1
b = 0
for m in range(min(x, n - 1), -1, -1):
if a[m] == 1:
i = m
b = 1
break
if b == 0:
s = -1
else:
s = 1
while i + x < n - 1:
j = i + 1
b = 0
for l in range(min(j + 2 * x, n - 1), j - 1, -1):
if a[l] == 1:
b = 1
k = l
break
if b == 0:
s = -1
break
else:
s += 1
i = k
print(s)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
import sys
n, r = map(int, input().split())
A = list(map(int, input().split()))
count = 0
i = 0
while i < n:
for j in range(min(i + r - 1, n - 1), max(i - r, -1), -1):
if A[j] == 1:
count += 1
if j + r > i:
i = j + r
break
else:
print(-1)
sys.exit()
else:
print(-1)
sys.exit()
print(count)
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
b = [0] * n
for i in range(n):
if a[i] == 1:
if i - r + 1 < 0:
c = 0
else:
c = i - r + 1
if i + r - 1 > n - 1:
c1 = n - 1
else:
c1 = i + r - 1
for j in range(c, c1 + 1):
b[j] += 1
x = sum(a)
y = 15
z = 0
if min(b) == 0:
print(-1)
else:
for i in range(n):
if a[i] == 1:
if i - r + 1 < 0:
c = 0
else:
c = i - r + 1
if i + r - 1 > n - 1:
c1 = n - 1
else:
c1 = i + r - 1
for j in range(c, c1 + 1):
if b[j] == 1:
z += 1
if z == 0:
x -= 1
for j in range(c, c1 + 1):
b[j] -= 1
z = 0
print(x)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
i = 0
d = 0
while True:
ki = min(n - 1, i + r - 1)
while ki >= 0 and not a[ki]:
ki -= 1
if ki < 0:
d = -1
break
a[ki] = 0
d += 1
i = ki + r
if i > n - 1:
break
if i < 0:
d = -1
break
print(d)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
R = lambda: map(int, input().split())
n, r = R()
a = [*R()] + [0] * (r - 1)
p = q = -r
c = 0
for i in range(n + r - 1):
if a[i]:
p = i
if i - q == 2 * r - 1:
if p == q:
c = -1
break
q = p
c += 1
print(c)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = [int(i) for i in input().split()]
ans = 0
last = -1
while last < n - 1:
pos = -1
for i in range(n - 1, max(-1, last - r + 1), -1):
if a[i] == 1 and i - r <= last:
pos = i
break
if pos == -1:
print(-1)
exit()
ans += 1
last = pos + r - 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
poscnt = 0
locs = list()
for i in range(n):
if a[i] == 1:
poscnt += 1
locs.append(i)
if poscnt == 0:
print(-1)
else:
loc = -1
left = right = 0
while right < n:
left = right
flag = False
while 1:
if loc + 1 < poscnt and locs[loc + 1] < left + r:
flag = True
loc += 1
else:
break
if flag == False:
ans = -1
break
else:
right = locs[loc] + r
if right - 1 < left:
ans = -1
break
ans += 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
h = list(map(int, input().split()))
h2 = [0] * len(h)
f = False
aux = 0
i = 0
while i < len(h2):
f = False
for j in range(r - 1, -r, -1):
if h[max(0, min(i + j, n - 1))] == 1:
i = i + j + r
aux += 1
f = True
break
if f == False:
aux = -1
break
print(aux)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
from sys import stdin, stdout
def right_index_search(i, arr, k):
j = i
index = -1
while j < len(arr) and j < i + k:
if arr[j] == 1:
index = j
j += 1
return index
def left_index_search(i, arr, k):
j = i - 1
index = -1
while j >= 0 and j > i - k:
if arr[j] == 1:
index = j
break
j -= 1
return index
def pylons(k, arr):
count = 0
i = 0
while i < len(arr):
index_right = right_index_search(i, arr, k)
if index_right == -1:
index_left = left_index_search(i, arr, k)
if index_left == -1:
return -1
else:
i = index_left + k
count += 1
else:
i = index_right + k
count += 1
return count
n, k = input().strip().split(" ")
n, k = [int(n), int(k)]
arr = list(map(int, input().strip().split(" ")))
result = pylons(k, arr)
print(result)
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def main():
n, r = map(int, input().split())
a = [int(e) for e in input().split()]
r -= 1
fr = 0
ans = 0
while fr < n:
i = min(fr + r, n - 1)
flag = False
while i >= max(fr - r, 0) and not flag:
if a[i] == 1:
ans += 1
fr = i + r + 1
flag = True
break
i -= 1
if not flag:
print(-1)
return
print(ans)
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def add(cnt, l, r, delta):
for i in range(l, r):
cnt[i] += delta
n, r = map(int, input().split())
a = list(map(int, input().split()))
cnt = [(0) for i in range(n)]
sooca = 0
for i in range(n):
if a[i] == 1:
add(cnt, max(0, i - r + 1), min(n, i + r), 1)
for i in cnt:
if i == 0:
print(-1)
exit(0)
ans = a.count(1)
for i in range(n):
if a[i] == 0:
continue
if not any(x == 1 for x in cnt[max(0, i - r + 1) : min(n, i + r)]):
add(cnt, max(0, i - r + 1), min(n, i + r), -1)
ans -= 1
print(ans)
|
FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
result = 0
i = 0
while i < len(a):
b = True
k = i
for j in range(min(n - 1, k + r - 1), max(0, k - r + 1) - 1, -1):
if a[j] == 1:
i = j + r
result += 1
b = False
break
if b:
break
if not b:
print(result)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
room = list(map(int, input().split()))
last = -1
ans = 0
trig = True
while trig:
if last + r < n:
for i in range(last + r, max(-1, last - r + 1), -1):
if room[i] == 1:
last = i + r - 1
ans += 1
break
elif i == max(-1, last - r + 1) + 1:
ans = -1
trig = False
else:
pass
elif last >= n - 1:
trig = False
else:
for i in range(n - 1, max(-1, last - r + 1), -1):
if room[i] == 1:
last = i + r - 1
ans += 1
if last >= n - 1:
trig = False
break
elif i == max(-1, last - r + 1) + 1:
ans = -1
trig = False
else:
pass
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
arr = list(map(int, input().split()))
heaters = []
counts = [0] * n
for i in range(0, n):
if arr[i] == 0:
continue
heaters.append(i)
for j in range(max(0, i - r + 1), min(n - 1, i + r - 1) + 1):
counts[j] += 1
if 0 in counts:
print(-1)
else:
ret = len(heaters)
for i in range(0, len(heaters)):
heater_idx = heaters[i]
found = False
for j in range(max(0, heater_idx - r + 1), min(n - 1, heater_idx + r - 1) + 1):
if counts[j] == 1:
found = True
break
if not found:
ret -= 1
for j in range(
max(0, heater_idx - r + 1), min(n - 1, heater_idx + r - 1) + 1
):
counts[j] -= 1
print(ret)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
x, y = map(int, input().split())
a = list(map(int, input().split()))
o = 0
i = 0
while i < len(a):
r = -1
for j in range(min(i + y - 1, len(a) - 1), max(i - y + 1, 0) - 1, -1):
if a[j] == 1:
o += 1
r = j + y
break
if r == -1:
print(-1)
exit()
i = r
print(o)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r1 = map(int, input().split())
a = list(map(int, input().split()))
right = -1
currentRight = -1
free = r1
c = 0
total = 0
finish, stop = 0, 0
idex = 0
lastIdex = -1
while idex < n and free:
free -= 1
if a[idex] == 1:
lastIdex = idex
right = idex + r1 - 1
c += 1
if right >= n - 1:
finish = 1
total += 1
break
idex += 1
if not free:
if c > 0:
total += 1
c = 0
free = r1 * 2 - 1
idex = lastIdex + 1
else:
stop = 1
break
if finish:
print(total)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [0] * n
for i in range(n):
if a[i] == 1:
if i - (r - 1) < 0:
b[0] += 1
else:
b[i - (r - 1)] += 1
if i + r < n:
b[i + r] += -1
for i in range(1, n):
b[i] += b[i - 1]
if b.count(0) == 0:
l = 0
for i in range(n):
if a[i] == 1:
if i - (r - 1) < 0:
p = 0
else:
p = i - (r - 1)
if i + r < n:
q = i + r
else:
q = n
t = 0
while p < q:
if b[p] == 1:
t += 1
break
p += 1
if t == 1:
l += 1
else:
if i - (r - 1) < 0:
p = 0
else:
p = i - (r - 1)
while p < q:
b[p] = b[p] - 1
p += 1
print(l)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = [int(x) for x in input().split()]
ns = [int(x) for x in input().split()]
def fin(i):
p = need + r - 1
last = max(need - r, -1)
j = min(len(ns) - 1, p)
while j > last:
if ns[j] > 0:
return j + r
j -= 1
return -1
need = 0
ans = 0
while need < len(ns):
ans += 1
need = fin(need)
if need == -1:
print(-1)
quit()
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR WHILE VAR VAR IF VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
l = list(map(int, input().split()))
num = 0
boo = False
last = 0
count = 0
if r >= n:
if l.count(1) == 0:
print(-1)
else:
print(1)
exit()
for i in range(r):
if l[i] == 1:
count += 1
last = i
if count == 0:
print(-1)
exit()
num += count - 1
i = last
while i < n:
count = 0
for j in range(1, 2 * r):
if i + j >= n:
boo = True
break
if l[i + j] == 1:
count += 1
last = j
if not boo and count == 0:
print(-1)
exit()
if boo:
if j <= r:
num += count
else:
if j - last > r:
print(-1)
exit()
num += max(count - 1, 0)
break
num += max(count - 1, 0)
i += last
last = 0
print(l.count(1) - num)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR IF VAR VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
from sys import exit
n, r = map(int, input().split())
lst = [*map(int, input().split())]
item, last, prev, res, i = r, -1, -1, 0, 0
for i, x in enumerate(lst[:r]):
if x == 1:
last = i
if last == prev:
print(-1)
exit()
prev, i, res, item = last, last + 1, 1, r * 2 - 1
while i < n:
if item == 0:
if last == prev:
print(-1)
exit()
prev, i = last, last + 1
item = r * 2 - 1
res += 1
if lst[i] == 1:
last = i
i += 1
item -= 1
if n - prev - 1 >= r:
if last == prev:
print(-1)
exit()
if n - last - 1 >= r:
print(-1)
exit()
res += 1
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
from sys import exit
n, r = map(int, input().split())
arr = list(map(int, input().split()))
intervals = []
for i in range(n):
if arr[i] != 1:
continue
pos = i
left = max(0, i - r + 1)
right = min(n - 1, i + r - 1)
intervals.append([left, right])
p = 0
res = 0
while p < n:
good = list(filter(lambda interv: interv[0] <= p <= interv[1], intervals))
if not good:
print(-1)
exit()
best = max(good, key=lambda interv: interv[1])
p = best[1] + 1
res += 1
if p >= n:
print(res)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = input().split()
i = -r
n_r = n - r
n_1 = n - 1
r1 = r * 2 - 1
res = 0
while i < n_r:
for j in range(min(i + r1, n_1), max(i, -1), -1):
if a[j] == "1":
i = j
res += 1
break
else:
res = -1
i = n_r
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def count_col(inp_ar, n, r):
res = 0
last = -1
while last < n - 1:
Flag = False
for i in list(reversed(range(max(0, last - r + 2), min(n, last + r + 1)))):
if int(inp_ar[i]) == 1:
last = i + r - 1
res += 1
Flag = True
break
if not Flag:
return -1
else:
Flag = False
return int(res)
temp = input().split()
n = int(temp[0])
r = int(temp[1])
inp_ar = input().split()
print(count_col(inp_ar, n, r))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def main():
n, r = list(map(int, input().split()))
arr = [(True if c == "1" else False) for c in input().split()]
last_heated = 0
tot = 0
last_turned = -1
while last_heated < n:
optim = last_heated + r - 1
while True:
if optim < 0:
print("-1")
return
if optim <= last_turned:
print("-1")
return
if optim >= n:
optim -= 1
continue
if arr[optim]:
tot += 1
last_heated = optim + r
last_turned = optim
break
optim -= 1
print(tot)
def __starting_point():
main()
__starting_point()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def solve(n, r, a):
i = k = -1
count = 0
while i < n:
if i == -1:
for j in range(min(r, n)):
if a[j] == 1:
i = j
else:
for j in range(k + 1, min(k + 2 * r, n)):
if a[j] == 1:
i = j
if i == k:
return -1
else:
count += 1
k = i
if n - k <= r:
return count
n, r = map(int, input().split())
a = list(map(int, input().split()))
ans = solve(n, r, a)
print(ans)
|
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
b = [0] * n
ans = 0
for i in range(n):
if b[i] == 0:
found = False
for j in range(min(n - 1, i + r - 1), max(-1, i - r), -1):
if a[j] == 1:
for k in range(min(n - 1, j + r - 1), max(-1, j - r), -1):
b[k] = 1
ans += 1
found = True
break
if not found:
ans = -1
break
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
location = list(map(int, input().split()))
heaterlist = []
for i in range(len(location)):
if location[i] == 1:
heaterlist.append(i)
heaterlist.append(10000000)
don = 0
if heaterlist[0] >= r or heaterlist[-2] < n - r:
print(-1)
don = 1
for i in range(len(heaterlist) - 2):
if heaterlist[i + 1] - heaterlist[i] > 2 * r - 1 and don == 0:
print(-1)
don = 1
unheated = 0
usedheaters = []
if don == 0:
while unheated < n:
i = 0
while heaterlist[i] < unheated + r:
good = heaterlist[i]
i += 1
usedheaters.append(good)
unheated = usedheaters[-1] + r
print(len(usedheaters))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = [0] + list(map(int, input().split()))
l = 0
i = 1
end = 0
while i <= n:
j = i + r - 1
j = min(j, n)
while j >= end and j <= n and a[j] < 1:
j -= 1
if j > end:
l += 1
end = j
i = j + r
else:
l = -1
break
print(l)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a, ans, p1 = list(map(int, input().split())), 0, 0
while p1 < n:
flag = 0
for i in range(min(n - 1, p1 + r - 1), p1 - 1, -1):
if a[i]:
ans += 1
p1 = i + r
flag = 1
a[i] = 0
break
if flag == 0:
c = 1
for i in range(p1 - 1, max(-1, p1 - r), -1):
if a[i]:
ans += 1
p1 = i + r
flag = 1
a[i] = 0
break
c += 1
if flag == 0:
ans = -1
break
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
arr = [int(x) for x in input().split()]
light = [(0) for x in range(n)]
heaters = 0
if max(arr) == 0 or r == 1 and min(arr) == 0:
print(-1)
else:
for i in range(n):
if arr[i] == 1:
heaters += 1
for j in range(max(0, i - r + 1), min(n, i + r)):
light[j] += 1
if not min(light):
print(-1)
else:
for i in range(n):
if arr[i] == 1:
needed = False
for j in range(max(0, i - r + 1), min(n, i + r)):
light[j] -= 1
if light[j] == 0:
needed = True
for k in range(max(0, i - r + 1), min(n, i + r)):
light[k] = 9999
if not needed:
heaters -= 1
print(heaters)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
R = lambda: map(int, input().split())
n, r = R()
a = [1] + [0] * (r - 1) + [*R()] + [0] * (r - 1)
c = p = 0
try:
while p < n:
p = next(i for i in range(p + 2 * r - 1, p, -1) if a[i])
c += 1
except:
c = -1
print(c)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
inf = float("inf")
n, r = map(int, input().split())
l = list(map(int, input().split()))
dp = [inf] * n
for i in range(r):
if i < n and l[i]:
dp[i] = 1
for i in range(n):
if l[i]:
for j in range(i - (r - 1) - (r - 1) - 1, i):
if j < 0:
continue
if dp[j] != inf:
dp[i] = min(dp[i], dp[j] + 1)
res = inf
for i in range(n - (r - 1) - 1, n):
if i < 0:
continue
res = min(res, dp[i])
if res == inf:
print(-1)
else:
print(res)
|
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(n):
if a[i] == 1:
b.append([max(0, i - r + 1), min(n - 1, i + r - 1)])
x = -1
k = 0
j = -1
ok = True
while x != n - 1:
i = 0
while i <= len(b) - 1 and b[i][0] <= x + 1:
i += 1
i -= 1
if i == j:
print(-1)
ok = False
break
j = i
k += 1
x = b[i][1]
if ok:
print(k)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
import sys
mod = 10**9 + 7
INF = float("inf")
def inp():
return int(sys.stdin.readline())
def inpl():
return list(map(int, sys.stdin.readline().split()))
n, r = inpl()
a = inpl()
b = [False] * n
res = 0
for i in range(n):
if b[i]:
continue
for j in range(-r + 1, r)[::-1]:
if i + j >= n or i + j < 0:
continue
if a[i + j]:
for k in range(i + j - r + 1, i + j + r):
if 0 <= k < n:
b[k] = True
res += 1
break
if sum(b) == n:
print(res)
else:
print(-1)
|
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
l = list(map(int, input().split()))
ka = [0] * n
flag = 0
for i in range(n):
if l[i] == 1:
z1 = i - r + 2
z2 = i + r
z1 = max(1, z1)
z2 = min(n, z2)
for i in range(z1 - 1, z2):
ka[i] += 1
count = 0
for _ in range(len(ka)):
if ka[_] == 0:
flag = 1
break
if flag == 0:
for i in range(n):
if l[i] == 1:
ma_flag = 0
z1 = i - r + 2
z2 = i + r
z1 = max(1, z1)
z2 = min(n, z2)
for i in range(z1 - 1, z2):
if ka[i] == 1:
ma_flag = 1
if ma_flag == 1:
count += 1
else:
for i in range(z1 - 1, z2):
ka[i] -= 1
print(count)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def solve(house, r):
last = -1
house[0] = -1 if house[0] == 0 else 0
for i in range(1, len(house)):
if house[i] == 1:
house[i] = i
else:
house[i] = house[i - 1]
count = 0
while last < len(house) - 1:
left = max(-1, last - r + 1)
right = last + r
closest = house[-1] if right >= len(house) else house[right]
if closest > left and closest <= right:
count += 1
last = closest + r - 1
else:
return -1
return count
tests, r = [int(x) for x in input().split()]
house = [int(x) for x in input().split()]
print(solve(house, r))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
import sys
input = sys.stdin.readline
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a, b):
return a * b / gcd(a, b)
def main():
n, r = map(int, input().split())
a = list(map(int, input().split()))
prev = -1
f = 1
ind = 0
ans = 0
while ind < n:
for i in range(ind, min(ind + r, n)):
if a[i] == 1:
prev = i
if prev == -1:
for i in range(ind, max(ind - r, 0), -1):
if a[i] == 1:
prev = i
break
if prev == -1:
f = 0
break
else:
ind = prev + r
ans += 1
prev = -1
if f == 0:
print(-1)
else:
print(ans)
return
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = tuple(map(int, input().split()))
arr = list(map(int, input().split()))
i, j = 0, 0
last_heater = -1
heaters_on = 0
while j < n:
if arr[j] == 1:
last_heater = j
if i == j - r + 1:
heaters_on += 1
if last_heater == -1:
print(-1)
exit()
i = last_heater + r
j = j + 1
last_heater = -1
else:
j += 1
if i >= n:
print(heaters_on)
elif last_heater != -1:
if last_heater + r - 1 >= n - 1:
print(heaters_on + 1)
else:
print(-1)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = tuple(map(int, input().split()))
i = 0
c = 0
while i < n:
j = min(n - 1, i + r - 1)
while not a[j]:
j -= 1
if j < 0 or j < i - r + 1:
print(-1)
exit()
c += 1
i = j + r
print(c)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
R = lambda: map(int, input().split())
z, r = R()
p = q = -1
a = 0
for i, x in enumerate(R()):
if x:
p = i + r - 1
if i - q == r:
if p == q:
break
q = p
a += 1
print((a, a + 1, -1)[(i > p) + (i > q)])
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
list_a = list(map(int, input().split()))
count = 0
next = 0
i = 0
ans = 0
if n <= r:
if 1 in list_a:
count = 1
else:
ans = -1
else:
while True:
if i > r - 1:
ans = -1
break
elif list_a[r - 1 - i] == 1:
next = r - 1 - i
count += 1
break
else:
i += 1
flag = 0
if ans != -1:
while True:
i = 0
while True:
if i >= 2 * (r - 1) + 1:
ans = -1
break
try:
if list_a[next + 2 * (r - 1) + 1 - i] == 1:
next += 2 * (r - 1) + 1 - i
count += 1
break
else:
i += 1
except IndexError:
flag = 1
break
if ans == -1 or flag == 1:
break
if ans != -1:
if next >= n - r:
pass
else:
while True:
if i > r - 1:
ans = -1
break
elif list_a[-r + i] == 1:
count += 1
break
else:
i += 1
if ans == -1:
print(ans)
else:
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR VAR WHILE NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = [int(i) for i in input().split()]
prevT = -1
result = 0
prevH = 0
flag = 0
while prevH < n:
if flag == 0:
aux = prevH + (r - 1)
while 1:
if aux < 0 or prevT >= aux:
print(-1)
flag = 1
break
if aux >= n:
aux = aux - 1
continue
if a[aux] == 1:
prevH = aux + r
prevT = aux
result = result + 1
break
aux = aux - 1
else:
break
if flag != 1:
print(result)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
sum, ans, p2, p1 = 0, 0, -1, -1
for i in range(n):
if a[i] == 1:
sum += 1
if p2 > p1:
ans += 1
p2 = i + r - 1
if i >= p1 + r:
if p2 > p1:
p1 = p2
else:
print(-1)
exit()
if p2 < n - 1:
print(-1)
exit()
if p2 > p1 and p1 >= n - 1:
ans += 1
print(sum - ans if sum else -1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split(" "))
array = list(map(int, input().split(" ")))
i = 0
sol = True
heaters = 0
while i < n:
found = False
maxLimit = min(n - 1, i + r - 1)
minLimit = max(i - r + 1, 0)
while maxLimit >= minLimit:
if array[maxLimit]:
found = True
break
maxLimit -= 1
if found:
heaters += 1
i = min(n, maxLimit + r)
else:
sol = False
break
if sol:
print(heaters)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
import sys
n, r = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
l = 0
l1 = -1
b = [(0) for i in range(n)]
for i in range(n):
if a[i] == 0:
continue
ans += 1
f = min(n, i + r)
s = max(0, i - r + 1)
for j in range(s, f):
b[j] += 1
for i in range(n):
if b[i] == 0:
print(-1)
sys.exit(0)
if a[i] == 0:
continue
f = min(n, i + r)
s = max(0, i - r + 1)
ok = True
for j in range(s, f):
if b[j] == 1:
ok = False
if ok:
for j in range(s, f):
b[j] -= 1
ans -= 1
print(ans)
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def solve(n, r, a):
ans = 0
last = -1
while last < n - 1:
pos = -1
for i in range(n - 1, max(-1, last - r + 1), -1):
if a[i] == 1 and i - r <= last:
pos = i
break
if pos == -1:
return -1
ans += 1
last = pos + r - 1
return ans
ans = []
n, r = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
print(solve(n, r, a))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
k = -1
for i in range(n):
if a[i] == 1:
if i - r > k:
ans = 0
break
tmp = 0
for j in range(i, n):
if a[j] == 1:
if j - r <= k:
tmp = j + r - 1
else:
break
ans += 1
k = tmp
if k >= n - 1:
break
if ans and k >= n - 1:
print(ans)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
ar = [int(i) for i in input().split()]
ans = 0
ind = -1
while ind < n - 1:
t = -10
q = ind - r + 1
q = max(q, -1)
for i in range(n - 1, q, -1):
if ar[i] and ind + r >= i:
t = i
ind = t + r - 1
ans = ans + 1
break
if t < 0:
ans = -1
break
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
lo = 0
ans = 0
pre = -1
while lo < n:
to = min(n - 1, lo + r - 1)
while to > pre:
if a[to] == 1:
break
to -= 1
else:
ans = -1
break
lo = to + r
pre = to
ans += 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = input().split()
def solve():
ret = 0
i = 0
while i < n:
for j in range(min(i + r - 1, n - 1), max(-1, i - r), -1):
if a[j] == "1":
ret += 1
i = j + r
break
else:
return -1
return ret
print(solve())
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, h_power = list(map(int, input().strip().split()))
arr = (
[1]
+ [0] * (h_power - 1)
+ list(map(int, input().strip().split()))
+ [0] * (h_power - 1)
+ [1]
)
n = len(arr)
max_dist = 0
dist = 0
for num in arr:
if num == 0:
dist += 1
else:
dist = 0
if dist > max_dist:
max_dist = dist
if max_dist > (h_power - 1) * 2:
print(-1)
else:
heater_count = 0
max_power_gap = (h_power - 1) * 2 + 1
best_idx_so_far = 0
last_idx = 0
for idx in range(n - 1):
if arr[idx] == 1:
best_idx_so_far = idx
if idx - last_idx == max_power_gap:
heater_count += 1
last_idx = best_idx_so_far
print(heater_count)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
arr = [1] + [0] * (r - 1) + [int(i) for i in input().split()]
n = len(arr)
i, prev, ans, crutch = 0, 0, 0, False
while i < n - 1:
ans += 1
prev = i
i += 2 * r - 1
if prev + r - 1 >= n - 1:
if crutch:
print(ans - 1)
break
print(-1)
break
if i > n - 1:
i = n - 1
while i > prev:
if arr[i] == 1:
crutch = True
break
i -= 1
else:
print(-1)
break
else:
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
from sys import stdin
n, m = map(int, input().split())
s = [-1] + list(map(int, stdin.readline().strip().split()))
dp = [[(10**4) for i in range(2)] for j in range(n + 1)]
dp[0][0] = 0
dp[0][1] = 0
for i in range(1, n + 1):
if s[i] == 1:
dp[i][1] = min(dp[max(i - m, 0)][0] + 1, dp[max(i - m, 0)][1] + 1)
for j in range(i + 1, min(n + 1, i + m)):
dp[j][0] = min(dp[j][0], dp[i][1])
for j in range(i - 1, max(-1, i - m), -1):
dp[j][0] = min(dp[j][0], dp[i][1])
x = min(dp[-1])
if x == 10000:
print(-1)
else:
print(x)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
cur = 0
res = 0
while cur < n:
b = max(0, cur - r + 1)
c = a[b : cur + r]
if sum(c):
i = len(c) - c[-1::-1].index(1) - 1
cur = b + i + r
res += 1
else:
print(-1)
break
else:
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
import sys
def minp():
return sys.stdin.readline().strip()
n, r = map(int, minp().split())
k = r
f = None
fl = None
res = 0
i = 0
for a in map(int, minp().split()):
if i == k:
if f == None:
print(-1)
exit(0)
fl = f
k = f + 2 * r
f = None
res += 1
if a == 1:
f = i
i += 1
if fl == None or i - fl > r:
if f == None:
print(-1)
exit(0)
if i - f > r:
print(-1)
exit(0)
res += 1
print(res)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NONE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NONE BIN_OP VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
if r == 1:
for e in a:
if e == 0:
print(-1)
break
else:
print(n)
elif r < n:
rm1 = r - 1
ci = rm1
c = 0
lasthit = -1
flag = True
while ci < n:
if a[ci] == 1:
lasthit = ci
c += 1
ci += 2 * r - 1
if ci > n - 1:
if lasthit + r > n - 1:
continue
else:
ci = n - 1
else:
ci -= 1
if ci == lasthit:
flag = False
print(-1)
break
if flag:
print(c)
elif 1 in a:
print(1)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def mainA():
T = int(input())
for i in range(T):
L, v, l, r = map(int, input().split())
ans = L // v
dow = l // v * v
up = r // v * v
if dow < l:
dow += v
ans -= max((up - dow) // v + 1, 0)
print(ans)
def mainB():
n, r = map(int, input().split())
a = [int(e) for e in input().split()]
r -= 1
fr = 0
ans = 0
while fr < n:
i = min(fr + r, n - 1)
flag = False
while i >= max(fr - r, 0) and not flag:
if a[i] == 1:
ans += 1
fr = i + r + 1
flag = True
break
i -= 1
if not flag:
print(-1)
return
print(ans)
mainB()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
s = [int(x) for x in input().split()]
ptr = 0
c = 1
temp = 0
lst = 0
ct = 0
ans = 0
while ptr < n:
if ptr == n - 1 and c > 0:
ptr = lst
c = r
if c == r and s[ptr] == 1:
c = -r + 2
ptr = ptr + 1
ans = ans + 1
elif c == r and s[ptr] != 1:
ptr = lst
c = r
else:
ptr = ptr + 1
c = c + 1
if ptr < n:
if s[ptr] == 1:
lst = ptr
ct = ct + 1
if ct > 100000:
temp = 1
break
if temp == 0:
print(ans)
else:
print("-1")
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def solve(arr):
if 1 in arr[::-1]:
return len(arr) - arr[::-1].index(1) - 1
return -1
n, r = map(int, input().split())
arr = list(map(int, input().split()))
flag = 1
curr = r
t = -1
count = 0
while True:
tt = t
t = solve(arr[t + 1 : curr]) + t + 1
if t == tt:
flag = 0
break
curr = 2 * r + t
count += 1
if curr > n:
curr = n
if t + r >= n:
break
if t + 1 >= n:
break
if flag == 1:
print(count)
else:
print(-1)
|
FUNC_DEF IF NUMBER VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, b = map(int, input().split())
arr = list(map(int, input().split()))
i = -1
j = b - 1
if j >= n:
j = n - 1
flag = 0
count = 0
while j < n:
while j > i:
if arr[j] == 1:
i = j
j += 2 * b - 1
count += 1
break
else:
j -= 1
if j - b >= n - 1:
break
if j > n - 1:
j = n - 1
if i == n - 1:
break
if i == j:
flag = 1
break
if flag == 1:
print(-1)
else:
print(count)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
b = []
j = 0
for i in range(n):
if a[i] == 1:
b.append(i)
i = 0
m = len(b)
ans = 0
while i < n:
while j + 1 < m and b[j + 1] <= i + r - 1:
j = j + 1
if j < m and b[j] <= i + r - 1 and b[j] >= i - r + 1:
ans = ans + 1
else:
ans = -1
break
i = b[j] + r
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
a, b = [int(z) for z in input().split()]
y = input().split()
h = b
q = 0
t = True
f = -1
Z = True
r = 0
for i in range(a):
if i < h:
if y[i] == "1":
if t:
q += 1
t = False
f = i
else:
if f == -1:
Z = False
break
h = f + 2 * b
t = True
f = -1
if y[i] == "1":
if t:
q += 1
t = False
f = i
if y[i] == "1":
r = 0
else:
r += 1
if f + b >= len(y) and f != -1:
break
if Z and q != 0 and f != -1 and r < b:
print(q)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR STRING IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
nos = list(map(int, input().split()))
arr = list(map(int, input().split()))
i, j, minj = 0, len(arr) - 1, len(arr)
heater, lock = 0, 0
while i < len(arr) and j >= 0:
if arr[j]:
if lock == 0 and minj == j:
heater = -1
break
if abs(i - j) <= nos[1] - 1:
if lock == 0:
if minj != j:
minj = j
heater += 1
lock = 1
i += 1
continue
elif i < j:
j -= 1
else:
lock = 0
j = len(arr) - 1
else:
j -= 1
if len(arr) > 0 and heater == 0:
print(-1)
else:
print(heater)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = input().split(" ")
heat = list(map(int, input().split(" ")))
n = int(n)
r = int(r)
if r is 1:
if 0 in heat:
ct = -1
else:
ct = n
elif r >= n:
if 1 in heat:
ct = 1
else:
ct = -1
else:
r = r - 1
ct = 0
pos = -1
posHeat = -1
while posHeat < n - 1:
it = posHeat + r + 1
if it > n - 1:
it = n - 1
flag = -1
for i in range(it, pos, -1):
if heat[i] is 1:
flag = 0
ct = ct + 1
posHeat = i + r
if posHeat >= n - 1:
flag = 1
break
pos = i
break
if flag is not 0:
if flag is -1:
ct = -1
break
print(ct)
|
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
def min(a, b):
if a < b:
return a
else:
return b
def serc(x, d):
if d < 0:
return 1000000000
if x == n:
if d < r - 1:
return 1000000000
return 0
if dp[x][d] != -1:
return dp[x][d]
dp[x][d] = 1000000000
if ar[x] == 1:
dp[x][d] = min(dp[x][d], 1 + serc(x + 1, (r - 1) * 2))
dp[x][d] = min(dp[x][d], serc(x + 1, d - 1))
return dp[x][d]
dp = []
ar = []
for i in range(1005):
dp.append([])
for j in range(2005):
dp[i].append(-1)
ar.append(0)
read = input().split(" ")
n = int(read[0])
r = min(n, int(read[1]))
read = input().split(" ")
for i in range(n):
ar[i] = int(read[i])
ans = serc(0, r - 1)
if ans == 1000000000:
ans = -1
print(ans)
|
FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = list(map(int, input().split()))
a = list(map(int, input().split()))
a.insert(0, 0)
pos = 1
ans = 0
last = 0
while True:
if pos > n:
break
prelast = last
for i in range(max(0, pos - r + 1), min(pos + r, n + 1)):
if a[i] == 1:
pos = i + r
last = i
if prelast == last:
ans = -1
break
else:
ans += 1
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
from itertools import accumulate
n, r = map(int, input().split())
A = list(map(int, input().split()))
imos = [0] * (n + 1)
for i in range(n):
if A[i] == 0:
continue
imos[max(0, i - r + 1)] += 1
imos[min(i + r - 1 + 1, n)] -= 1
imos = list(accumulate(imos))
for i in range(n):
if imos[i] == 0:
print(-1)
exit()
cnt = sum(A)
for i in range(n):
p = max(0, i - r + 1)
q = min(i + r - 1 + 1, n)
for j in range(p, q):
if imos[j] == 1:
break
else:
cnt -= 1
for j in range(p, q):
imos[j] -= 1
print(cnt)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
li = [int(i) for i in input().split()]
res = 1
x = -1
for i in range(min(n - 1, r - 1), -1, -1):
if li[i] == 1:
x = i
break
else:
print(-1)
exit()
while x + r - 1 < n - 1:
for i in range(min(n - 1, x + 2 * r - 1), x, -1):
if li[i] == 1:
x = i
res += 1
break
else:
print(-1)
exit()
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
i = ans = 0
while i < n:
pointer = i
f = 0
while pointer < n:
if pointer - r + 1 > i:
break
if a[pointer] == 1:
j = pointer
f = 1
pointer += 1
if f == 0:
pointer = i - 1
while pointer >= 0:
if pointer + r - 1 < i:
break
if a[pointer] == 1:
j = pointer
f = 1
break
pointer -= 1
if f == 0:
break
ans += 1
i = j + r
if f == 0:
print(-1)
else:
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, r = map(int, input().split())
a = list(map(int, input().split()))
i = 0
cnt = 1
flag = 1
k = 0
for i in range(min(n, r)):
if a[i] == 1:
flag = 0
k = i
i = k
if flag:
print(-1)
else:
while i + r - 1 < n - 1:
flag = 1
for j in range(i + 1, i + 2 * r):
if j < n:
if a[j] == 1:
i = j
flag = 0
cnt += 1
if flag:
break
if flag:
print(-1)
else:
print(cnt)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [0] * n
ans = 0
summ = 0
for i in range(n):
if a[i] == 1:
summ += 1
x = max(0, int(i - m + 1))
y = min(n - 1, int(i + m - 1))
for j in range(x, y + 1):
b[j] += 1
for i in range(n):
if a[i] == 1:
x = max(0, int(i - m + 1))
y = min(n - 1, int(i + m - 1))
dem = 0
for j in range(x, y + 1):
if b[j] >= 2:
dem += 1
if dem == y - x + 1:
ans += 1
for j in range(x, y + 1):
b[j] -= 1
ck = True
for i in b:
if i == 0:
ck = False
break
if ck:
print(summ - ans)
else:
print(-1)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vova's house is an array consisting of $n$ 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 $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.
Each heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[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 = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ 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 $n$ and $r$ ($1 \le n, r \le 1000$) β the number of elements in the array and the value of heaters.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 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 $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.
In the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.
In the third example there are no heaters so the answer is -1.
In the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$.
|
ab = list(map(int, input().split()))
a = ab[0]
b = ab[1]
c = list(map(int, input().split()))
s = [0] * a
idx = 0
while True:
tmp = -1
for j in range(idx - b + 1, idx + b):
if j >= 0 and j < a and c[j] == 1:
tmp = j
if tmp == -1:
print(-1)
exit()
s[tmp] = 1
idx = tmp + b
if idx >= a:
break
print(sum(s))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.