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.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | instruction | 0 | 53,406 | 15 | 106,812 |
Tags: greedy, implementation
Correct Solution:
```
n,m,i,j,a,b = map(int,input().split())
cor = [[1,1],[1,m],[n,1],[n,m]]
flag = 0
dis = 100000001
for k in cor:
x1 = abs(k[0] - i)
y1 = abs(k[1] - j)
if((x1 % a != 0) or (y1 % b != 0)):
continue
x1 = int(x1 / a)
y1 = int(y1 / b)
if((x1 % 2) != (y1 % 2)):
continue
else:
flag = 1
temp = max(x1,y1)
if temp < dis:
dis = temp
if [i,j] in cor:
print(0)
elif flag == 0 or n <= a or m <= b:
print("Poor Inna and pony!")
else:
print(dis)
``` | output | 1 | 53,406 | 15 | 106,813 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | instruction | 0 | 53,407 | 15 | 106,814 |
Tags: greedy, implementation
Correct Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
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----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=2**51, func=lambda a, b: a & b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: math.gcd(a , b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] < key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
n,m,i,j,a,b=map(int,input().split())
def check(x1,y1):
if abs(y1 - j) % b != 0:
return -1
if abs(x1 - i) % a != 0:
return -1
z=(y1-j)//b
z1=(x1-i)//a
ans=10**7
for p in range(2*max(m,n)+1):
q=p-(x1-i)//a
if q<0:
continue
if (z+p+q)%2==0:
p1=(z+p+q)//2
if p1<0:
continue
if p+q-p1>=0:
q1=(p+q-z)//2
t=0
if j+b*(p1-q1)==y1:
if p>q:
if 1<=i+min(p,1)*a<=n:
t+=1
elif q>p:
if 1<=i-min(q,1)*a<=n:
t+=1
else:
if 1<=i+min(p,1)*a<=n or 1<=i-min(q,1)*a<=n:
t+=1
if p1>q1:
if 1<=j+min(p1,1)*b<=m:
t+=1
elif q1>p1:
if 1<=j-min(q1,1)*b<=m:
t+=1
else:
if 1 <= j + min(p1, 1) * b <= m or 1 <= j - min(q1, 1) * b <= m:
t += 1
if t>=2:
ans=min(ans,p+q)
if ans==10**7:
ans=-1
return ans
an=10**7
aw=check(n,1)
if aw!=-1:
an=min(an,aw)
aw=check(1,m)
if aw!=-1:
an=min(an,aw)
aw=check(1,1)
if aw!=-1:
an=min(an,aw)
aw=check(n,m)
if aw!=-1:
an=min(an,aw)
if an==10**7:
print("Poor Inna and pony!")
else:
print(an)
``` | output | 1 | 53,407 | 15 | 106,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | instruction | 0 | 53,408 | 15 | 106,816 |
Tags: greedy, implementation
Correct Solution:
```
def find(x, y):
distx = abs(x - i)
disty = abs(y - j)
if distx == 0 and disty == 0:
return 0
if distx % a != 0 or disty % b != 0:
return float('inf')
elif abs((distx // a) - (disty // b)) % 2 != 0:
return float('inf')
elif distx == 0 and (x + a > n and x - a < 1):
return float('inf')
elif disty == 0 and (y + b > m and y - b < 1):
return float('inf')
else:
return max(distx // a, disty // b)
n, m, i, j, a, b = map(int, input().split())
ans = min(find(1, 1), find(1, m), find(n, 1), find(n, m))
print(ans if ans != float('inf') else 'Poor Inna and pony!')
``` | output | 1 | 53,408 | 15 | 106,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | instruction | 0 | 53,409 | 15 | 106,818 |
Tags: greedy, implementation
Correct Solution:
```
n, m, i, j, a, b = map(int, input().split())
x, y, t = [i - 1, n - i], [j - 1, m - j], []
if all(i < a for i in x) or all(j < b for j in y):
if 0 in x and 0 in y: t = [0]
else:
u = [d // a for d in x if d % a == 0]
v = [d // b for d in y if d % b == 0]
t = [max(i, j) for i in u for j in v if (i + j) % 2 == 0]
print(min(t) if t else 'Poor Inna and pony!')
``` | output | 1 | 53,409 | 15 | 106,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | instruction | 0 | 53,410 | 15 | 106,820 |
Tags: greedy, implementation
Correct Solution:
```
n,m,i,j,a,b = [int(x) for x in input().split()]
i -= 1
j -= 1
best = float('inf')
def find(x,y,a,b,n,m):
n = n//a
m = m//b
x = x//a
y = y//b
total = min(x,y)
x -= total
y -= total
if (y+x)%2 == 0:
if y == 0 and m > 1:
return y+x+total
elif x == 0 and n > 1:
return y+x+total
elif y == 0 and x == 0:
return y+x+total
else:
return float('inf')
else:
return float('inf')
if i%a == 0:
x = i
if j%b == 0:
y = j
best = min(best,find(x,y,a,b,n,m))
#print(1,best)
if (m-j-1)%b == 0:
y = m-j-1
best = min(best,find(x,y,a,b,n,m))
#print(2,best)
if (n-i-1)%a == 0:
x = n-i-1
if j%b == 0:
y = j
best = min(best,find(x,y,a,b,n,m))
#print(3,best)
if (m-j-1)%b == 0:
y = m-j-1
best = min(best,find(x,y,a,b,n,m))
#print(4,best)
if best == float('inf'):
print('Poor Inna and pony!')
else:
print(best)
``` | output | 1 | 53,410 | 15 | 106,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | instruction | 0 | 53,411 | 15 | 106,822 |
Tags: greedy, implementation
Correct Solution:
```
bot = True
n, m, i, j, a, b = map(int, input().split())
x, y, t = [i - 1, n - i], [j - 1, m - j], []
if all(i < a for i in x) or all(j < b for j in y):
if 0 in x and 0 in y: t = [0]
else:
u = [d // a for d in x if d % a == 0]
v = [d // b for d in y if d % b == 0]
t = [max(i, j) for i in u for j in v if (i + j) % 2 == 0]
print(min(t) if t else 'Poor Inna and pony!')
``` | output | 1 | 53,411 | 15 | 106,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | instruction | 0 | 53,412 | 15 | 106,824 |
Tags: greedy, implementation
Correct Solution:
```
n,m,i,j,a,b = map(int,input().split())
path1=path2=path3=path4=10000000000
temp1 =temp2=temp3=temp4=0
if int((i-1)/a) == (i-1)/a and int((j-1)/b) == (j-1)/b:
path1 = (i-1)/a
path_1 = (j-1)/b
# print(path1,path_1)
if path1%2 == 0 and path_1%2 == 0 :
path1 = max(path1,path_1)
temp1 = 1
elif path1%2 != 0 and path_1%2 !=0 :
path1 = max(path1,path_1)
temp1 = 1
else:
path1 = 100000000000
temp1 = 0
if int((i-1)/a) == (i-1)/a and int((m-j)/b) == (m-j)/b:
temp = 1
path2 = (i-1)/a
path_2 = (m-j)/b
if path2%2 == 0 and path_2%2 ==0 :
path2 = max(path2,path_2)
temp2 = 1
elif path2%2 != 0 and path_2%2 !=0 :
path2 = max(path2,path_2)
temp2 = 1
else:
path2 = 100000000000
temp2 = 0
if int((n-i)/a) == (n-i)/a and int((j-1)/b) == (j-1)/b:
temp = 1
path3 = (n-i)/a
path_3 = (j-1)/b
if path3%2 == 0 and path_3%2 ==0 :
path3 = max(path3,path_3)
temp3 = 1
elif path3%2 != 0 and path_3%2 !=0 :
path3 = max(path3,path_3)
temp3 = 1
else:
path3 = 10000000000000
temp3 = 0
if int((n-i)/a) == (n-i)/a and int((m-j)/b) == (m-j)/b:
temp = 1
path4 = (n-i)/a
path_4 = (m-j)/b
if path4%2 == 0 and path_4%2 ==0 :
path4 = max(path4,path_4)
temp4 = 1
elif path4%2 != 0 and path_4%2 !=0 :
path4 = max(path4,path_4)
temp4 = 1
else:
path4 = 1900000000000
temp4 = 0
mini = min(path1,path2,path3,path4)
if ((i + a > n and i - a < 1) or (j+b > m and j-b < 1)) and mini!=0:
print("Poor Inna and pony!")
elif temp1==0 and temp2==0 and temp3==0 and temp4==0:
print("Poor Inna and pony!")
else:
print(int(min(path1,path2,path3,path4)))
``` | output | 1 | 53,412 | 15 | 106,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | instruction | 0 | 53,413 | 15 | 106,826 |
Tags: greedy, implementation
Correct Solution:
```
n,m,i,j,a,b=map(int,input().split())
l=[]
if (i-1)%a==0 and (j-1)%b==0:
s1=(i-1)//a
s2=(j-1)//b
if (s1+s2)%2==0:l.append(max(s1,s2))
if (i-1)%a==0 and (m-j)%b==0:
s1=(i-1)//a
s2=(m-j)//b
if (s1+s2)%2==0:l.append(max(s1,s2))
if (n-i)%a==0 and (j-1)%b==0:
s1=(n-i)//a
s2=(j-1)//b
if (s1+s2)%2==0:l.append(max(s1,s2))
if (n-i)%a==0 and (m-j)%b==0:
s1=(n-i)//a
s2=(m-j)//b
if (s1+s2)%2==0:l.append(max(s1,s2))
if (i,j)==(0,0) or (i,j)==(n,m) or (i,j)==(n,1) or (i,j)==(1,m):print(0)
elif i+a>n and i-a<1:print( "Poor Inna and pony!")
elif j+b>m and j-b<1:print( "Poor Inna and pony!")
elif len(l)==0:print( "Poor Inna and pony!")
else:print(min(l))
``` | output | 1 | 53,413 | 15 | 106,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Submitted Solution:
```
n, m, i, j, a, b = map(int, input().split())
Good = lambda i, a, n, d: None if abs(i-d)%a != 0 else (abs(i-d)//a%2 if abs(i-d)//a >= 1 else (0 if i+a <= n or i-a >= 1 else -1))
res = None
xx, yy = [1, n], [1, m]
for dx in xx:
for dy in yy:
tx, ty = Good(i, a, n, dx), Good(j, b, m, dy)
if tx is None or ty is None:
continue
if tx == -1 or ty == -1:
if (tx == -1 and abs(j-dy)//b == 0) or (ty == -1 and abs(i-dx)//a == 0):
res = 0
continue
if tx != ty:
continue
new_res = max(abs(i-dx)//a, abs(j-dy)//b)
res = new_res if res is None or res > new_res else res
print('Poor Inna and pony!' if res is None else res)
``` | instruction | 0 | 53,414 | 15 | 106,828 |
Yes | output | 1 | 53,414 | 15 | 106,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Submitted Solution:
```
from sys import setrecursionlimit, exit
from math import ceil, floor, acos, pi
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from functools import reduce
import itertools
setrecursionlimit(10**7)
RI=lambda x=' ': list(map(int,input().split(x)))
RS=lambda x=' ': input().rstrip().split(x)
dX= [-1, 1, 0, 0,-1, 1,-1, 1]
dY= [ 0, 0,-1, 1, 1,-1,-1, 1]
mod=int(1e9+7)
eps=1e-6
MAX=1e9
#################################################
n, m, i, j, a, b = RI()
min_val=MAX
if (i,j)==(1,1) or (i,j)==(1,m) or (i,j)==(n,1) or (i,j)==(n,m):
print(0)
else:
if (n-i)%a==0 and (m-j)%b==0 and ((n-i)//a - (m-j)//b)%2==0 and a<=n-1 and b<=m-1:
min_val=min(min_val, max((n-i)//a,(m-j)//b))
if (i-1)%a==0 and (m-j)%b==0 and ((i-1)//a - (m-j)//b)%2==0 and a<=n-1 and b<=m-1:
min_val=min(min_val, max((i-1)//a,(m-j)//b))
if (n-i)%a==0 and (j-1)%b==0 and ((n-i)//a - (j-1)//b)%2==0 and a<=n-1 and b<=m-1:
min_val=min(min_val, max((n-i)//a,(j-1)//b))
if (i-1)%a==0 and (j-1)%b==0 and ((i-1)//a - (j-1)//b)%2==0 and a<=n-1 and b<=m-1:
min_val=min(min_val, max((i-1)//a,(j-1)//b))
if min_val==MAX:
print("Poor Inna and pony!")
else:
print(min_val)
``` | instruction | 0 | 53,415 | 15 | 106,830 |
Yes | output | 1 | 53,415 | 15 | 106,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Submitted Solution:
```
n,m,i,j,a,b=0,0,0,0,0,0
inf=1000000000
def check(x,y):
if x==i and y==j: return 0
if i-a<1 and i+a>n: return inf
if j-b<1 and j+b>m: return inf
if (abs(x-i)%a) !=0: return inf
if (abs(y-j)%b)!=0 : return inf
cost=0
cost=max(abs(x-i)//a, abs(y-j)//b)
tmp=abs(x-i)//a + abs(y-j)//b
if tmp&1: return inf
return cost
def solve():
ans=inf
ans=min(check(1,m),check(n,1))
ans=min(ans,min(check(n,m),check(1,1)))
if ans==inf: return "Poor Inna and pony!"
else: return ans
n,m,i,j,a,b=map(int,input().split())
print(solve())
``` | instruction | 0 | 53,416 | 15 | 106,832 |
Yes | output | 1 | 53,416 | 15 | 106,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Submitted Solution:
```
#import math
#n, m = input().split()
#n = int (n)
#m = int (m)
#n, m, k= input().split()
#n = int (n)
#m = int (m)
#k = int (k)
#l = int(l)
#m = int(input())
#s = input()
##t = input()
#a = list(map(char, input().split()))
#a.append('.')
#print(l)
#a = list(map(int, input().split()))
#c = sorted(c)
#x1, y1, x2, y2 =map(int,input().split())
#n = int(input())
#f = []
#t = [0]*n
#f = [(int(s1[0]),s1[1]), (int(s2[0]),s2[1]), (int(s3[0]), s3[1])]
#f1 = sorted(t, key = lambda tup: tup[0])
x1, y1, x3, y3, x2, y2 = input().split()
x1 = int(x1)
x2 = int(x2)
x3 = int(x3)
y1 = int(y1)
y2 = int(y2)
y3 = int(y3)
mi = x1*y1
if((x3-1) % x2 == 0 and (y3-1) % y2 == 0 and ((x3-1) / x2) %2 == ((y3-1)/ y2)%2):
mi = min(mi, max((x3-1)/x2,(y3-1)/y2))
if((x3-1) % x2 == 0 and (y1-y3) % y2 == 0 and ((x3-1) / x2) %2 == ((y1-y3)/ y2)%2):
mi = min(mi, max((x3-1)/x2,(y1-y3)/y2))
if((x1-x3) % x2 == 0 and (y3-1) % y2 == 0 and ((x1-x3) / x2) % 2 == ((y3-1)/ y2)%2):
mi = min(mi, max((x1-x3)/x2,(y3-1)/y2))
if((x1-x3) % x2 == 0 and (y1-y3) % y2 == 0 and ((x1-x3) / x2)%2 == ((y1-y3)/ y2)%2):
mi = min(mi, max((x1-x3)/x2,(y1-y3)/y2))
if (mi == x1*y1 or ( (x2 >= x3 and x2 > x1-x3) and mi != 0)or ((y2 >= y3 and y2 > y1-y3) and mi != 0)):
print("Poor Inna and pony!")
else:
print(int(mi))
``` | instruction | 0 | 53,417 | 15 | 106,834 |
Yes | output | 1 | 53,417 | 15 | 106,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Submitted Solution:
```
n, m, x, y, a, b = map(int, input().split())
if(x == 1 and y == 1 or x == 1 and y == m or x == n and y == 1 or x == n and y == m):
print(0)
exit(0)
if(x - a <= 0 and x + a > n or y + b > m and y - b <= 0):
print('Poor Inna and pony!')
exit(0)
ans = max(n, m)
for d in [(1, 1), (n, 1), (1, m), (n, m)]:
dx, dy = abs(x-d[0]), abs(y-d[1])
if dx % a == 0 and dy % b == 0 and (dx/a) % 2 == (dy/b) % 2:
ans = min(ans, dx/a + dy/b)
print(ans if ans != max(n, m) else 'Poor Inna and pony!')
``` | instruction | 0 | 53,418 | 15 | 106,836 |
No | output | 1 | 53,418 | 15 | 106,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Submitted Solution:
```
z=input()
x=z.split(" ")
y=[int(o) for o in x]
n=y[0]
m=y[1]
i=y[2]
j=y[3]
a=y[4]
b=y[5]
l=[]
if (n-i)%a==0 and (m-j)%b==0 :
if ((n-i)/a-(m-j)/b)%2==0 :
l.append(max((n-i)/a,(m-j)/b))
if (i-1)%a==0 and (j-1)%b==0 :
if ((i-1)/a-(j-1)/b)%2==0 :
l.append(max((i-1)/a,(j-1)/b))
if (i-1)%a==0 and (m-j)%b==0 :
if ((i-1)/a-(m-j)/b)%2==0 :
l.append(max((i-1)/a,(m-j)/b))
if (n-i)%a==0 and (j-1)%b==0 :
if ((n-i)/a-(j-1)/b)%2==0 :
l.append(max((n-i)/a,(j-1)/b))
if len(l)==0 :
print("Poor Inna and pony!")
else:
print(int(max(l)))
``` | instruction | 0 | 53,419 | 15 | 106,838 |
No | output | 1 | 53,419 | 15 | 106,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Submitted Solution:
```
#!/usr/bin/python3
def readln(): return list(map(int, input().split()))
n, m, i, j, a, b = readln()
def solve(w, h):
print(w, h)
if w == 0 and h == 0:
return 0
if w == 0 and a <= n and b % 2 == 0:
return b
if h == 0 and b <= m and a % 2 == 0:
return a
if w == 0 or h == 0:
return 1 << 30
if (w + h) % 2 == 0:
return max(w, h)
return 1 << 30
ans = min([1 << 30] + [solve(abs(i - p) // a, abs(j - q) // b) for p, q in ((1, 1), (1, m), (n, 1), (n, m)) if abs(i - p) % a == 0 and abs(j - q) % b == 0])
print('Poor Inna and pony!' if ans == 2**30 else ans)
``` | instruction | 0 | 53,420 | 15 | 106,840 |
No | output | 1 | 53,420 | 15 | 106,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Submitted Solution:
```
n,m,i,j,a,b=map(int,input().split())
def solve(x,y):
if (not abs(x-i)%a) and (not abs(m-y)%b) and (not (abs(x-i)//a + (y-j)//b)%2):
return max(abs(x-i)//a,abs(y-j)//b)
else:
return -1
vals=[(1,1),(n,1),(1,m),(n,m)]
ans=100000000
for values in vals:
res=solve(values[0],values[1])
if res!=-1:
ans=min(res,ans)
if ans==-1:
print("Poor Inna and pony")
else:
print(ans)
``` | instruction | 0 | 53,421 | 15 | 106,842 |
No | output | 1 | 53,421 | 15 | 106,843 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid of size W Γ H surrounded by walls. Some cells in the grid are occupied by slimes. The slimes want to unite with each other and become a "King Slime".
In each move, a slime can move to east, west, south and north direction until it enters a cell occupied by another slime or hit the surrounding wall. If two slimes come together, they unite and become a new slime.
Your task is write a program which calculates the minimum number of moves that all the slimes unite and become a King Slime. Suppose slimes move one by one and they never move simultaneously.
Input
The first line contains three integers N (2 β€ N β€ 40,000), W and H (1 β€ W, H β€ 100,000), which denote the number of slimes, the width and the height of the grid respectively.
The following N lines describe the initial coordinates of the slimes. The i-th line contains two integers xi (1 β€ xi β€ W) and yi (1 β€ yi β€ H), which indicate the coordinates of the i-th slime . All the coordinates are 1-based.
You may assume that each cell is occupied by at most one slime initially.
Output
Output the minimum number of moves that all the slimes unite and become a King Slime.
Examples
Input
4 3 3
1 1
1 3
3 1
3 3
Output
3
Input
2 3 3
2 2
3 3
Output
2
Input
2 4 4
2 2
3 3
Output
3
Input
2 4 4
2 2
2 3
Output
1 | instruction | 0 | 53,877 | 15 | 107,754 |
"Correct Solution:
```
import sys
class UnionFind:
def __init__(self,sz):
self.__ranks = [1] * sz
self.__parents = [ i for i in range(sz) ]
def find_parent(self, x):
if x == self.__parents[x]:
return x
else:
self.__parents[x] = self.find_parent(self.__parents[x])
return self.__parents[x]
def same(self, x, y):
return self.find_parent(x) == self.find_parent(y)
def unite(self, x, y):
px = self.find_parent(x)
py = self.find_parent(y)
if px == py:
return
if self.__ranks[px] > self.__ranks[py]:
self.__parents[py] = px
else:
self.__parents[px] = py
if self.__ranks[px] == self.__ranks[py]:
self.__ranks[py] += 1
def main():
n,w,h = map(int, input().split())
uf = UnionFind(n)
xd = {}
yd = {}
is_edges_slime = False
for i in range(n):
x,y = map(int, sys.stdin.readline().split())
if x == 1 or x == w:
is_edges_slime = True
if y == 1 or y == h:
is_edges_slime = True
if x in xd:
uf.unite(xd[x], i)
else:
xd[x] = i
if y in yd:
uf.unite(yd[y], i)
else:
yd[y] = i
root = set()
for i in range(n):
root.add( uf.find_parent(i) )
if len(root) == 1:
print(n - 1)
else:
ans = n - len(root) # ????Β΄?????????Β¨??????
ans += len(root) - (1 if is_edges_slime else 0) # ???????????????
ans += len(root) - 1 # ????????Β°???????????????
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 53,877 | 15 | 107,755 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid of size W Γ H surrounded by walls. Some cells in the grid are occupied by slimes. The slimes want to unite with each other and become a "King Slime".
In each move, a slime can move to east, west, south and north direction until it enters a cell occupied by another slime or hit the surrounding wall. If two slimes come together, they unite and become a new slime.
Your task is write a program which calculates the minimum number of moves that all the slimes unite and become a King Slime. Suppose slimes move one by one and they never move simultaneously.
Input
The first line contains three integers N (2 β€ N β€ 40,000), W and H (1 β€ W, H β€ 100,000), which denote the number of slimes, the width and the height of the grid respectively.
The following N lines describe the initial coordinates of the slimes. The i-th line contains two integers xi (1 β€ xi β€ W) and yi (1 β€ yi β€ H), which indicate the coordinates of the i-th slime . All the coordinates are 1-based.
You may assume that each cell is occupied by at most one slime initially.
Output
Output the minimum number of moves that all the slimes unite and become a King Slime.
Examples
Input
4 3 3
1 1
1 3
3 1
3 3
Output
3
Input
2 3 3
2 2
3 3
Output
2
Input
2 4 4
2 2
3 3
Output
3
Input
2 4 4
2 2
2 3
Output
1 | instruction | 0 | 53,878 | 15 | 107,756 |
"Correct Solution:
```
class UnionFind:
def __init__(self, n):
self.data=[-1 for i in range(n)]
def root(self,x):
if self.data[x]<0:
return x
else:
self.data[x]=self.root(self.data[x])
return self.data[x]
def uni(self,x,y):
x=self.root(x)
y=self.root(y)
if(x==y):
return
if self.data[y]<self.data[x]:
x,y=y,x
self.data[x]+= self.data[y]
self.data[y] = x
def same(self,x,y):
return self.root(x)==self.root(y)
def size(self,x):
return -self.data[self.root(x)]
n,w,h=map(int,input().split())
slime_w = [[]for i in range(w)]
slime_h = [[]for i in range(h)]
uf=UnionFind(n)
f=False
for i in range(n):
x,y=map(int,input().split())
if x==1 or y==1 or x==w or y==h:
f=True
x,y=x-1,y-1
slime_w[x].append(i)
slime_h[y].append(i)
for i in range(h):
if len(slime_h[i])>1:
p=slime_h[i][0]
for j in range(1,len(slime_h[i])):
uf.uni(p,slime_h[i][j])
for i in range(w):
if len(slime_w[i])>1:
p=slime_w[i][0]
for j in range(1,len(slime_w[i])):
uf.uni(p,slime_w[i][j])
count=[-e-1 for e in uf.data if e<0]
if f:
print(sum(count)+(len(count)-1+len(count)-1 if len(count)!=1 else 0))
else:
print(sum(count)+(len(count)+len(count)-1 if len(count)!=1 else 0))
``` | output | 1 | 53,878 | 15 | 107,757 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid of size W Γ H surrounded by walls. Some cells in the grid are occupied by slimes. The slimes want to unite with each other and become a "King Slime".
In each move, a slime can move to east, west, south and north direction until it enters a cell occupied by another slime or hit the surrounding wall. If two slimes come together, they unite and become a new slime.
Your task is write a program which calculates the minimum number of moves that all the slimes unite and become a King Slime. Suppose slimes move one by one and they never move simultaneously.
Input
The first line contains three integers N (2 β€ N β€ 40,000), W and H (1 β€ W, H β€ 100,000), which denote the number of slimes, the width and the height of the grid respectively.
The following N lines describe the initial coordinates of the slimes. The i-th line contains two integers xi (1 β€ xi β€ W) and yi (1 β€ yi β€ H), which indicate the coordinates of the i-th slime . All the coordinates are 1-based.
You may assume that each cell is occupied by at most one slime initially.
Output
Output the minimum number of moves that all the slimes unite and become a King Slime.
Examples
Input
4 3 3
1 1
1 3
3 1
3 3
Output
3
Input
2 3 3
2 2
3 3
Output
2
Input
2 4 4
2 2
3 3
Output
3
Input
2 4 4
2 2
2 3
Output
1 | instruction | 0 | 53,879 | 15 | 107,758 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
class UnionFind:
def __init__(self, size):
self.table = [-1 for _ in range(size)]
def find(self, x):
if self.table[x] < 0:
return x
else:
self.table[x] = self.find(self.table[x])
return self.table[x]
def union(self, x, y):
s1 = self.find(x)
s2 = self.find(y)
if s1 != s2:
if self.table[s1] <= self.table[s2]:
self.table[s1] += self.table[s2]
self.table[s2] = s1
else:
self.table[s2] += self.table[s1]
self.table[s1] = s2
return True
return False
def subsetall(self):
a = []
for i in range(len(self.table)):
if self.table[i] < 0:
a.append((i, -self.table[i]))
return a
def main():
rr = []
def f(n,w,h):
wd = {}
hd = {}
kf = 0
uf = UnionFind(n+1)
uc = 0
for i in range(n):
x,y = LI()
if x in wd:
if uf.union(wd[x], i):
uc += 1
else:
wd[x] = i
if y in hd:
if uf.union(hd[y], i):
uc += 1
else:
hd[y] = i
if x == 1 or y == 1 or x == w or y == h:
kf = 1
r = uc
nk = n - uc
if nk == 1:
return r
r += nk - kf + nk - 1
return r
while 1:
n,w,h = LI()
if n == 0:
break
rr.append(f(n,w,h))
# print(n, rr[-1])
break
return '\n'.join(map(str, rr))
print(main())
``` | output | 1 | 53,879 | 15 | 107,759 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid of size W Γ H surrounded by walls. Some cells in the grid are occupied by slimes. The slimes want to unite with each other and become a "King Slime".
In each move, a slime can move to east, west, south and north direction until it enters a cell occupied by another slime or hit the surrounding wall. If two slimes come together, they unite and become a new slime.
Your task is write a program which calculates the minimum number of moves that all the slimes unite and become a King Slime. Suppose slimes move one by one and they never move simultaneously.
Input
The first line contains three integers N (2 β€ N β€ 40,000), W and H (1 β€ W, H β€ 100,000), which denote the number of slimes, the width and the height of the grid respectively.
The following N lines describe the initial coordinates of the slimes. The i-th line contains two integers xi (1 β€ xi β€ W) and yi (1 β€ yi β€ H), which indicate the coordinates of the i-th slime . All the coordinates are 1-based.
You may assume that each cell is occupied by at most one slime initially.
Output
Output the minimum number of moves that all the slimes unite and become a King Slime.
Examples
Input
4 3 3
1 1
1 3
3 1
3 3
Output
3
Input
2 3 3
2 2
3 3
Output
2
Input
2 4 4
2 2
3 3
Output
3
Input
2 4 4
2 2
2 3
Output
1 | instruction | 0 | 53,880 | 15 | 107,760 |
"Correct Solution:
```
from collections import defaultdict
N, W, H = map(int, input().split())
*p, = range(N)
def root(x):
if x == p[x]:
return x
p[x] = y = root(p[x])
return y
def unite(x, y):
px = root(x); py = root(y)
if px < py:
p[py] = px
else:
p[px] = py
X = defaultdict(list)
Y = defaultdict(list)
for i in range(N):
x, y = map(int, input().split())
X[x].append(i)
Y[y].append(i)
for x, vs in X.items():
prv = vs[0]
for v in vs[1:]:
unite(prv, v)
prv = v
for y, vs in Y.items():
prv = vs[0]
for v in vs[1:]:
unite(prv, v)
prv = v
cnt = 0
for i in range(N):
pt = root(i)
if pt == i:
cnt += 1
ans = N-cnt + 2*(cnt-1)
if cnt > 1 and 1 not in X and W not in X and 1 not in Y and H not in Y:
ans += 1
print(ans)
``` | output | 1 | 53,880 | 15 | 107,761 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a strip with an infinite number of cells. Cells are numbered starting with 0. Initially the cell i contains a ball with the number i.
There are n pockets located at cells a_1, β¦, a_n. Each cell contains at most one pocket.
Filtering is the following sequence of operations:
* All pockets at cells a_1, β¦, a_n open simultaneously, which makes balls currently located at those cells disappear. After the balls disappear, the pockets close again.
* For each cell i from 0 to β, if the cell i contains a ball, we move that ball to the free cell j with the lowest number. If there is no free cell j < i, the ball stays at the cell i.
Note that after each filtering operation each cell will still contain exactly one ball.
For example, let the cells 1, 3 and 4 contain pockets. The initial configuration of balls is shown below (underscores display the cells with pockets):
0 1 2 3 4 5 6 7 8 9 ...
After opening and closing the pockets, balls 1, 3 and 4 disappear:
0 2 5 6 7 8 9 ...
After moving all the balls to the left, the configuration looks like this:
0 2 5 6 7 8 9 10 11 12 ...
Another filtering repetition results in the following:
0 5 8 9 10 11 12 13 14 15 ...
You have to answer m questions. The i-th of these questions is "what is the number of the ball located at the cell x_i after k_i repetitions of the filtering operation?"
Input
The first line contains two integers n and m β the number of pockets and questions respectively (1 β€ n, m β€ 10^5).
The following line contains n integers a_1, β¦, a_n β the numbers of cells containing pockets (0 β€ a_1 < β¦ < a_n β€ 10^9).
The following m lines describe questions. The i-th of these lines contains two integers x_i and k_i (0 β€ x_i, k_i β€ 10^9).
Output
Print m numbers β answers to the questions, in the same order as given in the input.
Example
Input
3 15
1 3 4
0 0
1 0
2 0
3 0
4 0
0 1
1 1
2 1
3 1
4 1
0 2
1 2
2 2
3 2
4 2
Output
0
1
2
3
4
0
2
5
6
7
0
5
8
9
10
Submitted Solution:
```
n_luz, k_vopr = map(int, input().split())
mass_luz = list(map(int, input().split()))
main_mass = []
for i in range(10000):
main_mass.append(i)
mass_of_answ = []
def otvet_na_vopros(op_vop):
for _ in range(op_vop[1]):
[main_mass.pop(luza) for luza in sorted(mass_luz, reverse=True)]
print(main_mass)
return main_mass[op_vop[0]]
for i in range(k_vopr):
op_vopr = list(map(int, input().split()))
mass_of_answ.append(otvet_na_vopros(op_vopr))
main_mass.clear()
for i in range(10000):
main_mass.append(i)
for i in mass_of_answ:
print(i)
``` | instruction | 0 | 53,944 | 15 | 107,888 |
No | output | 1 | 53,944 | 15 | 107,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a strip with an infinite number of cells. Cells are numbered starting with 0. Initially the cell i contains a ball with the number i.
There are n pockets located at cells a_1, β¦, a_n. Each cell contains at most one pocket.
Filtering is the following sequence of operations:
* All pockets at cells a_1, β¦, a_n open simultaneously, which makes balls currently located at those cells disappear. After the balls disappear, the pockets close again.
* For each cell i from 0 to β, if the cell i contains a ball, we move that ball to the free cell j with the lowest number. If there is no free cell j < i, the ball stays at the cell i.
Note that after each filtering operation each cell will still contain exactly one ball.
For example, let the cells 1, 3 and 4 contain pockets. The initial configuration of balls is shown below (underscores display the cells with pockets):
0 1 2 3 4 5 6 7 8 9 ...
After opening and closing the pockets, balls 1, 3 and 4 disappear:
0 2 5 6 7 8 9 ...
After moving all the balls to the left, the configuration looks like this:
0 2 5 6 7 8 9 10 11 12 ...
Another filtering repetition results in the following:
0 5 8 9 10 11 12 13 14 15 ...
You have to answer m questions. The i-th of these questions is "what is the number of the ball located at the cell x_i after k_i repetitions of the filtering operation?"
Input
The first line contains two integers n and m β the number of pockets and questions respectively (1 β€ n, m β€ 10^5).
The following line contains n integers a_1, β¦, a_n β the numbers of cells containing pockets (0 β€ a_1 < β¦ < a_n β€ 10^9).
The following m lines describe questions. The i-th of these lines contains two integers x_i and k_i (0 β€ x_i, k_i β€ 10^9).
Output
Print m numbers β answers to the questions, in the same order as given in the input.
Example
Input
3 15
1 3 4
0 0
1 0
2 0
3 0
4 0
0 1
1 1
2 1
3 1
4 1
0 2
1 2
2 2
3 2
4 2
Output
0
1
2
3
4
0
2
5
6
7
0
5
8
9
10
Submitted Solution:
```
a=input().split(" ")
b=input().split(" ")
p=0
cont =0
serie=list()
resp=list()
for i in range(int(b[int(a[0])-1])+1):
serie.append(p)
p=p+1
for i in range(int(a[1])):
data=input().split(" ")
while int(data[1])>cont:
for j in range(int(a[0])):
serie.append(p)
p=p+1
serie.pop(int(b[len(b)-j-1]))
cont =cont+1
resp.append(int(serie[int(data[0])]))
data=""
for i in range(len(resp)):
print(resp[i])
``` | instruction | 0 | 53,945 | 15 | 107,890 |
No | output | 1 | 53,945 | 15 | 107,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a strip with an infinite number of cells. Cells are numbered starting with 0. Initially the cell i contains a ball with the number i.
There are n pockets located at cells a_1, β¦, a_n. Each cell contains at most one pocket.
Filtering is the following sequence of operations:
* All pockets at cells a_1, β¦, a_n open simultaneously, which makes balls currently located at those cells disappear. After the balls disappear, the pockets close again.
* For each cell i from 0 to β, if the cell i contains a ball, we move that ball to the free cell j with the lowest number. If there is no free cell j < i, the ball stays at the cell i.
Note that after each filtering operation each cell will still contain exactly one ball.
For example, let the cells 1, 3 and 4 contain pockets. The initial configuration of balls is shown below (underscores display the cells with pockets):
0 1 2 3 4 5 6 7 8 9 ...
After opening and closing the pockets, balls 1, 3 and 4 disappear:
0 2 5 6 7 8 9 ...
After moving all the balls to the left, the configuration looks like this:
0 2 5 6 7 8 9 10 11 12 ...
Another filtering repetition results in the following:
0 5 8 9 10 11 12 13 14 15 ...
You have to answer m questions. The i-th of these questions is "what is the number of the ball located at the cell x_i after k_i repetitions of the filtering operation?"
Input
The first line contains two integers n and m β the number of pockets and questions respectively (1 β€ n, m β€ 10^5).
The following line contains n integers a_1, β¦, a_n β the numbers of cells containing pockets (0 β€ a_1 < β¦ < a_n β€ 10^9).
The following m lines describe questions. The i-th of these lines contains two integers x_i and k_i (0 β€ x_i, k_i β€ 10^9).
Output
Print m numbers β answers to the questions, in the same order as given in the input.
Example
Input
3 15
1 3 4
0 0
1 0
2 0
3 0
4 0
0 1
1 1
2 1
3 1
4 1
0 2
1 2
2 2
3 2
4 2
Output
0
1
2
3
4
0
2
5
6
7
0
5
8
9
10
Submitted Solution:
```
x,y=map(int,input().split())
s=list(map(int,input().split()))
p=[]
c=0
for i in range(y):
a,b=map(int,input().split())
for i in range(x):
if a<s[i]-i:
break
else:
c=c+1
z=a+c+(b-1)*x
p.append(z)
c=0
for i in p:
print(i)
``` | instruction | 0 | 53,946 | 15 | 107,892 |
No | output | 1 | 53,946 | 15 | 107,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a strip with an infinite number of cells. Cells are numbered starting with 0. Initially the cell i contains a ball with the number i.
There are n pockets located at cells a_1, β¦, a_n. Each cell contains at most one pocket.
Filtering is the following sequence of operations:
* All pockets at cells a_1, β¦, a_n open simultaneously, which makes balls currently located at those cells disappear. After the balls disappear, the pockets close again.
* For each cell i from 0 to β, if the cell i contains a ball, we move that ball to the free cell j with the lowest number. If there is no free cell j < i, the ball stays at the cell i.
Note that after each filtering operation each cell will still contain exactly one ball.
For example, let the cells 1, 3 and 4 contain pockets. The initial configuration of balls is shown below (underscores display the cells with pockets):
0 1 2 3 4 5 6 7 8 9 ...
After opening and closing the pockets, balls 1, 3 and 4 disappear:
0 2 5 6 7 8 9 ...
After moving all the balls to the left, the configuration looks like this:
0 2 5 6 7 8 9 10 11 12 ...
Another filtering repetition results in the following:
0 5 8 9 10 11 12 13 14 15 ...
You have to answer m questions. The i-th of these questions is "what is the number of the ball located at the cell x_i after k_i repetitions of the filtering operation?"
Input
The first line contains two integers n and m β the number of pockets and questions respectively (1 β€ n, m β€ 10^5).
The following line contains n integers a_1, β¦, a_n β the numbers of cells containing pockets (0 β€ a_1 < β¦ < a_n β€ 10^9).
The following m lines describe questions. The i-th of these lines contains two integers x_i and k_i (0 β€ x_i, k_i β€ 10^9).
Output
Print m numbers β answers to the questions, in the same order as given in the input.
Example
Input
3 15
1 3 4
0 0
1 0
2 0
3 0
4 0
0 1
1 1
2 1
3 1
4 1
0 2
1 2
2 2
3 2
4 2
Output
0
1
2
3
4
0
2
5
6
7
0
5
8
9
10
Submitted Solution:
```
x,y=map(int,input().split())
s=list(map(int,input().split()))
p=[]
c=0
for i in range(y):
a,b=map(int,input().split())
for i in range(x):
if a<s[i]-i:
break
else:
c=c+1
if a<s[0]:
z=a
else:
z=a+c+(b-1)*x
p.append(z)
c=0
for i in p:
print(i)
``` | instruction | 0 | 53,947 | 15 | 107,894 |
No | output | 1 | 53,947 | 15 | 107,895 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field. | instruction | 0 | 54,487 | 15 | 108,974 |
Tags: binary search, dp
Correct Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq,bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
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----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=2**51, func=lambda a, b: a & b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: max(a , b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] < key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
n=int(input())
s=list(input())
aster=[]
p=[]
for i in range(n):
if s[i]=='*':
aster.append(i)
elif s[i]=='P':
p.append(i)
def find(x):
t=0
for i in range(len(p)):
time=0
if aster[t]<p[i]:
time+=abs(p[i]-aster[t])
if time>x:
return False
else:
while (aster[t]<p[i]):
t+=1
if t==len(aster):
break
if t==len(aster):
break
cur=time
t-=1
time=0
while(time*2+cur<=x or cur*2+time<=x):
t+=1
if t==len(aster):
break
time=aster[t]-p[i]
if t==len(aster):
break
if t!=len(aster):
return False
return True
st=1
end=2*n
ans=2*n
while(st<=end):
mid=(st+end)//2
if find(mid)==True:
ans=mid
end=mid-1
else:
st=mid+1
print(ans)
``` | output | 1 | 54,487 | 15 | 108,975 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field. | instruction | 0 | 54,488 | 15 | 108,976 |
Tags: binary search, dp
Correct Solution:
```
'''
pacman->food
pacman->food->food :hmmge:
larges smaller val
smallest larger val
'''
def check(mid,p,f):
ptr=0
for x in p:
From=x
To=x
while ptr<len(f):
From=min(From,f[ptr])
To=max(To,f[ptr])
need=To-From+min(To-x,x-From)
if need>mid:
break
ptr+=1
if ptr==len(f):
return 1
return 0
def f(s,n):
p=[]
f=[]
for i in range(n):
if s[i]=="*":
f.append(i)
if s[i]=="P":
p.append(i)
lo=0
hi=10**9
while lo<=hi:
mid=(lo+hi)//2
if check(mid,p,f):
hi=mid-1
else:
lo=mid+1
return lo
n=int(input())
s=list(input().strip())
print(f(s,n))
``` | output | 1 | 54,488 | 15 | 108,977 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field. | instruction | 0 | 54,489 | 15 | 108,978 |
Tags: binary search, dp
Correct Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
s = stdin.readline().strip()
mins = []
packs = []
for i in range(len(s)):
if s[i] == '*':
mins.append(i)
elif s[i] == 'P':
packs.append(i)
l, r = -1, 2 * len(s) + 1
while r - l > 1:
m = (l + r) >> 1
test1 = mins[:]
test2 = packs[:]
while test2 and test1:
cnt = m
pos = test2.pop()
if pos > test1[-1]:
while test1 and abs(pos - test1[-1]) <= cnt:
cnt -= abs(pos - test1[-1])
pos = test1[-1]
test1.pop()
else:
cntl, cntr = 0, 0
if abs(test1[-1] - pos) > m:
break
lpos = (m + pos - test1[-1]) // 2
rpos = m - 2 * abs(test1[-1] - pos)
lb, rb = -1, len(test1)
while rb - lb > 1:
mb = (lb + rb) >> 1
if pos - test1[mb] <= lpos:
rb = mb
else:
lb = mb
cntl = len(test1) - rb
lb, rb = -1, len(test1)
while rb - lb > 1:
mb = (lb + rb) >> 1
if pos - test1[mb] <= rpos:
rb = mb
else:
lb = mb
cntr = len(test1) - rb
cnt = max(cntl, cntr)
while test1 and cnt:
test1.pop()
cnt -= 1
if not test1:
r = m
else:
l = m
stdout.write(str(r))
``` | output | 1 | 54,489 | 15 | 108,979 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field. | instruction | 0 | 54,490 | 15 | 108,980 |
Tags: binary search, dp
Correct Solution:
```
"""
https://codeforces.com/contest/847/problem/E
7
*..P*P* should output 3
10
.**PP.*P.* should output 2
"""
from typing import List
# assumes the positions are sorted, as well that the positions are distinct
def eatable_in_time(food: List[int], packmen: List[int], time: int) -> bool:
food_pointer = 0
food_amt = len(food) # convenient shorthand idk
for p in packmen:
have_to_eat = []
# idk, just assign this packmen all the ones to its left which haven't been eaten
while food_pointer < food_amt and food[food_pointer] < p:
have_to_eat.append(food[food_pointer])
food_pointer += 1
if not have_to_eat:
while food_pointer < food_amt and food[food_pointer] - p <= time:
food_pointer += 1
else:
if p - have_to_eat[0] > time:
return False
# try 2 diff strats- go to the left first and switch back or go to the right and switch back
left_time = p - have_to_eat[0]
right_free_time = time - 2 * left_time
left_first = -1
while food_pointer + left_first + 1 < food_amt and food[food_pointer + left_first + 1] - p <= right_free_time:
left_first += 1
right_free_time = time - left_time
right_first = -1
while food_pointer + right_first + 1 < food_amt and 2 * (food[food_pointer + right_first + 1] - p) <= right_free_time:
right_first += 1
food_pointer += max(left_first, right_first) + 1
if food_pointer >= food_amt: # ok all the food's been eaten
return True
return food_pointer >= food_amt
input()
field = input().upper()
all_food = []
all_packmen = []
for v, c in enumerate(field):
if c == 'P':
all_packmen.append(v)
elif c == '*':
all_food.append(v)
lo = 0
hi = len(field) * 2
valid = -1
while lo <= hi:
mid = (lo + hi) // 2
if eatable_in_time(all_food, all_packmen, mid):
valid = mid
hi = mid - 1
else:
lo = mid + 1
print(valid)
``` | output | 1 | 54,490 | 15 | 108,981 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field. | instruction | 0 | 54,491 | 15 | 108,982 |
Tags: binary search, dp
Correct Solution:
```
'''
pacman->food
pacman->food->food :hmmge:
larges smaller val
smallest larger val
'''
def check(mid,p,f):
u=0
for i in range(len(p)):
left=p[i]-f[u]
left=max(0,left)
if mid<left:
return 0
right=max(mid-2*left,0)
right=max(right,(mid-left)//2)
while u<len(f) and (f[u]<=p[i]+right): #bich mai fas ra
u+=1
if u==len(f):
return 1
return 0
def f(s,n):
p=[]
f=[]
for i in range(n):
if s[i]=="*":
f.append(i)
if s[i]=="P":
p.append(i)
lo=0
hi=10**9
while lo<=hi:
mid=(lo+hi)//2
if check(mid,p,f):
hi=mid-1
else:
lo=mid+1
return lo
n=int(input())
s=list(input().strip())
print(f(s,n))
``` | output | 1 | 54,491 | 15 | 108,983 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field. | instruction | 0 | 54,492 | 15 | 108,984 |
Tags: binary search, dp
Correct Solution:
```
from bisect import bisect_right,bisect_left
def check(t):
pt=0
contain_t=0
pt1,pt2=0,0
contain_t1,contain_t2=0,0
for i in range(l_p):
contain_t1,contain_t2=t,t
if pt>=l_s:
return 1
if ash[pt]<pokemon[i] and t<pokemon[i]-ash[pt]:
return 0
if ash[pt]<pokemon[i]:
contain_t1=(contain_t1-(pokemon[i]-ash[pt]))//2
contain_t2=max(0,(contain_t2-(pokemon[i]-ash[pt])*2))
pt=bisect_right(ash,pokemon[i]+max(contain_t1,contain_t2))
if pt>=l_s:
return 1
return 0
n=int(input())
st=input()
a=[]
for i in range(n):
a.append(st[i])
ans1,ans2=0,0
ash=[]
pokemon=[]
for i in range(n):
if a[i]=="*":
ash.append(i)
elif a[i]=="P":
pokemon.append(i)
l_p=len(pokemon)
l_s=len(ash)
i=0
j=10**10
while i<j:
m=(i+j)//2
if check(m):
j=m-1
else:
i=m+1
if check(i):
print(i)
else:
print(i+1)
``` | output | 1 | 54,492 | 15 | 108,985 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field. | instruction | 0 | 54,493 | 15 | 108,986 |
Tags: binary search, dp
Correct Solution:
```
import bisect
n = int(input())
s = input()
packmans = []
stars = []
for i in range(n):
if s[i] == '*':
stars.append(i)
elif s[i] == 'P':
packmans.append(i)
if len(stars) == 0:
print(0)
exit()
def check(t):
first_to_eat = 0
for i in range(len(packmans)):
x = stars[first_to_eat]
if packmans[i] > x:
if packmans[i] - x > t:
return False
d1 = t - 2 * (packmans[i] - x)
d2 = (t - (packmans[i] - x)) // 2
first_to_eat = bisect.bisect_right(stars, packmans[i] + max(d1, d2))
if first_to_eat < len(stars) and stars[first_to_eat] == packmans[i] + max(d1, d2):
first_to_eat += 1
else:
j = bisect.bisect_right(stars, packmans[i] + t)
if first_to_eat < len(stars) and stars[first_to_eat] == packmans[i] + t:
first_to_eat += 1
first_to_eat = max(j, first_to_eat)
if first_to_eat >= len(stars):
return True
return first_to_eat >= len(stars)
l = 0
r = 2 * n + 1
while r - l > 1:
m = (l + r) // 2
if check(m):
r = m
else:
l = m
print(r)
``` | output | 1 | 54,493 | 15 | 108,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.
Submitted Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
s = stdin.readline().strip()
mins = []
packs = []
for i in range(len(s)):
if s[i] == '*':
mins.append(i)
elif s[i] == 'P':
packs.append(i)
l, r = -1, len(s) + 1
while r - l > 1:
m = (l + r) >> 1
test1 = mins[:]
test2 = packs[:]
while test2 and test1:
cnt = m
pos = test2.pop()
if pos > test1[-1]:
while test1 and abs(pos - test1[-1]) <= cnt:
cnt -= abs(pos - test1[-1])
pos = test1[-1]
test1.pop()
else:
cntl, cntr = 0, 0
if abs(test1[-1] - pos) > m:
break
lpos = (m + pos - test1[-1]) // 2
rpos = m - 2 * abs(test1[-1] - pos)
lb, rb = -1, len(test1)
while rb - lb > 1:
mb = (lb + rb) >> 1
if pos - test1[mb] <= lpos:
rb = mb
else:
lb = mb
cntl = len(test1) - rb
lb, rb = -1, len(test1)
while rb - lb > 1:
mb = (lb + rb) >> 1
if pos - test1[mb] <= rpos:
rb = mb
else:
lb = mb
cntr = len(test1) - rb
cnt = max(cntl, cntr)
while test1 and cnt:
test1.pop()
cnt -= 1
if not test1:
r = m
else:
l = m
stdout.write(str(r))
``` | instruction | 0 | 54,494 | 15 | 108,988 |
No | output | 1 | 54,494 | 15 | 108,989 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.
Submitted Solution:
```
"""
https://codeforces.com/contest/847/problem/E
7
*..P*P* should output 3
10
.**PP.*P.* should output 2
"""
from sys import exit
from typing import List
from collections import deque
# assumes the positions are sorted, as well that the positions are distinct
def eatable_in_time(food: List[int], packmen: List[int], time: int) -> bool:
food = deque(food)
for p in packmen:
have_to_eat = []
while food and food[0] < p: # idk, just assign this packmen all the ones to its left which haven't been eaten
have_to_eat.append(food.popleft())
if not have_to_eat:
while food and food[0] - p <= time:
food.popleft()
else:
if p - have_to_eat[0] > time:
return False
# try 2 diff strats- go to the left first and switch back or go to the right and switch back
left_time = p - have_to_eat[0]
right_free_time = time - 2 * left_time
left_first = -1
while left_first + 1 < len(food) and food[left_first + 1] - p <= right_free_time:
left_first += 1
right_free_time = time - left_time
right_first = -1
while right_first + 1 < len(food) and (food[right_first + 1] - p) * 2 <= right_free_time:
right_first += 1
for _ in range(max(left_first, right_first) + 1):
food.popleft()
if not food: # ok all the pellets have been eaten
return True
return not bool(food)
input()
field = input().upper()
all_food = []
all_packmen = []
for v, c in enumerate(field):
if c == 'P':
all_packmen.append(v)
elif c == '*':
all_food.append(v)
lo = 0
hi = len(field)
valid = -1
while lo <= hi:
mid = (lo + hi) // 2
if eatable_in_time(all_food, all_packmen, mid):
valid = mid
hi = mid - 1
else:
lo = mid + 1
print(valid)
``` | instruction | 0 | 54,495 | 15 | 108,990 |
No | output | 1 | 54,495 | 15 | 108,991 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.
Submitted Solution:
```
"""
https://codeforces.com/contest/847/problem/E
7
*..P*P* should output 3
10
.**PP.*P.* should output 2
"""
from typing import List
from collections import deque
# assumes the positions are sorted, as well that the positions are distinct
def eatable_in_time(food: List[int], packmen: List[int], time: int) -> bool:
food = deque(food)
for p in packmen:
have_to_eat = []
while food and food[0] < p: # idk, just assign this packmen all the ones to its left which haven't been eaten
have_to_eat.append(food.popleft())
if not have_to_eat:
while food and food[0] - p <= time:
food.popleft()
else:
if p - have_to_eat[0] > time:
return False
# try 2 diff strats- go to the left first and switch back or go to the right and switch back
left_time = p - have_to_eat[0]
right_free_time = time - (p - 2 * left_time)
left_first = -1
while left_first + 1 < len(food) and food[left_first + 1] - p <= right_free_time:
left_first += 1
right_free_time = time - (p - left_time)
right_first = -1
while right_first + 1 < len(food) and (food[right_first + 1] - p) * 2 <= right_free_time:
right_first += 1
for _ in range(max(left_first, right_first) + 1):
food.popleft()
if not food:
return True
return bool(food)
input()
field = input().upper()
all_food = []
all_packmen = []
for v, c in enumerate(field):
if c == 'P':
all_packmen.append(v)
elif c == '*':
all_food.append(v)
lo = 0
hi = len(field)
valid = -1
while lo <= hi:
mid = (lo + hi) // 2
if eatable_in_time(all_food, all_packmen, mid):
valid = mid
hi = mid - 1
else:
lo = mid + 1
print(valid)
``` | instruction | 0 | 54,496 | 15 | 108,992 |
No | output | 1 | 54,496 | 15 | 108,993 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A game field is a strip of 1 Γ n square cells. In some cells there are Packmen, in some cells β asterisks, other cells are empty.
Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.
In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
Input
The first line contains a single integer n (2 β€ n β€ 105) β the length of the game field.
The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i β the cell i is empty. If there is symbol '*' in position i β in the cell i contains an asterisk. If there is symbol 'P' in position i β Packman is in the cell i.
It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Output
Print minimum possible time after which Packmen can eat all asterisks.
Examples
Input
7
*..P*P*
Output
3
Input
10
.**PP.*P.*
Output
2
Note
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.
In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.
Submitted Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq,bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
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----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=2**51, func=lambda a, b: a & b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: max(a , b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] < key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
n=int(input())
s=list(input())
aster=[]
p=[]
for i in range(n):
if s[i]=='*':
aster.append(i)
elif s[i]=='P':
p.append(i)
def find(x):
t=0
for i in range(len(p)):
time=0
if aster[t]<p[i]:
time+=abs(p[i]-aster[i])
if time>x:
return False
else:
while (aster[t]<p[i]):
t+=1
if t==len(aster):
break
if t==len(aster):
break
cur=time*2
t-=1
while(time<=x):
t+=1
if t==len(aster):
break
time=cur+aster[t]-p[i]
if t==len(aster):
break
if t!=len(aster):
return False
return True
st=1
end=2*n
ans=2*n
while(st<=end):
mid=(st+end)//2
if find(mid)==True:
ans=mid
end=mid-1
else:
st=mid+1
print(ans)
``` | instruction | 0 | 54,497 | 15 | 108,994 |
No | output | 1 | 54,497 | 15 | 108,995 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2 | instruction | 0 | 54,529 | 15 | 109,058 |
Tags: dp, greedy
Correct Solution:
```
a = []
a.append(['X'] + list(input()) + ['X'])
n = len(a[0])
a.append(['X'] + list(input()) + ['X'])
cnt = 0
for i in range(1, n):
#print(a[0][i])
#print(a[1][i])
if a[0][i] == '0' and a[1][i] == '0':
if a[0][i-1] == '0':
cnt += 1
a[0][i-1] = 'X'
a[0][i] = 'X'
a[1][i] = 'X'
continue
elif a[1][i-1] == '0':
cnt += 1
a[1][i-1] = 'X'
a[0][i] = 'X'
a[1][i] = 'X'
continue
elif a[0][i+1] == '0':
cnt += 1
a[0][i+1] = 'X'
a[0][i] = 'X'
a[1][i] = 'X'
continue
elif a[1][i+1] == '0':
cnt += 1
a[1][i+1] = 'X'
a[0][i] = 'X'
a[1][i] = 'X'
continue
print(cnt)
``` | output | 1 | 54,529 | 15 | 109,059 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2 | instruction | 0 | 54,530 | 15 | 109,060 |
Tags: dp, greedy
Correct Solution:
```
# Codeforces Round #491 (Div. 2)
import collections
from functools import cmp_to_key
#key=cmp_to_key(lambda x,y: 1 if x not in y else -1 )
import math
import sys
def getIntList():
return list(map(int, input().split()))
import bisect
try :
import numpy
dprint = print
dprint('debug mode')
except ModuleNotFoundError:
def dprint(*args, **kwargs):
pass
def makePair(z):
return [(z[i], z[i+1]) for i in range(0,len(z),2) ]
z0 = input()
z1 = input()
zz = [z0,z1]
dp = [ [-10000, -10000, -10000, 0]]
n = len(z0)
dprint(n)
for i in range(n):
t = [0,0,0,0]
t[0] = dp[-1][3]
t[1] = t[0]
t[2] = t[0]
if zz[0][i] == '0' and zz[1][i] == '0':
t[1] = max(t[1], dp[-1][0] + 1)
t[2] = max(t[2], dp[-1][0] + 1)
t[3] = max(t[0],t[1],t[2])
if zz[0][i] == '0' or zz[1][i] == '0':
t[3] = max(t[3], dp[-1][0] + 1)
if zz[0][i] == '0' and zz[1][i] == '0':
t[3] = max(t[3], dp[-1][1] + 1, dp[-1][2] + 1)
if zz[0][i] == 'X' or zz[1][i] == 'X':
t[0] = -10000
if zz[0][i] == 'X':
t[2] = -10000
if zz[1][i] == 'X':
t[1] = -10000
dp.append(t)
dprint(dp)
print(dp[-1][3])
``` | output | 1 | 54,530 | 15 | 109,061 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2 | instruction | 0 | 54,531 | 15 | 109,062 |
Tags: dp, greedy
Correct Solution:
```
s = [list(input()), list(input())]
ans = 0
for i in range(len(s[0]) - 1):
if s[1][i] == s[0][i] == '0':
if s[0][i + 1] == '0':
ans += 1
s[0][i] = s[0][i + 1] = s[1][i] = 'X'
elif s[1][i + 1] == '0':
ans += 1
s[0][i] = s[1][i + 1] = s[1][i] = 'X'
if s[1][i + 1] == s[0][i + 1] == '0':
if s[0][i] == '0':
ans += 1
s[0][i] = s[0][i + 1] = s[1][i + 1] = 'X'
elif s[1][i] == '0':
ans += 1
s[1][i + 1] = s[0][i + 1] = s[1][i] = 'X'
print(ans)
``` | output | 1 | 54,531 | 15 | 109,063 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2 | instruction | 0 | 54,532 | 15 | 109,064 |
Tags: dp, greedy
Correct Solution:
```
s1 = input()
s2 = input()
L1 = [c == "0" for c in s1]
L2 = [c == "0" for c in s2]
ans = 0
for i in range(len(s1)-1):
if L1[i] and L2[i]:
if L1[i+1]:
L1[i] = L2[i] = L1[i+1] = False
ans+= 1
elif L2[i+1]:
L1[i] = L2[i] = L2[i+1] = False
ans+= 1
elif L1[i+1] and L2[i+1]:
if L1[i]:
L1[i] = L1[i+1] = L2[i+1] = False
ans+= 1
elif L2[i]:
L2[i] = L1[i+1] = L2[i+1] = False
ans+= 1
print(ans)
``` | output | 1 | 54,532 | 15 | 109,065 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2 | instruction | 0 | 54,533 | 15 | 109,066 |
Tags: dp, greedy
Correct Solution:
```
board = []
for i in range(2):
board.append([i == "0" for i in input()])
before_up = False
before_down = False
s = 0
for i in range(len(board[0])):
if before_up and before_down:
if board[0][i]:
s += 1
before_up = False
before_down = board[1][i]
elif board[1][i]:
s += 1
before_down = False
before_up = board[0][i]
else:
before_up = False
before_down = False
elif before_up or before_down:
if board[0][i] and board[1][i]:
s += 1
before_up = False
before_down = False
else:
before_up = board[0][i]
before_down = board[1][i]
else:
before_up = board[0][i]
before_down = board[1][i]
print(s)
``` | output | 1 | 54,533 | 15 | 109,067 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2 | instruction | 0 | 54,534 | 15 | 109,068 |
Tags: dp, greedy
Correct Solution:
```
a = []
a.append(input())
a.append(input())
dp = [-200] * 4
dp[1 * (a[0][0] == 'X') + 2 * (a[1][0] == 'X')] = 0
for i in range(1, len(a[0])):
odp = dp
dp = [-200] * 4
state = 1 * (a[0][i] == 'X') + 2 * (a[1][i] == 'X')
for last_state in range(4):
dp[state] = max(dp[state], odp[last_state])
if last_state != 3 and state == 0:
dp[3] = max(dp[3], odp[last_state] + 1)
if last_state == 0:
if ((~state) & 1):
dp[state + 1] = max(dp[state + 1], odp[last_state] + 1)
if ((~state) & 2):
dp[state + 2] = max(dp[state + 2], odp[last_state] + 1)
print(max(max(dp[0], dp[1]), max(dp[2], dp[3])))
``` | output | 1 | 54,534 | 15 | 109,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2 | instruction | 0 | 54,535 | 15 | 109,070 |
Tags: dp, greedy
Correct Solution:
```
num = []
num.append(list(i for i in input()))
num.append(list(i for i in input()))
ans = 0
for i in range(len(num[0])-1):
if num[0][i] == '0' and num[1][i] == '0':
if num[0][i+1] == '0':
ans += 1
num[0][i+1] = 'X'
elif num[1][i+1] == '0':
ans += 1
num[1][i+1] = 'X'
elif num[0][i+1] == '0' and num[1][i+1] == '0':
if num[0][i] == '0' or num[1][i] == '0':
ans += 1
num[0][i+1] = 'X'
num[1][i+1] = 'X'
print(ans)
``` | output | 1 | 54,535 | 15 | 109,071 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2 | instruction | 0 | 54,536 | 15 | 109,072 |
Tags: dp, greedy
Correct Solution:
```
a = list(input())
b = list(input())
r = 0
for i in range(len(a)-1):
if a[i] == '0' and b[i] == '0':
if a[i+1] == '0':
a[i+1] = 'X'
r += 1
elif b[i+1] == '0':
b[i+1] = 'X'
r += 1
elif a[i] == '0' or b[i] == '0':
if a[i+1] == '0' and b[i+1] == '0':
a[i+1] = 'X'
b[i+1] = 'X'
r += 1
print(r)
``` | output | 1 | 54,536 | 15 | 109,073 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
Submitted Solution:
```
a=list(input())
b=list(input())
n=len(a)
ans=0
for i in range(n-1):
if(a[i+1]=='0' and a[i]=='0' and b[i]=='0'):
ans+=1
a[i]=a[i+1]=b[i]='X'
elif(b[i+1]=='0' and a[i]=='0' and b[i]=='0'):
ans+=1
a[i]=b[i+1]=b[i]='X'
elif(b[i+1]=='0' and a[i+1]=='0' and b[i]=='0'):
ans+=1
a[i+1]=b[i+1]=b[i]='X'
elif(b[i+1]=='0' and a[i+1]=='0' and a[i]=='0'):
ans+=1
a[i+1]=b[i+1]=a[i]='X'
print(ans)
``` | instruction | 0 | 54,537 | 15 | 109,074 |
Yes | output | 1 | 54,537 | 15 | 109,075 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
Submitted Solution:
```
top = list(input())
bottom = list(input())
length = len(top)
count = 0
i = 0
while(i+1 < length):
#print("top",top[i])
#print("bottom",bottom[i])
if top[i] == "0" and bottom[i] == "0":
if top[i+1] == "0":
count += 1
top[i+1] = "X"
i += 1
elif bottom[i+1] == "0":
count += 1
bottom[i+1] = "X"
i += 1
else:
i += 2
elif top[i+1] == "0" and bottom[i+1] == "0":
if top[i] == "0" or bottom[i] == "0":
count += 1
i += 2
else:
i += 1
else:
i += 1
print(count)
``` | instruction | 0 | 54,538 | 15 | 109,076 |
Yes | output | 1 | 54,538 | 15 | 109,077 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
Submitted Solution:
```
l1 = list(input().strip())
l2 = list(input().strip())
r = 0
for i in range(len(l1) - 1):
if l1[i] == '0' and l2[i] == '0':
if l2[i+1] == '0':
l1[i] = 'X'
l2[i] = 'X'
l2[i+1] = 'X'
r += 1
elif l1[i+1] == '0':
l1[i] = 'X'
l2[i] = 'X'
l1[i+1] = 'X'
r += 1
elif l1[i] == '0':
if l1[i+1] == '0' and l2[i+1] == '0':
l1[i] = 'X'
l1[i+1] = 'X'
l2[i+1] = 'X'
r += 1
elif l2[i] == '0':
if l1[i+1] == '0' and l2[i+1] == '0':
l2[i] = 'X'
l1[i+1] = 'X'
l2[i+1] = 'X'
r += 1
print(r)
``` | instruction | 0 | 54,539 | 15 | 109,078 |
Yes | output | 1 | 54,539 | 15 | 109,079 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
Submitted Solution:
```
ans = 0
ch1 = ['1'] * 150
ch2 = ['1'] * 150
t1 = list(input())
t2 = list(input())
len1 = len(t1)
for i in range(len1):
ch1[i] = t1[i]
ch2[i] = t2[i]
for i in range(len1):
if ch1[i] == '0' and ch2[i] == '0' and ch1[i + 1] == '0':
ch1[i] = ch1[i + 1] = ch2[i] = 'x'
ans += 1
elif ch1[i] == '0' and ch2[i] == '0' and ch2[i + 1] == '0':
ch1[i] = ch2[i] = ch2[i + 1] = 'x'
ans += 1
elif ch1[i] == '0' and ch1[i + 1] == '0' and ch2[i + 1] == '0':
ch1[i] = ch1[i + 1] = ch2[i + 1] = 'x'
ans += 1
elif ch2[i] == '0' and ch1[i + 1] == '0' and ch2[i + 1] == '0':
ch2[i] = ch1[i + 1] = ch2[i + 1] = 'x'
ans += 1
print(ans)
``` | instruction | 0 | 54,540 | 15 | 109,080 |
Yes | output | 1 | 54,540 | 15 | 109,081 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
Submitted Solution:
```
l = [*({'0': 1, 'X': 0}[c] for cc in zip(input(), input()) for c in cc), 5]
i = r = 0
while True:
s = sum(l[i:i + 4])
if s > 2:
if s > 4:
print(r + (s == 8))
break
r += 1
i += 7 - s
else:
i += 3 - s
``` | instruction | 0 | 54,541 | 15 | 109,082 |
No | output | 1 | 54,541 | 15 | 109,083 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
Submitted Solution:
```
from collections import Counter
s1 = input()
s2 = input()
rdymax = False
rdymin = False
total = 0
for i in range(len(s1)):
if s1[i] == s2[i] == 'X':
rdymax = False
rdymin = False
continue
if (s1[i] == '0') ^ (s2[i] == '0'):
if rdymax:
total+=1
rdymax = False
continue
rdymin = True
continue
if rdymin:
total+=1
rdymin = False
continue
if rdymax:
total+=1
rdymin = True
continue
rdymax = True
print(total)
``` | instruction | 0 | 54,542 | 15 | 109,084 |
No | output | 1 | 54,542 | 15 | 109,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
Submitted Solution:
```
first = input()
second = input()
count = 0
n = len(first)
a = []
for i in range(2):
a.append([0] * n)
for i in range(n):
if first[i] == 'X':
a[0][i] = 1
if second[i] == 'X':
a[1][i] = 1
#print(a)
i = 1
while i < n:
#print(i)
#print(a[0][i - 1], a[0][i])
#print(a[1][i - 1], a[1][i])
if a[0][i] + a[1][i] + a[0][i - 1] + a[1][i - 1] <= 1:
count += 1
i += 1
print(count)
``` | instruction | 0 | 54,543 | 15 | 109,086 |
No | output | 1 | 54,543 | 15 | 109,087 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.