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 a correct Python 3 solution for this coding contest problem.
Problem
Gaccho owns a field separated by W horizontal x H vertical squares. The cell in the x-th column and the y-th row is called the cell (x, y). Only one plant with a height of 0 cm is planted on the land of some trout, and nothing is planted on the land of other trout.
Gaccho sprinkles fertilizer on the field at a certain time. When a plant is planted in a fertilized trout, the height of the plant grows by 1 cm. When no plants are planted, nothing happens. The time it takes for the plant to grow is so short that it can be ignored. Gaccho can fertilize multiple trout at the same time. However, fertilizer is never applied to the same square more than once at the same time.
Calculate the sum of the heights of the plants in the field at time T, given the record of Gaccho's fertilizing.
Constraints
* 1 β€ W, H, T β€ 50
* 0 β€ p β€ min (W Γ H Γ T, 50)
* 0 β€ xi <W
* 0 β€ yi <H
* 0 β€ ti <T
* sj, k = 0 or 1
Input
The input is given in the following format.
W H T
p
x0 y0 t0
x1 y1 t1
...
xpβ1 ypβ1 tpβ1
s0,0 s1,0β¦ sWβ1,0
s0,1 s1,1β¦ sWβ1,1
...
s0, Hβ1 s1, Hβ1β¦ sWβ1, Hβ1
The width W of the field, the height H of the field, and the time T are given on the first line. Gaccho can sprinkle fertilizer by time T.
On the second line, p is given the number of times Gaccho sprinkled fertilizer.
The three integers given from the 3rd line to the 2 + p line indicate that Gaccho sprinkled fertilizer on the mass (xi, yi) at time ti.
From the 3 + p line to the 2 + p + H line, W Γ H integers indicating whether or not a plant is planted in each square of the first field are given. When sj, k is 1, it means that only one plant with a height of 0 cm is planted in the trout (j, k), and when sj, k is 0, it means that there is no plant in the trout (j, k). Indicates that it has not been planted.
Output
Output the sum of the heights of the plants in the field at time T on one line.
Examples
Input
3 3 3
5
2 0 0
0 1 0
1 1 1
1 2 1
2 2 0
0 0 0
0 1 0
0 0 0
Output
1
Input
2 3 4
2
0 0 0
1 1 3
1 0
0 0
0 0
Output
1
Input
3 8 6
6
0 4 3
2 5 3
0 2 3
2 2 5
1 1 3
2 2 1
1 1 1
1 1 1
1 1 1
1 0 1
0 1 1
1 1 0
1 0 1
0 1 0
Output
4
Input
8 3 3
7
0 1 1
5 1 0
4 0 2
3 2 0
3 1 1
3 0 1
5 1 1
1 0 1 1 0 0 1 0
0 0 1 1 0 1 0 1
0 1 0 0 0 1 0 1
Output
4 | instruction | 0 | 6,535 | 15 | 13,070 |
"Correct Solution:
```
# AOJ 1587: Plants
# Python3 2018.7.13 bal4u
hi = [[0 for j in range(51)] for i in range(51)]
W, H, T = map(int, input().split())
P = int(input())
for i in range(P):
x, y, t = map(int, input().split())
hi[x][y] += 1
ans = 0;
for y in range(H):
v = list(map(int, input().split()))
for x in range(W):
if v[x]: ans += hi[x][y]
print(ans)
``` | output | 1 | 6,535 | 15 | 13,071 |
Provide a correct Python 3 solution for this coding contest problem.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8 | instruction | 0 | 6,540 | 15 | 13,080 |
"Correct Solution:
```
#!/usr/bin/python3
from heapq import heappush, heappop
solution = [i + 1 for i in range(15)] + [0]
sol_idx = (15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
MAX_DEPTH = 45
count = 0
neighbor = (
(1, 4),
(0, 2, 5),
(1, 6, 3),
(2, 7),
(0, 5, 8),
(1, 4, 6, 9),
(2, 5, 7, 10),
(3, 6, 11),
(4, 9, 12),
(5, 8, 10, 13),
(6, 9, 11, 14),
(7, 10, 15),
(8, 13),
(9, 12, 14),
(10, 13, 15),
(11, 14)
)
distance = (
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0),
(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
(1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
(2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
(3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
(1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
(2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
(3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
(4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
(2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
(3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
(4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
(5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
(3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
(4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
(5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1),
(6, 5, 4, 3, 5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0)
)
def get_diff(B):
diff = 0
for i, v in enumerate(B):
diff += distance[v][i]
return diff
def get_next_board(board, space, prev):
for nxt in neighbor[space]:
if nxt == prev:
continue
b = board[:]
b[space], b[nxt] = b[nxt], 0
yield b, nxt
def answer_is_odd(board):
return sum(divmod(board.index(0), 4)) % 2
def search(board):
lower = get_diff(board)
start_depth = lower
if (lower % 2) ^ answer_is_odd(board):
start_depth += 1
for limit in range(start_depth, MAX_DEPTH + 1, 2):
get_next(board, limit, 0, board.index(0), None, lower)
if count > 0:
return limit
def get_next(board, limit, move, space, prev, lower):
if move == limit:
if board == solution:
global count
count += 1
else:
for b, nxt in get_next_board(board, space, prev):
p = board[nxt]
new_lower = lower - distance[p][nxt] + distance[p][space]
if new_lower + move <= limit:
get_next(b, limit, move + 1, nxt, space, new_lower)
# main
boad = []
for i in range(4):
boad.extend(map(int, input().split()))
print(search(boad))
exit()
``` | output | 1 | 6,540 | 15 | 13,081 |
Provide a correct Python 3 solution for this coding contest problem.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8 | instruction | 0 | 6,541 | 15 | 13,082 |
"Correct Solution:
```
from heapq import heappop, heappush
from functools import lru_cache
@lru_cache(maxsize=None)
def manhattan(size, i, n):
if n == 0:
return 0
else:
dn, mn = divmod(n-1, size)
di, mi = divmod(i, size)
return abs(dn-di) + abs(mn-mi)
class Board:
__slots__ = ('size', 'nums', 'code', '_hash')
def __init__(self, size, nums, code=None):
self.size = size
self.nums = nums
self._hash = hash(nums)
if code is None:
self.code = sum([manhattan(self.size, i, n)
for i, n in enumerate(self.nums) if n != i+1])
else:
self.code = code
def __eq__(self, other):
return self.code == other.code
def __lt__(self, other):
return self.code < other.code
def __gt__(self, other):
return self.code > other.code
def __hash__(self):
return self._hash
def same(self, other):
if other is None:
return False
if self.__class__ != other.__class__:
return False
for i in range(self.size * self.size):
if self.nums[i] != other.nums[i]:
return False
return True
def solved(self):
for i in range(self.size * self.size):
if self.nums[i] > 0 and self.nums[i] - 1 != i:
return False
return True
def find(self, num):
for i in range(self.size * self.size):
if self.nums[i] == num:
return i
raise IndexError()
def move(self, p1, p2):
nums = list(self.nums)
v1, v2 = nums[p1], nums[p2]
code = (self.code - manhattan(self.size, p1, v1)
- manhattan(self.size, p2, v2)
+ manhattan(self.size, p2, v1)
+ manhattan(self.size, p1, v2))
nums[p1], nums[p2] = v2, v1
return self.__class__(self.size, tuple(nums), code)
def moves(self):
i = self.find(0)
if i > self.size-1:
yield self.move(i, i-self.size)
if i % self.size > 0:
yield self.move(i, i-1)
if i < self.size*(self.size-1):
yield self.move(i, i+self.size)
if (i+1) % self.size > 0:
yield self.move(i, i+1)
def __str__(self):
s = ''
for i in range(self.size*self.size):
s += ' {}'.format(self.nums[i])
if (i + 1) % self.size == 0:
s += '\n'
return s
class FifteenPuzzle:
def __init__(self, board, maxmove):
self.board = board
self.maxmove = maxmove
if not board.solved():
self.steps = self._solve()
else:
self.steps = 0
def _solve(self):
bs = []
checked = set()
i = 0
heappush(bs, (self.board.code, self.board.code, i, self.board, 0))
while len(bs) > 0:
w, _, _, b, step = heappop(bs)
checked.add(b)
step += 1
for nb in b.moves():
if nb.solved():
return step
elif self.maxmove < nb.code + step:
continue
elif nb in checked:
continue
else:
i += 1
heappush(bs, (nb.code + step, nb.code, i, nb, step))
return -1
def run():
ns = []
for i in range(4):
ns.extend([int(i) for i in input().split()])
board = Board(4, tuple(ns))
puzzle = FifteenPuzzle(board, 45)
print(puzzle.steps)
if __name__ == '__main__':
run()
``` | output | 1 | 6,541 | 15 | 13,083 |
Provide a correct Python 3 solution for this coding contest problem.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8 | instruction | 0 | 6,542 | 15 | 13,084 |
"Correct Solution:
```
from sys import stdin
readline = stdin.readline
GOAL = [i for i in range(1, 16)] + [0]
MAX_DEPTH = 45 # 80
count = 0
# ??Β£??\?????????
adjacent = (
(1, 4), # 0
(0, 2, 5), # 1
(1, 6, 3), # 2
(2, 7), # 3
(0, 5, 8), # 4
(1, 4, 6, 9), # 5
(2, 5, 7, 10), # 6
(3, 6, 11), # 7
(4, 9, 12), # 8
(5, 8, 10, 13), # 9
(6, 9, 11, 14), # 10
(7, 10, 15), # 11
(8, 13), # 12
(9, 12, 14), # 13
(10, 13, 15), # 14
(11, 14) # 15
)
# ???????????????????????Β’
distance = (
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
(1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
(2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
(3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
(1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
(2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
(3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
(4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
(2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
(3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
(4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
(5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
(3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
(4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
(5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1)
)
def manhattan_distance(board):
return sum(distance[bi][i] for i, bi in enumerate(board))
def answer_is_odd(board):
return sum(divmod(board.index(0), 4)) % 2
def search(board):
lower = manhattan_distance(board)
start_depth = lower
if (lower % 2) ^ answer_is_odd(board):
start_depth += 1
for limit in range(start_depth, MAX_DEPTH + 1, 2):
id_lower_search(board, limit, 0, board.index(0), None, lower)
if count > 0:
return limit
def nxt_board(board, space, prev):
for nxt in adjacent[space]:
if nxt == prev:
continue
b = board[:]
b[space], b[nxt] = b[nxt], 0
yield b, nxt
def id_lower_search(board, limit, move, space, prev, lower):
if move == limit:
if board == GOAL:
global count
count += 1
else:
for b, nxt in nxt_board(board, space, prev):
p = board[nxt]
new_lower = lower - distance[p][nxt] + distance[p][space]
if new_lower + move <= limit:
id_lower_search(b, limit, move + 1, nxt, space, new_lower)
def main():
start = (map(int, readline().split()) for _ in range(4))
start = [y for x in start for y in x]
print(search(start))
main()
``` | output | 1 | 6,542 | 15 | 13,085 |
Provide a correct Python 3 solution for this coding contest problem.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8 | instruction | 0 | 6,543 | 15 | 13,086 |
"Correct Solution:
```
from heapq import heappop, heappush
manhattan = (
(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
(1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
(2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
(3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
(1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
(2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
(3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
(4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
(2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
(3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
(4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
(5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
(3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
(4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
(5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1),
(6, 5, 4, 3, 5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0))
movables = ((1, 4), (0, 2, 5), (1, 3, 6), (2, 7), (0, 5, 8), (1, 4, 6, 9), (2, 5, 7, 10), (3, 6, 11),
(4, 9, 12), (5, 8, 10, 13), (6, 9, 11, 14), (7, 10, 15), (8, 13), (9, 12, 14), (10, 13, 15), (11, 14))
swap_mul = tuple(tuple((1 << mf) - (1 << mt) for mt in range(0, 64, 4)) for mf in range(0, 64, 4))
destination = 0xfedcba9876543210
i = 0
board_init = 0
blank_init = 0
for _ in range(4):
for n in map(int, input().split()):
if n:
n -= 1
else:
n = 15
blank_init = i
board_init += n * 16 ** i
i += 1
estimation_init = sum(manhattan[i][((board_init >> (4 * i)) & 15)] for i in range(16) if i != blank_init)
queue = [(estimation_init, board_init, blank_init)]
visited = set()
while True:
estimation, board, blank = heappop(queue)
if board in visited:
continue
elif board == destination:
print(estimation)
break
visited.add(board)
for new_blank in movables[blank]:
num = (board >> (4 * new_blank)) & 15
new_board = board + swap_mul[new_blank][blank] * (15 - num)
if new_board in visited:
continue
new_estimation = estimation + 1 - manhattan[new_blank][num] + manhattan[blank][num]
if new_estimation > 45:
continue
heappush(queue, (new_estimation, new_board, new_blank))
``` | output | 1 | 6,543 | 15 | 13,087 |
Provide a correct Python 3 solution for this coding contest problem.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8 | instruction | 0 | 6,544 | 15 | 13,088 |
"Correct Solution:
```
from heapq import heappop, heappush
manhattan = (
(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
(1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
(2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
(3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
(1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
(2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
(3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
(4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
(2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
(3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
(4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
(5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
(3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
(4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
(5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1),
(6, 5, 4, 3, 5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0))
movables = ((1, 4), (0, 2, 5), (1, 3, 6), (2, 7), (0, 5, 8), (1, 4, 6, 9), (2, 5, 7, 10), (3, 6, 11),
(4, 9, 12), (5, 8, 10, 13), (6, 9, 11, 14), (7, 10, 15), (8, 13), (9, 12, 14), (10, 13, 15), (11, 14))
swap_mul = [[(1 << mf) - (1 << mt) for mt in range(0, 64, 4)] for mf in range(0, 64, 4)]
destination = 0xfedcba9876543210
i = 0
board_init = 0
blank_init = 0
for _ in range(4):
for n in map(int, input().split()):
if n:
n -= 1
else:
n = 15
blank_init = i
board_init += n * 16 ** i
i += 1
estimation_init = sum(manhattan[i][((board_init >> (4 * i)) & 15)] for i in range(16) if i != blank_init)
queue = [(estimation_init, board_init, blank_init)]
visited = set()
while True:
estimation, board, blank = heappop(queue)
if board in visited:
continue
elif board == destination:
print(estimation)
break
visited.add(board)
for new_blank in movables[blank]:
num = (board >> (4 * new_blank)) & 15
new_board = board + swap_mul[new_blank][blank] * (15 - num)
if new_board in visited:
continue
new_estimation = estimation + 1 - manhattan[new_blank][num] + manhattan[blank][num]
if new_estimation > 45:
continue
heappush(queue, (new_estimation, new_board, new_blank))
``` | output | 1 | 6,544 | 15 | 13,089 |
Provide a correct Python 3 solution for this coding contest problem.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8 | instruction | 0 | 6,545 | 15 | 13,090 |
"Correct Solution:
```
from sys import stdin
from heapq import heappush, heappop
solution = [i for i in range(1, 16)] + [0]
sol_idx = (15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
MAX_DEPTH = 45
count = 0
neighbor = (
(1, 4),
(0, 2, 5),
(1, 6, 3),
(2, 7),
(0, 5, 8),
(1, 4, 6, 9),
(2, 5, 7, 10),
(3, 6, 11),
(4, 9, 12),
(5, 8, 10, 13),
(6, 9, 11, 14),
(7, 10, 15),
(8, 13),
(9, 12, 14),
(10, 13, 15),
(11, 14)
)
distance = (
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
(1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
(2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
(3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
(1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
(2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
(3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
(4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
(2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
(3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
(4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
(5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
(3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
(4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
(5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1),
(6, 5, 4, 3, 5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0)
)
def get_diff(B):
return sum([distance[v][i] for i, v in enumerate(B)])
def get_next_board(board, space, prev):
for nxt in neighbor[space]:
if nxt == prev: continue
b = board[:]
b[space], b[nxt] = b[nxt], 0
yield b, nxt
def answer_is_odd(board):
return sum(divmod(board.index(0), 4)) % 2
def search(board):
lower = get_diff(board)
start_depth = lower
if (lower % 2) ^ answer_is_odd(board): start_depth += 1
for limit in range(start_depth, MAX_DEPTH + 1, 2):
get_next(board, limit, 0, board.index(0), None, lower)
if count > 0: return limit
def get_next(board, limit, move, space, prev, lower):
global count
if move == limit:
if board == solution: count += 1
else:
for b, nxt in get_next_board(board, space, prev):
p = board[nxt]
new_lower = lower - distance[p][nxt] + distance[p][space]
if new_lower + move <= limit:
get_next(b, limit, move + 1, nxt, space, new_lower)
boad = []
for i in range(4):
boad.extend(map(int, stdin.readline().split()))
print(search(boad))
``` | output | 1 | 6,545 | 15 | 13,091 |
Provide a correct Python 3 solution for this coding contest problem.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8 | instruction | 0 | 6,546 | 15 | 13,092 |
"Correct Solution:
```
distance = (
(),
(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
(1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
(2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
(3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
(1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
(2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
(3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
(4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
(2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
(3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
(4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
(5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
(3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
(4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
(5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1)
)
def get_distance(board):
sd = 0
for i in range(16):
p = board[i]
if p == 0:
continue
sd += distance[p][i]
return sd
adjacent = (
(1, 4), # 0
(0, 2, 5), # 1
(1, 3, 6), # 2
(2, 7), # 3
(0, 5, 8), # 4
(1, 4, 6, 9), # 5
(2, 5, 7, 10), # 6
(3, 6, 11), # 7
(4, 9, 12), # 8
(5, 8, 10, 13), # 9
(6, 9, 11, 14), # 10
(7, 10, 15), # 11
(8, 13), # 12
(9, 12, 14), # 13
(10, 13, 15), # 14
(11, 14) # 15
)
GOAL = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]
def df_lower_search(limit, move, space, lower, pre_p):
if move == limit:
if board == GOAL:
return True
else:
for i in adjacent[space]:
p = board[i]
if p == pre_p:
continue
board[space] = p
board[i] = 0
new_lower = lower - distance[p][i] + distance[p][space]
if new_lower + move <= limit:
if df_lower_search(limit, move + 1, i, new_lower, p):
return True
board[space] = 0
board[i] = p
import sys
board = list(map(int, sys.stdin.read().split()))
n = get_distance(board)
for l in range(n, 46):
if df_lower_search(l, 0, board.index(0), n, None):
print(l)
break
``` | output | 1 | 6,546 | 15 | 13,093 |
Provide a correct Python 3 solution for this coding contest problem.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8 | instruction | 0 | 6,547 | 15 | 13,094 |
"Correct Solution:
```
board = [int(s) for _ in range(4) for s in input().split()]
move_piece = [None]* 46
GOAL = list(range(1,16)) + [0]
def create_adjacent(h, w):
adjacent = [[] for _ in range(h*w)]
for i in range(h * w):
if i % w != w-1:
adjacent[i].append(i+1)
if i % w != 0:
adjacent[i].append(i-1)
if i // h < h-1:
adjacent[i].append(i+w)
if i // h > 0:
adjacent[i].append(i-w)
return adjacent
def id_search(limit, move, space, lower):
if move == limit:
if board == GOAL:
global count
count += 1
print(move)
exit()
else:
for x in adjacent[space]:
p = board[x]
if move_piece[move] == p:
continue
board[space], board[x] = p, 0
move_piece[move + 1] = p
new_lower = lower - distance[p][x] + distance[p][space]
if new_lower + move <= limit:
id_search(limit, move+1, x, new_lower)
board[space], board[x] = 0, p
def create_distance(h, w):
distance = [[0] * h * w for _ in range(h *w)]
for i in range(h*w):
if i == 0:
continue
ye, xe = divmod(i-1, w)
for j in range(h *w):
y, x = divmod(j,w)
distance[i][j] = abs(ye-y) + abs(xe-x)
return distance
def get_distance(board):
v = 0
for x in range(len(board)):
p = board[x]
if p == 0:
continue
v += distance[p][x]
return v
adjacent = create_adjacent(4, 4)
distance = create_distance(4,4)
n = get_distance(board)
count = 0
for x in range(n,46):
id_search(x, 0, board.index(0), n)
if count > 0:
break
``` | output | 1 | 6,547 | 15 | 13,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8
Submitted Solution:
```
from heapq import heappop, heappush
manhattan = tuple(tuple(abs((i % 4) - (j % 4)) + abs((i // 4) - (j // 4)) for j in range(16)) for i in range(16))
movables = ((1, 4), (0, 2, 5), (1, 3, 6), (2, 7), (0, 5, 8), (1, 4, 6, 9), (2, 5, 7, 10), (3, 6, 11),
(4, 9, 12), (5, 8, 10, 13), (6, 9, 11, 14), (7, 10, 15), (8, 13), (9, 12, 14), (10, 13, 15), (11, 14))
swap_mul = tuple(tuple((1 << mf) - (1 << mt) for mt in range(0, 64, 4)) for mf in range(0, 64, 4))
destination = 0xfedcba9876543210
i = 0
board_init = 0
blank_init = 0
for _ in range(4):
for n in map(int, input().split()):
if n:
n -= 1
else:
n = 15
blank_init = i
board_init += n * 16 ** i
i += 1
estimation_init = sum(manhattan[i][((board_init >> (4 * i)) & 15)] for i in range(16) if i != blank_init)
queue = [(estimation_init, board_init, blank_init)]
visited = set()
while True:
estimation, board, blank = heappop(queue)
if board in visited:
continue
elif board == destination:
print(estimation)
break
visited.add(board)
for new_blank in movables[blank]:
num = (board >> (4 * new_blank)) & 15
new_board = board + swap_mul[new_blank][blank] * (15 - num)
if new_board in visited:
continue
new_estimation = estimation + 1 - manhattan[new_blank][num] + manhattan[blank][num]
if new_estimation > 45:
continue
heappush(queue, (new_estimation, new_board, new_blank))
``` | instruction | 0 | 6,548 | 15 | 13,096 |
Yes | output | 1 | 6,548 | 15 | 13,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8
Submitted Solution:
```
# Manhattan Distance
distance = (
(),
(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
(1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
(2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
(3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
(1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
(2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
(3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
(4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
(2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
(3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
(4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
(5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
(3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
(4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
(5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1)
)
def get_distance(board):
sd = 0
for i in range(16):
p = board[i]
if p == 0:
continue
sd += distance[p][i]
return sd
adjacent = (
(1, 4), # 0
(0, 2, 5), # 1
(1, 3, 6), # 2
(2, 7), # 3
(0, 5, 8), # 4
(1, 4, 6, 9), # 5
(2, 5, 7, 10), # 6
(3, 6, 11), # 7
(4, 9, 12), # 8
(5, 8, 10, 13), # 9
(6, 9, 11, 14), # 10
(7, 10, 15), # 11
(8, 13), # 12
(9, 12, 14), # 13
(10, 13, 15), # 14
(11, 14) # 15
)
GOAL = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]
import sys
import heapq
# A* algorithm
def solve():
board = list(map(int, sys.stdin.read().split()))
h_cost = get_distance(board)
state = (h_cost, board)
q = [state] # priority queue
d = {tuple(board): True}
while q:
state1 = heapq.heappop(q)
h_cost, board = state1
if board == GOAL:
return h_cost
space = board.index(0)
for i in adjacent[space]:
p = board[i]
new_board = board[:]
new_board[space], new_board[i] = p, 0
new_h_cost = h_cost + 1 - distance[p][i] + distance[p][space]
key = tuple(new_board)
if key not in d and new_h_cost <= 45:
new_state = (new_h_cost, new_board)
heapq.heappush(q, new_state)
d[key] = True
print(solve())
``` | instruction | 0 | 6,549 | 15 | 13,098 |
Yes | output | 1 | 6,549 | 15 | 13,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8
Submitted Solution:
```
from sys import stdin
from heapq import heappush, heappop
solution = [i for i in range(1, 16)] + [0]
sol_idx = (15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
MAX_DEPTH = 45
count = 0
neighbor = (
(1, 4),
(0, 2, 5),
(1, 6, 3),
(2, 7),
(0, 5, 8),
(1, 4, 6, 9),
(2, 5, 7, 10),
(3, 6, 11),
(4, 9, 12),
(5, 8, 10, 13),
(6, 9, 11, 14),
(7, 10, 15),
(8, 13),
(9, 12, 14),
(10, 13, 15),
(11, 14)
)
distance = (
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6),
(1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5),
(2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4),
(3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3),
(1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5),
(2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4),
(3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3),
(4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2),
(2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4),
(3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3),
(4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2),
(5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1),
(3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3),
(4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2),
(5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1),
(6, 5, 4, 3, 5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0)
)
def get_diff(B):
return sum([distance[v][i] for i, v in enumerate(B)])
def get_next_board(board, space, prev):
for nxt in neighbor[space]:
if nxt == prev: continue
b = board[:]
b[space], b[nxt] = b[nxt], 0
yield b, nxt
def answer_is_odd(board):
return sum(divmod(board.index(0), 4)) % 2
def search(board):
lower = get_diff(board)
start_depth = lower
if (lower % 2) ^ answer_is_odd(board): start_depth += 1
for limit in range(start_depth, MAX_DEPTH + 1, 2):
get_next(board, limit, 0, board.index(0), None, lower)
if count > 0: return limit
def get_next(board, limit, move, space, prev, lower):
global count
if move == limit:
if board == solution: count += 1
else:
for b, nxt in get_next_board(board, space, prev):
p = board[nxt]
new_lower = lower - distance[p][nxt] + distance[p][space]
if new_lower + move <= limit:
get_next(b, limit, move + 1, nxt, space, new_lower)
boad = [int(a) for _ in range(4) for a in stdin.readline().split()]
print(search(boad))
``` | instruction | 0 | 6,550 | 15 | 13,100 |
Yes | output | 1 | 6,550 | 15 | 13,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8
Submitted Solution:
```
from heapq import heappop, heappush
manhattan = [[abs((i % 4) - (j % 4)) + abs((i // 4) - (j // 4)) for j in range(16)] for i in range(16)]
movables = [{1, 4}, {0, 2, 5}, {1, 3, 6}, {2, 7}, {0, 5, 8}, {1, 4, 6, 9}, {2, 5, 7, 10}, {3, 6, 11},
{4, 9, 12}, {5, 8, 10, 13}, {6, 9, 11, 14}, {7, 10, 15}, {8, 13}, {9, 12, 14}, {10, 13, 15}, {11, 14}]
swap_cache = [[(1 << mf) - (1 << mt) for mt in range(0, 64, 4)] for mf in range(0, 64, 4)]
destination = 0xfedcba9876543210
def swap(board, move_from, move_to):
return board + swap_cache[move_from][move_to] * (15 - ((board >> (4 * move_from)) & 15))
i = 0
board_init = 0
blank_init = 0
for _ in range(4):
for n in map(int, input().split()):
if n:
n -= 1
else:
n = 15
blank_init = i
board_init += n * 16 ** i
i += 1
estimation_init = sum(manhattan[i][((board_init >> (4 * i)) & 15)] for i in range(16) if i != blank_init)
queue = [(estimation_init, board_init, blank_init)]
visited = set()
while True:
estimation, board, blank = heappop(queue)
if board in visited:
continue
elif board == destination:
print(estimation)
break
visited.add(board)
for new_blank in movables[blank]:
new_board = swap(board, new_blank, blank)
if new_board in visited:
continue
num = (board >> (4 * new_blank)) & 15
new_estimation = estimation + 1 - manhattan[new_blank][num] + manhattan[blank][num]
if new_estimation > 45:
continue
heappush(queue, (new_estimation, new_board, new_blank))
``` | instruction | 0 | 6,551 | 15 | 13,102 |
Yes | output | 1 | 6,551 | 15 | 13,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8
Submitted Solution:
```
from collections import deque
def move(P,p):
if p > 3:
tmp = P[:]
tmp[p],tmp[p-4] = tmp[p-4],tmp[p]
tmpp = p - 4
yield tmp,tmpp
if p < 12:
tmp = P[:]
tmp[p],tmp[p+4] = tmp[p+4],tmp[p]
tmpp = p + 4
yield tmp,tmpp
if p%4 > 0:
tmp = P[:]
tmp[p],tmp[p-1] = tmp[p-1],tmp[p]
tmpp = p - 1
yield tmp,tmpp
if p%4 < 3:
tmp = P[:]
tmp[p],tmp[p+1] = tmp[p+1],tmp[p]
tmpp = p + 1
yield tmp,tmpp
A = []
B = [int(i)%16 for i in range(1,17)]
for i in range(4):
A+=[int(i) for i in input().split()]
dp = {str(A) : (1,0),str(B) : (2,0)}
d = deque([(A,0,A.index(0))])
e = deque([(B,0,15)])
flag = True
while(flag):
tmp,count,p = d.pop()
for i,j in move(tmp,p):
key = str(i)
if key in dp:
if dp[key][0] == 2:
ans = count + 1 + dp[key][1]
flag = False
else:
continue
elif key == "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]":
ans = count + 1
flag = False
else:
dp[key] = (1,count+1)
d.appendleft((i,count+1,j))
tmp,count,p = e.pop()
for i,j in move(tmp,p):
key = str(i)
if key in dp:
if dp[key][0] == 1:
ans = count + 1 + dp[key][1]
flag = False
else:
continue
else:
dp[key] = (2,count+1)
e.appendleft((i,count+1,j))
if str(A) == "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]":
ans = 0
print(ans)
``` | instruction | 0 | 6,552 | 15 | 13,104 |
No | output | 1 | 6,552 | 15 | 13,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8
Submitted Solution:
```
from heapq import heapify, heappush, heappop
N = 4
m = {0: {1, 4}, 1: {0, 2, 5}, 2: {1, 3, 6}, 3: {2, 7},
4: {0, 5, 8}, 5: {1, 4, 6, 9}, 6: {2, 5, 7, 10}, 7: {3, 6, 11},
8: {4, 9, 12}, 9: {5, 8, 10, 13}, 10: {6, 9, 11, 14}, 11: {7, 10, 15},
12: {8, 13}, 13: {9, 12, 14}, 14: {10, 13, 15}, 15: {11, 14}}
goal = 0x123456789abcdef0
def g(i, j, a):
t = a // (16 ** j) % 16
return a - t * (16 ** j) + t * (16 ** i)
def solve():
MAP = sum((input().split() for _ in range(N)), [])
start = int("".join(f"{int(i):x}" for i in MAP), base=16)
if start == goal:
return 0
zero = 15 - MAP.index("0")
dp = [(0, start, zero, 1), (0, goal, 0, 0)]
TABLE = {start: (1, 0), goal: (0, 0)}
while dp:
cnt, M, yx, flg = heappop(dp)
cnt += 1
for nyx in m[yx]:
key = g(yx, nyx, M)
if key in TABLE:
if TABLE[key][0] != flg:
return TABLE[key][1] + cnt
continue
TABLE[key] = (flg, cnt)
heappush(dp, (cnt, key, nyx, flg))
def MAIN():
print(solve())
MAIN()
``` | instruction | 0 | 6,553 | 15 | 13,106 |
No | output | 1 | 6,553 | 15 | 13,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8
Submitted Solution:
```
from collections import deque
import heapq
def move(P,p):
if p > 3:
tmp = P[:]
tmp[p],tmp[p-4] = tmp[p-4],tmp[p]
tmpp = p - 4
yield tmp,tmpp
if p < 12:
tmp = P[:]
tmp[p],tmp[p+4] = tmp[p+4],tmp[p]
tmpp = p + 4
yield tmp,tmpp
if p%4 > 0:
tmp = P[:]
tmp[p],tmp[p-1] = tmp[p-1],tmp[p]
tmpp = p - 1
yield tmp,tmpp
if p%4 < 3:
tmp = P[:]
tmp[p],tmp[p+1] = tmp[p+1],tmp[p]
tmpp = p + 1
yield tmp,tmpp
def evaluate(P,Q):
mht = 0
for i in range(16):
pi = P.index(i)
qi = Q.index(i)
pc,pr = pi//4,pi%4
qc,qr = qi//4,qi%4
mht += abs(pc-qc)+abs(pr-qr)
return mht
A = []
B = [int(i)%16 for i in range(1,17)]
for i in range(4):
A+=[int(i) for i in input().split()]
dp = {str(A) : (1,0),str(B) : (2,0)}
h = [(evaluate(A,B),A,0,A.index(0))]
e = [(evaluate(A,B),B,0,15)]
heapq.heapify(h)
heapq.heapify(e)
ans = 46
while(len(h)>0 and len(e)>0):
_,tmp,count,p = heapq.heappop(h)
for i,j in move(tmp,p):
key = str(i)
if key in dp:
if dp[key][0] == 2:
tmpcount = count + 1 + dp[key][1]
if tmpcount < ans:
ans = tmpcount
else:
continue
else:
dp[key] = (1,count+1)
mht = evaluate(B,i)
if count+mht//2 < ans:
heapq.heappush(h,(mht+count,i,count+1,j))
_,tmp,count,p = heapq.heappop(e)
for i,j in move(tmp,p):
key = str(i)
if key in dp:
if dp[key][0] == 1:
tmpcount = count + 1 + dp[key][1]
if tmpcount < ans:
ans = tmpcount
else:
continue
else:
dp[key] = (2,count+1)
mht = evaluate(A,i)
if count+mht//2 < ans:
heapq.heappush(e,(mht+count,i,count+1,j))
if str(A) == "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]":
ans = 0
print(ans)
``` | instruction | 0 | 6,554 | 15 | 13,108 |
No | output | 1 | 6,554 | 15 | 13,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The goal of the 15 puzzle problem is to complete pieces on $4 \times 4$ cells where one of the cells is empty space.
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 15 as shown below.
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
Constraints
* The given puzzle is solvable in at most 45 steps.
Input
The $4 \times 4$ integers denoting the pieces or space are given.
Output
Print the fewest steps in a line.
Example
Input
1 2 3 4
6 7 8 0
5 10 11 12
9 13 14 15
Output
8
Submitted Solution:
```
# 15 Puzzle
import copy
[N, d] = [4, 0]
start = []
goal = [[i + j*N for i in range(1, N + 1)] for j in range(N)]
goal[3][3] = 0
for i in range(N):
start.append(list(map(int, input().split())))
check = start[i].count(0)
if check != 0:
[s_r, s_c] = [i, start[i].index(0)]
def manhattan(value, pairs):
h = 0
if value != 0:
h = abs(pairs[0] - int((value - 1)/4)) + abs(pairs[1] - (value - 4*int((value - 1)/4) - 1))
return h
def calculate(queue, d):
if d > 45:
return 0
while len(queue) != 0:
short_n = queue.pop(0)
h = short_n[0] - short_n[2]
state = short_n[1]
g = short_n[2]
[r, c] = short_n[3]
flag = short_n[4]
#print("left_Q: ", len(queue), "depth: ", d, "h: ", h, "g: ", g, "state: ", state, "g+h: ", short_n[0])
if h == 0:
return short_n[2]
if r - 1 >= 0 and flag != 3:
temp = copy.deepcopy(state)
h2 = short_n[0] - short_n[2] - manhattan(temp[r - 1][c], [r - 1, c]) + manhattan(temp[r - 1][c], [r, c])
[temp[r][c], temp[r - 1][c]] = [temp[r - 1][c], temp[r][c]]
if g + 1 + h2 <= d:
queue.append([h2 + g + 1, temp, g + 1, [r - 1, c], 1])
if c + 1 < N and flag != 4:
temp = copy.deepcopy(state)
h2 = short_n[0] - short_n[2] - manhattan(temp[r][c + 1], [r, c + 1]) + manhattan(temp[r][c + 1], [r, c])
[temp[r][c], temp[r][c + 1]] = [temp[r][c + 1], temp[r][c]]
if g + 1 + h2 <= d:
queue.append([h2 + g + 1, temp, g + 1, [r, c + 1], 2])
if r + 1 < N and flag != 1:
temp = copy.deepcopy(state)
h2 = short_n[0] - short_n[2] - manhattan(temp[r + 1][c], [r + 1, c]) + manhattan(temp[r + 1][c], [r, c])
[temp[r][c], temp[r + 1][c]] = [temp[r + 1][c], temp[r][c]]
if g + 1 + h2 <= d:
queue.append([h2 + g + 1, temp, g + 1, [r + 1, c], 3])
if c - 1 >= 0 and flag != 2:
temp = copy.deepcopy(state)
h2 = short_n[0] - short_n[2] - manhattan(temp[r][c - 1], [r, c - 1]) + manhattan(temp[r][c - 1], [r, c])
[temp[r][c], temp[r][c - 1]] = [temp[r][c - 1], temp[r][c]]
if g + 1 + h2 <= d:
queue.append([h2 + g + 1, temp, g + 1, [r, c - 1], 4])
queue.sort(key = lambda data:data[0])
queue.sort(key = lambda data:data[2], reverse = True)
return -1
s_h = 0
for i in range(N):
for j in range(N):
s_h += manhattan(start[i][j], [i, j])
d = s_h
while True:
queue = [[s_h, start, 0, [s_r, s_c], 0]]
result = calculate(queue, d)
d += 1
if result >= 0:
print(result)
break
``` | instruction | 0 | 6,555 | 15 | 13,110 |
No | output | 1 | 6,555 | 15 | 13,111 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11. | instruction | 0 | 6,679 | 15 | 13,358 |
Tags: greedy
Correct Solution:
```
from sys import stdin,stdout
for i in stdin:
n,m,d=list(map(int,i.strip().split()))
break
for i in stdin:
C=list(map(int,i.strip().split()))
break
if sum(C) + (d-1)*(m+1)<n:
stdout.write('NO\n')
else:
stdout.write('YES\n')
L=[]
k=n-sum(C)
i=1
while k:
L+=[0 for j in range(min(k,d-1))]
if i!=len(C)+1:
L+=[i for j in range(C[i-1])]
i+=1
k-=min(k,d-1)
while i<=len(C):
L+=[i for j in range(C[i-1])]
i+=1
L=list(map(str,L))
stdout.write(' '.join(L))
``` | output | 1 | 6,679 | 15 | 13,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11. | instruction | 0 | 6,680 | 15 | 13,360 |
Tags: greedy
Correct Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 22 19:56:29 2020
@author: dennis
"""
import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
river_width, n_platforms, jump_length = [int(x) for x in input().split()]
platforms = [int(x) for x in input().split()]
river = [0]*river_width
position = -1
remaining = platforms.copy()
for i, platform in enumerate(platforms):
np = position+jump_length
while sum(remaining)+np > river_width:
np -= 1
for x in range(np, np+platform):
river[x] = i+1
position = x
remaining = remaining[1:]
found = False
for x in range(1, jump_length+1):
if river[-x] != 0:
print('YES')
print(*river)
found = True
break
if not found:
print('NO')
``` | output | 1 | 6,680 | 15 | 13,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11. | instruction | 0 | 6,681 | 15 | 13,362 |
Tags: greedy
Correct Solution:
```
n,m,d = map(int,input().split())
c = list(map(int,input().split()))
sc = sum(c)
if sc + (len(c)+ 1) * (d - 1) < n:
print("NO")
else:
print("YES")
ko = n - sc
for i in range(m):
if ko != 0:
if ko >= d - 1:
print("0 " * (d -1 ), end = "")
ko -= d -1
else:
print("0 " * (ko ), end = "")
ko = 0
print((str(i + 1) + " ") * c[i], end = "")
print("0 " * (ko ), end = "")
``` | output | 1 | 6,681 | 15 | 13,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11. | instruction | 0 | 6,682 | 15 | 13,364 |
Tags: greedy
Correct Solution:
```
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int,minp().split())
def solve():
n, m, d = mints()
a = list(mints())
b = [0]*m
s = sum(a)
p = 0
for i in range(m):
b[i] = p + d
p = p + d + a[i] - 1
if b[-1] + a[-1] - 1 + d < n + 1:
print("NO")
return
p = n + 1
c = [0]*n
for i in range(m-1,-1,-1):
if b[i] + a[i] - 1 >= p:
b[i] = p - a[i]
p = b[i]
#print(i, b[i], a[i], b[i]+a[i])
for j in range(b[i], b[i] + a[i]):
c[j-1] = i + 1
print("YES")
print(*c)
solve()
``` | output | 1 | 6,682 | 15 | 13,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11. | instruction | 0 | 6,683 | 15 | 13,366 |
Tags: greedy
Correct Solution:
```
n,m,d=map(int,input().split())
arr=list(map(int,input().split()))
if((m+1)*(d-1)>=(n-sum(arr))):
print("YES")
water=n-sum(arr)
ans=[]
co=0
for i in arr:
co+=1
if(water>d-1):
jump=d-1
water-=jump
else:
jump=water
water=0
for j in range(jump):
ans+=[0]
for j in range(i):
ans+=[co]
for i in range(water):
ans+=[0]
print(*ans)
else:
print("NO")
``` | output | 1 | 6,683 | 15 | 13,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11. | instruction | 0 | 6,684 | 15 | 13,368 |
Tags: greedy
Correct Solution:
```
n, m, d = map(int, input().split())
d -= 1
C = list(map(int, input().split()))
h = n - sum(C)
kek = []
for i in range(m):
if h >= d:
kek += [0] * d + [i + 1] * C[i]
h -= d
else:
kek += [0] * h + [i + 1] * C[i]
h = 0
if h > d:
print('NO')
else:
kek += [0] * h
print('YES')
print(*kek)
``` | output | 1 | 6,684 | 15 | 13,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11. | instruction | 0 | 6,685 | 15 | 13,370 |
Tags: greedy
Correct Solution:
```
n, m, d = map(int, input().split())
c= [int(x) for x in input().split()]
s = sum(c)
# m + 1 intervals
# n -s number of cells
#print(m+1, n -s)
if (n - s ) / (m+1) > d- 1:
print("NO")
else:
print("YES")
r = []
cells = n - s
for i, p in enumerate(c, start=1):
gap = min(cells, d-1)
r.extend([0] * gap)
cells -= gap
r.extend([i] * p)
r.extend([0]*cells)
print(*r)
``` | output | 1 | 6,685 | 15 | 13,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11. | instruction | 0 | 6,686 | 15 | 13,372 |
Tags: greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
N, M, D = map(int, input().split())
C = list(map(int, input().split()))
L = sum(C)+(M+1)*(D-1)
if L < N:
print('NO')
else:
print('YES')
rem = L - N
water = []
for i in range(M+1):
if rem >= D-1:
water.append(0)
rem -= D-1
elif rem > 0:
water.append(D-1-rem)
rem = 0
else:
water.append(D-1)
ans = []
for i in range(M):
ans += [0]*water[i]
ans += [i+1]*C[i]
ans += [0]*water[M]
for a in ans:
print(a, end=' ')
print()
``` | output | 1 | 6,686 | 15 | 13,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11.
Submitted Solution:
```
n, m, d = map(int, input().split())
arr = list(map(int, input().split()))
tmp = [0] * (d - 1)
ans = list()
for idx in range(m):
tmp.extend([idx + 1] * arr[idx])
tmp.extend([0] * (d - 1))
if len(tmp) < n:
print("NO")
exit()
clear_zeros = len(tmp) - n
for v in tmp:
if v == 0 and clear_zeros > 0:
clear_zeros -= 1
else:
ans.append(v)
print("YES")
print(*ans)
``` | instruction | 0 | 6,687 | 15 | 13,374 |
Yes | output | 1 | 6,687 | 15 | 13,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11.
Submitted Solution:
```
from math import ceil
n,m,d = map(int,input().split())
c = list(map(int,input().split()))
total_gap = n-sum(c)
dist_gap = total_gap/(m+1)
#print(total_gap)
#print(dist_gap)
if ceil(dist_gap)<d:
print('YES')
a,b = ceil(dist_gap),ceil(dist_gap)-1 #possible gaps
no_a = (total_gap - ((m+1)*b))//(a-b) #no of a gaps
no_b = m+1-no_a
res = []
ci = 0
while ci<m:
if no_a:
for i in range(a):
res.append('0')
no_a -= 1
else:
for i in range(b):
res.append('0')
no_b -= 1
for i in range(c[ci]):
res.append(str(ci+1))
ci += 1
if no_a:
for i in range(a):
res.append('0')
no_a -= 1
else:
for i in range(b):
res.append('0')
no_b -= 1
print(' '.join(res))
else:
print('NO')
``` | instruction | 0 | 6,688 | 15 | 13,376 |
Yes | output | 1 | 6,688 | 15 | 13,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11.
Submitted Solution:
```
import sys
input = sys.stdin.readline
n,m,d=map(int,input().split())
C=list(map(int,input().split()))
x=sum(C)
if -((n-x)//(-(m+1)))>=d:
print("NO")
else:
print("YES")
y=(n-x)//(m+1)
z=(n-x)-y*(m+1)
ANS=[0]*n
ind=0
for i in range(z):
ind+=y+1
for k in range(C[i]):
ANS[ind]=i+1
ind+=1
#print(*ANS)
for i in range(z,m):
ind+=y
for k in range(C[i]):
ANS[ind]=i+1
ind+=1
print(*ANS)
``` | instruction | 0 | 6,689 | 15 | 13,378 |
Yes | output | 1 | 6,689 | 15 | 13,379 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11.
Submitted Solution:
```
n, m, d = map(int, input().split())
a = list(map(int, input().split()))
if sum(a) + (m + 1) * (d - 1) >= n:
print('YES')
ans = []
m = n - sum(a)
for i, c in enumerate(a):
r = min(m, d - 1)
ans.extend([i + 1] * c + [0] * r)
m -= r
ans = [0] * m + ans
print(*ans)
else:
print('NO')
``` | instruction | 0 | 6,690 | 15 | 13,380 |
Yes | output | 1 | 6,690 | 15 | 13,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11.
Submitted Solution:
```
n, m, d = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a = []
i = 0
j = 0
while i + d < n + 1:
if j >= len(c):
print("NO")
i += d + c[j] - 1
a += [0] * (d - 1)
a += [j+1] * (c[j])
j += 1
s = 0
if len(a) > n:
s += len(a) - n
elif len(a) < n:
a += [0] * (n - len(a))
for i in range(j, m):
s += c[i]
if s != 0:
for i in range(n-1, -1, -1):
if a[i] == 0:
s -= 1
if s == 0:
break
f = i
a = a[:f]
while j < m:
a += [j+1]*(c[j])
j += 1
for i in a:
print(i, end=" ")
``` | instruction | 0 | 6,691 | 15 | 13,382 |
No | output | 1 | 6,691 | 15 | 13,383 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11.
Submitted Solution:
```
'''
https://codeforces.com/contest/1256/problem/C
'''
n, m, d = map(int, input().split())
cs = list(map(int, input().split()))
ls = [0] * m
ls[m - 1] = cs[m - 1]
for i in range(m - 1):
ls[m - i - 2] = ls[m - i - 1] + cs[m - i - 2]
if ls[0] + (d - 1) * (m + 1) < n:
print('NO')
else:
ns = [0] * n
ci = 0
jump = 1
i = 0
while i < n and ci < m:
if ls[ci] < n - i:
if jump < d:
jump += 1
i += 1
ns[i] = 0
else:
print(cs[ci])
for i in range(i, i + cs[ci]):
ns[i] = ci + 1
jump = 1
i += cs[ci]
ci += 1
else:
print(cs[ci])
for i in range(i, i + cs[ci]):
ns[i] = ci + 1
jump = 1
i += cs[ci]
ci += 1
print(' '.join(list(map(str, ns))))
``` | instruction | 0 | 6,692 | 15 | 13,384 |
No | output | 1 | 6,692 | 15 | 13,385 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11.
Submitted Solution:
```
n,m,d = map(int,input().split())
w = list(map(int,input().split()))
arr = [0]*(n+1)
indx = m-1
curr_pos = n+1
flag = True
while curr_pos > 1:
curr_pos -= d
if curr_pos <= 0:
if indx >= 0:
start = 0
while start != w[indx]:
arr[start+1] = (indx+1)
start += 1
break
elif indx < 0:
flag = False
break
else:
start = 0
pos = curr_pos
while start != w[indx]:
arr[pos] = (indx+1)
pos -= 1
start += 1
curr_pos -= (w[indx]-1)
indx -= 1
if flag:
print('YES')
for i in range(1,n+1):
print(arr[i],end=' ')
print()
else:
print('NO')
``` | instruction | 0 | 6,693 | 15 | 13,386 |
No | output | 1 | 6,693 | 15 | 13,387 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.
You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.
You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow:
<image> The first example: n=7.
Input
The first line of the input contains three integers n, m and d (1 β€ n, m, d β€ 1000, m β€ n) β the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains m integers c_1, c_2, ..., c_m (1 β€ c_i β€ n, β_{i=1}^{m} c_i β€ n), where c_i is the length of the i-th platform.
Output
If it is impossible to reach n+1 from 0, print NO in the first line. Otherwise, print YES in the first line and the array a of length n in the second line β the sequence of river cells (excluding cell 0 and cell n + 1).
If the cell i does not belong to any platform, a_i should be 0. Otherwise, it should be equal to the index of the platform (1-indexed, platforms are numbered from 1 to m in order of input) to which the cell i belongs.
Note that all a_i equal to 1 should form a contiguous subsegment of the array a of length c_1, all a_i equal to 2 should form a contiguous subsegment of the array a of length c_2, ..., all a_i equal to m should form a contiguous subsegment of the array a of length c_m. The leftmost position of 2 in a should be greater than the rightmost position of 1, the leftmost position of 3 in a should be greater than the rightmost position of 2, ..., the leftmost position of m in a should be greater than the rightmost position of m-1.
See example outputs for better understanding.
Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0, 1, 0, 2, 2, 0, 3]. The sequence of jumps you perform is 0 β 2 β 4 β 5 β 7 β 8.
Consider the second example: it does not matter how to place the platform because you always can jump from 0 to 11.
Consider the third example: the answer is [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]. The sequence of jumps you perform is 0 β 5 β 6 β 11.
Submitted Solution:
```
if __name__ == '__main__':
n, m, d = map(int, input().split(' '))
c = list(map(int, input().split(' ')))
l = sum(c)
if (m + 1)*(d - 1) + l >= n - 1:
print('YES')
ans = [0]*n
i = 0
p = 0
while i < n:
if l + i + d - 1 < n + 1:
i += d - 1
while i < n and c[p] > 0:
l -= 1
c[p] -= 1
ans[i] = p + 1
i += 1
p += 1
else:
while i < n and p < m and c[p] > 0:
c[p] -= 1
ans[i] = p + 1
i += 1
p += 1
if i + d >= n + 1:
break
print(' '.join(map(str, ans)))
else:
print('NO')
``` | instruction | 0 | 6,694 | 15 | 13,388 |
No | output | 1 | 6,694 | 15 | 13,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations. | instruction | 0 | 6,810 | 15 | 13,620 |
Tags: brute force, geometry, greedy
Correct Solution:
```
t=int(input())
for _ in range(t):
n,k=map(int,input().split())
xs=[]
ys=[]
for ii in range(n):
x,y=map(int,input().split())
xs.append(x)
ys.append(y)
#print(xs)
#print(ys)
done=False
for i in range(n):
max_k=-1
for j in range(n):
max_k = max(max_k, abs(xs[i]-xs[j]) + abs(ys[i]-ys[j]))
if max_k<=k:
print(1)
done=True
break
if not done:
print(-1)
``` | output | 1 | 6,810 | 15 | 13,621 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations. | instruction | 0 | 6,811 | 15 | 13,622 |
Tags: brute force, geometry, greedy
Correct Solution:
```
import math
for _ in range(int(input())):
n,k=map(int,input().split())
l=[]
for __ in range(n):
x,y=map(int,input().split())
l.append([x,y])
c=0
cc=0
#print(l,len(l),n)
for i in range(n):
c=0
for j in range(n):
if i!=j and ((abs(l[i][0]-l[j][0])+abs(l[i][1]-l[j][1])))<=k:
c+=1
#print(i,j,((abs(l[i][0]-l[j][0])**2+abs(l[i][1]-l[j][1])**2)),k)
if c==n-1:
print(1)
cc=1
break
if cc!=1:
print(-1)
``` | output | 1 | 6,811 | 15 | 13,623 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations. | instruction | 0 | 6,812 | 15 | 13,624 |
Tags: brute force, geometry, greedy
Correct Solution:
```
t = int(input())
for i in range(t):
n,k = map(int,input().split())
L = []
for j in range(n):
x,y = map(int,input().split())
L.append([x,y])
mn = 0
ok = False
for i in range(n):
mn = 0
for j in range(n):
if i!= j:
x = abs(L[i][0]-L[j][0]) + abs(L[i][1] - L[j][1])
mn = max(mn,x)
if mn<=k:
ok = True
if ok == False:
print(-1)
else:
print(1)
``` | output | 1 | 6,812 | 15 | 13,625 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations. | instruction | 0 | 6,813 | 15 | 13,626 |
Tags: brute force, geometry, greedy
Correct Solution:
```
import io
import os
from collections import Counter, defaultdict, deque
def solve(N, K, balls):
for x1, y1 in balls:
bad = False
for x2, y2 in balls:
if x1 == x2 and y1 == y2:
continue
if abs(x1 - x2) + abs(y1 - y2) > K:
bad = True
break
if not bad:
return 1
return -1
if __name__ == "__main__":
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
N, K = [int(x) for x in input().split()]
balls = [[int(x) for x in input().split()] for i in range(N)]
ans = solve(N, K, balls)
print(ans)
``` | output | 1 | 6,813 | 15 | 13,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations. | instruction | 0 | 6,814 | 15 | 13,628 |
Tags: brute force, geometry, greedy
Correct Solution:
```
import sys
import os
from io import BytesIO, IOBase
#Fast IO Region
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")
T = int(input())
for _ in range(T):
n, k = map(int, input().split())
arr = []
for i in range(n):
x, y = map(int, input().split())
arr.append((x,y))
ok = False
for i in range(n):
cx, cy = arr[i]
for j in range(n):
if abs(cx - arr[j][0]) + abs(cy - arr[j][1]) > k:
break
else:
ok = True
break
if ok:
print(1)
continue
print(-1)
``` | output | 1 | 6,814 | 15 | 13,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations. | instruction | 0 | 6,815 | 15 | 13,630 |
Tags: brute force, geometry, greedy
Correct Solution:
```
t=int(input())
for s in range(t):
n,k=map(int,input().split())
l=[]
for i in range(n):
x,y=map(int,input().split())
l.append([x,y])
flag=0
for i in range(0,n):
c=0
for j in range(0,n):
if(abs(l[i][0]-l[j][0]) + abs(l[i][1]-l[j][1])>k):
break
c=c+1
if(c==n):
flag=1
print(1)
break
if(flag==0):
print(-1)
``` | output | 1 | 6,815 | 15 | 13,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations. | instruction | 0 | 6,816 | 15 | 13,632 |
Tags: brute force, geometry, greedy
Correct Solution:
```
from os import path
import sys
# mod = int(1e9 + 7)
# import re # can use multiple splits
from math import ceil, floor,gcd,log
from collections import defaultdict , Counter
# from bisect import bisect_left, bisect_right
#popping from the end is less taxing,since you don't have to shift any elements
maxx = float('inf')
if (path.exists('input.txt')):
#------------------Sublime--------------------------------------#
sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
I = lambda :int(sys.stdin.buffer.readline())
tup= lambda : map(int , sys.stdin.buffer.readline().split())
lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda: sys.stdin.readline().replace('\n', '').strip()
# def grid(r, c): return [lint() for i in range(r)]
# def debug(*args, c=6): print('\033[3{}m'.format(c), *args, '\033[0m', file=sys.stderr)
stpr = lambda x : sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
else:
#------------------PYPY FAst I/o--------------------------------#
I = lambda :int(sys.stdin.buffer.readline())
tup= lambda : map(int , sys.stdin.buffer.readline().split())
lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda: sys.stdin.readline().replace('\n', '').strip()
# def grid(r, c): return [lint() for i in range(r)]
stpr = lambda x : sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
# input = sys.stdin.readline
for _ in range(I()):
n , k = tup()
a=[]
for i in range(n):
b ,c = tup()
a.append((b,c))
ans = maxx
for i in range(n):
se = set()
f = 1
for j in range(n):
if i != j :
ana = abs(a[i][0] - a[j][0]) + abs(a[i][1] - a[j][1])
if ana <= k:
se.add(ana)
else:
f =0
break
if f:
ans= min(ans ,len(se))
se.clear()
else:
se.clear()
if ans == maxx:
print(-1)
else:print(1)
``` | output | 1 | 6,816 | 15 | 13,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations. | instruction | 0 | 6,817 | 15 | 13,634 |
Tags: brute force, geometry, greedy
Correct Solution:
```
t= int(input())
for _ in range(t):
n, k = map(int, input().split())
XY = []
for i in range(n):
x,y = map(int, input().split())
XY.append((x, y))
flag = False
for i in range(n):
xi, yi = XY[i]
for j in range(n):
xj, yj = XY[j]
if abs(xi-xj)+abs(yi-yj)>k:
break
else:
flag = True
break
if flag:
print(1)
else:
print(-1)
``` | output | 1 | 6,817 | 15 | 13,635 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations.
Submitted Solution:
```
rn=lambda:int(input())
rl=lambda:list(map(int,input().split()))
rns=lambda:map(int,input().split())
rs=lambda:input()
yn=lambda x:print('Yes') if x else print('No')
YN=lambda x:print('YES') if x else print('NO')
for _ in range(rn()):
n,k=rns()
points=[]
ans=-1
for i in range(n):
points.append(rl())
for i in range(n):
b=[]
for j in range(n):
if i!=j:
b.append(abs(points[i][0]-points[j][0])+abs(points[i][1]-points[j][1]))
if all([i<=k for i in b]):
ans=1
break
print(ans)
``` | instruction | 0 | 6,818 | 15 | 13,636 |
Yes | output | 1 | 6,818 | 15 | 13,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations.
Submitted Solution:
```
# cook your dish here
remaing_test_cases = int(input())
while remaing_test_cases > 0:
points_count,K = map(int,input().split())
points = []
for i in range(points_count):
x,y = map(int,input().split())
points.append([x,y])
flag = 0
for i in range(points_count):
count_power = 0
for j in range(points_count):
if (abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1])) <= K:
count_power = count_power + 1
if count_power == points_count:
print(1)
flag = 1
break
if flag == 0:
print(-1)
remaing_test_cases = remaing_test_cases - 1
``` | instruction | 0 | 6,819 | 15 | 13,638 |
Yes | output | 1 | 6,819 | 15 | 13,639 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations.
Submitted Solution:
```
for i in range(int(input())):
n,k=map(int,input().split());l=[];t=-1
for i in range(n):x,y=map(int,input().split());l.append([x,y])
for i in l:
q=0
for j in l:
if abs(i[1]-j[1])+abs(i[0]-j[0])<=k:q+=1
else:break
if q==n:t=1
print(t)
``` | instruction | 0 | 6,820 | 15 | 13,640 |
Yes | output | 1 | 6,820 | 15 | 13,641 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations.
Submitted Solution:
```
n_tests = int(input())
for test in range(n_tests):
[n, k] = input().split(' ')
k = int(k)
d = []
n = int(n)
for i in range(n):
p = input().split(' ')
p = [int(w) for w in p]
d.append(p)
md = []
t = False
for i in range(n):
q = d[i]
a = 0
for j in range(n):
s = abs(q[0] - d[j][0]) + abs(q[1] - d[j][1])
if s <= k:
a += 1
if a == n:
t = True
break
if t:
print(1)
else:
print(-1)
``` | instruction | 0 | 6,821 | 15 | 13,642 |
Yes | output | 1 | 6,821 | 15 | 13,643 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations.
Submitted Solution:
```
# Competitive Programming Template --> Ankit Josh
import os
from math import factorial,sqrt,ceil,floor,log
from itertools import groupby
import sys
from io import BytesIO, IOBase
def inp(): return sys.stdin.readline().strip()
def IIX(): return (int(x) for x in sys.stdin.readline().split())
def II(): return (int(inp()))
def LI(): return list(map(int, inp().split()))
def LS(): return list(map(str, inp().split()))
def L(x):return list(x)
def out(var): return sys.stdout.write(str(var))
#Graph using Ajdacency List
class GraphAL:
def __init__(self,Nodes,isDirected=False):
self.nodes=[x for x in range(1,Nodes+1) ]
self.adj_list={}
self.isDirected=isDirected
for node in self.nodes:
self.adj_list[node]=[]
def add_edge(self,x,y):
self.adj_list[x].append(y)
if self.isDirected==False:
self.adj_list[y].append(x)
def return_graph(self):
return(self.adj_list)
#Graph using Ajdacency Matrix
class GraphAM:
def __init__(self,Nodes,isDirected=False):
self.adj_matrix=[ [0]*(Nodes+1) for x in range(Nodes+2)]
self.isDirected=isDirected
def add_edge(self,x,y):
if self.isDirected:
self.adj_matrix[x][y]=1
elif self.isDirected==False:
self.adj_matrix[x][y]=1
self.adj_matrix[y][x]=1
def delete_edge(self,x,y):
if self.isDirected:
self.adj_matrix[x][y]=0
if self.isDirected==False:
self.adj_matrix[x][y]=0
self.adj_matrix[y][x]=0
def degree(self,x):
l=self.adj_matrix[x]
return l.count(1)
def return_graph(self):
return self.adj_matrix
'''
nodes=II()
edges=II()
graph=GraphAL(nodes) #Add 'True' to the Graph method, for directed graph
connections=[]
for i in range(edges):
l=LI()
connections.append(l)
for connect in connections:
graph.add_edge(connect[0],connect[1])
grp=graph.return_graph()
'''
def primeFact(n):
hashMap={}
for i in range(2,int(sqrt(n))+1):
if n % i==0:
hashMap[i]=0
while n % i==0:
hashMap[i]+=1
n/=i
if n>1:
hashMap[n]=1
return hashMap
#Code goes here
def solve():
#Start coding
n,k=IIX()
c=[]
for _ in range(n):
x,y=IIX()
c.append([x,y])
xmin=float("inf")
xmax=0
for x in c:
xmin=min(xmin,x[0])
xmax=max(xmax,x[0])
ymin=float("inf")
ymax=0
for x in c:
ymin=min(ymin,x[1])
ymax=max(ymax,x[1])
diffx=abs(xmax-xmin);diffy=abs(ymax-ymin)
if diffx>k or diffy>k:
print(-1)
return
else:
print(1)
return
return
# region fastio
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.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
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")
# endregion
if __name__ == "__main__":
try:
t=II()
for _ in range(t):
solve()
except EOFError as e:
out('')
except RuntimeError as r:
out('')
``` | instruction | 0 | 6,822 | 15 | 13,644 |
No | output | 1 | 6,822 | 15 | 13,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations.
Submitted Solution:
```
def solve(points, k, n):
for i in range(1, n):
if abs(points[i][0] - points[0][0]) + abs(points[i][1] - points[0][1]) > k:
return -1
return 1
for _ in range(int(input())):
n, k = map(int, input().split())
points = []
for i in range(n):
x, y = map(int, input().split())
points.append([x, y])
print(solve(points, k, n))
``` | instruction | 0 | 6,823 | 15 | 13,646 |
No | output | 1 | 6,823 | 15 | 13,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations.
Submitted Solution:
```
def dis(x,y,x1,y1):
return abs(x-x1) + abs(y-y1)
for _ in range(int(input())):
n,k = map(int,input().split())
li = []
for i in range(n):
p = list(map(int,input().split()))
li.append(p)
li = sorted(li,key=lambda x:x[1])
flag = False
count = 0
for i in range(n):
for j in range(i,n):
val = dis(li[i][0],li[i][1],li[j][0],li[j][1])
if val == 0:
count += 1
if val > k:
flag = True
break
if flag:
print(-1)
else:
if count == ((n)*(n+1))//2:
print(0)
else:
print(1)
``` | instruction | 0 | 6,824 | 15 | 13,648 |
No | output | 1 | 6,824 | 15 | 13,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n distinct points (x_1, y_1),β¦,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls.
In one operation, you can select a ball i to charge it. Once charged, all balls with Manhattan distance at most k from ball i move to the position of ball i. Many balls may have the same coordinate after an operation.
More formally, for all balls j such that |x_i - x_j| + |y_i - y_j| β€ k, we assign x_j:=x_i and y_j:=y_i.
<image> An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls.
Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible.
Input
The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains two integers n, k (2 β€ n β€ 100, 0 β€ k β€ 10^6) β the number of balls and the attract power of all balls, respectively.
The following n lines describe the balls' coordinates. The i-th of these lines contains two integers x_i, y_i (0 β€ x_i, y_i β€ 10^5) β the coordinates of the i-th ball.
It is guaranteed that all points are distinct.
Output
For each test case print a single integer β the minimum number of operations to move all balls to the same position, or -1 if it is impossible.
Example
Input
3
3 2
0 0
3 3
1 1
3 3
6 7
8 8
6 9
4 1
0 0
0 1
0 2
0 3
Output
-1
1
-1
Note
In the first test case, there are three balls at (0, 0), (3, 3), and (1, 1) and the attract power is 2. It is possible to move two balls together with one operation, but not all three balls together with any number of operations.
In the second test case, there are three balls at (6, 7), (8, 8), and (6, 9) and the attract power is 3. If we charge any ball, the other two will move to the same position, so we only require one operation.
In the third test case, there are four balls at (0, 0), (0, 1), (0, 2), and (0, 3), and the attract power is 1. We can show that it is impossible to move all balls to the same position with a sequence of operations.
Submitted Solution:
```
for i in range(int(input())):
n,k=map(int,input().split())
l=[]
for i in range(n):
x,y=map(int,input().split())
l.append([x,y])
c=0
q=0
for i in range(n):
if c==n:
q+=1
print(1)
break
c=0
for j in range(n):
if abs(l[i][0]-l[j][0])+abs(l[i][1]-l[j][1])<=k:
c+=1
if q==0:
print(-1)
``` | instruction | 0 | 6,825 | 15 | 13,650 |
No | output | 1 | 6,825 | 15 | 13,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all.
<image>
The park can be represented as a rectangular n Γ m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.
Om Nom isn't yet sure where to start his walk from but he definitely wants:
* to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
* to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).
We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.
Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.
Input
The first line contains three integers n, m, k (2 β€ n, m β€ 2000; 0 β€ k β€ m(n - 1)).
Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down).
It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.
Output
Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.
Examples
Input
3 3 4
...
R.L
R.U
Output
0 2 2
Input
2 2 2
..
RL
Output
1 1
Input
2 2 2
..
LR
Output
0 0
Input
3 4 8
....
RRLL
UUUU
Output
1 3 3 1
Input
2 2 2
..
UU
Output
0 0
Note
Consider the first sample. The notes below show how the spider arrangement changes on the field over time:
... ... ..U ...
R.L -> .*U -> L.R -> ...
R.U .R. ..R ...
Character "*" represents a cell that contains two spiders at the same time.
* If Om Nom starts from the first cell of the first row, he won't see any spiders.
* If he starts from the second cell, he will see two spiders at time 1.
* If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | instruction | 0 | 7,009 | 15 | 14,018 |
Tags: implementation, math
Correct Solution:
```
n,m,k=[int(x) for x in input().split()]
park=[input() for i in range(n)]
answers=[0]*m
for inot in range(n):
for j in range(m):
if park[inot][j]!='.':
i=(j,inot,park[inot][j])
if i[2]=='R':
if i[0]+i[1]<m:
answers[i[0]+i[1]]+=1
elif i[2]=='L':
if i[0]-i[1]>=0:
answers[i[0]-i[1]]+=1
elif i[2]=='U':
if not i[1]&1:
answers[i[0]]+=1
for i in answers:
print(i,end=' ')
``` | output | 1 | 7,009 | 15 | 14,019 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.