message stringlengths 2 48.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 318 108k | cluster float64 8 8 | __index_level_0__ int64 636 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anya loves to fold and stick. Today she decided to do just that.
Anya has n cubes lying in a line and numbered from 1 to n from left to right, with natural numbers written on them. She also has k stickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes.
Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120.
You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most k exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can you do it?
Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.
Input
The first line of the input contains three space-separated integers n, k and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n, 1 ≤ S ≤ 1016) — the number of cubes and the number of stickers that Anya has, and the sum that she needs to get.
The second line contains n positive integers ai (1 ≤ ai ≤ 109) — the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.
Multiple cubes can contain the same numbers.
Output
Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.
Examples
Input
2 2 30
4 3
Output
1
Input
2 2 7
4 3
Output
1
Input
3 1 1
1 1 1
Output
6
Note
In the first sample the only way is to choose both cubes and stick an exclamation mark on each of them.
In the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them.
In the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six.
Submitted Solution:
```
from itertools import combinations
import math
if __name__ == '__main__':
string = input().split(' ')
n, k, G = int(string[0]), int(string[1]), int(string[2])
a = []
for each in input().split(' '):
a.append(int(each))
b = list(range(len(a)))
arr = set()
# for i in range(k + 1):
# com = list(combinations(b, i))
# for each in com:
# arr1 = a[:]
# for value in each:
# arr1[value] = math.factorial(arr1[value])
# arr.add(tuple(arr1))
com = list(combinations(b, k))
for each in com:
arr1 = a[:]
for value in each:
arr1[value] = math.factorial(arr1[value])
arr.add(tuple(arr1))
count = 0
for each in arr:
for i in range(1, len(a) + 1):
for d in combinations(each, i):
if sum(d) == G:
count += 1
print(count)
``` | instruction | 0 | 26,918 | 8 | 53,836 |
No | output | 1 | 26,918 | 8 | 53,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anya loves to fold and stick. Today she decided to do just that.
Anya has n cubes lying in a line and numbered from 1 to n from left to right, with natural numbers written on them. She also has k stickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes.
Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120.
You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most k exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can you do it?
Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.
Input
The first line of the input contains three space-separated integers n, k and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n, 1 ≤ S ≤ 1016) — the number of cubes and the number of stickers that Anya has, and the sum that she needs to get.
The second line contains n positive integers ai (1 ≤ ai ≤ 109) — the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.
Multiple cubes can contain the same numbers.
Output
Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.
Examples
Input
2 2 30
4 3
Output
1
Input
2 2 7
4 3
Output
1
Input
3 1 1
1 1 1
Output
6
Note
In the first sample the only way is to choose both cubes and stick an exclamation mark on each of them.
In the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them.
In the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six.
Submitted Solution:
```
from collections import defaultdict
N, K, S = map( int, input().split() )
A = list( map( int, input().split() ) )
fact = [ 1 ]
for i in range( 1, 20, 1 ):
fact.append( fact[ i - 1 ] * i )
ldp = [ [ defaultdict( int ) for i in range( 1 << N // 2 ) ] for j in range( K + 1 ) ]
ldp[ 0 ][ 0 ][ 0 ] = 1
for i in range( K + 1 ):
for s in range( 1 << N // 2 ):
for j in range( N // 2 ):
if ~s >> j & 1:
for key in ldp[ i ][ s ]:
ldp[ i ][ s | 1 << j ][ key + A[ j ] ] += ldp[ i ][ s ][ key ]
if i + 1 <= K and A[ j ] <= 18:
ldp[ i + 1 ][ s | 1 << j ][ key + fact[ A[ j ] ] ] += ldp[ i ][ s ][ key ]
lbag = [ defaultdict( int ) for i in range( K + 1 ) ]
for i in range( K + 1 ):
for j in range( 1 << N // 2 ):
for key in ldp[ i ][ j ]:
lbag[ i ][ key ] += ldp[ i ][ j ][ key ]
rdp = [ [ defaultdict( int ) for i in range( 1 << N - N // 2 ) ] for j in range( K + 1 ) ]
rdp[ 0 ][ 0 ][ 0 ] = 1
for i in range( K + 1 ):
for s in range( 1 << N - N // 2 ):
for j in range( N - N // 2 ):
if ~s >> j & 1:
for key in rdp[ i ][ s ]:
rdp[ i ][ s | 1 << j ][ key + A[ N // 2 + j ] ] += rdp[ i ][ s ][ key ]
if i + 1 <= K and A[ N // 2 + j ] <= 18:
rdp[ i + 1 ][ s | 1 << j ][ key + fact[ A[ N // 2 + j ] ] ] += rdp[ i ][ s ][ key ]
ans = 0
for i in range( K + 1 ):
for s in range( 1 << N - N // 2 ):
for key in rdp[ i ][ s ]:
for j in range( 0, K - i + 1, 1 ):
ans += lbag[ j ][ S - key ]
print( ans )
``` | instruction | 0 | 26,919 | 8 | 53,838 |
No | output | 1 | 26,919 | 8 | 53,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anya loves to fold and stick. Today she decided to do just that.
Anya has n cubes lying in a line and numbered from 1 to n from left to right, with natural numbers written on them. She also has k stickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes.
Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120.
You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most k exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can you do it?
Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.
Input
The first line of the input contains three space-separated integers n, k and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n, 1 ≤ S ≤ 1016) — the number of cubes and the number of stickers that Anya has, and the sum that she needs to get.
The second line contains n positive integers ai (1 ≤ ai ≤ 109) — the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.
Multiple cubes can contain the same numbers.
Output
Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.
Examples
Input
2 2 30
4 3
Output
1
Input
2 2 7
4 3
Output
1
Input
3 1 1
1 1 1
Output
6
Note
In the first sample the only way is to choose both cubes and stick an exclamation mark on each of them.
In the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them.
In the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six.
Submitted Solution:
```
def fact(n):
result = 1
for i in range(2, n + 1):
result *= i
return result
n, k, S = (int(s) for s in input().split())
numbers = [int(s) for s in input().split()]
factorials = [fact(numbers[i]) for i in range(n)]
def recursion(i, _k, _s):
if i < n and _s == 0:
return 0
if i >= n and _s > 0:
return 0
if _s < 0:
return 0
if _s == 0:
return 1
print(i, _k, _s)
if _k > 0:
return recursion(i + 1, _k - 1, _s - factorials[i]) + recursion(i + 1, _k, _s - numbers[i])
else:
return recursion(i + 1, _k, _s - numbers[i])
print(recursion(0, k, S))
``` | instruction | 0 | 26,920 | 8 | 53,840 |
No | output | 1 | 26,920 | 8 | 53,841 |
Provide a correct Python 3 solution for this coding contest problem.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667 | instruction | 0 | 27,315 | 8 | 54,630 |
"Correct Solution:
```
n,x = map(int, input().split())
max_slope = 0.0
for i in range(n):
xi,hi = map(float, input().split())
slope = hi/xi
if slope > max_slope : max_slope = slope
print(x*max_slope)
``` | output | 1 | 27,315 | 8 | 54,631 |
Provide a correct Python 3 solution for this coding contest problem.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667 | instruction | 0 | 27,316 | 8 | 54,632 |
"Correct Solution:
```
n,t=map(int,input().split())
ans=0
for i in range(n):
x,h=map(int,input().split())
ans=max(ans,h*t/x)
print(ans)
``` | output | 1 | 27,316 | 8 | 54,633 |
Provide a correct Python 3 solution for this coding contest problem.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667 | instruction | 0 | 27,317 | 8 | 54,634 |
"Correct Solution:
```
n,t = map(int,input().split())
ans = 0
for i in range(n):
a,b = map(int,input().split())
if ans <= b/a:
ans = b/a
print(ans * t)
``` | output | 1 | 27,317 | 8 | 54,635 |
Provide a correct Python 3 solution for this coding contest problem.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667 | instruction | 0 | 27,318 | 8 | 54,636 |
"Correct Solution:
```
n, t = map(int, input().split())
a = [tuple(map(int, input().split())) for _ in range(n)]
print(max([a[i][1] / a[i][0] for i in range(n)]) * t)
``` | output | 1 | 27,318 | 8 | 54,637 |
Provide a correct Python 3 solution for this coding contest problem.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667 | instruction | 0 | 27,319 | 8 | 54,638 |
"Correct Solution:
```
n,t=map(int,input().split())
lsx=[]
lsh=[]
for i in range(n):
x,h=map(int,input().split())
lsx.append(x)
lsh.append(h)
tam=0
for j in range(n):
if lsh[j]/lsx[j]>tam:
tam=lsh[j]/lsx[j]
print(t*tam)
``` | output | 1 | 27,319 | 8 | 54,639 |
Provide a correct Python 3 solution for this coding contest problem.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667 | instruction | 0 | 27,320 | 8 | 54,640 |
"Correct Solution:
```
N,t = [int(i) for i in input().split()]
r = 0.0
for l in range(N):
x,h = [float(i) for i in input().split()]
r = max(r, h / x)
print(float(t)*r)
``` | output | 1 | 27,320 | 8 | 54,641 |
Provide a correct Python 3 solution for this coding contest problem.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667 | instruction | 0 | 27,321 | 8 | 54,642 |
"Correct Solution:
```
import math
n, t = map(int, input().split())
angle = 0
for _ in range(n):
x, h = map(int, input().split())
tmp = math.atan2(h, x)
if tmp > angle:
angle = tmp
y = math.tan(angle) * t
print(y)
``` | output | 1 | 27,321 | 8 | 54,643 |
Provide a correct Python 3 solution for this coding contest problem.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667 | instruction | 0 | 27,322 | 8 | 54,644 |
"Correct Solution:
```
n, t = map(int, input().split())
min_height = 0
for _ in range(n):
x, h = map(int, input().split())
min_height = max(min_height, h / x * t)
print(min_height)
``` | output | 1 | 27,322 | 8 | 54,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667
Submitted Solution:
```
N, t = map(int, input().split())
ans = 0
for i in range(N):
x, h = map(int, input().split())
ans = max(ans, h*t/x)
print(ans)
``` | instruction | 0 | 27,323 | 8 | 54,646 |
Yes | output | 1 | 27,323 | 8 | 54,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667
Submitted Solution:
```
N, t = map(int, input().split())
tan = 0
for _ in range(N):
x, h = map(int, input().split())
tan = max(tan, h/x)
print(tan*t)
``` | instruction | 0 | 27,324 | 8 | 54,648 |
Yes | output | 1 | 27,324 | 8 | 54,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A project is underway to build a new viewing tower in Bange town called “Bange Hills Tower” whose selling point will be the gorgeous view of the entire main keep of Wakamatsu Castle from top to bottom. Therefore, the view line from the top of the tower must reach the bottom of the keep without being hindered by any of the buildings in the town.
<image>
Write a program to calculate the minimum tower height required to view the keep in its entirety based on the following information: the planned location of the tower and the heights and locations of existing buildings. Assume all the buildings, including the keep, are vertical lines without horizontal stretch. “view of the entire keep” means that the view line from the tower top can cover the keep from the bottom to the top without intersecting (contacts at the top are exempted) any of the other vertical lines (i.e., buildings).
Input
The input is given in the following format.
N t
x_1 h_1
x_2 h_2
:
x_N h_N
The first line provides the number of existing buildings N (1≤N≤1000) and the planned location of the tower t (2≤t≤105) in integers. Each of the subsequent N lines provides the information of the i-th building: location x_i (1 ≤ x_i < t) and height from the ground h_i (1 ≤ h_i ≤ 100). All position information is one-dimensional along the ground line whose origin coincides with the Keep location. No more than one building is located in the same location (i.e. if i ≠ j, then x_i ≠ x_j).
Output
Output the required height as a real number. No limits on the number of decimal places as long as the error does not exceed ± 10-3.
Example
Input
3 10
6 4
4 2
3 2
Output
6.666667
Submitted Solution:
```
N, t = [int(e) for e in input().split()]
ts = [[int(e) for e in input().split()] for _ in range(N)]
result = 0
for x, h in ts:
r = h * t / x
result = max(result, r)
print(result)
``` | instruction | 0 | 27,325 | 8 | 54,650 |
Yes | output | 1 | 27,325 | 8 | 54,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.
Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1].
<image>
Input
The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follow n lines containing two integers xi and hi ( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.
Output
Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes the i-th domino to the right (including the domino itself).
Examples
Input
4
16 5
20 5
10 10
18 2
Output
3 1 4 1
Input
4
0 10
1 5
9 10
15 10
Output
4 1 2 1 | instruction | 0 | 27,871 | 8 | 55,742 |
Tags: binary search, data structures, sortings
Correct Solution:
```
from typing import TypeVar, Generic, Callable, List
import sys
from array import array # noqa: F401
from bisect import bisect_left, bisect_right
def input():
return sys.stdin.buffer.readline().decode('utf-8')
T = TypeVar('T')
class SegmentTree(Generic[T]):
__slots__ = ["size", "tree", "identity", "op", "update_op"]
def __init__(self, size: int, identity: T, op: Callable[[T, T], T],
update_op: Callable[[T, T], T]) -> None:
self.size = size
self.tree = [identity] * (size * 2)
self.identity = identity
self.op = op
self.update_op = update_op
def build(self, a: List[T]) -> None:
tree = self.tree
tree[self.size:self.size + len(a)] = a
for i in range(self.size - 1, 0, -1):
tree[i] = self.op(tree[i << 1], tree[(i << 1) + 1])
def find(self, left: int, right: int) -> T:
left += self.size
right += self.size
tree, result, op = self.tree, self.identity, self.op
while left < right:
if left & 1:
result = op(tree[left], result)
left += 1
if right & 1:
result = op(tree[right - 1], result)
left, right = left >> 1, right >> 1
return result
def update(self, i: int, value: T) -> None:
op, tree = self.op, self.tree
i = self.size + i
tree[i] = self.update_op(tree[i], value)
while i > 1:
i >>= 1
tree[i] = op(tree[i << 1], tree[(i << 1) + 1])
n = int(input())
a = sorted((tuple(map(int, input().split())) + (i, ) for i in range(n)), reverse=True)
pos = sorted(x for x, _, _ in a)
x_set = set()
for x, h, i in a:
x_set.add(x)
x_set.add(x + h)
comp_dict = {x: i for i, x in enumerate(sorted(x_set), start=1)}
segt = SegmentTree[int](len(x_set) + 10, -10**9, max, max)
ans = [0] * n
for x, h, i in a:
rx = max(x + h, segt.find(comp_dict[x], comp_dict[x + h]))
res = bisect_left(pos, rx - 1) - bisect_left(pos, x)
# print(x, rx, pos, bisect_left(pos, rx - 1))
ans[i] = res
segt.update(comp_dict[x], rx)
print(*ans)
``` | output | 1 | 27,871 | 8 | 55,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.
Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1].
<image>
Input
The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follow n lines containing two integers xi and hi ( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.
Output
Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes the i-th domino to the right (including the domino itself).
Examples
Input
4
16 5
20 5
10 10
18 2
Output
3 1 4 1
Input
4
0 10
1 5
9 10
15 10
Output
4 1 2 1
Submitted Solution:
```
from typing import List, Tuple
from bisect import bisect_left
import math
def get_max(tree: List[int], node: int, nl:int, nr: int, l: int, r: int) -> int:
if nl >= l and nr <= r:
return tree[node]
if nl > r or nr < l:
return -1
mid = (nl + nr) // 2
return max(
get_max(tree, 2 * node + 1, nl, mid, l, r),
get_max(tree, 2 * node + 2, mid + 1, nr, l, r),
)
def update_max(tree: List[int], node: int, nl:int, nr: int, id: int, val: int) -> int:
if nl == id and nr == id:
tree[node] = max(tree[node], val)
return tree[node]
mid = (nl + nr) // 2
if id <= mid:
new_max = update_max(tree, 2 * node + 1, nl, mid, id, val)
else:
new_max = update_max(tree, 2 * node + 2, mid + 1, nr, id, val)
tree[node] = max(tree[node], new_max)
return tree[node]
def compute(n: int, d: List[Tuple[int, int]]) -> List[int]:
if n == 1:
return [1]
d = [(x, i, h) for i, (x, h) in enumerate(d)]
d = sorted(d)
xp = [x for x, _, _ in d]
tree_levels = int(math.ceil(math.log2(n)))
tree_size = 2 ** (tree_levels + 1) - 1
tree = [0] * tree_size
output = [1] * n
for i in range(n - 1, -1, -1):
x, id, h = d[i]
dist = x + h
j = bisect_left(xp, dist, i) - 1
if i == j:
max_dist = dist
else:
max_dist = max(get_max(tree, 0, 0, n - 1, i + 1, j), dist)
output[id] = bisect_left(xp, max_dist, i) - i
update_max(tree, 0, 0, n - 1, i, max_dist)
return output
if __name__ == '__main__':
N = int(input())
D = [input().split() for _ in range(N)]
D = [(int(x), int(h)) for x, h in D]
print(' '.join([str(val) for val in compute(N, D)]))
``` | instruction | 0 | 27,872 | 8 | 55,744 |
No | output | 1 | 27,872 | 8 | 55,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.
Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1].
<image>
Input
The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follow n lines containing two integers xi and hi ( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.
Output
Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes the i-th domino to the right (including the domino itself).
Examples
Input
4
16 5
20 5
10 10
18 2
Output
3 1 4 1
Input
4
0 10
1 5
9 10
15 10
Output
4 1 2 1
Submitted Solution:
```
from typing import TypeVar, Generic, Callable, List
import sys
from array import array # noqa: F401
from bisect import bisect_left, bisect_right
def input():
return sys.stdin.buffer.readline().decode('utf-8')
T = TypeVar('T')
class SegmentTree(Generic[T]):
__slots__ = ["size", "tree", "identity", "op", "update_op"]
def __init__(self, size: int, identity: T, op: Callable[[T, T], T],
update_op: Callable[[T, T], T]) -> None:
self.size = size
self.tree = [identity] * (size * 2)
self.identity = identity
self.op = op
self.update_op = update_op
def build(self, a: List[T]) -> None:
tree = self.tree
tree[self.size:self.size + len(a)] = a
for i in range(self.size - 1, 0, -1):
tree[i] = self.op(tree[i << 1], tree[(i << 1) + 1])
def find(self, left: int, right: int) -> T:
left += self.size
right += self.size
tree, result, op = self.tree, self.identity, self.op
while left < right:
if left & 1:
result = op(tree[left], result)
left += 1
if right & 1:
result = op(tree[right - 1], result)
left, right = left >> 1, right >> 1
return result
def update(self, i: int, value: T) -> None:
op, tree = self.op, self.tree
i = self.size + i
tree[i] = self.update_op(tree[i], value)
while i > 1:
i >>= 1
tree[i] = op(tree[i << 1], tree[(i << 1) + 1])
n = int(input())
a = sorted((tuple(map(int, input().split())) + (i, ) for i in range(n)), reverse=True)
pos = sorted(x for x, _, _ in a)
x_set = set()
for x, h, i in a:
x_set.add(x)
x_set.add(x + h)
comp_dict = {x: i for i, x in enumerate(sorted(x_set), start=1)}
segt = SegmentTree[int](len(x_set) + 10, 0, max, max)
ans = [0] * n
for x, h, i in a:
rx = max(x + h, segt.find(comp_dict[x], comp_dict[x + h]))
res = bisect_right(pos, rx - 1) - bisect_left(pos, x)
ans[i] = res
segt.update(comp_dict[x], rx)
print(*ans)
``` | instruction | 0 | 27,873 | 8 | 55,746 |
No | output | 1 | 27,873 | 8 | 55,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.
Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1].
<image>
Input
The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follow n lines containing two integers xi and hi ( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.
Output
Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes the i-th domino to the right (including the domino itself).
Examples
Input
4
16 5
20 5
10 10
18 2
Output
3 1 4 1
Input
4
0 10
1 5
9 10
15 10
Output
4 1 2 1
Submitted Solution:
```
from typing import TypeVar, Generic, Callable, List
import sys
from array import array # noqa: F401
from bisect import bisect_left, bisect_right
def input():
return sys.stdin.buffer.readline().decode('utf-8')
T = TypeVar('T')
class SegmentTree(Generic[T]):
__slots__ = ["size", "tree", "identity", "op", "update_op"]
def __init__(self, size: int, identity: T, op: Callable[[T, T], T],
update_op: Callable[[T, T], T]) -> None:
self.size = size
self.tree = [identity] * (size * 2)
self.identity = identity
self.op = op
self.update_op = update_op
def build(self, a: List[T]) -> None:
tree = self.tree
tree[self.size:self.size + len(a)] = a
for i in range(self.size - 1, 0, -1):
tree[i] = self.op(tree[i << 1], tree[(i << 1) + 1])
def find(self, left: int, right: int) -> T:
left += self.size
right += self.size
tree, result, op = self.tree, self.identity, self.op
while left < right:
if left & 1:
result = op(tree[left], result)
left += 1
if right & 1:
result = op(tree[right - 1], result)
left, right = left >> 1, right >> 1
return result
def update(self, i: int, value: T) -> None:
op, tree = self.op, self.tree
i = self.size + i
tree[i] = self.update_op(tree[i], value)
while i > 1:
i >>= 1
tree[i] = op(tree[i << 1], tree[(i << 1) + 1])
n = int(input())
a = sorted((tuple(map(int, input().split())) + (i, ) for i in range(n)), reverse=True)
pos = sorted(x for x, _, _ in a)
x_set = set()
for x, h, i in a:
x_set.add(x)
x_set.add(x + h)
comp_dict = {x: i for i, x in enumerate(sorted(x_set), start=1)}
segt = SegmentTree[int](len(x_set) + 10, 0, max, max)
ans = [0] * n
for x, h, i in a:
rx = max(x + h, segt.find(comp_dict[x], comp_dict[x + h]))
res = bisect_left(pos, rx - 1) - bisect_left(pos, x)
# print(x, rx, pos, bisect_left(pos, rx - 1))
ans[i] = res
segt.update(comp_dict[x], rx)
print(*ans)
``` | instruction | 0 | 27,874 | 8 | 55,748 |
No | output | 1 | 27,874 | 8 | 55,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.
Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1].
<image>
Input
The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follow n lines containing two integers xi and hi ( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.
Output
Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes the i-th domino to the right (including the domino itself).
Examples
Input
4
16 5
20 5
10 10
18 2
Output
3 1 4 1
Input
4
0 10
1 5
9 10
15 10
Output
4 1 2 1
Submitted Solution:
```
from typing import List, Tuple
from bisect import bisect_left
import math
def get_max(tree: List[int], node: int, nl:int, nr: int, l: int, r: int) -> int:
if nl >= l and nr <= r:
return tree[node]
if nl > r or nr < l:
return -1
mid = (nl + nr) // 2
return max(
get_max(tree, 2 * node + 1, nl, mid, l, r),
get_max(tree, 2 * node + 2, mid + 1, nr, l, r),
)
def update_max(tree: List[int], node: int, nl:int, nr: int, id: int, val: int) -> int:
if nl == id and nr == id:
tree[node] = max(tree[node], val)
return tree[node]
mid = (nl + nr) // 2
if id <= mid:
new_max = update_max(tree, 2 * node + 1, nl, mid, id, val)
else:
new_max = update_max(tree, 2 * node + 2, mid + 1, nr, id, val)
tree[node] = max(tree[node], new_max)
return tree[node]
def compute(n: int, d: List[Tuple[int, int]]) -> List[int]:
if n == 1:
return [1]
d = [(x, i, h) for i, (x, h) in enumerate(d)]
d = sorted(d)
xp = [x for x, _, _ in d]
tree_levels = int(math.ceil(math.log2(n)))
tree_size = 2 ** (tree_levels + 1) - 1
tree = [0] * tree_size
output = [1] * n
for i in range(n - 1, -1, -1):
x, id, h = d[i]
j = bisect_left(xp, x + h, i) - 1
if i == j:
update_max(tree, 0, 0, n - 1, i, x + h)
else:
max_dist = max(get_max(tree, 0, 0, n - 1, i, j), x + h)
update_max(tree, 0, 0, n - 1, i, max_dist)
output[id] = bisect_left(xp, max_dist, i) - i
return output
if __name__ == '__main__':
N = int(input())
D = [input().split() for _ in range(N)]
D = [(int(x), int(h)) for x, h in D]
print(' '.join([str(val) for val in compute(N, D)]))
``` | instruction | 0 | 27,875 | 8 | 55,750 |
No | output | 1 | 27,875 | 8 | 55,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem. In the interaction section below you will find the information about flushing the output.
The New Year tree of height h is a perfect binary tree with vertices numbered 1 through 2h - 1 in some order. In this problem we assume that h is at least 2. The drawing below shows one example New Year tree of height 3:
<image>
Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that h ≥ 2). It won't be easy because Limak is a little bear and he doesn't even see the whole tree. Can you help him?
There are t testcases. In each testcase, you should first read h from the input. Then you can ask at most 16 questions of format "? x" (without quotes), where x is an integer between 1 and 2h - 1, inclusive. As a reply you will get the list of neighbours of vertex x (more details in the "Interaction" section below). For example, for a tree on the drawing above after asking "? 1" you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root y and print it in the format "! y". You will be able to read h for a next testcase only after printing the answer in a previous testcase and flushing the output.
Each tree is fixed from the beginning and it doesn't change during your questions.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 500) — the number of testcases.
At the beginning of each testcase you should read from the input a single integer h (2 ≤ h ≤ 7) — the height of the tree. You can't read the value of h in a next testcase until you answer a previous testcase.
Interaction
To ask a question about neighbours of vertex x, print "? x" (without quotes) on a separate line. Note, you must print an end-of-line character after the last character of the line and flush your output to get a response.
The response will consist of two lines. The first line will contain a single integer k (1 ≤ k ≤ 3) — the number of neighbours of vertex x. The second line will contain k distinct integers t1, ..., tk (1 ≤ t1 < ... < tk ≤ 2h - 1) — indices of neighbours of vertex x, gives in the increasing order.
After asking at most 16 questions you have to say y — the index of the root. Print "! y" (without quotes) and an end-of-line character, and flush the output.
Each tree is fixed from the beginning and it doesn't change during your questions.
You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output.
To flush you can use (just printing a query/answer and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
In any moment if the program reads h = 0 or k = 0 it should immediately terminate normally (for example, calling exit(0)). It means that the system detected incorrect request/output from your program and printed 0 because if can't process your requests anymore. In this case you'll receive verdict "Wrong Answer", but if you ignore case h = 0 or k = 0 it could lead to "Runtime Error", "Time/Memory limit exceeded" or any other verdict because your program could read a trash from the closed input stream.
Hacking. To hack someone, use the following format:
The first line should contain a single integer t equal to 1 (only one testcase is allowed in hacks). The second line should contain a single integer h. Each of next 2h - 2 lines should contain two distinct integers ai and bi (1 ≤ ai, bi ≤ 2h - 1), denoting two nodes connected with an edge. The printed edges must form a perfect binary tree of height h.
Of course, contestant programs will not be able to see this input.
Examples
Input
1
3
3
4 5 7
2
1 2
1
2
Output
? 1
? 5
? 6
! 5
Input
2
2
1
3
2
1 2
2
1 2
4
3
3 12 13
Output
? 1
? 3
? 3
! 3
? 6
! 1
Note
In the first sample, a tree corresponds to the drawing from the statement.
In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below.
<image>
Submitted Solution:
```
import sys
def req(k, dic):
print("?", k)
sys.stdout.flush()
res = list(map(int, input().split()))
m = res.pop(0)
if m:
dic[k] = res
return m
T = int(input())
for t in range(T):
h = int(input())
if not h: break
dic = {}
old = None
path = [1]
nxt = 0
while True:
while True:
cur = path[-1]
nxt = req(cur, dic)
if nxt < 3:
break
else:
for v in dic[cur]:
if v not in path:
path.append(v)
break
if nxt == 0:
exit()
elif nxt == 2:
break
elif old:
#print(old, path)
big = list(reversed(old)) + path[1:]
nn = len(big) // 2
old = big[nn:]
#print(nn, big, old)
path = [old[0]]
cur = path[-1]
#print(nn, big, old, path, cur)
for v in dic[cur]:
if v not in big:
path.append(v)
break
else:
old = path
path = [old[0]]
cur = path[-1]
#print(old, path, cur)
for v in dic[cur]:
if v not in old:
path.append(v)
break
print("!", cur)
sys.stdout.flush()
``` | instruction | 0 | 27,929 | 8 | 55,858 |
No | output | 1 | 27,929 | 8 | 55,859 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem. In the interaction section below you will find the information about flushing the output.
The New Year tree of height h is a perfect binary tree with vertices numbered 1 through 2h - 1 in some order. In this problem we assume that h is at least 2. The drawing below shows one example New Year tree of height 3:
<image>
Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that h ≥ 2). It won't be easy because Limak is a little bear and he doesn't even see the whole tree. Can you help him?
There are t testcases. In each testcase, you should first read h from the input. Then you can ask at most 16 questions of format "? x" (without quotes), where x is an integer between 1 and 2h - 1, inclusive. As a reply you will get the list of neighbours of vertex x (more details in the "Interaction" section below). For example, for a tree on the drawing above after asking "? 1" you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root y and print it in the format "! y". You will be able to read h for a next testcase only after printing the answer in a previous testcase and flushing the output.
Each tree is fixed from the beginning and it doesn't change during your questions.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 500) — the number of testcases.
At the beginning of each testcase you should read from the input a single integer h (2 ≤ h ≤ 7) — the height of the tree. You can't read the value of h in a next testcase until you answer a previous testcase.
Interaction
To ask a question about neighbours of vertex x, print "? x" (without quotes) on a separate line. Note, you must print an end-of-line character after the last character of the line and flush your output to get a response.
The response will consist of two lines. The first line will contain a single integer k (1 ≤ k ≤ 3) — the number of neighbours of vertex x. The second line will contain k distinct integers t1, ..., tk (1 ≤ t1 < ... < tk ≤ 2h - 1) — indices of neighbours of vertex x, gives in the increasing order.
After asking at most 16 questions you have to say y — the index of the root. Print "! y" (without quotes) and an end-of-line character, and flush the output.
Each tree is fixed from the beginning and it doesn't change during your questions.
You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output.
To flush you can use (just printing a query/answer and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
In any moment if the program reads h = 0 or k = 0 it should immediately terminate normally (for example, calling exit(0)). It means that the system detected incorrect request/output from your program and printed 0 because if can't process your requests anymore. In this case you'll receive verdict "Wrong Answer", but if you ignore case h = 0 or k = 0 it could lead to "Runtime Error", "Time/Memory limit exceeded" or any other verdict because your program could read a trash from the closed input stream.
Hacking. To hack someone, use the following format:
The first line should contain a single integer t equal to 1 (only one testcase is allowed in hacks). The second line should contain a single integer h. Each of next 2h - 2 lines should contain two distinct integers ai and bi (1 ≤ ai, bi ≤ 2h - 1), denoting two nodes connected with an edge. The printed edges must form a perfect binary tree of height h.
Of course, contestant programs will not be able to see this input.
Examples
Input
1
3
3
4 5 7
2
1 2
1
2
Output
? 1
? 5
? 6
! 5
Input
2
2
1
3
2
1 2
2
1 2
4
3
3 12 13
Output
? 1
? 3
? 3
! 3
? 6
! 1
Note
In the first sample, a tree corresponds to the drawing from the statement.
In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below.
<image>
Submitted Solution:
```
import sys
act = 0
def get_n(v):
global act
act += 1
print('?', v)
sys.stdout.flush()
return [int(t) for t in input().split()]
def win(v):
print('!', v)
sys.stdout.flush()
used = set()
def dfs(v, d):
global used
global act
if v in used:
return 0
used.add(v)
if (d == 2):
return 0
if (act == 16):
win(v)
return 1
g = get_n(v)
if (len(g) == 2):
win(v)
return 1
if (len(g) == 1):
return 2
for u in g:
k = dfs(u, d + 1)
if (k != 0):
return k
return 0
def play():
global used
global act
act = 0
h = int(input())
q = [1]
b = 0
used = {1}
while(b < min(10, 2 ** h - 1)):
v = q[b]
g = get_n(v)
if (len(g) == 2):
win(v)
return
for u in g:
if u in used:
continue
used.add(u)
q.append(u)
b += 1
if (b > 4) and (len(g) == 1):
b += 1
if len(q) - b == 12:
l = len(get_n(q[b]))
if (l == 2):
win(q[b])
return
if (l == 1):
b += 3
b += 1
else:
while(True):
k = dfs(q[b], 0)
if (k == 1):
return
if (k == 2):
b += 1
b += 1
n = int(input())
for i in range(n):
play()
``` | instruction | 0 | 27,930 | 8 | 55,860 |
No | output | 1 | 27,930 | 8 | 55,861 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem. In the interaction section below you will find the information about flushing the output.
The New Year tree of height h is a perfect binary tree with vertices numbered 1 through 2h - 1 in some order. In this problem we assume that h is at least 2. The drawing below shows one example New Year tree of height 3:
<image>
Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that h ≥ 2). It won't be easy because Limak is a little bear and he doesn't even see the whole tree. Can you help him?
There are t testcases. In each testcase, you should first read h from the input. Then you can ask at most 16 questions of format "? x" (without quotes), where x is an integer between 1 and 2h - 1, inclusive. As a reply you will get the list of neighbours of vertex x (more details in the "Interaction" section below). For example, for a tree on the drawing above after asking "? 1" you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root y and print it in the format "! y". You will be able to read h for a next testcase only after printing the answer in a previous testcase and flushing the output.
Each tree is fixed from the beginning and it doesn't change during your questions.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 500) — the number of testcases.
At the beginning of each testcase you should read from the input a single integer h (2 ≤ h ≤ 7) — the height of the tree. You can't read the value of h in a next testcase until you answer a previous testcase.
Interaction
To ask a question about neighbours of vertex x, print "? x" (without quotes) on a separate line. Note, you must print an end-of-line character after the last character of the line and flush your output to get a response.
The response will consist of two lines. The first line will contain a single integer k (1 ≤ k ≤ 3) — the number of neighbours of vertex x. The second line will contain k distinct integers t1, ..., tk (1 ≤ t1 < ... < tk ≤ 2h - 1) — indices of neighbours of vertex x, gives in the increasing order.
After asking at most 16 questions you have to say y — the index of the root. Print "! y" (without quotes) and an end-of-line character, and flush the output.
Each tree is fixed from the beginning and it doesn't change during your questions.
You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output.
To flush you can use (just printing a query/answer and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
In any moment if the program reads h = 0 or k = 0 it should immediately terminate normally (for example, calling exit(0)). It means that the system detected incorrect request/output from your program and printed 0 because if can't process your requests anymore. In this case you'll receive verdict "Wrong Answer", but if you ignore case h = 0 or k = 0 it could lead to "Runtime Error", "Time/Memory limit exceeded" or any other verdict because your program could read a trash from the closed input stream.
Hacking. To hack someone, use the following format:
The first line should contain a single integer t equal to 1 (only one testcase is allowed in hacks). The second line should contain a single integer h. Each of next 2h - 2 lines should contain two distinct integers ai and bi (1 ≤ ai, bi ≤ 2h - 1), denoting two nodes connected with an edge. The printed edges must form a perfect binary tree of height h.
Of course, contestant programs will not be able to see this input.
Examples
Input
1
3
3
4 5 7
2
1 2
1
2
Output
? 1
? 5
? 6
! 5
Input
2
2
1
3
2
1 2
2
1 2
4
3
3 12 13
Output
? 1
? 3
? 3
! 3
? 6
! 1
Note
In the first sample, a tree corresponds to the drawing from the statement.
In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below.
<image>
Submitted Solution:
```
import sys
act = 0
def get_n(v):
act += 1
print(v)
sys.stdout.flush()
return [int(t) for t in input.split()]
def win(v):
print(v)
sys.stdout.flush()
used = set()
def dfs(v, d):
global used
global act
if v in used:
return 0
used.add(v)
if (d == 2):
return 0
if (act == 16):
win(v)
return 1
g = get_n(v)
if (len(g) == 2):
win(v)
return 1
if (len(g) == 1):
return 2
for u in g:
k = dfs(u, d + 1)
if (k != 0):
return k
return 0
def play():
global used
global act
act = 0
h = int(input())
q = [1]
b = 0
used = {1}
while(b < min(10, 2 ** h - 1)):
v = q[b]
g = get_n(v)
if (len(g) == 2):
win(v)
return
for u in g:
if u in used:
continue
used.add(u)
q.append(u)
b += 1
if (b > 4) and (len(g) == 1):
b += 1
if len(q) - b == 12:
l = len(get_n(q[b]))
if (l == 2):
win(q[b])
return
if (l == 1):
b += 3
b += 1
else:
while(True):
k = dfs(q[b], 0)
if (k == 1):
return
if (k == 2):
b += 1
b += 1
``` | instruction | 0 | 27,931 | 8 | 55,862 |
No | output | 1 | 27,931 | 8 | 55,863 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem. In the interaction section below you will find the information about flushing the output.
The New Year tree of height h is a perfect binary tree with vertices numbered 1 through 2h - 1 in some order. In this problem we assume that h is at least 2. The drawing below shows one example New Year tree of height 3:
<image>
Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that h ≥ 2). It won't be easy because Limak is a little bear and he doesn't even see the whole tree. Can you help him?
There are t testcases. In each testcase, you should first read h from the input. Then you can ask at most 16 questions of format "? x" (without quotes), where x is an integer between 1 and 2h - 1, inclusive. As a reply you will get the list of neighbours of vertex x (more details in the "Interaction" section below). For example, for a tree on the drawing above after asking "? 1" you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root y and print it in the format "! y". You will be able to read h for a next testcase only after printing the answer in a previous testcase and flushing the output.
Each tree is fixed from the beginning and it doesn't change during your questions.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 500) — the number of testcases.
At the beginning of each testcase you should read from the input a single integer h (2 ≤ h ≤ 7) — the height of the tree. You can't read the value of h in a next testcase until you answer a previous testcase.
Interaction
To ask a question about neighbours of vertex x, print "? x" (without quotes) on a separate line. Note, you must print an end-of-line character after the last character of the line and flush your output to get a response.
The response will consist of two lines. The first line will contain a single integer k (1 ≤ k ≤ 3) — the number of neighbours of vertex x. The second line will contain k distinct integers t1, ..., tk (1 ≤ t1 < ... < tk ≤ 2h - 1) — indices of neighbours of vertex x, gives in the increasing order.
After asking at most 16 questions you have to say y — the index of the root. Print "! y" (without quotes) and an end-of-line character, and flush the output.
Each tree is fixed from the beginning and it doesn't change during your questions.
You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output.
To flush you can use (just printing a query/answer and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
In any moment if the program reads h = 0 or k = 0 it should immediately terminate normally (for example, calling exit(0)). It means that the system detected incorrect request/output from your program and printed 0 because if can't process your requests anymore. In this case you'll receive verdict "Wrong Answer", but if you ignore case h = 0 or k = 0 it could lead to "Runtime Error", "Time/Memory limit exceeded" or any other verdict because your program could read a trash from the closed input stream.
Hacking. To hack someone, use the following format:
The first line should contain a single integer t equal to 1 (only one testcase is allowed in hacks). The second line should contain a single integer h. Each of next 2h - 2 lines should contain two distinct integers ai and bi (1 ≤ ai, bi ≤ 2h - 1), denoting two nodes connected with an edge. The printed edges must form a perfect binary tree of height h.
Of course, contestant programs will not be able to see this input.
Examples
Input
1
3
3
4 5 7
2
1 2
1
2
Output
? 1
? 5
? 6
! 5
Input
2
2
1
3
2
1 2
2
1 2
4
3
3 12 13
Output
? 1
? 3
? 3
! 3
? 6
! 1
Note
In the first sample, a tree corresponds to the drawing from the statement.
In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below.
<image>
Submitted Solution:
```
from sys import stdout
class jmp(Exception):
pass
for i in range(int(input())):
h=int(input())
k=dict()
print("? 1")
n=int(input())
l=[int(i) for i in input().split()]
if n==2:
print("! 1")
break
k[1]=list(l)
#taboo={1,*l}
try:
while k:
m=dict()
for j in k.keys():
for o in k[j]:
print("?",o)
n=int(input())
l=[int(x) for x in input().split() if not int(x)==j]
if n==2:
print("!",o)
stdout.flush()
raise jmp
elif n==1:
#pass
if len(k[j])==2 and len(k)>1:
break
else:
m[o]=l
#taboo.update(l)
#print(k,m)
k1=k
k=m
except jmp:
pass
stdout.flush()
``` | instruction | 0 | 27,932 | 8 | 55,864 |
No | output | 1 | 27,932 | 8 | 55,865 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth. | instruction | 0 | 27,933 | 8 | 55,866 |
Tags: *special, constructive algorithms, sortings
Correct Solution:
```
n,l,r=map(int,input().split())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
l=l-1
r=r-1
A=A[0:l]+A[r+1:]
B=B[0:l]+B[r+1:]
if A==B:
print('TRUTH')
else:
print('LIE')
``` | output | 1 | 27,933 | 8 | 55,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth. | instruction | 0 | 27,934 | 8 | 55,868 |
Tags: *special, constructive algorithms, sortings
Correct Solution:
```
import sys
from collections import defaultdict
N=[];N1=[];
M=defaultdict(int);M1=defaultdict(int);
n,l,r=map(int,input().split());
N=list(map(int,input().split()))
N1=list(map(int,input().split()))
Pass=True
for i in range(l-1):
if N[i]!=N1[i]:
Pass=False
break
if Pass is True:
for i in range(r,n):
if N[i]!=N1[i]:
Pass=False
break
if Pass is True:
for i in range(l-1,r):
M[N[i]]+=1
M1[N1[i]]+=1
for i in range(n+1):
if M[i]!=M1[i]:
Pass=False
break
if Pass:print("TRUTH")
else:print("LIE")
``` | output | 1 | 27,934 | 8 | 55,869 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth. | instruction | 0 | 27,935 | 8 | 55,870 |
Tags: *special, constructive algorithms, sortings
Correct Solution:
```
nlr = input().split()
a = input().split()
b = input().split()
if (a[:int(nlr[1])-1] != b[:int(nlr[1])-1]) | (a[int(nlr[2])+1:] != b[int(nlr[2])+1:]) | (sorted(a[int(nlr[1])-1:int(nlr[2])]) != sorted(b[int(nlr[1])-1:int(nlr[2])])):
print("LIE")
else:
print("TRUTH")
# for i in range(nlr[1]):
# if a[i] != b[i]:
# print("LIE")
# exit()
# for i in range(nlr[2] + 1, n):
# if a[i] != b[i]:
# print("LIE")
# exit()
``` | output | 1 | 27,935 | 8 | 55,871 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth. | instruction | 0 | 27,936 | 8 | 55,872 |
Tags: *special, constructive algorithms, sortings
Correct Solution:
```
n, l, r = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 'TRUTH'
for i in range(0, l - 1):
if a[i] != b[i]:
ans = 'LIE'
#print(i)
break
for i in range(r, n):
if a[i] != b[i]:
ans = 'LIE'
#print(i)
break
print(ans)
``` | output | 1 | 27,936 | 8 | 55,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth. | instruction | 0 | 27,937 | 8 | 55,874 |
Tags: *special, constructive algorithms, sortings
Correct Solution:
```
n,l,r=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l=l-1;
r=r-1;
l3=[]
l4=[]
c=0
for i in range(l,r+1):
l3.append(l1[i])
l4.append(l2[i])
for i in range(0,l):
if(l1[i]!=l2[i]):
c=c+1
for i in range(r+1,n):
if(l1[i]!=l2[i]):
c=c+1
l3.sort()
l4.sort()
for i in range(0,r-l+1):
if(l3[i]!=l4[i]):
c=c+1
if(c==0):
print("TRUTH")
else:
print("LIE")
``` | output | 1 | 27,937 | 8 | 55,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth. | instruction | 0 | 27,938 | 8 | 55,876 |
Tags: *special, constructive algorithms, sortings
Correct Solution:
```
l, a, b = map(int, input().split())
before = list(map(int, input().split()))
after = list(map(int, input().split()))
if sorted(before[a - 1: b]) != sorted(after[a - 1: b]):
print("LIE")
elif before[:a-1] != after[:a-1]:
print("LIE")
elif before[b:] != after[b:]:
print("LIE")
else:
print("TRUTH")
``` | output | 1 | 27,938 | 8 | 55,877 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth. | instruction | 0 | 27,939 | 8 | 55,878 |
Tags: *special, constructive algorithms, sortings
Correct Solution:
```
f = lambda: input().split()
n, l, r = map(int, f())
a, b = f(), f()
print('TRUTH' if a[:l - 1] == b[:l - 1] and a[r:] == b[r:] else 'LIE')
``` | output | 1 | 27,939 | 8 | 55,879 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth. | instruction | 0 | 27,940 | 8 | 55,880 |
Tags: *special, constructive algorithms, sortings
Correct Solution:
```
vals=lambda:map(int,input().split())
n,l,r=vals()
a1=list(vals())
a2=list(vals())
if a1[:l-1] == a2[:l-1] and a1[r:] == a2[r:]:
print("TRUTH")
else:
print("LIE")
``` | output | 1 | 27,940 | 8 | 55,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
Submitted Solution:
```
N = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a=set()
b=set()
for i in range (N[1]-1,N[2]):
a.add(A[i])
b.add(B[i])
flag=True
for i in range (N[1]-1):
if A[i]!=B[i]:
flag=False
for i in range (N[2],N[0]):
if A[i]!=B[i]:
flag=False
if a!=b:
flag=False
if flag==True:
print("TRUTH")
else:
print ("LIE")
``` | instruction | 0 | 27,941 | 8 | 55,882 |
Yes | output | 1 | 27,941 | 8 | 55,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
Submitted Solution:
```
import math
import bisect
from collections import defaultdict
from collections import OrderedDict
from itertools import combinations
import heapq
from collections import deque
#for _ in range(int(input())):
n,l,r = map(int,input().split())
#n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
at = a[l-1:r]
al = a[:l-1]
ar = a[r:]
bt = b[l-1:r]
bl = b[:l-1]
br = b[r:]
#print(al,at,ar,bl,bt,br)
if sorted(at) == sorted(bt) and ar ==br and al == bl:
print('TRUTH')
else:
print('LIE')
"""
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
"""
``` | instruction | 0 | 27,942 | 8 | 55,884 |
Yes | output | 1 | 27,942 | 8 | 55,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
Submitted Solution:
```
n, x1, x2 = [int(i) for i in input().split()]
a = input().split()
a = a[:x1 - 1] + a[x2:]
b = input().split()
b = b[:x1 - 1] + b[x2:]
check = True
for i in range(len(a)):
if (a[i] != b[i]):
check = False
break
if (check):
print("TRUTH")
else:
print("LIE")
``` | instruction | 0 | 27,943 | 8 | 55,886 |
Yes | output | 1 | 27,943 | 8 | 55,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
Submitted Solution:
```
n, l, r = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
f = True
for i in range(l - 1):
if a[i] != b[i]:
f = False
for i in range(r, n):
if a[i] != b[i]:
f = False
a1 = []
b1 = []
for i in range(l - 1, r):
b1.append(b[i])
a1.append(a[i])
a1.sort()
b1.sort()
for i in range(len(a1)):
if a1[i] != b1[i]:
f = False
if f:
print("TRUTH")
else:
print("LIE")
``` | instruction | 0 | 27,944 | 8 | 55,888 |
Yes | output | 1 | 27,944 | 8 | 55,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
Submitted Solution:
```
import sys
from collections import defaultdict
N=[];N1=[];
M=defaultdict(int);M1=defaultdict(int);
n,l,r=map(int,input().split());
N=list(map(int,input().split()))
N1=list(map(int,input().split()))
for i in range(l-1,r):
M[N[i]]+=1
M1[N1[i]]+=1
Pass=True
for i in range(n+1):
if M[i]!=M1[i]:
Pass=False
break
if Pass:
print("TRUTH")
else:print("LIE")
``` | instruction | 0 | 27,945 | 8 | 55,890 |
No | output | 1 | 27,945 | 8 | 55,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
Submitted Solution:
```
import math
import bisect
from collections import defaultdict
from collections import OrderedDict
from itertools import combinations
import heapq
from collections import deque
#for _ in range(int(input())):
n,l,r = map(int,input().split())
#n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
at = a[l-1:r]
al = a[:l-1]
ar = a[r:]
bt = b[l-1:r]
bl = b[:l-1]
br = b[r:]
#print(al,at,ar,bl,bt,br)
if sorted(at) == sorted(bt) and sorted(ar) == sorted(br) and sorted(al) == sorted(bl):
print('TRUTH')
else:
print('LIE')
"""
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
"""
``` | instruction | 0 | 27,946 | 8 | 55,892 |
No | output | 1 | 27,946 | 8 | 55,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
Submitted Solution:
```
n,l,r=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[0 for i in range(100005)]
for i in range(l-1,r):
print(a[i])
c[a[i]]+=1
c[b[i]]-=1
t=True
for i in c:
if i!=0:
t=False
if t:
print('TRUTH')
else:
print('LIE')
``` | instruction | 0 | 27,947 | 8 | 55,894 |
No | output | 1 | 27,947 | 8 | 55,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Examples
Input
5 2 4
3 4 2 3 1
3 2 3 4 1
Output
TRUTH
Input
3 1 2
1 2 3
3 1 2
Output
LIE
Input
4 2 4
1 1 1 1
1 1 1 1
Output
TRUTH
Note
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values l and r there is a situation when Stepan said the truth.
Submitted Solution:
```
n, l, r = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
f = True
for i in range(l - 1):
if a[i] != b[i]:
f = False
for i in range(r, n):
if a[i] != b[i]:
f = False
a1 = []
b1 = []
for i in range(l - 1, r):
b1.append(b[i])
a1.append(a[i])
a1.sort()
b1.sort()
print(*a1)
print(*b1)
for i in range(len(a1)):
if a1[i] != b1[i]:
f = False
if f:
print("TRUTH")
else:
print("LIE")
``` | instruction | 0 | 27,948 | 8 | 55,896 |
No | output | 1 | 27,948 | 8 | 55,897 |
Provide a correct Python 3 solution for this coding contest problem.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0 | instruction | 0 | 28,168 | 8 | 56,336 |
"Correct Solution:
```
from heapq import heappush, heappop
while True:
n, m = map(int, input().split())
if n == 0: break
mp = ["X" + input() + "X" for _ in range(m)]
mp.insert(0, "X" * (n + 2))
mp.append("X" * (n + 2))
que = []
visited = [[False] * (n + 2) for _ in range(m + 2)]
sur_flag = False
for i in range(1, n + 1):
if mp[1][i] =="&" or mp[m][i] == "&":
sur_flag = True
break
c1, s1 = (1, 0) if mp[1][i] == "#" else (0, 1)
c2, s2 = (1, 0) if mp[m][i] == "#" else (0, 1)
heappush(que, (c1, s1, (i, 1)))
heappush(que, (c2, s2, (i, m)))
visited[1][i] = True
visited[m][i] = True
for i in range(1, m + 1):
if mp[i][1] == "&" or mp[i][n] == "&":
sur_flag = True
break
c1, s1 = (1, 0) if mp[i][1] == "#" else (0, 1)
c2, s2 = (1, 0) if mp[i][n] == "#" else (0, 1)
heappush(que, (c1, s1, (1, i)))
heappush(que, (c2, s2, (n, i)))
visited[i][1] = True
visited[i][n] = True
if sur_flag:
print(0)
continue
direct = ((0, 1), (0, -1), (1, 0), (-1, 0))
reached = False
"""
到達コストの低い順で、堀の中にいるものを優先に幅優先探索
"""
while que and not reached:
cost, status, point = heappop(que)
x, y = point
for dx, dy in direct:
newx, newy = x + dx, y + dy
if not visited[newy][newx]:
visited[newy][newx] = True
sym = mp[newy][newx]
if sym == "&":
print(cost)
reached = True
break
elif sym == "#":
if status == 1:
heappush(que, (cost + 1, 0, (newx, newy)))
else:
heappush(que, (cost, 0, (newx, newy)))
elif sym == ".":
heappush(que,(cost, 1, (newx, newy)))
``` | output | 1 | 28,168 | 8 | 56,337 |
Provide a correct Python 3 solution for this coding contest problem.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0 | instruction | 0 | 28,169 | 8 | 56,338 |
"Correct Solution:
```
from collections import deque
dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
while 1:
N, M = map(int, input().split())
if N == M == 0:
break
MP = [input() for i in range(M)]
cost = [[-1]*N for i in range(M)]
cur = 0
que = deque()
for i in range(M):
for j in range(N):
if MP[i][j] == '&':
que.append((i, j))
cost[i][j] = 0
while que:
r, c = que.popleft()
s = cost[r][c]
a = MP[r][c]
for dr, dc in dd:
nr = r + dr; nc = c + dc
if not 0 <= nr < M or not 0 <= nc < N or cost[nr][nc] != -1:
continue
b = MP[nr][nc]
t = s + (a != '#' and b == '#')
cost[nr][nc] = t
if s == t:
que.appendleft((nr, nc))
else:
que.append((nr, nc))
ans = N*M
for i in range(M):
ans = min(ans, cost[i][0], cost[i][-1])
for i in range(N):
ans = min(ans, cost[0][i], cost[-1][i])
print(ans)
``` | output | 1 | 28,169 | 8 | 56,339 |
Provide a correct Python 3 solution for this coding contest problem.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0 | instruction | 0 | 28,170 | 8 | 56,340 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0156
"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline
def bfs(field, sx, sy):
"""
??????????????°???????????°??????????????§??????????????????
:param field:
:param sx: ??????????????°????????§?¨?
:param sy:
:return: ????????????????????´???????????§????????¢
"""
# ????????????????§????????????´????????§?¨?????????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
# ??°???????????????(?§????????????£????????????????????????????????§??????????????¨)
Y = len(field)
X = len(field[0])
d = [[float('inf')] * X for _ in range(Y)] # ??????????????°?????????????????¢???INF??§?????????
moats = set() # ????????§???????????????????????§?¨?
# ???????????????????????????????????°???????????´????????°????????§????????¢????±???????
Q = deque()
Q.append((sx, sy))
d[sy][sx] = 0 # ??????????????°??????????????????0
while Q:
cx, cy = Q.popleft() # ?????¨??°?????§?¨?
for i in range(4):
nx = cx + dx[i]
ny = cy + dy[i]
# ?§?????????? ??°????????????????????????????????? and ?£?????????£???????????? and ??¢?????¢?´¢?????§?????? ??´???
if (not 0 <= nx < X) or (not 0 <= ny < Y):
return True, moats
elif d[ny][nx] == float('inf'):
if field[ny][nx] == '#':
moats.add((nx, ny))
else:
Q.append((nx, ny))
d[ny][nx] = d[cy][cx] + 1
return False, moats
def fill_moat(field, moats):
# ??¢??\???????????¨????????????????????£??????????????????'^'??§?????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
# ??°???????????????(?§????????????£????????????????????????????????§??????????????¨)
Y = len(field)
X = len(field[0])
# d = [[float('inf')] * X for _ in range(Y)] # ??????????????°?????????????????¢???INF??§?????????
while moats:
cx, cy = moats.pop() # ?????¨??°?????§?¨?
field[cy][cx] = '^'
for i in range(4):
nx = cx + dx[i]
ny = cy + dy[i]
if (0 <= nx < X) and (0 <= ny < Y):
if field[ny][nx] == '#':
moats.add((nx, ny))
# d[ny][nx] = d[cy][cx] + 1
def solve(field):
# ??????????????°???(????????£: &)?????????
start = (0, 0)
for y, row in enumerate(field):
if '&' in row:
start = (row.index('&'), y)
sx, sy = start
climb = 0 # ???????????????????????° = ??????????????????????????°
while True:
res, moats = bfs(field, sx, sy) # ????????£????????°?????????????????????????¢????
if res is False:
climb += 1
fill_moat(field, moats) # ??¢??\?????????????????????
else:
break
return climb
def main(args):
while True:
n, m = map(int, input().split())
if n == 0 and m == 0:
break
# data = [
# '.###.',
# '#...#',
# '#.&.#',
# '#...#',
# '.###.'
# ]
data = [input().strip('\n') for _ in range(m)]
field = [list(x) for x in data] # data????????????????????????????????????????????????
result = solve(field)
print(result)
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 28,170 | 8 | 56,341 |
Provide a correct Python 3 solution for this coding contest problem.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0 | instruction | 0 | 28,171 | 8 | 56,342 |
"Correct Solution:
```
from heapq import heappush, heappop
while True:
n, m = map(int, input().split())
if n == 0: break
mp = ["X" + input() + "X" for _ in range(m)]
mp.insert(0, "X" * (n + 2))
mp.append("X" * (n + 2))
que = []
visited = [[False] * (n + 2) for _ in range(m + 2)]
sur_flag = False
for i in range(1, n + 1):
if mp[1][i] =="&" or mp[m][i] == "&":
sur_flag = True
break
c1, s1 = (1, 0) if mp[1][i] == "#" else (0, 1)
c2, s2 = (1, 0) if mp[m][i] == "#" else (0, 1)
heappush(que, (c1, s1, (i, 1)))
heappush(que, (c2, s2, (i, m)))
visited[1][i] = True
visited[m][i] = True
for i in range(1, m + 1):
if mp[i][1] == "&" or mp[i][n] == "&":
sur_flag = True
break
c1, s1 = (1, 0) if mp[i][1] == "#" else (0, 1)
c2, s2 = (1, 0) if mp[i][n] == "#" else (0, 1)
heappush(que, (c1, s1, (1, i)))
heappush(que, (c2, s2, (n, i)))
visited[i][1] = True
visited[i][n] = True
if sur_flag:
print(0)
continue
direct = ((0, 1), (0, -1), (1, 0), (-1, 0))
reached = False
while que and not reached:
cost, status, point = heappop(que)
x, y = point
for dx, dy in direct:
newx, newy = x + dx, y + dy
if not visited[newy][newx]:
visited[newy][newx] = True
if mp[newy][newx] == "&":
print(cost)
reached = True
break
elif mp[newy][newx] == "#":
if status == 1:
heappush(que, (cost + 1, 0, (newx, newy)))
else:
heappush(que, (cost, 0, (newx, newy)))
elif mp[newy][newx] == ".":
heappush(que,(cost, 1, (newx, newy)))
``` | output | 1 | 28,171 | 8 | 56,343 |
Provide a correct Python 3 solution for this coding contest problem.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0 | instruction | 0 | 28,172 | 8 | 56,344 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0156
"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline
def bfs(field, sx, sy):
"""
??????????????°???????????°??????????????§??????????????????
:param field:
:param sx: ??????????????°????????§?¨?
:param sy:
:return: ????????????????????´???????????§????????¢
"""
# ????????????????§????????????´????????§?¨?????????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
# ??°???????????????(?§????????????£????????????????????????????????§??????????????¨)
Y = len(field)
X = len(field[0])
d = [[float('inf')] * X for _ in range(Y)] # ??????????????°?????????????????¢???INF??§?????????
moats = set() # ????????§???????????????????????§?¨?
# ???????????????????????????????????°???????????´????????°????????§????????¢????±???????
Q = deque()
Q.append((sx, sy))
d[sy][sx] = 0 # ??????????????°??????????????????0
while Q:
cx, cy = Q.popleft() # ?????¨??°?????§?¨?
for i in range(4):
nx = cx + dx[i]
ny = cy + dy[i]
# ?§?????????? ??°????????????????????????????????? and ?£?????????£???????????? and ??¢?????¢?´¢?????§?????? ??´???
if (not 0 <= nx < X) or (not 0 <= ny < Y):
return True, moats
elif d[ny][nx] == float('inf'):
if field[ny][nx] == '#':
moats.add((nx, ny))
else:
Q.append((nx, ny))
d[ny][nx] = d[cy][cx] + 1
return False, moats
def fill_moat(field, moats):
# ??¢??\???????????¨????????????????????£??????????????????'^'??§?????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
# ??°???????????????(?§????????????£????????????????????????????????§??????????????¨)
Y = len(field)
X = len(field[0])
d = [[float('inf')] * X for _ in range(Y)] # ??????????????°?????????????????¢???INF??§?????????
while moats:
cx, cy = moats.pop() # ?????¨??°?????§?¨?
field[cy][cx] = '^'
for i in range(4):
nx = cx + dx[i]
ny = cy + dy[i]
if (0 <= nx < X) and (0 <= ny < Y) and d[ny][nx] == float('inf'):
if field[ny][nx] == '#':
moats.add((nx, ny))
d[ny][nx] = d[cy][cx] + 1
def solve(field):
# ??????????????°???(S)??¨??´????????°???(N)?????????
start = (0, 0)
for y, row in enumerate(field):
if '&' in row:
start = (row.index('&'), y)
sx, sy = start
climb = 0
while True:
res, moats = bfs(field, sx, sy)
if res is False:
climb += 1
fill_moat(field, moats)
else:
break
return climb
def main(args):
while True:
n, m = map(int, input().split())
if n == 0 and m == 0:
break
# data = [
# '.###.',
# '#...#',
# '#.&.#',
# '#...#',
# '.###.'
# ]
data = [input().strip('\n') for _ in range(m)]
field = [list(x) for x in data] # data????????????????????????????????????????????????
result = solve(field)
print(result)
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 28,172 | 8 | 56,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0156
"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline
def bfs(field, sx, sy, d=None, q=[]):
"""
??????????????°???????????°??????????????§??????????????????
:param field:
:param sx: ??????????????°????????§?¨?
:param sy:
:return: ????????????????????´???????????§????????¢
"""
# ????????????????§????????????´????????§?¨?????????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
# ??°???????????????(?§????????????£????????????????????????????????§??????????????¨)
Y = len(field)
X = len(field[0])
if d == None:
d = [[float('inf')] * X for _ in range(Y)] # ??????????????°?????????????????¢???INF??§?????????
moats = set() # ????????§???????????????????????§?¨?
# ???????????????????????????????????°???????????´????????°????????§????????¢????±???????
Q = deque()
if q == []:
Q.append((sx, sy))
d[sy][sx] = 0 # ??????????????°??????????????????0
else:
Q.extend(q)
while Q:
cx, cy = Q.popleft() # ?????¨??°?????§?¨?
for i in range(4):
nx = cx + dx[i]
ny = cy + dy[i]
# ?§?????????? ??°????????????????????????????????? and ?£?????????£???????????? and ??¢?????¢?´¢?????§?????? ??´???
if (not 0 <= nx < X) or (not 0 <= ny < Y):
return True, moats, d
elif d[ny][nx] == float('inf'):
if field[ny][nx] == '#':
moats.add((nx, ny))
else:
Q.append((nx, ny))
d[ny][nx] = d[cy][cx] + 1
return False, moats, d
def fill_moat(field, moats):
# ??¢??\???????????¨????????????????????£??????????????????'^'??§?????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
# ??°???????????????(?§????????????£????????????????????????????????§??????????????¨)
Y = len(field)
X = len(field[0])
while moats:
cx, cy = moats.pop() # ?????¨??°?????§?¨?
field[cy][cx] = '^'
for i in range(4):
nx = cx + dx[i]
ny = cy + dy[i]
if (0 <= nx < X) and (0 <= ny < Y):
if field[ny][nx] == '#':
moats.add((nx, ny))
def solve(field):
# ??????????????°???(????????£: &)?????????
start = (0, 0)
for y, row in enumerate(field):
if '&' in row:
start = (row.index('&'), y)
sx, sy = start
climb = 0 # ???????????????????????° = ??????????????????????????°
d = None
q = []
while True:
res, moats, d = bfs(field, sx, sy, d, q) # ????????£????????°?????????????????????????¢????
q = list(moats)
if res is False:
climb += 1
fill_moat(field, moats) # ??¢??\?????????????????????
else:
break
return climb
def main(args):
while True:
n, m = map(int, input().split())
if n == 0 and m == 0:
break
# data = [
# '.###.',
# '#...#',
# '#.&.#',
# '#...#',
# '.###.'
# ]
data = [input().strip('\n') for _ in range(m)]
field = [list(x) for x in data] # data????????????????????????????????????????????????
result = solve(field)
print(result)
if __name__ == '__main__':
main(sys.argv[1:])
``` | instruction | 0 | 28,173 | 8 | 56,346 |
No | output | 1 | 28,173 | 8 | 56,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0
Submitted Solution:
```
from heapq import heappush, heappop
while True:
n, m = map(int, input().split())
if n == 0: break
mp = ["X" + input() + "X" for _ in range(m)]
mp.insert(0, "X" * (n + 2))
mp.append("X" * (n + 2))
que = []
visited = [[False] * (n + 2) for _ in range(m + 2)]
sur_flag = False
for i in range(1, n + 1):
if mp[1][i] =="&" or mp[m][i] == "&":
sur_flag = True
break
c1 = 1 if mp[1][i] == "#" else 0
c2 = 1 if mp[m][i] == "#" else 0
heappush(que, (c1, (i, 1)))
heappush(que, (c2, (i, m)))
visited[1][i] = True
visited[m][i] = True
for i in range(1, m + 1):
if mp[i][1] == "&" or mp[i][n] == "&":
sur_flag = True
break
c1 = 1 if mp[i][1] == "#" else 0
c2 = 1 if mp[i][n] == "#" else 0
heappush(que, (c1, (1, i)))
heappush(que, (c2, (n, i)))
visited[i][1] = True
visited[i][n] = True
if sur_flag:
print(0)
continue
direct = ((0, 1), (0, -1), (1, 0), (-1, 0))
reached = False
while que and not reached:
cost, point = heappop(que)
x, y = point
for dx, dy in direct:
newx, newy = x + dx, y + dy
if not visited[newy][newx]:
visited[newy][newx] = True
if mp[newy][newx] == "&":
print(cost)
reached = True
break
elif mp[newy][newx] == "#":
heappush(que, (cost + 1, (newx, newy)))
elif mp[newy][newx] in (".", "&"):
heappush(que, (cost, (newx, newy)))
``` | instruction | 0 | 28,174 | 8 | 56,348 |
No | output | 1 | 28,174 | 8 | 56,349 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0
Submitted Solution:
```
from heapq import heappush, heappop
while True:
n, m = map(int, input().split())
if n == 0: break
mp = ["X" + input() + "X" for _ in range(m)]
mp.insert(0, "X" * (n + 2))
mp.append("X" * (n + 2))
que = []
visited = [[False] * (n + 2) for _ in range(m + 2)]
for i in range(1, n + 1):
c1 = 1 if mp[1][i] == "#" else 0
c2 = 1 if mp[m][i] == "#" else 0
heappush(que, (c1, (i, 1)))
heappush(que, (c2, (i, m)))
visited[1][i] = True
visited[m][i] = True
for i in range(1, m + 1):
c1 = 1 if mp[i][1] == "#" else 0
c2 = 1 if mp[i][n] == "#" else 0
heappush(que, (c1, (1, i)))
heappush(que, (c2, (n, i)))
visited[i][1] = True
visited[i][n] = True
direct = ((0, 1), (0, -1), (1, 0), (-1, 0))
reached = False
while que and not reached:
cost, point = heappop(que)
x, y = point
for dx, dy in direct:
newx, newy = x + dx, y + dy
if not visited[newy][newx]:
visited[newy][newx] = True
if mp[newy][newx] == "&":
print(cost)
reached = True
break
if mp[newy][newx] == "#":
heappush(que, (cost + 1, (newx, newy)))
elif mp[newy][newx] == ".":
heappush(que, (cost, (newx, newy)))
``` | instruction | 0 | 28,175 | 8 | 56,350 |
No | output | 1 | 28,175 | 8 | 56,351 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now, a ninja is planning to sneak into the castle tower from outside the castle. This ninja can easily run on the ground and swim in the moat, but he is not very good at climbing up from the moat, so he wants to enter the moat as few times as possible.
Create a program that takes a sketch of the castle as input and outputs the minimum number of times you have to crawl up from the moat from outside the castle to the castle tower. The sketch of the castle is given as a two-dimensional grid. The symbols drawn on the sketch show the positions of "&" indicating the position of the castle tower and "#" indicating the position of the moat, and "." (Half-width period) at other points. In addition, it is assumed that there is only one castle tower in the castle, and the ninja moves one square at a time in the north, south, east, and west directions when running or swimming, and does not move diagonally.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
c1,1 c1,2 ... c1, n
c2,1c2,2 ... c2, n
::
cm, 1cm, 2 ... cm, n
The first line gives the east-west width n and the north-south width m (1 ≤ n, m ≤ 100) of the sketch. The following m lines give the information on the i-th line of the sketch. Each piece of information is a character string of length n consisting of the symbols "&", "#", and ".".
The number of datasets does not exceed 50.
Output
Outputs the minimum number of times (integer) that must be climbed from the moat for each dataset on one line.
Examples
Input
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
Output
1
2
0
0
Input
5 5
.###.
...#
.&.#
...#
.###.
18 15
..####....####....
..####....####
...............##
.#.############.##
..#..........#.##
.#.#.########.#.##
..#.#......#.#.##
.#.#....&...#.#.##
..#........#.#.##
.#.#.########.#.##
..#..........#.##
.#.############.##
...............##
.#################
9 10
........#
.#
.....#.#
.###.#.#
.#&#.#.#
.#...#.#
.#####.#
.......#
9 3
...###
.#.&.#.#
...###
0 0
</pre>
Output
1
2
0
0
Submitted Solution:
```
from heapq import heappush, heappop
while True:
n, m = map(int, input().split())
if n == 0: break
mp = ["X" + input() + "X" for _ in range(m)]
mp.insert(0, "X" * (n + 2))
mp.append("X" * (n + 2))
que = []
visited = [[False] * (n + 2) for _ in range(m + 2)]
for i in range(1, n + 1):
c1 = 1 if mp[1][i] == "#" else 0
c2 = 1 if mp[m][i] == "#" else 0
heappush(que, (c1, (i, 1)))
heappush(que, (c2, (i, m)))
visited[1][i] = True
visited[m][i] = True
for i in range(1, m + 1):
c1 = 1 if mp[i][1] == "#" else 0
c2 = 1 if mp[i][n] == "#" else 0
heappush(que, (c1, (1, i)))
heappush(que, (c2, (n, i)))
visited[i][1] = True
visited[i][n] = True
direct = ((0, 1), (0, -1), (1, 0), (-1, 0))
reached = False
while que and not reached:
cost, point = heappop(que)
x, y = point
if mp[y][x] == "&":
print(cost)
break
for dx, dy in direct:
newx, newy = x + dx, y + dy
if not visited[newy][newx]:
visited[newy][newx] = True
if mp[newy][newx] == "&":
print(cost)
reached = True
break
if mp[newy][newx] == "#":
heappush(que, (cost + 1, (newx, newy)))
elif mp[newy][newx] == ".":
heappush(que, (cost, (newx, newy)))
``` | instruction | 0 | 28,176 | 8 | 56,352 |
No | output | 1 | 28,176 | 8 | 56,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan has n songs on his phone. The size of the i-th song is a_i bytes. Ivan also has a flash drive which can hold at most m bytes in total. Initially, his flash drive is empty.
Ivan wants to copy all n songs to the flash drive. He can compress the songs. If he compresses the i-th song, the size of the i-th song reduces from a_i to b_i bytes (b_i < a_i).
Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most m. He can compress any subset of the songs (not necessarily contiguous).
Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to m).
If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^9) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.
The next n lines contain two integers each: the i-th line contains two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^9, a_i > b_i) — the initial size of the i-th song and the size of the i-th song after compression.
Output
If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.
Examples
Input
4 21
10 8
7 4
3 1
5 4
Output
2
Input
4 16
10 8
7 4
3 1
5 4
Output
-1
Note
In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to 8 + 7 + 1 + 5 = 21 ≤ 21. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal 8 + 4 + 3 + 5 = 20 ≤ 21. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to 10 + 4 + 3 + 5 = 22 > 21).
In the second example even if Ivan compresses all the songs the sum of sizes will be equal 8 + 4 + 1 + 4 = 17 > 16. | instruction | 0 | 28,252 | 8 | 56,504 |
Tags: sortings
Correct Solution:
```
n, m = map(int, input().split())
a = []
total_sum, cnt, j, flag = 0, 0, 0, 0
for i in range(n):
x = list(map(int, input().split()))
a.append(x[0] - x[1])
total_sum += x[0]
a.sort(reverse=True)
while total_sum > m:
try:
total_sum -= a[j]
cnt += 1
j += 1
except IndexError:
flag = 1
print(-1)
break
if flag == 0:
print(cnt)
``` | output | 1 | 28,252 | 8 | 56,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan has n songs on his phone. The size of the i-th song is a_i bytes. Ivan also has a flash drive which can hold at most m bytes in total. Initially, his flash drive is empty.
Ivan wants to copy all n songs to the flash drive. He can compress the songs. If he compresses the i-th song, the size of the i-th song reduces from a_i to b_i bytes (b_i < a_i).
Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most m. He can compress any subset of the songs (not necessarily contiguous).
Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to m).
If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^9) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.
The next n lines contain two integers each: the i-th line contains two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^9, a_i > b_i) — the initial size of the i-th song and the size of the i-th song after compression.
Output
If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.
Examples
Input
4 21
10 8
7 4
3 1
5 4
Output
2
Input
4 16
10 8
7 4
3 1
5 4
Output
-1
Note
In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to 8 + 7 + 1 + 5 = 21 ≤ 21. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal 8 + 4 + 3 + 5 = 20 ≤ 21. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to 10 + 4 + 3 + 5 = 22 > 21).
In the second example even if Ivan compresses all the songs the sum of sizes will be equal 8 + 4 + 1 + 4 = 17 > 16. | instruction | 0 | 28,253 | 8 | 56,506 |
Tags: sortings
Correct Solution:
```
n,k = map(int,input().split())
origin=[];com=[];diff=[]
for i in range(n):
a,b = map(int,input().split())
origin.append(a)
diff.append(a-b)
sum=sum(origin)
diff.sort()
count=0
if sum>k:
for i in range(n-1,-1,-1):
sum=sum-diff[i]
count+=1
if sum<=k:
break
if sum<=k:
print(count)
else:
print(-1)
``` | output | 1 | 28,253 | 8 | 56,507 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.