message stringlengths 2 19.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | instruction | 0 | 7,010 | 15 | 14,020 |
Tags: implementation, math
Correct Solution:
```
class CodeforcesTask436BSolution:
def __init__(self):
self.result = ''
self.n_m_k = []
self.field = []
def read_input(self):
self.n_m_k = [int(x) for x in input().split(" ")]
for x in range(self.n_m_k[0]):
self.field.append(input())
def process_task(self):
result_row = [0 for x in range(self.n_m_k[1])]
for x in range(self.n_m_k[0]):
for y in range(self.n_m_k[1]):
# print(x, y)
field = self.field[x][y]
if field == "R":
pos = y + x
if pos < self.n_m_k[1]:
result_row[pos] += 1
elif field == "L":
pos = y - x
if pos >= 0:
result_row[pos] += 1
elif field == "U" and not x % 2:
result_row[y] += 1
self.result = " ".join([str(x) for x in result_row])
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask436BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
``` | output | 1 | 7,010 | 15 | 14,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | instruction | 0 | 7,011 | 15 | 14,022 |
Tags: implementation, math
Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
f = sys.stdin
n, m, k = map(int, f.readline().strip().split())
s = f.readline().strip()
sp = [0]*m
for i in range(1, n):
s = f.readline().strip()
for l in range(m):
if s[l]=='U':
if i % 2 == 0:
sp[l] += 1
elif s[l]=='R':
mi = l+i
if mi<m :
sp[mi] += 1
elif s[l]=='L':
mi = l-i
if mi>=0 :
sp[mi] += 1
#print(i, s, sp)
print(' '.join([str(it) for it in sp]))
``` | output | 1 | 7,011 | 15 | 14,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | instruction | 0 | 7,012 | 15 | 14,024 |
Tags: implementation, math
Correct Solution:
```
def f():
n, m, k = map(int, input().split())
s = []
a = []
for i in range(n):
s.append(input())
for j in range(m):
ans = 0
for i in range(n):
if i + i < n and s[i + i][j] == 'U':
ans += 1
if j - i >= 0 and s[i][j - i] == 'R':
ans += 1
if j + i < m and s[i][j + i] == 'L':
ans += 1
a.append(ans)
print(' '.join(map(str, a)))
f()
``` | output | 1 | 7,012 | 15 | 14,025 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | instruction | 0 | 7,013 | 15 | 14,026 |
Tags: implementation, math
Correct Solution:
```
n, m, k = map(int, input().split())
field = []
counts = []
for j in range(m):
counts.append(0)
for i in range(n):
row = list(input())
field.append(row)
for i in range(n):
for j in range(m):
if field[i][j] == "L":
jth = j - i
if jth >= 0:
counts[jth] += 1
elif field[i][j] == "R":
jth = i + j
if jth < m:
counts[jth] += 1
elif field[i][j] == "U":
if i % 2 == 0:
counts[j] += 1
#counts = []
#for j in range(m):
# count = 0
# for i in range(n):
# if newfield[i][j] == "S":
# count += 1
# counts.append(str(count))
#print(field)
#print(newfield)
print(" ".join(list(map(str, counts))))
``` | output | 1 | 7,013 | 15 | 14,027 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | instruction | 0 | 7,014 | 15 | 14,028 |
Tags: implementation, math
Correct Solution:
```
#!/usr/bin/python
import re
import inspect
from sys import argv, exit
def rstr():
return input()
def rint():
return int(input())
def rints(splitchar=' '):
return [int(i) for i in input().split(splitchar)]
def varnames(obj, namespace=globals()):
return [name for name in namespace if namespace[name] is obj]
def pvar(var, override=False):
prnt(varnames(var), var)
def prnt(*args, override=False):
if '-v' in argv or override:
print(*args)
class Spider():
def __init__(self, x, y, d):
self.x = x
self.y = y
self.d = d
def get_copy(self):
return Spider(self.x, self.y, self.d)
def move(self):
if self.d == 'R':
self.x += 1
elif self.d == 'L':
self.x -= 1
elif self.d == 'U':
self.y -= 1
elif self.d == 'D':
self.y += 1
else:
raise Exception('WHAT HAVE YOU DONE', self.d)
def main(i, n, m, lders, rders, uders):
sightings = 0
iders = [s for s in uders if s.x == i and s.y % 2 == 0]
sightings += len(iders)
prnt('id', len(iders))
ulders = [s for s in rders if s.y == (i - s.x)]
sightings += len(ulders)
prnt('uld', len(ulders))
urders = [s for s in lders if s.y == (s.x - i)]
sightings += len(urders)
prnt('urd', len(urders))
return str(sightings)
if __name__ == '__main__':
(n,m,k) = rints()
field = [rstr() for i in range(n)]
si = [0 for i in range(m)]
spiders = []
for j,row in enumerate(field):
for i,space in enumerate(row):
if space == 'R':
if i+j < len(si):
si[i+j] += 1
if space == 'L':
if i-j >= 0:
si[i-j] += 1
if space == 'U':
if j % 2 == 0:
si[i] += 1
print(' '.join([str(i) for i in si]))
``` | output | 1 | 7,014 | 15 | 14,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | instruction | 0 | 7,015 | 15 | 14,030 |
Tags: implementation, math
Correct Solution:
```
def solve():
answer = [0] * m
for i in range(1, n):
for j in range(m):
if j - i >= 0:
if park[i][j - i] == 'R':
answer[j] += 1
if j + i < m:
if park[i][j + i] == 'L':
answer[j] += 1
if 2 * i < n:
if park[2 * i][j] == 'U':
answer[j] += 1
return answer
n, m, k = tuple(map(int, input().split()))
park = [input() for i in range(n)]
print(' '.join(map(str, solve())))
``` | output | 1 | 7,015 | 15 | 14,031 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | instruction | 0 | 7,016 | 15 | 14,032 |
Tags: implementation, math
Correct Solution:
```
n, m, k = map(int, str.split(input()))
f = tuple(map(lambda _: str.strip(input()), range(n)))
r = []
for x in range(m):
cr = sum(map(lambda y: f[y][x] == "U", range(0, n, 2)))
for cx in range(max(0, x + 1 - n), x):
cr += f[x - cx][cx] == "R"
for cx in range(x + 1, min(m, n + x)):
cr += f[cx - x][cx] == "L"
r.append(cr)
print(str.join(" ", map(str, r)))
``` | output | 1 | 7,016 | 15 | 14,033 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
Submitted Solution:
```
n, m, k = map(int, input().split())
input()
s = [0] * m
for i in range(1, n):
for j, x in enumerate(input()):
if x == 'L':
if j - i >= 0: s[j - i] += 1
elif x == 'R':
if j + i < m: s[j + i] += 1
elif x == 'U' and i & 1 == 0: s[j] += 1
print(' '.join(map(str, s)))
``` | instruction | 0 | 7,017 | 15 | 14,034 |
Yes | output | 1 | 7,017 | 15 | 14,035 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
Submitted Solution:
```
I = lambda:map(int,input().split())
n,m,k = I()
f = [['.' for _ in range(m)] for _ in range(n)]
for i in range(n):
s = input()
for j in range(len(s)):
if s[j] != '.':
f[i][j] = s[j]
ans = [0] * m
for i in range(n):
for j in range(m):
if j - i >= 0 and f[i][j - i] == 'R':
ans[j] += 1
if j + i < m and f[i][j + i] == 'L':
ans[j] += 1
if f[0][j] == 'D':
ans[j] += 1
if i + i < n and f[i + i][j] == 'U':
ans[j] += 1
print (*ans)
``` | instruction | 0 | 7,018 | 15 | 14,036 |
Yes | output | 1 | 7,018 | 15 | 14,037 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
Submitted Solution:
```
n, m, k = map( int, input().split() )
ans = [0] * m
for i in range( n ):
field = input()
for j in range( m ):
if ( field[j] == 'U' ) and ( i % 2 == 0 ): ans[j] += 1
elif ( field[j] == 'L' ) and ( j >= i ): ans[j - i] += 1
elif ( field[j] == 'R' ) and ( j + i < m ): ans[j + i] += 1
print ( ' '.join( map( str, ans ) ) )
``` | instruction | 0 | 7,019 | 15 | 14,038 |
Yes | output | 1 | 7,019 | 15 | 14,039 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
Submitted Solution:
```
n, m, k = map( int, input().split() )
ans = [0] * m
for i in range( n ):
field = input()
for j in range( m ):
if ( field[j] == 'U' ) and ( i % 2 == 0 ): ans[j] += 1
elif ( field[j] == 'L' ) and ( j >= i ): ans[j - i] += 1
elif ( field[j] == 'R' ) and ( j + i < m ): ans[j + i] += 1
print ( ' '.join( map( str, ans ) ) )
# Made By Mostafa_Khaled
``` | instruction | 0 | 7,020 | 15 | 14,040 |
Yes | output | 1 | 7,020 | 15 | 14,041 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
Submitted Solution:
```
class CodeforcesTask436BSolution:
def __init__(self):
self.result = ''
self.n_m_k = []
self.field = []
def read_input(self):
self.n_m_k = [int(x) for x in input().split(" ")]
for x in range(self.n_m_k[0]):
self.field.append(input())
def process_task(self):
result_row = [0 for x in range(self.n_m_k[1])]
for x in range(self.n_m_k[0]):
for y in range(self.n_m_k[1]):
# print(x, y)
field = self.field[x][y]
if field == "R":
for j in range(y + x, min(self.n_m_k[1], y + 1 + x)):
result_row[j] += 1
elif field == "L":
for j in range(max(0, y - x), y):
result_row[j] += 1
elif field == "U" and x > 1:
result_row[y] += 1
self.result = " ".join([str(x) for x in result_row])
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask436BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
``` | instruction | 0 | 7,021 | 15 | 14,042 |
No | output | 1 | 7,021 | 15 | 14,043 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
Submitted Solution:
```
def f():
n, m, k = map(int, input().split())
a = []
for i in range(n):
a.append(list(input().strip()))
res = [0 for i in range(n)]
for i in range(m):
for j in range(1, n):
if j >= i and a[j][i - j] == 'R':
res[i] += 1
if j < n - i and a[j][i + j] == 'L':
res[i] += 1
if j + j < m and a[j + j][i] == 'U':
res[i] += 1
print(' '.join(list(map(str, res))))
f()
``` | instruction | 0 | 7,022 | 15 | 14,044 |
No | output | 1 | 7,022 | 15 | 14,045 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
Submitted Solution:
```
# -*- coding: utf-8 -*-
import sys
f = sys.stdin
n, m, k = map(int, f.readline().strip().split())
sp = [0]*m
for i in range(n):
s = f.readline().strip()
for l in range(m):
if s[l]=='U' and i>1:
sp[l] += 1
elif s[l]=='R':
mi = l+i
if mi<m :
sp[mi] += 1
elif s[l]=='L':
mi = l-i
if mi>=0 :
sp[mi] += 1
print(' '.join([str(it) for it in sp]))
``` | instruction | 0 | 7,023 | 15 | 14,046 |
No | output | 1 | 7,023 | 15 | 14,047 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)).
Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.
Submitted Solution:
```
n,m,k=map(int,input().split())
seen = [0 for _ in range(m)]
for i in range(n):
S = list(input())
for j in range(m):
if S[j] == 'U':
seen[j]+=1
elif S[j] == 'L' and j-i>=0:
seen[j-i]+=1
elif S[j] == 'R' and j+i<m:
seen[j+i]+=1
print(*seen, sep=" ")
``` | instruction | 0 | 7,024 | 15 | 14,048 |
No | output | 1 | 7,024 | 15 | 14,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios. | instruction | 0 | 8,403 | 15 | 16,806 |
Tags: graphs
Correct Solution:
```
n,k=map(int,input().split())
arr=list(map(int,input().split()))
dict1={}
for i in range(1,n+1):
dict1[i]=[]
for i in range(k):
dict1[arr[i]].append(i)
ans=0
for i in range(1,n+1):
if(i==1):
if(n==1):
break
if(len(dict1[1])==0):
ans+=2
else:
if(len(dict1[2])==0):
ans+=1
else:
if(dict1[2][-1]<dict1[1][0]):
ans+=1
elif(i==n):
if(len(dict1[n])==0):
ans+=2
else:
if(len(dict1[n-1])==0):
ans+=1
else:
if(dict1[n-1][-1]<dict1[n][0]):
ans+=1
else:
if(len(dict1[i])==0):
ans+=3
else:
if(len(dict1[i-1])==0):
ans+=1
else:
if(dict1[i-1][-1]<dict1[i][0]):
ans+=1
if(len(dict1[i+1])==0):
ans+=1
else:
if(dict1[i+1][-1]<dict1[i][0]):
ans+=1
print(ans)
``` | output | 1 | 8,403 | 15 | 16,807 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios. | instruction | 0 | 8,404 | 15 | 16,808 |
Tags: graphs
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().strip("\r\n")
n, k = map(int, input().split())
x = list(map(int, input().split()))
first = [k] * (n+1)
last = [-1] * (n+1)
for i in range(1, k+1):
last[x[i-1]] = i
for i in range(k, 0, -1):
first[x[i-1]] = i
ans = 0
# print(first)
# print(last)
for i in range(1, n+1):
if last[i] == -1:
ans += 1
for i in range(1, n):
if first[i] >= last[i+1]:
ans += 1
if first[i+1] >= last[i]:
ans += 1
print(ans)
``` | output | 1 | 8,404 | 15 | 16,809 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios. | instruction | 0 | 8,405 | 15 | 16,810 |
Tags: graphs
Correct Solution:
```
import sys
n, k = map(int, input().split())
q = list(map(int, input().split()))
if n == 1:
print(0)
sys.exit()
res = max(n - 2, 0) * 3 + 4
#
# st = set(q)
# res -= len(st)
#
# arr = [2] + [3] * max(n - 2, 0) + [2]
arr = [0] * n
l_flags = [False] * n
r_flags = [False] * n
for i in q:
i -= 1
if arr[i] == 0:
res -= 1
arr[i] += 1
if i > 0 and arr[i - 1] > 0 and not l_flags[i]:
res -= 1
l_flags[i] = True
if i < n - 1 and arr[i + 1] > 0 and not r_flags[i]:
res -= 1
r_flags[i] = True
print(res)
``` | output | 1 | 8,405 | 15 | 16,811 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios. | instruction | 0 | 8,406 | 15 | 16,812 |
Tags: graphs
Correct Solution:
```
from math import *
from collections import *
from bisect import *
import sys
input=sys.stdin.readline
t=1
while(t):
t-=1
n,k=map(int,input().split())
a=list(map(int,input().split()))
vis=set()
for i in a:
vis.add((i,i))
if((i-1,i-1) in vis):
vis.add((i-1,i))
if((i+1,i+1) in vis):
vis.add((i+1,i))
r=n+(2*n)-2
print(r-len(vis))
``` | output | 1 | 8,406 | 15 | 16,813 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios. | instruction | 0 | 8,407 | 15 | 16,814 |
Tags: graphs
Correct Solution:
```
def getn():
return int(input())
def getns():
return [int(x)for x in input().split()]
# n=getn()
# ns=getns()
n,k=getns()
ks=getns()
f=[None]*(n+1)
l=[None]*(n+1)
ans=3*n-2
for i in range(k):
c=ks[i]
if f[c]==None:
f[c]=i
ans-=1
l[c]=i
for i in range(1,n):
if f[i]!=None and l[i+1]!=None and f[i]<l[i+1]:
ans-=1
for i in range(2,n+1):
if f[i]!=None and l[i-1]!=None and f[i]<l[i-1]:
ans-=1
print(ans)
``` | output | 1 | 8,407 | 15 | 16,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios. | instruction | 0 | 8,408 | 15 | 16,816 |
Tags: graphs
Correct Solution:
```
def solve():
n,k = map(int,input().split())
x = list(map(int,input().split()))
fpos = [k for i in range(n+1)]
lpos = [-1 for i in range(n+1)]
for i in range(1,k+1):
lpos[x[i-1]] = i
for i in range(k,0,-1):
fpos[x[i-1]] = i
ans = 0
for i in range(1,n+1):
if lpos[i] == -1:
ans+=1
# print(ans)
for i in range(1,n):
if fpos[i] >= lpos[i+1]:
ans += 1
if fpos[i+1] >= lpos[i]:
ans += 1
# print(i,i+1,ans)
print(ans)
if __name__ == '__main__':
solve()
``` | output | 1 | 8,408 | 15 | 16,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios. | instruction | 0 | 8,409 | 15 | 16,818 |
Tags: graphs
Correct Solution:
```
(n,k)=[int(x) for x in input().split()]
q =[int(x) for x in input().split()]
f = 0
incl = set()
counted={}
for i in range(k-1,-1,-1):
if q[i]+1 in incl and str(q[i])+"+1" not in counted:
f+=1
counted[str(q[i])+"+1"]=True
if q[i]-1 in incl and str(q[i])+"-1" not in counted:
f+=1
counted[str(q[i])+"-1"]=True
incl.add(q[i])
print(3*n-2-f-len(incl))
``` | output | 1 | 8,409 | 15 | 16,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios. | instruction | 0 | 8,410 | 15 | 16,820 |
Tags: graphs
Correct Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
first_appear = {}
last_appear = {}
for i in range(k):
last_appear[a[i]] = i
for i in range(k)[::-1]:
first_appear[a[i]] = i
count = 0
for i in range(1,n+1):
for j in range(i-1,i+2):
if 1 <= j <= n:
if i not in first_appear:
count += 1
elif j not in last_appear:
count += 1
else:
if last_appear[j] < first_appear[i]:
count += 1
print(count)
``` | output | 1 | 8,410 | 15 | 16,821 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Submitted Solution:
```
s = input().split()
n = int(s[0])
k = int(s[1])
s = input().split()
l = [int(x) for x in s]
f = [False for i in range(n + 1)]
for i in range(k):
f[l[i]] = True
ans = 0
L = [0 for i in range(n + 1)]
R = [0 for i in range(n + 1)]
for i in range(k):
R[l[i]] = i
for i in range(k - 1, -1, -1):
L[l[i]] = i
for i in range(1, n + 1):
ans += not f[i]
for i in range(1, n):
if not f[i] or not f[i + 1]:
ans += 2
continue
if L[i] > R[i + 1]:
ans += 1
if L[i + 1] > R[i]:
ans += 1
print(ans)
``` | instruction | 0 | 8,411 | 15 | 16,822 |
Yes | output | 1 | 8,411 | 15 | 16,823 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Submitted Solution:
```
from collections import defaultdict
n, k = [int(item) for item in input().split()]
x = [int(item) for item in input().split()]
occ = defaultdict(list)
for i in range(k):
occ[x[i]].append(i)
ans = 0
for i in range(1, n + 1):
old = ans
if not i in occ:
if i == 1 or i == n:
ans += 2
else:
ans += 3
continue
if i < n and (i + 1 not in occ or occ[i][0] > occ[i + 1][-1]):
ans += 1
if i > 1 and (i - 1 not in occ or occ[i][0] > occ[i - 1][-1]):
ans += 1
print(ans)
``` | instruction | 0 | 8,412 | 15 | 16,824 |
Yes | output | 1 | 8,412 | 15 | 16,825 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Submitted Solution:
```
N,cnt=[int(1e5+5000),int(0)]
class edg:
pnt,nxt=[int(0),int(0)]
fir=[0]*N
ed=[edg()]
for i in range(1,N):
ed.append(edg())
def link(u,v):
global cnt
cnt+=1
ed[cnt].nxt=fir[u]
fir[u]=cnt
ed[cnt].pnt=v
# print(ed[1].pnt,ed[2].pnt,ed[3].pnt)
# print(cnt,ed[1].pnt,ed[1].nxt)
# print(ed[2].pnt)
#print(ed[0].pnt)
n,k=list(map(int,input().split(" ")))
#print(n," ",k)
q=[0]+list(map(int,input().split(" ")))
#print(q[1])
#link(1,1)
#'''
for i in range(1,k+1):
# u,v=[1,1]
u,v=[q[i],i]
link(u,v)
# print(u,v)
#'''
#stay
'''
print(q)
u=5
e=fir[u]
print(ed[2].pnt)
'''
'''
print(ed[2].pnt,ed[3].pnt)
ed[2].pnt=1
print(ed[2].pnt,ed[3].pnt)
'''
ans=int(0)
for i in range(1,n+1):
if(fir[i]==0):
ans+=1
#left
for i in range(2,n+1):
minx,maxx=[(1<<27),-(1<<27)]
u=i
e=fir[u]
while(e!=0):
minx=min(minx,ed[e].pnt)
e=ed[e].nxt
u=i-1
e=fir[u]
while(e!=0):
maxx=max(maxx,ed[e].pnt)
e=ed[e].nxt
if(minx>maxx):
ans+=1
# print(i,i-1)
#right
for i in range(1,n):
minx,maxx=[(1<<27),-(1<<27)]
u=i
e=fir[u]
while(e!=0):
minx=min(minx,ed[e].pnt)
e=ed[e].nxt
u=i+1
e=fir[u]
while(e!=0):
maxx=max(maxx,ed[e].pnt)
e=ed[e].nxt
if(minx>maxx):
ans+=1
# print(i,i+1)
'''
if(i==4):
# print(minx,maxx)
u=i+1
e=fir[u]
while(e!=0):
print(ed[e].pnt)
e=ed[e].nxt
'''
print(ans)
``` | instruction | 0 | 8,413 | 15 | 16,826 |
Yes | output | 1 | 8,413 | 15 | 16,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
count = (n - 1) * 2 + n
b = [[0, -1] for i in range(n + 2)]
c = set()
for i in range(len(a)):
if b[a[i] - 1][0] != 0 and b[a[i]][0] == 0:
count -= 1
if b[a[i] + 1][0] != 0 and b[a[i]][0] == 0:
count -= 1
if b[a[i]][0] == 0:
count -= 1
l1 = (a[i] + 1, a[i])
l2 = (a[i], a[i] + 1)
if b[a[i] + 1][1] > b[a[i]][1] and b[a[i] + 1][0] != 0 and b[a[i]][0] != 0 and l1 not in c and l2 not in c:
count -= 1
c.add(l1)
c.add(l2)
l3 = (a[i], a[i] - 1)
l4 = (a[i] - 1, a[i])
if b[a[i] - 1][1] > b[a[i]][1] and b[a[i] - 1][0] != 0 and b[a[i]][0] != 0 and l3 not in c and l4 not in c:
count -= 1
c.add(l3)
c.add(l4)
b[a[i]][0] = 1
b[a[i]][1] = i
print(count)
``` | instruction | 0 | 8,414 | 15 | 16,828 |
Yes | output | 1 | 8,414 | 15 | 16,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Submitted Solution:
```
n, k = map(int, input().split())
array = list(map(int, input().split()))
d = {i: 0 for i in range(1, n + 1)}
dind = {}
ind = 1
for e in array:
d[e] += 1
dind[e] = ind
ind += 1
sum = 0
prooved = set()
for i, x in enumerate(array):
if x > 1 and (x, x - 1) not in prooved:
if d[x - 1] == 0:
sum += 1
prooved.add((x, x - 1))
else:
if dind[x - 1] < i:
sum += 1
prooved.add((x, x - 1))
if x < n:
if d[x + 1] == 0 and (x, x + 1) not in prooved:
sum += 1
prooved.add((x, x + 1))
else:
if dind[x + 1] < i:
sum += 1
prooved.add((x, x + 1))
for i in range(1, n+1):
if d[i] == 0:
sum += 1
if i != 1:
sum += 1
if i != n:
sum += 1
print(sum)
``` | instruction | 0 | 8,415 | 15 | 16,830 |
No | output | 1 | 8,415 | 15 | 16,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Submitted Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
d={i:0 for i in range(1,n+1)}
for i in range(k):
d[a[i]]+=i+1
s=0
#print(d)
if(n==1):
if(k==0):
print(1)
exit()
else:
print(0)
exit()
for i in range(1,n+1):
if(d[i]==0):
if(i==1)or(i==n):
s+=2
else:
s+=3
else:
if(i==1):
if(d[i]>d[i+1]):
s+=1
elif(i==n):
if(d[i]>d[i-1]):
s+=1
else:
if(d[i+1]>=d[i])and(d[i-1]>=d[i]):
continue
elif(d[i+1]<d[i])and(d[i-1]<d[i]):
s+=2
else:
s+=1
print(s)
``` | instruction | 0 | 8,416 | 15 | 16,832 |
No | output | 1 | 8,416 | 15 | 16,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Submitted Solution:
```
from collections import defaultdict
n,k=map(int,input().split())
ar=[int(x) for x in input().split()]
d=defaultdict(int)
for i in ar:
d[i]+=1
ans=0
ok=False
for i in range(1,n+1):
if(i not in d):
ans+=1
ok=True
if(i<n):
if(i not in d and i+1 not in d):
ans+=2
prev=defaultdict(int)
for i in ar:
d[i]-=1
if(d[i]==0):
d.pop(i)
prev[i]+=1
if (i - 1 > 0 and i - 1 not in d):
ans += 1
ok=True
#print(i-1,i)
if (i + 1 <= n and i + 1 not in d):
ans += 1
ok=True
#print(i,i+1)
if(i-1>0 and i-1 not in d and i-1 not in prev):
ans+=1
ok=True
if(i+1<=n and i+1 not in d and i+1 not in prev):
ans+=1
ok=True
if(not ok):
ans=0
break
print(ans)
``` | instruction | 0 | 8,417 | 15 | 16,834 |
No | output | 1 | 8,417 | 15 | 16,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Submitted Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
import threading
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
n,k=map(int,input().split())
first=[n]*max(n,k)
last=[-1]*max(n,k)
a=list(map(int,input().split()))
for i in range (k):
last[a[i]-1]=i
if first[a[i]-1]==max(n,k):
first[a[i]-1]=i
c=0
for i in range (n):
if first[i]==n:
c+=1
if i!=n-1:
if first[i+1]>=last[i]:
c+=1
if i!=0:
if first[i-1]>=last[i]:
c+=1
print(c)
``` | instruction | 0 | 8,418 | 15 | 16,836 |
No | output | 1 | 8,418 | 15 | 16,837 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3 | instruction | 0 | 8,717 | 15 | 17,434 |
Tags: dfs and similar, dsu, graphs, number theory
Correct Solution:
```
import math
x, y = map(int, input().split())
x -= 1; y -= 1
print(math.gcd(x, y)+1)
``` | output | 1 | 8,717 | 15 | 17,435 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3 | instruction | 0 | 8,718 | 15 | 17,436 |
Tags: dfs and similar, dsu, graphs, number theory
Correct Solution:
```
n, m = map(int, input().split())
while 1 == 1:
if n == m:
print(n)
break
if n > m:
n, m = m, n
m -= (n - 1)
``` | output | 1 | 8,718 | 15 | 17,437 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3 | instruction | 0 | 8,719 | 15 | 17,438 |
Tags: dfs and similar, dsu, graphs, number theory
Correct Solution:
```
import math
n,m=map(int,input().split())
print(math.gcd(n-1,m-1)+1)
``` | output | 1 | 8,719 | 15 | 17,439 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3 | instruction | 0 | 8,720 | 15 | 17,440 |
Tags: dfs and similar, dsu, graphs, number theory
Correct Solution:
```
n, m = map(int, input().split())
while True:
if n == m:
print(n)
break
if n > m:
n, m = m, n
m -= (n - 1)
``` | output | 1 | 8,720 | 15 | 17,441 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3 | instruction | 0 | 8,721 | 15 | 17,442 |
Tags: dfs and similar, dsu, graphs, number theory
Correct Solution:
```
from math import *
n,m=sorted(list(map(int,input().split())))
print(1+gcd(n-1,m-n))
``` | output | 1 | 8,721 | 15 | 17,443 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3
Submitted Solution:
```
import sys
n, m = map(int, sys.stdin.readline().split())
def solve(n, m):
if n == m:
print(n)
return
if n > m:
n,m = m,n
if (m - 1) % (n - 1) == 0:
print(n)
return
print(gcd(2*(n-1), 2*(m-1)))
def gcd(a,b):
if b < a:
a,b = b,a
while b % a != 0:
a,b = b % a, a
return a
solve(n,m)
``` | instruction | 0 | 8,722 | 15 | 17,444 |
No | output | 1 | 8,722 | 15 | 17,445 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3
Submitted Solution:
```
def solve(n, m):
if n == m:
print(n)
return
if n > m:
n,m = m,n
if (m - 1) % (n - 1) == 0:
print(n)
return
print(gcd(2*(n-1), 2*(m-1)))
def gcd(a,b):
if b < a:
a,b = b,a
while b % a != 0:
a,b = b % a, a
return a
``` | instruction | 0 | 8,723 | 15 | 17,446 |
No | output | 1 | 8,723 | 15 | 17,447 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3
Submitted Solution:
```
def solve(n, m):
if n == m:
return n
if n > m:
n,m = m,n
if (m - 1) % (n - 1) == 0:
return n
return gcd(2*(n-1), 2*(m-1))
def gcd(a,b):
if b < a:
a,b = b,a
while b % a != 0:
a,b = b % a, a
return a
``` | instruction | 0 | 8,724 | 15 | 17,448 |
No | output | 1 | 8,724 | 15 | 17,449 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3
Submitted Solution:
```
n,m=sorted(list(map(int,input().split())))
if n%(n-1)==m%(n-1):
print(n)
else:
print(2)
``` | instruction | 0 | 8,725 | 15 | 17,450 |
No | output | 1 | 8,725 | 15 | 17,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path.
Joe's house has n floors, each floor is a segment of m cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right.
Now Joe is on the n-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right.
Joe moves by a particular algorithm. Every second he makes one of the following actions:
* If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved.
* Otherwise consider the next cell in the current direction of the gaze.
* If the cell is empty, then Joe moves into it, the gaze direction is preserved.
* If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite.
* If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits).
Joe calms down as soon as he reaches any cell of the first floor.
The figure below shows an example Joe's movements around the house.
<image>
Determine how many seconds Joe will need to calm down.
Input
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 104).
Next n lines contain the description of Joe's house. The i-th of these lines contains the description of the (n - i + 1)-th floor of the house — a line that consists of m characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall.
It is guaranteed that the first cell of the n-th floor is empty.
Output
Print a single number — the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3 5
..+.#
#+..+
+.#+.
Output
14
Input
4 10
...+.##+.+
+#++..+++#
++.#++++..
.+##.++#.+
Output
42
Input
2 2
..
++
Output
Never | instruction | 0 | 9,380 | 15 | 18,760 |
Tags: brute force
Correct Solution:
```
import sys
from math import *
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
n, m = mints()
a = list(minp())
x = 0
t = 0
d = 1
for i in range(n-1):
b = list(minp())
l = x
r = x
wall = 0
while True:
t += 1
if b[x] == '.':
break
if x + d == m or x + d < 0 or a[x+d] == '#':
wall += 1
d = -d
if wall == 2:
print("Never")
exit(0)
elif a[x+d] == '+':
wall = 0
a[x+d] = '.'
d = -d
elif l <= x+d and x+d <= r:
if d == 1:
t += r-x-1
x = r
else:
t += x-l-1
x = l
else:
x += d
r = max(r,x)
l = min(l,x)
a, b = b, a
print(t)
``` | output | 1 | 9,380 | 15 | 18,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path.
Joe's house has n floors, each floor is a segment of m cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right.
Now Joe is on the n-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right.
Joe moves by a particular algorithm. Every second he makes one of the following actions:
* If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved.
* Otherwise consider the next cell in the current direction of the gaze.
* If the cell is empty, then Joe moves into it, the gaze direction is preserved.
* If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite.
* If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits).
Joe calms down as soon as he reaches any cell of the first floor.
The figure below shows an example Joe's movements around the house.
<image>
Determine how many seconds Joe will need to calm down.
Input
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 104).
Next n lines contain the description of Joe's house. The i-th of these lines contains the description of the (n - i + 1)-th floor of the house — a line that consists of m characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall.
It is guaranteed that the first cell of the n-th floor is empty.
Output
Print a single number — the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3 5
..+.#
#+..+
+.#+.
Output
14
Input
4 10
...+.##+.+
+#++..+++#
++.#++++..
.+##.++#.+
Output
42
Input
2 2
..
++
Output
Never | instruction | 0 | 9,381 | 15 | 18,762 |
Tags: brute force
Correct Solution:
```
n, m = [int(i) for i in input().split()]
current_floor = list(input())
x, t, direction = 0, 0, 1
for i in range(n-1):
floor = list(input())
l, r = x, x
wall = 0
while True:
t += 1
if floor[x] == '.':
break
if (x + direction == m) or (x + direction < 0) or (current_floor[x+direction] == '#'):
wall += 1
direction = -direction
if wall == 2:
print("Never")
exit(0)
elif current_floor[x+direction] == '+':
wall = 0
current_floor[x+direction] = '.'
direction = -direction
elif l <= x+direction and x+direction <= r:
if direction == 1:
t += r-x-1
x = r
else:
t += x-l-1
x = l
else:
x += direction
r = max(r, x)
l = min(l, x)
current_floor, floor = floor, current_floor
print(t)
``` | output | 1 | 9,381 | 15 | 18,763 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a grid with n rows and n columns. Each cell is either empty (denoted by '.') or blocked (denoted by 'X').
Two empty cells are directly connected if they share a side. Two cells (r1, c1) (located in the row r1 and column c1) and (r2, c2) are connected if there exists a sequence of empty cells that starts with (r1, c1), finishes with (r2, c2), and any two consecutive cells in this sequence are directly connected. A connected component is a set of empty cells such that any two cells in the component are connected, and there is no cell in this set that is connected to some cell not in this set.
Your friend Limak is a big grizzly bear. He is able to destroy any obstacles in some range. More precisely, you can choose a square of size k × k in the grid and Limak will transform all blocked cells there to empty ones. However, you can ask Limak to help only once.
The chosen square must be completely inside the grid. It's possible that Limak won't change anything because all cells are empty anyway.
You like big connected components. After Limak helps you, what is the maximum possible size of the biggest connected component in the grid?
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 500) — the size of the grid and Limak's range, respectively.
Each of the next n lines contains a string with n characters, denoting the i-th row of the grid. Each character is '.' or 'X', denoting an empty cell or a blocked one, respectively.
Output
Print the maximum possible size (the number of cells) of the biggest connected component, after using Limak's help.
Examples
Input
5 2
..XXX
XX.XX
X.XXX
X...X
XXXX.
Output
10
Input
5 3
.....
.XXX.
.XXX.
.XXX.
.....
Output
25
Note
In the first sample, you can choose a square of size 2 × 2. It's optimal to choose a square in the red frame on the left drawing below. Then, you will get a connected component with 10 cells, marked blue in the right drawing.
<image>
Submitted Solution:
```
from collections import *
c=ans=0
D=Counter()
n,k=map(int,input().split())
a=[[-2]*(n+2)]+[[-2]+list(input())+[-2] for i in range(n)]+[[-2]*(n+2)]
for i in range(n):
for j in range(n):
a[i+1][j+1]=-(a[i+1][j+1]=='X')
def dfs(i,j):
if a[i][j]: return
a[i][j]=c; D[c]+=1
dfs(i-1,j); dfs(i+1,j)
dfs(i,j-1); dfs(i,j+1)
for i in range(n):
for j in range(n):
if a[i+1][j+1]==0: c+=1; dfs(i+1,j+1)
for j in range(1,n-k+2):
d,s=[0]*(c+3),set()
for i in range(1,n+1):
for y in range(k):
d[a[i][j+y]+2]+=1
if d[a[i][j+y]+2]==1: s.add(a[i][j+y]+2)
if i>k:
for y in range(k):
d[a[i-k][j+y]+2]-=1
if d[a[i][j+y]+2]==0: s.remove(a[i][j+y]+2)
if i>=k:
for q in range(k):
s|={a[i+1][j+q]+2,a[i-k][j+q]+2,a[i-q][j-1]+2,a[i-q][j+k]+2}
t=sum(D[key-2] for key in s-{0,1})+d[1]
if t>ans: ans=t
print(ans)
``` | instruction | 0 | 9,523 | 15 | 19,046 |
No | output | 1 | 9,523 | 15 | 19,047 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a grid with n rows and n columns. Each cell is either empty (denoted by '.') or blocked (denoted by 'X').
Two empty cells are directly connected if they share a side. Two cells (r1, c1) (located in the row r1 and column c1) and (r2, c2) are connected if there exists a sequence of empty cells that starts with (r1, c1), finishes with (r2, c2), and any two consecutive cells in this sequence are directly connected. A connected component is a set of empty cells such that any two cells in the component are connected, and there is no cell in this set that is connected to some cell not in this set.
Your friend Limak is a big grizzly bear. He is able to destroy any obstacles in some range. More precisely, you can choose a square of size k × k in the grid and Limak will transform all blocked cells there to empty ones. However, you can ask Limak to help only once.
The chosen square must be completely inside the grid. It's possible that Limak won't change anything because all cells are empty anyway.
You like big connected components. After Limak helps you, what is the maximum possible size of the biggest connected component in the grid?
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 500) — the size of the grid and Limak's range, respectively.
Each of the next n lines contains a string with n characters, denoting the i-th row of the grid. Each character is '.' or 'X', denoting an empty cell or a blocked one, respectively.
Output
Print the maximum possible size (the number of cells) of the biggest connected component, after using Limak's help.
Examples
Input
5 2
..XXX
XX.XX
X.XXX
X...X
XXXX.
Output
10
Input
5 3
.....
.XXX.
.XXX.
.XXX.
.....
Output
25
Note
In the first sample, you can choose a square of size 2 × 2. It's optimal to choose a square in the red frame on the left drawing below. Then, you will get a connected component with 10 cells, marked blue in the right drawing.
<image>
Submitted Solution:
```
cell = []
mcell = 0
ccell = 0
def grid(a,b,ni):
global cell
global mcell
global ccell
if a < 0 or b < 0 or a > mcell or b > mcell: return 0
if cell[a][b] == 'X': return 0
if cell[a][b] != '.': return 0
cell[a][b] = str(ni)
ccell += 1
grid(a+1,b,ni)
grid(a-1,b,ni)
grid(a,b+1,ni)
grid(a,b-1,ni)
return 0
def checkgrid(i,j,k):
global cell
s = set()
for t in range(k):
r1 = cg(i-1,j+t)
r2 = cg(i+k,j+t)
r3 = cg(i+t,j-1)
r4 = cg(i+t,j+k)
if r1 != -1: s.add(r1)
if r2 != -1: s.add(r2)
if r3 != -1: s.add(r3)
if r4 != -1: s.add(r4)
return(list(s))
def cg(i,j):
global cell
if i < 0 or j < 0 or i > mcell or j > mcell: return -1
if cell[i][j] == 'X': return -1
return int(cell[i][j])
def cgc(i,j,k):
global cell
c = 0
for ii in range(k):
for jj in range(k):
if cell[i+ii][j+jj] == 'X': c += 1
return c
n,k = [int(x) for x in input().split(' ')]
mcell = n-1
for nn in range(n):
cell.append(list(input()))
ni = 0
mres = 0
cellcount = []
for i in range(n):
for j in range(n):
if cell[i][j] == '.':
grid(i,j,ni)
ni += 1
cellcount.append(ccell)
ccell = 0
for i in range(n-k+1):
for j in range(n-k+1):
s = checkgrid(i,j,k)
cc = 0
for ss in s:
cc += cellcount[ss]
cc += cgc(i,j,k)
mres = max(mres,cc)
print(mres)
``` | instruction | 0 | 9,524 | 15 | 19,048 |
No | output | 1 | 9,524 | 15 | 19,049 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a grid with n rows and n columns. Each cell is either empty (denoted by '.') or blocked (denoted by 'X').
Two empty cells are directly connected if they share a side. Two cells (r1, c1) (located in the row r1 and column c1) and (r2, c2) are connected if there exists a sequence of empty cells that starts with (r1, c1), finishes with (r2, c2), and any two consecutive cells in this sequence are directly connected. A connected component is a set of empty cells such that any two cells in the component are connected, and there is no cell in this set that is connected to some cell not in this set.
Your friend Limak is a big grizzly bear. He is able to destroy any obstacles in some range. More precisely, you can choose a square of size k × k in the grid and Limak will transform all blocked cells there to empty ones. However, you can ask Limak to help only once.
The chosen square must be completely inside the grid. It's possible that Limak won't change anything because all cells are empty anyway.
You like big connected components. After Limak helps you, what is the maximum possible size of the biggest connected component in the grid?
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 500) — the size of the grid and Limak's range, respectively.
Each of the next n lines contains a string with n characters, denoting the i-th row of the grid. Each character is '.' or 'X', denoting an empty cell or a blocked one, respectively.
Output
Print the maximum possible size (the number of cells) of the biggest connected component, after using Limak's help.
Examples
Input
5 2
..XXX
XX.XX
X.XXX
X...X
XXXX.
Output
10
Input
5 3
.....
.XXX.
.XXX.
.XXX.
.....
Output
25
Note
In the first sample, you can choose a square of size 2 × 2. It's optimal to choose a square in the red frame on the left drawing below. Then, you will get a connected component with 10 cells, marked blue in the right drawing.
<image>
Submitted Solution:
```
from collections import *
c=ans=0
n,k=map(int,input().split())
a=[[-2]*(n+2)]+[[-2]+list(input())+[-2] for i in range(n)]+[[-2]*(n+2)]
for i in range(n):
for j in range(n):
a[i+1][j+1]=-(a[i+1][j+1]=='X')
D=[0]*(n*n+10)
def dfs(i,j):
l=r=j; u=d=i
for t in range(j+1,n+2):
r=t
if a[i][t]: break
for t in range(j-1,-1,-1):
l=t
if a[i][t]: break
for t in range(i+1,n+2):
d=t
if a[t][j]: break
for t in range(i-1,-1,-1):
u=t
if a[t][j]: break
for t in range(j+1,r): D[c]+=1; a[i][t]=c
for t in range(j-1,l,-1): D[c]+=1; a[i][t]=c
for t in range(i+1,d): D[c]+=1; a[t][j]=c
for t in range(i-1,u,-1): D[c]+=1; a[t][j]=c
for t in range(j+1,r): dfs(i,t)
for t in range(j-1,l,-1): dfs(i,t)
for t in range(i+1,d): dfs(t,j)
for t in range(i-1,u,-1): dfs(t,j)
for i in range(n):
for j in range(n):
if a[i+1][j+1]==0: c+=1; D[c]+=1; a[i+1][j+1]=c; dfs(i+1,j+1)
for x in a: print(x)
for j in range(1,n-k+2):
d=Counter()
for i in range(1,n+1):
for y in range(k): d[a[i][j+y]]+=1
if i>k:
for y in range(k):
d[a[i-k][j+y]]-=1
if d[a[i-k][j+y]]==0: del d[a[i-k][j+y]]
if i>=k:
s=set(d)
for q in range(k):
s|={a[i+1][j+q],a[i-k][j+q],a[i-q][j-1],a[i-q][j+k]}
t=sum(D[key] for key in s-{-2,-1})+d[-1]
if t>ans: ans=t
print(ans)
``` | instruction | 0 | 9,525 | 15 | 19,050 |
No | output | 1 | 9,525 | 15 | 19,051 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a grid with n rows and n columns. Each cell is either empty (denoted by '.') or blocked (denoted by 'X').
Two empty cells are directly connected if they share a side. Two cells (r1, c1) (located in the row r1 and column c1) and (r2, c2) are connected if there exists a sequence of empty cells that starts with (r1, c1), finishes with (r2, c2), and any two consecutive cells in this sequence are directly connected. A connected component is a set of empty cells such that any two cells in the component are connected, and there is no cell in this set that is connected to some cell not in this set.
Your friend Limak is a big grizzly bear. He is able to destroy any obstacles in some range. More precisely, you can choose a square of size k × k in the grid and Limak will transform all blocked cells there to empty ones. However, you can ask Limak to help only once.
The chosen square must be completely inside the grid. It's possible that Limak won't change anything because all cells are empty anyway.
You like big connected components. After Limak helps you, what is the maximum possible size of the biggest connected component in the grid?
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 500) — the size of the grid and Limak's range, respectively.
Each of the next n lines contains a string with n characters, denoting the i-th row of the grid. Each character is '.' or 'X', denoting an empty cell or a blocked one, respectively.
Output
Print the maximum possible size (the number of cells) of the biggest connected component, after using Limak's help.
Examples
Input
5 2
..XXX
XX.XX
X.XXX
X...X
XXXX.
Output
10
Input
5 3
.....
.XXX.
.XXX.
.XXX.
.....
Output
25
Note
In the first sample, you can choose a square of size 2 × 2. It's optimal to choose a square in the red frame on the left drawing below. Then, you will get a connected component with 10 cells, marked blue in the right drawing.
<image>
Submitted Solution:
```
from collections import *
c=ans=0
D=Counter()
n,k=map(int,input().split())
a=[[-2]*(n+2)]+[[-2]+list(input())+[-2] for i in range(n)]+[[-2]*(n+2)]
for i in range(n):
for j in range(n):
a[i+1][j+1]=-(a[i+1][j+1]=='X')
k+=2
def dfs(i,j):
if a[i][j]: return
a[i][j]=c; D[c]+=1
dfs(i-1,j); dfs(i+1,j)
dfs(i,j-1); dfs(i,j+1)
for i in range(n):
for j in range(n):
if a[i+1][j+1]==0: c+=1; dfs(i+1,j+1)
for j in range(n-k+3):
d=Counter()
for i in range(n+2):
for y in range(k): d[a[i][j+y]]+=1
if i>k-1:
for y in range(k): d[a[i-k][j+y]]-=1
if i>=k-1:
d[a[i+1-k][j]]-=1; d[a[i+1-k][j+k-2]]-=1; d[a[i][j]]-=1; d[a[i][j+k-2]]-=1
t=sum(D[key] for key in d if key>0 and d[key]>0)+d[-1]
d[a[i+1-k][j]]+=1; d[a[i+1-k][j+k-2]]+=1; d[a[i][j]]+=1;d[a[i][j+k-2]]+=1
if t>ans: ans=t
print(ans)
``` | instruction | 0 | 9,526 | 15 | 19,052 |
No | output | 1 | 9,526 | 15 | 19,053 |
Provide a correct Python 3 solution for this coding contest problem.
There is a pond with a rectangular shape. The pond is divided into a grid with H rows and W columns of squares. We will denote the square at the i-th row from the top and j-th column from the left by (i,\ j).
Some of the squares in the pond contains a lotus leaf floating on the water. On one of those leaves, S, there is a frog trying to get to another leaf T. The state of square (i,\ j) is given to you by a character a_{ij}, as follows:
* `.` : A square without a leaf.
* `o` : A square with a leaf floating on the water.
* `S` : A square with the leaf S.
* `T` : A square with the leaf T.
The frog will repeatedly perform the following action to get to the leaf T: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."
Snuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T. Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.
Constraints
* 2 ≤ H, W ≤ 100
* a_{ij} is `.`, `o`, `S` or `T`.
* There is exactly one `S` among a_{ij}.
* There is exactly one `T` among a_{ij}.
Input
Input is given from Standard Input in the following format:
H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Output
If the objective is achievable, print the minimum necessary number of leaves to remove. Otherwise, print `-1` instead.
Examples
Input
3 3
S.o
.o.
o.T
Output
2
Input
3 4
S...
.oo.
...T
Output
0
Input
4 3
.S.
.o.
.o.
.T.
Output
-1
Input
10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo
Output
5 | instruction | 0 | 9,737 | 15 | 19,474 |
"Correct Solution:
```
from collections import deque
INF = float("inf")
TO = 0; CAP = 1; REV = 2
class Dinic:
def __init__(self, N):
self.N = N
self.V = [[] for _ in range(N)] # to, cap, rev
# 辺 e = V[n][m] の逆辺は V[e[TO]][e[REV]]
self.level = [0] * N
def add_edge(self, u, v, cap):
self.V[u].append([v, cap, len(self.V[v])])
self.V[v].append([u, 0, len(self.V[u])-1])
def add_edge_undirected(self, u, v, cap): # 未検証
self.V[u].append([v, cap, len(self.V[v])])
self.V[v].append([u, cap, len(self.V[u])-1])
def bfs(self, s: int) -> bool:
self.level = [-1] * self.N
self.level[s] = 0
q = deque()
q.append(s)
while len(q) != 0:
v = q.popleft()
for e in self.V[v]:
if e[CAP] > 0 and self.level[e[TO]] == -1: # capが1以上で未探索の辺
self.level[e[TO]] = self.level[v] + 1
q.append(e[TO])
return True if self.level[self.g] != -1 else False # 到達可能
def dfs(self, v: int, f) -> int:
if v == self.g:
return f
for i in range(self.ite[v], len(self.V[v])):
self.ite[v] = i
e = self.V[v][i]
if e[CAP] > 0 and self.level[v] < self.level[e[TO]]:
d = self.dfs(e[TO], min(f, e[CAP]))
if d > 0: # 増加路
e[CAP] -= d # cap を減らす
self.V[e[TO]][e[REV]][CAP] += d # 反対方向の cap を増やす
return d
return 0
def solve(self, s, g):
self.g = g
flow = 0
while self.bfs(s): # 到達可能な間
self.ite = [0] * self.N
f = self.dfs(s, INF)
while f > 0:
flow += f
f = self.dfs(s, INF)
return flow
H, W = map(int, input().split())
dinic = Dinic(H+W+2)
for i in range(H):
a = input()
for j, a_ in enumerate(a):
if a_=="S":
start = i, j
elif a_=="T":
goal = i, j
if a_!=".":
dinic.add_edge_undirected(H + j, i, 1)
dinic.add_edge_undirected(H+W, start[0], 1<<30)
dinic.add_edge_undirected(H+W, start[1]+H, 1<<30)
dinic.add_edge_undirected(H+W+1, goal[0], 1<<30)
dinic.add_edge_undirected(H+W+1, goal[1]+H, 1<<30)
if start[0]==goal[0] or start[1]==goal[1]:
print(-1)
else:
print(dinic.solve(H+W, H+W+1))
``` | output | 1 | 9,737 | 15 | 19,475 |
Provide a correct Python 3 solution for this coding contest problem.
There is a pond with a rectangular shape. The pond is divided into a grid with H rows and W columns of squares. We will denote the square at the i-th row from the top and j-th column from the left by (i,\ j).
Some of the squares in the pond contains a lotus leaf floating on the water. On one of those leaves, S, there is a frog trying to get to another leaf T. The state of square (i,\ j) is given to you by a character a_{ij}, as follows:
* `.` : A square without a leaf.
* `o` : A square with a leaf floating on the water.
* `S` : A square with the leaf S.
* `T` : A square with the leaf T.
The frog will repeatedly perform the following action to get to the leaf T: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."
Snuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T. Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.
Constraints
* 2 ≤ H, W ≤ 100
* a_{ij} is `.`, `o`, `S` or `T`.
* There is exactly one `S` among a_{ij}.
* There is exactly one `T` among a_{ij}.
Input
Input is given from Standard Input in the following format:
H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Output
If the objective is achievable, print the minimum necessary number of leaves to remove. Otherwise, print `-1` instead.
Examples
Input
3 3
S.o
.o.
o.T
Output
2
Input
3 4
S...
.oo.
...T
Output
0
Input
4 3
.S.
.o.
.o.
.T.
Output
-1
Input
10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo
Output
5 | instruction | 0 | 9,738 | 15 | 19,476 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(1000000)
def FF(E, s, t):
NN = H+W+2
G = [[] for _ in range(NN)]
for a, b, f in E:
G[a].append([b, f, len(G[b])])
G[b].append([a, 0, len(G[a])-1])
def dfs(s, t, f):
if s == t:
return f
used[s] = 1
for i, (b, _f, r) in enumerate(G[s]):
if used[b] or _f == 0: continue
d = dfs(b, t, min(f, _f))
if d > 0:
G[s][i][1] -= d
G[b][r][1] += d
return d
return 0
flow = 0
while 1:
used = [0] * NN
f = dfs(s, t, 10**100)
if f == 0:
return flow
flow += f
E = []
H, W = map(int, input().split())
for i in range(H):
A = input()
for j in range(W):
if A[j] == "o":
E.append((i, j+H, 1))
E.append((j+H, i, 1))
elif A[j] == "S":
S = (i, j+H)
elif A[j] == "T":
T = (i, j+H)
E.append((H+W, S[0], 10**6))
E.append((H+W, S[1], 10**6))
E.append((T[0], H+W+1, 10**6))
E.append((T[1], H+W+1, 10**6))
ff = FF(E, H+W, H+W+1)
print(ff if ff < 10**6 else -1)
``` | output | 1 | 9,738 | 15 | 19,477 |
Provide a correct Python 3 solution for this coding contest problem.
There is a pond with a rectangular shape. The pond is divided into a grid with H rows and W columns of squares. We will denote the square at the i-th row from the top and j-th column from the left by (i,\ j).
Some of the squares in the pond contains a lotus leaf floating on the water. On one of those leaves, S, there is a frog trying to get to another leaf T. The state of square (i,\ j) is given to you by a character a_{ij}, as follows:
* `.` : A square without a leaf.
* `o` : A square with a leaf floating on the water.
* `S` : A square with the leaf S.
* `T` : A square with the leaf T.
The frog will repeatedly perform the following action to get to the leaf T: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."
Snuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T. Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.
Constraints
* 2 ≤ H, W ≤ 100
* a_{ij} is `.`, `o`, `S` or `T`.
* There is exactly one `S` among a_{ij}.
* There is exactly one `T` among a_{ij}.
Input
Input is given from Standard Input in the following format:
H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Output
If the objective is achievable, print the minimum necessary number of leaves to remove. Otherwise, print `-1` instead.
Examples
Input
3 3
S.o
.o.
o.T
Output
2
Input
3 4
S...
.oo.
...T
Output
0
Input
4 3
.S.
.o.
.o.
.T.
Output
-1
Input
10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo
Output
5 | instruction | 0 | 9,739 | 15 | 19,478 |
"Correct Solution:
```
# https://tjkendev.github.io/procon-library/python/max_flow/dinic.html
# Dinic's algorithm
from collections import deque
class Dinic:
def __init__(self, N):
self.N = N
self.G = [[] for i in range(N)]
def add_edge(self, fr, to, cap):
forward = [to, cap, None]
forward[2] = backward = [fr, 0, forward]
self.G[fr].append(forward)
self.G[to].append(backward)
def add_multi_edge(self, v1, v2, cap1, cap2):
edge1 = [v2, cap1, None]
edge1[2] = edge2 = [v1, cap2, edge1]
self.G[v1].append(edge1)
self.G[v2].append(edge2)
def bfs(self, s, t):
self.level = level = [None]*self.N
deq = deque([s])
level[s] = 0
G = self.G
while deq:
v = deq.popleft()
lv = level[v] + 1
for w, cap, _ in G[v]:
if cap and level[w] is None:
level[w] = lv
deq.append(w)
return level[t] is not None
def dfs(self, v, t, f):
if v == t:
return f
level = self.level
for e in self.it[v]:
w, cap, rev = e
if cap and level[v] < level[w]:
d = self.dfs(w, t, min(f, cap))
if d:
e[1] -= d
rev[1] += d
return d
return 0
def flow(self, s, t):
flow = 0
INF = 10**9 + 7
G = self.G
while self.bfs(s, t):
*self.it, = map(iter, self.G)
f = INF
while f:
f = self.dfs(s, t, INF)
flow += f
return flow
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline
h,w = map(int,readline().split())
b = read().split()
#g = [[] for _ in range(h+w+2)]
S = h+w
T = h+w+1
D = Dinic(h+w+2)
for i in range(h):
for j in range(w):
if b[i][j] == "S":
#print("S")
sh,sw = i,j
D.add_edge(S, i, 200)
D.add_edge(S, j+h, 200)
#g[S].append((i,200))
#g[S].append((j+h,200))
elif b[i][j] == "T":
#print("T")
th,tw = i,j
D.add_edge(i, T, 200)
D.add_edge(j+h, T, 200)
#g[i].append((T,200))
#g[j+h].append((T,200))
elif b[i][j] == "o":
#print("o")
D.add_edge(i, j+h, 1)
D.add_edge(j+h, i, 1)
#g[i].append((j+h,1))
#g[j+h].append((i,1))
if sh==th or sw==tw: print(-1)
else:
print(D.flow(S,T))
``` | output | 1 | 9,739 | 15 | 19,479 |
Provide a correct Python 3 solution for this coding contest problem.
There is a pond with a rectangular shape. The pond is divided into a grid with H rows and W columns of squares. We will denote the square at the i-th row from the top and j-th column from the left by (i,\ j).
Some of the squares in the pond contains a lotus leaf floating on the water. On one of those leaves, S, there is a frog trying to get to another leaf T. The state of square (i,\ j) is given to you by a character a_{ij}, as follows:
* `.` : A square without a leaf.
* `o` : A square with a leaf floating on the water.
* `S` : A square with the leaf S.
* `T` : A square with the leaf T.
The frog will repeatedly perform the following action to get to the leaf T: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."
Snuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T. Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.
Constraints
* 2 ≤ H, W ≤ 100
* a_{ij} is `.`, `o`, `S` or `T`.
* There is exactly one `S` among a_{ij}.
* There is exactly one `T` among a_{ij}.
Input
Input is given from Standard Input in the following format:
H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Output
If the objective is achievable, print the minimum necessary number of leaves to remove. Otherwise, print `-1` instead.
Examples
Input
3 3
S.o
.o.
o.T
Output
2
Input
3 4
S...
.oo.
...T
Output
0
Input
4 3
.S.
.o.
.o.
.T.
Output
-1
Input
10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo
Output
5 | instruction | 0 | 9,740 | 15 | 19,480 |
"Correct Solution:
```
from collections import deque
class mf_graph:
n=1
g=[[] for i in range(1)]
pos=[]
def __init__(self,N):
self.n=N
self.g=[[] for i in range(N)]
def add_edge(self,From,To,cap):
assert 0<=From and From<self.n
assert 0<=To and To<self.n
assert 0<=cap
m=len(self.pos)
self.pos.append((From,len(self.g[From])))
self.g[From].append({"to":To,"rev":len(self.g[To]),"cap":cap})
self.g[To].append({"to":From,"rev":len(self.g[From])-1,"cap":0})
return m
def get_edge(self,i):
m=len(self.pos)
assert 0<=i and i<m
_e=self.g[self.pos[i][0]][self.pos[i][1]]
_re=self.g[_e["to"]][_e["rev"]]
return {"from":self.pos[i][0],
"to":_e["to"],
"cap":_e["cap"]+_re["cap"],
"flow":_re["cap"]}
def edges(self):
m=len(self.pos)
result=[]
for i in range(m):
result.append(self.get_edge(i))
return result
def change_edge(self,i,new_cap,new_flow):
m=len(self.pos)
assert 0<=i and i<m
assert 0<=new_flow and new_flow<=new_cap
_e=self.g[self.pos[i][0]][self.pos[i][1]]
_re=self.g[_e["to"]][_e["rev"]]
_e["cap"]=new_cap-new_flow
_re["cap"]=new_flow
def flow(self,s,t,flow_limit=(2**31)-1):
assert 0<=s and s<self.n
assert 0<=t and t<self.n
level=[0 for i in range(self.n)]
Iter=[0 for i in range(self.n)]
que=deque([])
def bfs():
for i in range(self.n):
level[i]=-1
level[s]=0
que=deque([])
que.append(s)
while(len(que)>0):
v=que.popleft()
for e in self.g[v]:
if e["cap"]==0 or level[e["to"]]>=0:continue
level[e["to"]]=level[v]+1
if e["to"]==t:return
que.append(e["to"])
def dfs(func,v,up):
if (v==s):return up
res=0
level_v=level[v]
for i in range(Iter[v],len(self.g[v])):
e=self.g[v][i]
if (level_v<=level[e["to"]] or self.g[e["to"]][e["rev"]]["cap"]==0):continue
d=func(func,e["to"],min(up-res,self.g[e["to"]][e["rev"]]["cap"]))
if d<=0:continue
self.g[v][i]["cap"]+=d
self.g[e["to"]][e["rev"]]["cap"]-=d
res+=d
if res==up:break
return res
flow=0
while(flow<flow_limit):
bfs()
if level[t]==-1:
break
for i in range(self.n):
Iter[i]=0
while(flow<flow_limit):
f=dfs(dfs,t,flow_limit-flow)
if not(f):break
flow+=f
return flow
def min_cut(self,s):
visited=[False for i in range(self.n)]
que=deque([])
que.append(s)
while(len(que)>0):
p=que.popleft()
visited[p]=True
for e in self.g[p]:
if e["cap"] and not(visited[e["to"]]):
visited[e["to"]]=True
que.append(e["to"])
return visited
H,W=map(int,input().split())
a=[list(input()) for i in range(H)]
G=mf_graph(H+W+2)
INF=10**9
s=H+W
t=H+W+1
for i in range(H):
for j in range(W):
if a[i][j]=="S":
G.add_edge(s,i,INF)
G.add_edge(s,j+H,INF)
if a[i][j]=="T":
G.add_edge(i,t,INF)
G.add_edge(j+H,t,INF)
if a[i][j]!=".":
G.add_edge(i,j+H,1)
G.add_edge(j+H,i,1)
ans=G.flow(s,t)
if ans>=INF:
print(-1)
else:
print(ans)
``` | output | 1 | 9,740 | 15 | 19,481 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.