message stringlengths 2 19.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a frog staying to the left of the string s = s_1 s_2 β¦ s_n consisting of n characters (to be more precise, the frog initially stays at the cell 0). Each character of s is either 'L' or 'R'. It means that if the frog is staying at the i-th cell and the i-th character is 'L', the frog can jump only to the left. If the frog is staying at the i-th cell and the i-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 0.
Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.
The frog wants to reach the n+1-th cell. The frog chooses some positive integer value d before the first jump (and cannot change it later) and jumps by no more than d cells at once. I.e. if the i-th character is 'L' then the frog can jump to any cell in a range [max(0, i - d); i - 1], and if the i-th character is 'R' then the frog can jump to any cell in a range [i + 1; min(n + 1; i + d)].
The frog doesn't want to jump far, so your task is to find the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it can jump by no more than d cells at once. It is guaranteed that it is always possible to reach n+1 from 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. The i-th test case is described as a string s consisting of at least 1 and at most 2 β
10^5 characters 'L' and 'R'.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2 β
10^5 (β |s| β€ 2 β
10^5).
Output
For each test case, print the answer β the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it jumps by no more than d at once.
Example
Input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
Output
3
2
3
1
7
1
Note
The picture describing the first test case of the example and one of the possible answers:
<image>
In the second test case of the example, the frog can only jump directly from 0 to n+1.
In the third test case of the example, the frog can choose d=3, jump to the cell 3 from the cell 0 and then to the cell 4 from the cell 3.
In the fourth test case of the example, the frog can choose d=1 and jump 5 times to the right.
In the fifth test case of the example, the frog can only jump directly from 0 to n+1.
In the sixth test case of the example, the frog can choose d=1 and jump 2 times to the right.
Submitted Solution:
```
tests = int(input())
for _ in range(tests):
jumps = input()
n = len(jumps)
indices = [0]
dists = []
for i in range(n):
if jumps[i] == "R":
indices.append(i+1)
dists.append(i - indices[-2] + 1)
#Add the last jump
dists.append(n-indices[-1]+1)
print(max(dists))
``` | instruction | 0 | 69,082 | 15 | 138,164 |
Yes | output | 1 | 69,082 | 15 | 138,165 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a frog staying to the left of the string s = s_1 s_2 β¦ s_n consisting of n characters (to be more precise, the frog initially stays at the cell 0). Each character of s is either 'L' or 'R'. It means that if the frog is staying at the i-th cell and the i-th character is 'L', the frog can jump only to the left. If the frog is staying at the i-th cell and the i-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 0.
Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.
The frog wants to reach the n+1-th cell. The frog chooses some positive integer value d before the first jump (and cannot change it later) and jumps by no more than d cells at once. I.e. if the i-th character is 'L' then the frog can jump to any cell in a range [max(0, i - d); i - 1], and if the i-th character is 'R' then the frog can jump to any cell in a range [i + 1; min(n + 1; i + d)].
The frog doesn't want to jump far, so your task is to find the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it can jump by no more than d cells at once. It is guaranteed that it is always possible to reach n+1 from 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. The i-th test case is described as a string s consisting of at least 1 and at most 2 β
10^5 characters 'L' and 'R'.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2 β
10^5 (β |s| β€ 2 β
10^5).
Output
For each test case, print the answer β the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it jumps by no more than d at once.
Example
Input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
Output
3
2
3
1
7
1
Note
The picture describing the first test case of the example and one of the possible answers:
<image>
In the second test case of the example, the frog can only jump directly from 0 to n+1.
In the third test case of the example, the frog can choose d=3, jump to the cell 3 from the cell 0 and then to the cell 4 from the cell 3.
In the fourth test case of the example, the frog can choose d=1 and jump 5 times to the right.
In the fifth test case of the example, the frog can only jump directly from 0 to n+1.
In the sixth test case of the example, the frog can choose d=1 and jump 2 times to the right.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 26 18:13:42 2020
@author: alexi
"""
#http://codeforces.com/contest/1324/problem/C -- Alexis Galvan
def check_l(string):
output = 0
for i in range(len(string)):
if string[i] == 'L':
maximum = 1
for j in range(i+1, len(string)):
if string[j] == 'L':
maximum += 1
else:
break
if maximum > output:
output = maximum
if output == 0:
output = 1
return output + 1
def frog_jumps():
cases = int(input())
strings = []
for i in range(cases):
strings.append(input())
for i in range(len(strings)):
jump = check_l(strings[i])
print(jump)
frog_jumps()
``` | instruction | 0 | 69,083 | 15 | 138,166 |
No | output | 1 | 69,083 | 15 | 138,167 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a frog staying to the left of the string s = s_1 s_2 β¦ s_n consisting of n characters (to be more precise, the frog initially stays at the cell 0). Each character of s is either 'L' or 'R'. It means that if the frog is staying at the i-th cell and the i-th character is 'L', the frog can jump only to the left. If the frog is staying at the i-th cell and the i-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 0.
Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.
The frog wants to reach the n+1-th cell. The frog chooses some positive integer value d before the first jump (and cannot change it later) and jumps by no more than d cells at once. I.e. if the i-th character is 'L' then the frog can jump to any cell in a range [max(0, i - d); i - 1], and if the i-th character is 'R' then the frog can jump to any cell in a range [i + 1; min(n + 1; i + d)].
The frog doesn't want to jump far, so your task is to find the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it can jump by no more than d cells at once. It is guaranteed that it is always possible to reach n+1 from 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. The i-th test case is described as a string s consisting of at least 1 and at most 2 β
10^5 characters 'L' and 'R'.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2 β
10^5 (β |s| β€ 2 β
10^5).
Output
For each test case, print the answer β the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it jumps by no more than d at once.
Example
Input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
Output
3
2
3
1
7
1
Note
The picture describing the first test case of the example and one of the possible answers:
<image>
In the second test case of the example, the frog can only jump directly from 0 to n+1.
In the third test case of the example, the frog can choose d=3, jump to the cell 3 from the cell 0 and then to the cell 4 from the cell 3.
In the fourth test case of the example, the frog can choose d=1 and jump 5 times to the right.
In the fifth test case of the example, the frog can only jump directly from 0 to n+1.
In the sixth test case of the example, the frog can choose d=1 and jump 2 times to the right.
Submitted Solution:
```
t = int(input())
for i in range(t) :
s = input()
count = 0
for j in range(len(s)-1,-1,-1) :
if s[j] == "L" :
count += 1
else :
break
if 'L' in s and count == 0 :
print(len(s))
elif 'L' not in s :
print(1)
else :
print(count+1)
``` | instruction | 0 | 69,084 | 15 | 138,168 |
No | output | 1 | 69,084 | 15 | 138,169 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a frog staying to the left of the string s = s_1 s_2 β¦ s_n consisting of n characters (to be more precise, the frog initially stays at the cell 0). Each character of s is either 'L' or 'R'. It means that if the frog is staying at the i-th cell and the i-th character is 'L', the frog can jump only to the left. If the frog is staying at the i-th cell and the i-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 0.
Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.
The frog wants to reach the n+1-th cell. The frog chooses some positive integer value d before the first jump (and cannot change it later) and jumps by no more than d cells at once. I.e. if the i-th character is 'L' then the frog can jump to any cell in a range [max(0, i - d); i - 1], and if the i-th character is 'R' then the frog can jump to any cell in a range [i + 1; min(n + 1; i + d)].
The frog doesn't want to jump far, so your task is to find the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it can jump by no more than d cells at once. It is guaranteed that it is always possible to reach n+1 from 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. The i-th test case is described as a string s consisting of at least 1 and at most 2 β
10^5 characters 'L' and 'R'.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2 β
10^5 (β |s| β€ 2 β
10^5).
Output
For each test case, print the answer β the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it jumps by no more than d at once.
Example
Input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
Output
3
2
3
1
7
1
Note
The picture describing the first test case of the example and one of the possible answers:
<image>
In the second test case of the example, the frog can only jump directly from 0 to n+1.
In the third test case of the example, the frog can choose d=3, jump to the cell 3 from the cell 0 and then to the cell 4 from the cell 3.
In the fourth test case of the example, the frog can choose d=1 and jump 5 times to the right.
In the fifth test case of the example, the frog can only jump directly from 0 to n+1.
In the sixth test case of the example, the frog can choose d=1 and jump 2 times to the right.
Submitted Solution:
```
test=int(input())
for i in range(test):
str_=input()
string=list(str_)
n=len(string)
max_so_far=0
curr_so_far=1
while(i<n):
if string[i]=="L":
if i<(n-1) and string[i]==string[i+1] :
curr_so_far+=1
i+=1
else:
max_so_far=max(max_so_far,curr_so_far)
curr_so_far=1
i+=1
else:
i+=1
print(max_so_far+1)
``` | instruction | 0 | 69,085 | 15 | 138,170 |
No | output | 1 | 69,085 | 15 | 138,171 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a frog staying to the left of the string s = s_1 s_2 β¦ s_n consisting of n characters (to be more precise, the frog initially stays at the cell 0). Each character of s is either 'L' or 'R'. It means that if the frog is staying at the i-th cell and the i-th character is 'L', the frog can jump only to the left. If the frog is staying at the i-th cell and the i-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 0.
Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.
The frog wants to reach the n+1-th cell. The frog chooses some positive integer value d before the first jump (and cannot change it later) and jumps by no more than d cells at once. I.e. if the i-th character is 'L' then the frog can jump to any cell in a range [max(0, i - d); i - 1], and if the i-th character is 'R' then the frog can jump to any cell in a range [i + 1; min(n + 1; i + d)].
The frog doesn't want to jump far, so your task is to find the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it can jump by no more than d cells at once. It is guaranteed that it is always possible to reach n+1 from 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. The i-th test case is described as a string s consisting of at least 1 and at most 2 β
10^5 characters 'L' and 'R'.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2 β
10^5 (β |s| β€ 2 β
10^5).
Output
For each test case, print the answer β the minimum possible value of d such that the frog can reach the cell n+1 from the cell 0 if it jumps by no more than d at once.
Example
Input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
Output
3
2
3
1
7
1
Note
The picture describing the first test case of the example and one of the possible answers:
<image>
In the second test case of the example, the frog can only jump directly from 0 to n+1.
In the third test case of the example, the frog can choose d=3, jump to the cell 3 from the cell 0 and then to the cell 4 from the cell 3.
In the fourth test case of the example, the frog can choose d=1 and jump 5 times to the right.
In the fifth test case of the example, the frog can only jump directly from 0 to n+1.
In the sixth test case of the example, the frog can choose d=1 and jump 2 times to the right.
Submitted Solution:
```
from sys import stdin
def inp():
return (ss.rstrip() for ss in stdin).__next__()
def itg():
return int(stdin.buffer.readline())
def mpint():
return map(int, stdin.buffer.readline().split())
def dfs(index):
global ans
if index - d <= -1:
ans = d
if index in visited or ans:
return
visited.add(index)
# left
for i in range(index - d, index):
if s[i] == 'R':
dfs(i)
# right
for i in range(index + 1, min(size, index + 1 + d)):
if s[i] == 'L':
dfs(i)
for case in range(itg()):
s = inp()
size = len(s)
start = s.rfind('R')
if start == -1:
print(size + 1)
continue
d = size - start
ans = 0
while True:
visited = {size}
try:
dfs(start)
except RuntimeError:
ans = size + 1
if ans:
break
d += 1
print(ans)
``` | instruction | 0 | 69,086 | 15 | 138,172 |
No | output | 1 | 69,086 | 15 | 138,173 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an n Γ m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.
You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not.
A matrix is prime if at least one of the two following conditions fulfills:
* the matrix has a row with prime numbers only;
* the matrix has a column with prime numbers only;
Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
Input
The first line contains two integers n, m (1 β€ n, m β€ 500) β the number of rows and columns in the matrix, correspondingly.
Each of the following n lines contains m integers β the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.
The numbers in the lines are separated by single spaces.
Output
Print a single integer β the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.
Examples
Input
3 3
1 2 3
5 6 1
4 4 1
Output
1
Input
2 3
4 8 8
9 2 9
Output
3
Input
2 2
1 3
4 2
Output
0
Note
In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.
In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.
In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.
Submitted Solution:
```
def f(n):
m = int(n ** 0.5) + 1
t = [1, 0] * (n // 2 + 1)
t[0], t[1], t[2] = 1, 1, 0
for i in range(3, m):
if t[i] == 0: t[i * i :: 2 * i] = [1] * ((n - i * i) // (2 * i) + 1)
for i in range(n - 1, -1, -1):
if t[i]: t[i] = t[i + 1] + 1
return t
q = f(100007)
n, m = map(int, input().split())
t = [[q[j] for j in map(int, input().split())] for i in range(n)]
print(min(min(sum(t[i]) for i in range(n)), min(sum(t[i][j] for i in range(n)) for j in range(m))))
``` | instruction | 0 | 69,257 | 15 | 138,514 |
Yes | output | 1 | 69,257 | 15 | 138,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an n Γ m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.
You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not.
A matrix is prime if at least one of the two following conditions fulfills:
* the matrix has a row with prime numbers only;
* the matrix has a column with prime numbers only;
Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
Input
The first line contains two integers n, m (1 β€ n, m β€ 500) β the number of rows and columns in the matrix, correspondingly.
Each of the following n lines contains m integers β the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.
The numbers in the lines are separated by single spaces.
Output
Print a single integer β the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.
Examples
Input
3 3
1 2 3
5 6 1
4 4 1
Output
1
Input
2 3
4 8 8
9 2 9
Output
3
Input
2 2
1 3
4 2
Output
0
Note
In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.
In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.
In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.
Submitted Solution:
```
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def MAP2():return map(float,input().split())
def LIST(): return list(map(int, input().split()))
def STRING(): return input()
import string
import sys
from heapq import heappop , heappush
from bisect import *
from collections import deque , Counter , defaultdict
from math import *
from itertools import permutations , accumulate
dx = [-1 , 1 , 0 , 0 ]
dy = [0 , 0 , 1 , - 1]
#visited = [[False for i in range(m)] for j in range(n)]
#sys.stdin = open(r'input.txt' , 'r')
#sys.stdout = open(r'output.txt' , 'w')
#for tt in range(INT()):
#CODE
def solve(n):
prime = [True for i in range(n + 1)]
prime[0] = prime[1] = False
p = 2
while p * p <= n:
if prime[p]:
i = p * p
while i <= n:
prime[i] = False
i += p
p += 1
d = []
for i in range(2, n + 1):
if prime[i]:
d.append(i)
return d
def search(arr , l , r , x):
while l <= r :
mid = l + (r - l) // 2
if arr[mid] == x :
return arr[mid]
elif arr[mid] > x :
r = mid - 1
else:
l = mid + 1
return arr[l]
n , m = MAP()
g = []
for i in range(n):
g.append(LIST())
p = solve(10**5+9)
rows = []
for i in range(n):
c = 0
for j in range(m):
x = search(p , 0 , len(p) , g[i][j])
c += x - g[i][j]
rows.append(c)
cols = []
for i in range(m):
c = 0
for j in range(n):
x = search(p , 0 , len(p) , g[j][i])
c += x - g[j][i]
cols.append(c)
r1 = 1000000000
r2 = 1000000000
if len(rows)>0:
r1 = min(rows)
if len(cols) > 0:
r2 = min(cols)
r3 = min(r1,r2)
print(r3)
``` | instruction | 0 | 69,258 | 15 | 138,516 |
Yes | output | 1 | 69,258 | 15 | 138,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an n Γ m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.
You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not.
A matrix is prime if at least one of the two following conditions fulfills:
* the matrix has a row with prime numbers only;
* the matrix has a column with prime numbers only;
Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
Input
The first line contains two integers n, m (1 β€ n, m β€ 500) β the number of rows and columns in the matrix, correspondingly.
Each of the following n lines contains m integers β the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.
The numbers in the lines are separated by single spaces.
Output
Print a single integer β the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.
Examples
Input
3 3
1 2 3
5 6 1
4 4 1
Output
1
Input
2 3
4 8 8
9 2 9
Output
3
Input
2 2
1 3
4 2
Output
0
Note
In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.
In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.
In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.
Submitted Solution:
```
#pyrival orz
import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a%b)
def seive(n):
primes = [True]*(n+1)
for i in range(2, n):
if not primes[i]:
continue
j = 2*i
while j <= n:
primes[j] = False
j += i
return primes
def factors(n):
factors = []
x = 2
while x*x <= n:
while n % x == 0:
factors.append(x)
n //= x
if n > 1:
factors.append(x)
return factors
# Functions: list of factors, seive of primes, gcd of two numbers
def main():
try:
primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101]
from bisect import bisect_left
n, m = invr()
a = []
c = [0]*(n + m)
for i in range(n):
a.append(inlt())
for i in range(n):
for j in range(m):
val = bisect_left(primes, a[i][j])
if primes[val] != a[i][j]:
c[i] += primes[val] - a[i][j]
c[n+j] += primes[val] - a[i][j]
print(min(c))
except Exception as e:
print(e)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 69,263 | 15 | 138,526 |
No | output | 1 | 69,263 | 15 | 138,527 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got an n Γ m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.
You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not.
A matrix is prime if at least one of the two following conditions fulfills:
* the matrix has a row with prime numbers only;
* the matrix has a column with prime numbers only;
Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
Input
The first line contains two integers n, m (1 β€ n, m β€ 500) β the number of rows and columns in the matrix, correspondingly.
Each of the following n lines contains m integers β the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.
The numbers in the lines are separated by single spaces.
Output
Print a single integer β the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.
Examples
Input
3 3
1 2 3
5 6 1
4 4 1
Output
1
Input
2 3
4 8 8
9 2 9
Output
3
Input
2 2
1 3
4 2
Output
0
Note
In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.
In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.
In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.
Submitted Solution:
```
def f(n):
m = int(n ** 0.5) + 1
t = [1, 0] * (n // 2 + 1)
t[0], t[1], t[2] = 1, 1, 0
for i in range(3, m):
if t[i] == 0: t[i * i :: 2 * i] = [1] * ((n - i * i) // (2 * i) + 1)
for i in range(n - 1, -1, -1):
if t[i]: t[i] = t[i + 1] + 1
return t
n, m = map(int, input().split())
matrix = []
for i in range (n):
v = list(map(int, input().split()))
matrix.append(v)
q = f(100007)
print(min(min(sum(matrix[i]) for i in range(n)), min(sum(matrix[i][j] for i in range(n)) for j in range(m))))
``` | instruction | 0 | 69,264 | 15 | 138,528 |
No | output | 1 | 69,264 | 15 | 138,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | instruction | 0 | 69,506 | 15 | 139,012 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
mod = 1000000007
def sum(x, y, k, add) :
if k < add : return 0
up = x + add
if up > k : up = k
add = add + 1
return y * ( ( (add + up) * (up - add + 1) // 2 ) % mod ) % mod
def solve(x, y, k, add = 0) :
if x == 0 or y == 0 : return 0
if x > y :
x, y = y, x
pw = 1
while (pw << 1) <= y :
pw <<= 1
if pw <= x :
return ( sum(pw, pw, k, add)\
+ sum(pw, x + y - pw - pw, k, add + pw)\
+ solve(x - pw, y - pw, k, add) ) % mod
else :
return ( sum(pw, x, k, add)\
+ solve(x, y - pw, k, add + pw) ) % mod
q = int(input())
for i in range(0, q) :
x1, y1, x2, y2, k = list(map(int, input().split()))
ans = ( solve(x2, y2, k)\
- solve(x1 - 1, y2, k)\
- solve(x2, y1 - 1, k)\
+ solve(x1 - 1, y1 - 1, k) ) % mod
if ans < 0 : ans += mod
print(ans)
``` | output | 1 | 69,506 | 15 | 139,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | instruction | 0 | 69,507 | 15 | 139,014 |
Tags: combinatorics, divide and conquer, dp
Correct Solution:
```
mod = 1000000007
def sum(x,y,k,add) :
if k<add:return 0
up=x+add
if up>k:up=k
add=add+1
return y*(((add+up)*(up-add+1)//2)%mod)%mod
def solve(x,y,k,add=0) :
if x==0 or y==0:return 0
if x>y:x,y=y,x
pw = 1
while (pw<<1)<=y:pw<<=1
if pw<=x:return (sum(pw,pw,k,add)+sum(pw,x+y-pw-pw,k,add+pw)+solve(x-pw,y-pw,k,add))%mod
else:return (sum(pw,x,k,add)+solve(x,y-pw,k,add+pw))%mod
q=int(input())
for i in range(0,q):
x1,y1,x2,y2,k=list(map(int,input().split()))
ans=(solve(x2, y2, k)-solve(x1 - 1, y2, k)-solve(x2, y1 - 1, k)+solve(x1-1,y1-1,k))%mod
if ans<0:ans+=mod
print(ans)
``` | output | 1 | 69,507 | 15 | 139,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | instruction | 0 | 69,573 | 15 | 139,146 |
Tags: implementation
Correct Solution:
```
n = int(input())
s = input() + "A"
cnt = 0
i = 0
while i < n:
if s[i] == "U" and s[i + 1] == "R":
cnt += 1
i += 2
elif s[i] == "R" and s[i + 1] == "U":
cnt += 1
i += 2
else:
i += 1
print(n - cnt)
``` | output | 1 | 69,573 | 15 | 139,147 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | instruction | 0 | 69,574 | 15 | 139,148 |
Tags: implementation
Correct Solution:
```
n = int(input())
s = input()
a = ''
for i in s:
if len(a) == 0 or a[-1] == 'D' or a[-1] == i:
a = a + i
else:
a = a[:-1] + 'D'
print(len(a))
``` | output | 1 | 69,574 | 15 | 139,149 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | instruction | 0 | 69,575 | 15 | 139,150 |
Tags: implementation
Correct Solution:
```
import re
i=input
i()
print(len(re.sub('UR|RU','D',i())))
``` | output | 1 | 69,575 | 15 | 139,151 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | instruction | 0 | 69,576 | 15 | 139,152 |
Tags: implementation
Correct Solution:
```
n=int(input())
s=list(input())
for i in range(n-1):
if (s[i]=='U' and s[i+1]=="R") or (s[i]=='R' and s[i+1]=='U'):
s[i+1]="D"
print(len(s)-s.count("D"))
``` | output | 1 | 69,576 | 15 | 139,153 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | instruction | 0 | 69,577 | 15 | 139,154 |
Tags: implementation
Correct Solution:
```
n=int(input())
S=input()
index = 0
summ=0
while index+1 < n:
if S[index]!=S[index+1]:
summ+=1
index+=1
index +=1
print(n-summ)
``` | output | 1 | 69,577 | 15 | 139,155 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | instruction | 0 | 69,578 | 15 | 139,156 |
Tags: implementation
Correct Solution:
```
n = int(input())
s = input()
i = 0
cnt = 0
while i < (n-1):
if s[i:i+2] == 'UR' or s[i:i+2] == 'RU':
cnt += 1
i += 2
else:
i += 1
print(n-cnt)
``` | output | 1 | 69,578 | 15 | 139,157 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | instruction | 0 | 69,579 | 15 | 139,158 |
Tags: implementation
Correct Solution:
```
import sys
import re
def main():
n, s = sys.stdin.read().strip().split()
return int(n) - len(re.findall('UR|RU', s))
print(main())
``` | output | 1 | 69,579 | 15 | 139,159 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | instruction | 0 | 69,580 | 15 | 139,160 |
Tags: implementation
Correct Solution:
```
n = input()
path = input()
subs = 0
i = 0
while i < len(path) - 1:
if path[i:i+2] in ("RU", "UR"):
subs += 1
i += 1
i += 1
print(len(path) - subs)
``` | output | 1 | 69,580 | 15 | 139,161 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Submitted Solution:
```
def solution(a):
i=0
ans=len(a)
while i<len(a)-1:
if a[i]!=a[i+1]:
ans-=1
i+=1
i+=1
return ans
def answer():
n = int(input())
a = list(input())
print(solution(a))
answer()
``` | instruction | 0 | 69,581 | 15 | 139,162 |
Yes | output | 1 | 69,581 | 15 | 139,163 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Submitted Solution:
```
N = int(input().strip())
L = list(input().strip())
i = 0
while i<len(L)-1:
if L[i] == 'R' and L[i + 1] == 'U' or \
L[i] == 'U' and L[i + 1] == 'R':
if i+2 < len(L):
L = L[:i]+['D']+L[i+2:]
else:
L = L[:i] + ['D']
i += 1
print(len(L))
``` | instruction | 0 | 69,582 | 15 | 139,164 |
Yes | output | 1 | 69,582 | 15 | 139,165 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Submitted Solution:
```
n = int(input())
s = input()
ans = n
used = [False for i in range(n)]
for i in range(1, n):
if not used[i - 1] and (s[i] == 'R' and s[i - 1] == 'U' or s[i] == 'U' and s[i - 1] == 'R'):
ans -= 1
used[i] = True
used[i - 1] = True
print(ans)
``` | instruction | 0 | 69,583 | 15 | 139,166 |
Yes | output | 1 | 69,583 | 15 | 139,167 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Submitted Solution:
```
le = int(input())
st = input()
c = 0
if le == 2:
if st[0] != st[1]:
print(1)
else:
print(2)
elif le == 1:
print(1)
else:
x = st[0]
for i in range(0, le):
if x != st[i] and i != le - 1:
c += 1
x = st[i + 1]
i += 1
elif x != st[i] and i == le - 1:
c += 1
print(le - c)
``` | instruction | 0 | 69,584 | 15 | 139,168 |
Yes | output | 1 | 69,584 | 15 | 139,169 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Submitted Solution:
```
n =input()
a = n.replace("RU", "D").replace("UR", "D")
b = n.replace("UR", "D").replace("RU", "D")
print(min([len(a), len(b)]))
``` | instruction | 0 | 69,585 | 15 | 139,170 |
No | output | 1 | 69,585 | 15 | 139,171 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Submitted Solution:
```
n = int(input())
s = input()
a = s.find('RU')
while a != -1:
s = s[:a] + 'D' + s[a + 2:]
a = s.find('RU')
a = s.find('UR')
while a != -1:
s = s[:a] + 'D' + s[a + 2:]
a = s.find('UR')
print(len(s))
``` | instruction | 0 | 69,586 | 15 | 139,172 |
No | output | 1 | 69,586 | 15 | 139,173 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Submitted Solution:
```
user_in1 = input()
user_in2 = input()
newstr = ""
count = 0
while True:
if count >= len(user_in2)-1:
if count >= len(user_in2):
newstr = newstr + user_in2[count-1]
else:
newstr = newstr + user_in2[count]
break
else:
if user_in2[count] + user_in2[count+1] == "RU" or user_in2[count] + user_in2[count+1] == "UR":
newstr = newstr + "d"
count += 2
else:
newstr = newstr + user_in2[count]
count += 1
print(len(newstr))
``` | instruction | 0 | 69,587 | 15 | 139,174 |
No | output | 1 | 69,587 | 15 | 139,175 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Submitted Solution:
```
a = int(input())
n =input()
a = n.replace("RU", "D").replace("UR", "D")
b = n.replace("UR", "D").replace("RU", "D")
print(min([len(a), len(b)]))
``` | instruction | 0 | 69,588 | 15 | 139,176 |
No | output | 1 | 69,588 | 15 | 139,177 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor.
Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them.
In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume).
He may perform the following operation arbitrarily many times:
* Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point.
Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square.
Find the minimum scatteredness after he performs arbitrarily many operations.
Constraints
* 2 \leq N \leq 10^5
* 1 \leq D \leq 1000
* 0 \leq x_i, y_i \leq 10^9
* Given coordinates are pairwise distinct
* All numbers given in input are integers
Input
Input is given from Standard Input in the following format:
N D
x_1 y_1
:
x_N y_N
Output
Print the answer.
Examples
Input
3 1
0 0
1 0
2 0
Output
1
Input
19 2
1 3
2 3
0 1
1 1
2 1
3 1
4 4
5 4
6 4
7 4
8 4
8 3
8 2
8 1
8 0
7 0
6 0
5 0
4 0
Output
4
Input
8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5
Output
4 | instruction | 0 | 69,645 | 15 | 139,290 |
"Correct Solution:
```
n,d = map(int,input().split())
num = [[0]*d for i in range(d)]
a = 0
for i in range(n):
x,y = map(int,input().split())
x %=d
y%=d
num[x][y] += 1
a = max(a,num[x][y])
x=1
while x*x<a:
x += 1
r = (x-1)*d
a = x-1
dai = d-1
syo = 0
anum = [[[0]*d for i in range(d)]for i in range(3)]
rui = [[[0]*(2*d+1) for i in range(2*d+1)]for i in range(3)]
for i in range(d):
for j in range(d):
z = num[i][j]
if z > (a+1)**2:
anum[0][i][j]=1
anum[1][i][j]=1
anum[2][i][j]=1
elif z> a*(a+1):
anum[1][i][j]=1
anum[2][i][j]=1
elif z > a*a:
anum[1][i][j]=1
for x in range(3):
for i in range(1,2*d+1):
for j in range(1,2*d+1):
rui[x][i][j]+=rui[x][i-1][j]+rui[x][i][j-1]-rui[x][i-1][j-1]+anum[x][(i-1)%d][(j-1)%d]
def cheak(kon):
for i in range(1,d+1):
for j in range(1,d+1):
if rui[0][i+kon][j+kon] - rui[0][i - 1][j+kon] - rui[0][i+kon][j - 1] + rui[0][i - 1][j - 1]:
continue
if rui[1][i+d-1][j+d-1] - rui[1][i+kon+1 - 1][j+d-1] - rui[1][i+d-1][j+kon+1 - 1] + rui[1][i+kon+1 - 1][j+kon+1 - 1]:
continue
if rui[2][i+d-1][j+kon] - rui[2][i+kon+1 - 1][j+kon] - rui[2][i+d-1][j - 1] + rui[2][i+kon+1 - 1][j - 1]:
continue
if rui[2][i+kon][j+d-1] - rui[2][i - 1][j+d-1] - rui[2][i+kon][j+kon+1 - 1] + rui[2][i - 1][j+kon+1 - 1]:
continue
return 1
return 0
kon = (dai+syo)//2
while True:
if dai-syo <= 1:
if cheak(syo) == 1:
dai = syo
break
kon = (dai+syo)//2
c = cheak(kon)
if c == 1:
dai = kon
else:
syo = kon
print(dai+r)
``` | output | 1 | 69,645 | 15 | 139,291 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid with H rows and W columns. The state of the cell at the i-th (1β€iβ€H) row and j-th (1β€jβ€W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2β€H,Wβ€100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Submitted Solution:
```
import numpy as np
from scipy.signal import convolve2d
h, w = map(int, input().split())
field = []
ei, ej = -1, -1
for i in range(h):
row = input()
field.append([c == 'o' for c in row])
if ei == -1:
ej = row.find('E')
if ej != -1:
ei = i
field = np.array(field)
wh = max(ei + 1, h - ei)
ww = max(ej + 1, w - ej)
window = np.ones((wh, ww))
ans = int(convolve2d(field, window, mode='valid').max())
print(ans)
``` | instruction | 0 | 69,690 | 15 | 139,380 |
No | output | 1 | 69,690 | 15 | 139,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid with H rows and W columns. The state of the cell at the i-th (1β€iβ€H) row and j-th (1β€jβ€W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2β€H,Wβ€100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12
Submitted Solution:
```
import numpy as np
H, W = map(int, input().split())
lines = [input() for _ in range(H)]
for ii in range(H):
for jj in range(W):
if lines[ii][jj] == "E":
i = ii
j = jj
A = np.array([["o" in c for c in l] for l in lines])
h = max(i+1, H-i)
w = max(j+1, W-j)
M = 0
for i in range(H-h+1):
for j in range(W-w+1):
s = A[i:i+h,j:j+w].sum()
M = max(s, M)
print(M)
``` | instruction | 0 | 69,691 | 15 | 139,382 |
No | output | 1 | 69,691 | 15 | 139,383 |
Provide a correct Python 3 solution for this coding contest problem.
One morning when you woke up, it was in a springy labyrinth.
I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth.
When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth.
This labyrinth is shaped like a rectangle with several types of tiles, for example:
.. ## g. #
* #. * #. #
...... #
* s # *. * #
".":floor. You can walk around freely on this.
"#":wall. You can't get inside the wall.
"S": Your first position. Below this tile is the floor.
"G": Stairs. If you ride on this tile, you have escaped from the labyrinth.
"*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal.
The type of tile is one of these five.
You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname.
Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring.
Input
W H
c11 c12 ... c1w
c21 c22 ... c2w
:::
ch1 ch2 ... ccw
On the first line of input, the integer W (3 β€ W β€ 500) and the integer H (3 β€ H β€ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth.
The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall.
In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing.
Output
Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9.
Examples
Input
8 6
########
#..##g.#
#*#.*#.#
#......#
#*s#*.*#
########
Output
5.857142857143
Input
8 6
..##g.#
*#.*#.#
......#
*s#*.*#
Output
5.857142857143
Input
21 11
*#*.*.*.*.*.*.*.*#*#
*#...............#*#
*#.#.#.#.#.#.#.#.#*#
.#.#.#.#.#.#.#.#.##
...................#
.#.#.#.#.#.#.#.#.##
s#.#.#.#.#.#.#.#.#g#
...................#
...................#
Output
20.000000000000
Input
9 8
...#*.*#
.*.#...#
...#*.*#
.s....g#
.*#.*..#
Output
5.000000000000 | instruction | 0 | 69,781 | 15 | 139,562 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def bs(f, mi, ma):
st = time.time()
mm = -1
mi = fractions.Fraction(mi, 1)
ma = fractions.Fraction(ma, 1)
while ma > mi + eps:
gt = time.time()
mm = (ma+mi) / 2
if gt - st > 35:
return mm
if isinstance(mm, float):
tk = max(1, int(10**15 / mm))
mm = fractions.Fraction(int(mm*tk), tk)
if float(mm) == float(ma) or float(mm) == float(mi):
return mm
if f(mm):
mi = mm
else:
ma = mm
if f(mm):
return mm + eps
return mm
def main():
rr = []
def f(w,h):
a = [S() for _ in range(h)]
si = sj = -1
for i in range(1,h-1):
for j in range(1,w-1):
if a[i][j] == 's':
si = i
sj = j
break
def search(sc):
d = collections.defaultdict(lambda: inf)
q = []
for i in range(1,h-1):
for j in range(1,w-1):
if a[i][j] == sc:
d[(i,j)] = 0
heapq.heappush(q, (0, (i,j)))
v = collections.defaultdict(bool)
while len(q):
k, u = heapq.heappop(q)
if v[u]:
continue
v[u] = True
for di,dj in dd:
ni = u[0] + di
nj = u[1] + dj
if not a[ni][nj] in '.s':
continue
uv = (ni,nj)
if v[uv]:
continue
vd = k + 1
if d[uv] > vd:
d[uv] = vd
heapq.heappush(q, (vd, uv))
return d
gd = search('g')
wd = search('*')
cgs = []
cws = []
for i in range(1,h-1):
for j in range(1,w-1):
if not a[i][j] in '.s':
continue
if gd[(i,j)] >= inf:
cgs.append((i,j))
else:
cws.append((i,j))
cc = len(cgs) + len(cws)
cgc = len(cgs)
cgw = sum([wd[(i,j)] for i,j in cgs])
sgw = [(inf,0,0)]
for i,j in cws:
gt = gd[(i,j)]
wt = wd[(i,j)]
sgw.append((gt-wt, wt, gt))
sgw.sort()
# print(sgw[:5], sgw[-5:])
ls = len(sgw) - 1
# print('ls', ls, len(cws))
def ff(t):
# print('ff', t)
s = cgw
si = bisect.bisect_left(sgw,(t,0,0))
# print('si', si, sgw[si], sgw[si-1])
tc = cgc
s2 = cgw
tc2 = tc + ls - si
for i in range(si):
s2 += sgw[i][2]
for i in range(si,ls):
s2 += sgw[i][1]
# for i,j in cws:
# gt = gd[(i,j)]
# wt = wd[(i,j)]
# if t + wt < gt:
# s += wt
# tc += 1
# else:
# s += gt
# print('s,s2,tc,tc2', s,s2,tc,tc2)
av = (s2 + t*tc2) / cc
# print('av,t', float(av), float(t))
return t < av
k = bs(ff, 0, 10**10)
gt = gd[(si,sj)]
wt = wd[(si,sj)]
r = gt
if wt + k < gt:
r = wt + k
# print('r', r)
return '{:0.10f}'.format(float(r))
while 1:
n,m = LI()
if n == 0:
break
rr.append(f(n,m))
break
return '\n'.join(map(str, rr))
print(main())
``` | output | 1 | 69,781 | 15 | 139,563 |
Provide a correct Python 3 solution for this coding contest problem.
One morning when you woke up, it was in a springy labyrinth.
I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth.
When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth.
This labyrinth is shaped like a rectangle with several types of tiles, for example:
.. ## g. #
* #. * #. #
...... #
* s # *. * #
".":floor. You can walk around freely on this.
"#":wall. You can't get inside the wall.
"S": Your first position. Below this tile is the floor.
"G": Stairs. If you ride on this tile, you have escaped from the labyrinth.
"*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal.
The type of tile is one of these five.
You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname.
Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring.
Input
W H
c11 c12 ... c1w
c21 c22 ... c2w
:::
ch1 ch2 ... ccw
On the first line of input, the integer W (3 β€ W β€ 500) and the integer H (3 β€ H β€ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth.
The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall.
In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing.
Output
Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9.
Examples
Input
8 6
########
#..##g.#
#*#.*#.#
#......#
#*s#*.*#
########
Output
5.857142857143
Input
8 6
..##g.#
*#.*#.#
......#
*s#*.*#
Output
5.857142857143
Input
21 11
*#*.*.*.*.*.*.*.*#*#
*#...............#*#
*#.#.#.#.#.#.#.#.#*#
.#.#.#.#.#.#.#.#.##
...................#
.#.#.#.#.#.#.#.#.##
s#.#.#.#.#.#.#.#.#g#
...................#
...................#
Output
20.000000000000
Input
9 8
...#*.*#
.*.#...#
...#*.*#
.s....g#
.*#.*..#
Output
5.000000000000 | instruction | 0 | 69,782 | 15 | 139,564 |
"Correct Solution:
```
from collections import deque
w, h = map(int, input().split())
mp = [input() for _ in range(h)]
springs = []
tile_cnt = 0
for y in range(h):
for x in range(w):
if mp[y][x] == "*":
springs.append((x, y))
if mp[y][x] == "g":
gx, gy = x, y
if mp[y][x] == "s":
sx, sy = x, y
tile_cnt += 1
if mp[y][x] == ".":
tile_cnt += 1
vec = ((1, 0), (0, -1), (-1, 0), (0, 1))
INF = 10 ** 10
g_dist = [[INF] * w for _ in range(h)]
que = deque()
que.append((0, gx, gy))
g_dist[gy][gx] = 0
while que:
score, x, y = que.popleft()
for dx, dy in vec:
nx, ny = x + dx, y + dy
if mp[ny][nx] in (".", "s"):
if g_dist[ny][nx] == INF:
g_dist[ny][nx] = score + 1
que.append((score + 1, nx, ny))
s_dist = [[INF] * w for _ in range(h)]
que = deque()
for x, y in springs:
s_dist[y][x] = 0
que.append((0, x, y))
while que:
score, x, y = que.popleft()
for dx, dy in vec:
nx, ny = x + dx, y + dy
if mp[ny][nx] in (".", "s"):
if s_dist[ny][nx] == INF:
s_dist[ny][nx] = score + 1
que.append((score + 1, nx, ny))
sorted_tiles = sorted([(g_dist[y][x] - s_dist[y][x] if g_dist[y][x] != INF else INF, x, y) for y in range(h) for x in range(w) if mp[y][x] in (".", "s")])
acc_g = 0
acc_s = 0
acc_t = 0
acc_g_dic = {}
acc_s_dic = {}
acc_t_dic = {}
keys = set()
for key, x, y in sorted_tiles:
acc_g += g_dist[y][x]
acc_s += s_dist[y][x]
acc_t += 1
acc_g_dic[key] = acc_g
acc_s_dic[key] = acc_s
acc_t_dic[key] = acc_t
keys.add(key)
keys = sorted(list(keys))
length = len(keys)
for i in range(length - 1):
key = keys[i]
next_key = keys[i + 1]
E = (acc_g_dic[key] + acc_s - acc_s_dic[key]) / acc_t_dic[key]
if key <= E < next_key:
print(min(g_dist[sy][sx], s_dist[sy][sx] + E))
break
else:
print(g_dist[sy][sx])
``` | output | 1 | 69,782 | 15 | 139,565 |
Provide a correct Python 3 solution for this coding contest problem.
One morning when you woke up, it was in a springy labyrinth.
I don't know why I'm in such a strange place. You have the option of waiting for help here, but you know from experience that if you stay in a labyrinth like this for a long time, a gust of wind will surely blow you away. It was. However, the time until the gust blows cannot be judged. So you thought it was best to act to minimize the expected number of moves required to escape the labyrinth.
When I picked up an unidentified scroll that had fallen under my feet and read it as a trial, I happened to be able to perceive the entire map of the labyrinth. Furthermore, by drinking the grass that I happened to have, I was able to know the positions of all the traps. Apparently, there are no traps other than dangerous monsters and springs in this labyrinth.
This labyrinth is shaped like a rectangle with several types of tiles, for example:
.. ## g. #
* #. * #. #
...... #
* s # *. * #
".":floor. You can walk around freely on this.
"#":wall. You can't get inside the wall.
"S": Your first position. Below this tile is the floor.
"G": Stairs. If you ride on this tile, you have escaped from the labyrinth.
"*":Spring. If you ride on this tile, it will be randomly blown onto one of the floors (excluding stairs and spring tiles). The odds of being hit on each floor are all equal.
The type of tile is one of these five.
You can move one square at a time from the tile you are currently riding to an adjacent tile in either the up, down, left, or right direction. However, you cannot move to Naname.
Given a map of the labyrinth, find the expected number of moves required to escape the labyrinth if you take the best strategy. It is not included in the number of movements that is blown by the spring.
Input
W H
c11 c12 ... c1w
c21 c22 ... c2w
:::
ch1 ch2 ... ccw
On the first line of input, the integer W (3 β€ W β€ 500) and the integer H (3 β€ H β€ 500) are written in this order, separated by blanks. The integer W represents the width of the labyrinth, and the integer H represents the height of the labyrinth.
The following H line contains W letters (not blank delimiters) that represent the map of the labyrinth. The format of this part is as in the example given above. Note that "s" and "g" appear exactly once in the data. In addition, one square around the labyrinth is always surrounded by a wall.
In addition, in the given labyrinth, no matter how you move and how it is blown by the spring, you will fall into a state where you cannot escape even if you can arbitrarily set the floor to be blown by the spring. You can assume that there is no such thing.
Output
Output the expected number of moves to escape when you take the best strategy. The output may contain errors, but the relative error to the true value must be less than 10-9.
Examples
Input
8 6
########
#..##g.#
#*#.*#.#
#......#
#*s#*.*#
########
Output
5.857142857143
Input
8 6
..##g.#
*#.*#.#
......#
*s#*.*#
Output
5.857142857143
Input
21 11
*#*.*.*.*.*.*.*.*#*#
*#...............#*#
*#.#.#.#.#.#.#.#.#*#
.#.#.#.#.#.#.#.#.##
...................#
.#.#.#.#.#.#.#.#.##
s#.#.#.#.#.#.#.#.#g#
...................#
...................#
Output
20.000000000000
Input
9 8
...#*.*#
.*.#...#
...#*.*#
.s....g#
.*#.*..#
Output
5.000000000000 | instruction | 0 | 69,783 | 15 | 139,566 |
"Correct Solution:
```
from collections import deque
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
W, H = map(int, readline().split())
S = [readline().strip() for i in range(H)]
R = [[0]*W for i in range(H)]
P = []
cnt = 0
for i in range(H):
Si = S[i]
for j in range(W):
if Si[j] == 's':
sx = j; sy = i
R[i][j] = 1
cnt += 1
elif Si[j] == 'g':
gx = j; gy = i
elif Si[j] == '*':
P.append((j, i))
elif Si[j] == '.':
cnt += 1
R[i][j] = 1
dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
INF = 10**18
dist1 = [[INF]*W for i in range(H)]
que = deque(P)
for x, y in P:
dist1[y][x] = 0
while que:
x, y = que.popleft()
c = dist1[y][x]+1
for dx, dy in dd:
nx = x + dx; ny = y + dy
if S[ny][nx] in '*#' or dist1[ny][nx] != INF:
continue
dist1[ny][nx] = c
que.append((nx, ny))
dist2 = [[INF]*W for i in range(H)]
que = deque([(gx, gy)])
dist2[gy][gx] = 0
while que:
x, y = que.popleft()
c = dist2[y][x]+1
for dx, dy in dd:
nx = x + dx; ny = y + dy
if S[ny][nx] in '*#' or dist2[ny][nx] != INF:
continue
dist2[ny][nx] = c
que.append((nx, ny))
left = 0; right = 10**21
BASE = 10**9
for i in range(71):
mid = (left + right) // 2
su = 0
for i in range(H):
for j in range(W):
if not R[i][j]:
continue
su += min(dist1[i][j]*BASE + mid, dist2[i][j]*BASE)
if mid*cnt > su:
right = mid
else:
left = mid
write("%.16f\n" % min(dist1[sy][sx] + left / BASE, dist2[sy][sx]))
solve()
``` | output | 1 | 69,783 | 15 | 139,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR | instruction | 0 | 69,989 | 15 | 139,978 |
Tags: constructive algorithms, implementation
Correct Solution:
```
from sys import stdin
def main():
n, m, k = map(int, stdin.readline().split())
for _ in range (k):
x, y = map(int, stdin.readline().split())
ans = str()
for _ in range (n - 1):
ans += 'U'
for _ in range (m - 1):
ans += 'L'
for i in range (n):
if i % 2:
char = 'L'
else:
char = 'R'
for _ in range (m - 1):
ans += char
ans += 'D'
print (len(ans))
print (ans)
main()
``` | output | 1 | 69,989 | 15 | 139,979 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR | instruction | 0 | 69,990 | 15 | 139,980 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n, m, k = map(int,input().split())
l = ""
for a in range(2*k):
input()
for i in range(n-1):
l = l+"U"
for j in range(m-1):
l = l+"L"
for h in range(n*m-1):
if h % m == m-1:
l = l+"D"
else:
if h//m % 2 == 0:
l = l+"R"
else:
l = l+"L"
print(len(l))
print(l)
``` | output | 1 | 69,990 | 15 | 139,981 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR | instruction | 0 | 69,991 | 15 | 139,982 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n, m, k = map(int, input().strip().split())
# n = int(n)
# m = int(m)
# k = int(k)
output = "U" * (n-1)
output = output + "L"*(m-1)
letter = "R"
for a in range(n):
output = output + letter*(m-1)
output = output + "D"
letter = "R" if letter == "L" else "L"
print((n+m-2) + n * m)
print(output)
``` | output | 1 | 69,991 | 15 | 139,983 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR | instruction | 0 | 69,992 | 15 | 139,984 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n, m, k = map(int, input().split())
for i in range(k):
input()
res = ""
res += 'U' * (n - 1)
res += 'L' * (m - 1)
for i in range(n):
if(i % 2 == 0):
res += 'R' * (m - 1)
else :
res += 'L' * (m - 1)
if(i != n - 1):
res += 'D'
print(len(res))
print(res)
``` | output | 1 | 69,992 | 15 | 139,985 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR | instruction | 0 | 69,993 | 15 | 139,986 |
Tags: constructive algorithms, implementation
Correct Solution:
```
from sys import stdin
from collections import deque
mod = 10**9 + 7
import sys
import random
# sys.setrecursionlimit(10**6)
from queue import PriorityQueue
# def rl():
# return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq
# input = lambda: sys.stdin.readline().rstrip()
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f
# def ncr(x, y):
# return f(x) // (f(y) * f(x - y))
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
from collections import defaultdict
from heapq import heappop, heappush, heapify
n,m,k = map(int,input().split())
max1 = 0
max2 = 0
ans = ''
hash = defaultdict(set)
for i in range(k):
a,b = map(int,input().split())
seti = set()
max1 = max(max1,b)
max2 = max(max2,a)
for j in range(b,0,-1):
seti.add((a,j))
for j in range(a,0,-1):
seti.add((j,1))
hash[i+1] = seti
ba = set()
sa = []
ans = 'L'*(max1) + 'U'*(max2)
# print(max1,max2)
for i in range(k):
a,b = map(int,input().split())
if (a,b) not in hash[i+1]:
sa.append([b,a])
sa.sort()
x,y = 1,1
for i in sa:
b,a = i
# print(a,b)
if a>x:
ans+='D'*(a-x)
elif x>a:
ans+='U'*(x-a)
ans+='R'*(b-y)
x,y = a,b
if len(ans)<=2*m*n:
print(len(ans))
print(ans)
else:
print(-1)
``` | output | 1 | 69,993 | 15 | 139,987 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR | instruction | 0 | 69,994 | 15 | 139,988 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n, m, k = map(int, input().split())
mass = list()
answer = ''
for i in range(k):
x, y = map(int, input().split())
mass.append([x, y])
for i in range(k):
x, y = map(int, input().split())
answer = answer + ''.join(['R'] * m)
answer = answer + ''.join(['D'] * n)
for i in range(n):
if i % 2 == 0:
answer = answer + ''.join(['L'] * (m - 1))
else:
answer = answer + ''.join(['R'] * (m - 1))
if i != n - 1:
answer = answer + 'U'
print(len(answer))
print(answer)
``` | output | 1 | 69,994 | 15 | 139,989 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR | instruction | 0 | 69,995 | 15 | 139,990 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n,m,k=list(map(int,input().split()))
for i in range(k):
sx,sy=map(int,input().split())
for i in range(k):
fx,fy=map(int,input().split())
ans="L"*(m-1)+"U"*(n-1)
for i in range(n):
if(i!=0):
ans=ans+"D"
if(i%2==0):
ans=ans+"R"*(m-1)
elif(i%2!=0):
ans=ans+"L"*(m-1)
print(len(ans))
print(ans)
``` | output | 1 | 69,995 | 15 | 139,991 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR | instruction | 0 | 69,996 | 15 | 139,992 |
Tags: constructive algorithms, implementation
Correct Solution:
```
def main():
import sys
input=sys.stdin.readline
n,m,k=map(int,input().split())
ans="L"*(m-1)+"U"*(n-1)
flag=1
for i in range(m):
if flag==1:
ans+="D"*(n-1)+"R"
else:
ans+="U"*(n-1)+"R"
flag*=-1
print(len(ans))
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 69,996 | 15 | 139,993 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
Submitted Solution:
```
t = input()
n = int(t.split(" ")[0])
m = int(t.split(" ")[1])
k = int(t.split(" ")[2])
out = []
for i in range(0, k):
b = input()
for i in range(0, k):
b = input()
if n > 1 and m > 1:
print(2 * (n - 1) + (n + 1) * (m - 1))
print("U" * (n - 1) + "L" * (m - 1), end="")
for i in range(n):
if i != 0:
print("D", end="")
if i % 2 == 0:
print("R" * (m - 1), end="")
else:
print("L" * (m - 1), end="")
elif n == 1:
out.append("R"*(m-1))
out.append("L"*(m-1))
print(2*m-2)
else:
out.append("U"*(n-1))
out.append("D"*(n-1))
print(2*n-2)
for i in range(0, len(out)):
print(out[i], end="")
``` | instruction | 0 | 69,997 | 15 | 139,994 |
Yes | output | 1 | 69,997 | 15 | 139,995 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
Submitted Solution:
```
lx, ly, nc = map(int, input().split())
s = 'L'*(ly-1) + 'U'*(lx-1)
cur = 0
for i in range(ly):
if i:
s += 'R'
s += ('DU'[cur])*(lx-1)
cur = 1-cur
print(len(s))
print(s)
``` | instruction | 0 | 69,998 | 15 | 139,996 |
Yes | output | 1 | 69,998 | 15 | 139,997 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
Submitted Solution:
```
n, m, _ = map(int, input().split())
print(2 * (n - 1) + (n + 1) * (m - 1))
print("U" * (n - 1) + "L" * (m - 1), end="")
for i in range(n):
if i != 0:
print("D", end="")
if i % 2 == 0:
print("R" * (m - 1), end="")
else:
print("L" * (m - 1), end="")
``` | instruction | 0 | 69,999 | 15 | 139,998 |
Yes | output | 1 | 69,999 | 15 | 139,999 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
Submitted Solution:
```
import sys
n, m, k = map(int, sys.stdin.readline().split())
s = [[int(x) for x in sys.stdin.readline().split()] for _ in range(k)]
g = [[int(x) for x in sys.stdin.readline().split()] for _ in range(k)]
def main():
res = 'L' * (m - 1) + 'U' * (n - 1)
for i in range(n):
if (i + 1) & 1:
res += 'R' * (m - 1)
if i < n - 1:
res += 'D'
else:
res += 'L' * (m - 1)
if i < n - 1:
res += 'D'
yield len(res)
yield res
if __name__ == "__main__":
ans = main()
print(*ans, sep='\n')
``` | instruction | 0 | 70,000 | 15 | 140,000 |
Yes | output | 1 | 70,000 | 15 | 140,001 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
Submitted Solution:
```
def solver(n,m,k):
ans = ""
ans = ans + "U"*(n-1)
ans = ans + "L"*(m-1)
dirc = "R"
for i in range(n):
if dirc == "R":
ans = ans + "R"*(m-1)
if i+1!=n:
ans +="D"
dirc = "L"
else:
ans = ans + "L"*(m-1)
if i+1!=n:
ans +="D"
dirc="R"
return ans
n,m,k= map(int,input().split())
for _ in range(2*k):
waste = input()
ans = solver(n,m,k)
print(ans)
``` | instruction | 0 | 70,001 | 15 | 140,002 |
No | output | 1 | 70,001 | 15 | 140,003 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
Submitted Solution:
```
import sys
input = sys.stdin.readline
n, m, k = [int(x) for x in input().strip().split()]
ks = [[] for i in range(k)]
ke = [[] for i in range(k)]
for ki in range(k):
ks[ki] = [int(x) for x in input().strip().split()]
for ki in range(k):
ke[ki] = [int(x) for x in input().strip().split()]
ans = []
for ki in range(k):
dx, dy = ke[ki][0] - ks[ki][0], ke[ki][1] - ks[ki][1]
if dx>0:
for i in range(dx):
ans.append('D')
elif dx<0:
for i in range(abs(dx)):
ans.append('U')
if dy>0:
for i in range(dy):
ans.append('R')
elif dy<0:
for i in range(abs(dy)):
ans.append('L')
for kj in range(ki+1, k):
ks[kj][0] += dx
if ks[kj][0] <=0:
ks[kj][0] = 1
elif ks[kj][0] >n:
ks[kj][0] = n
ks[kj][1] += dy
if ks[kj][1] <=0:
ks[kj][1] = 1
elif ks[kj][1] >m:
ks[kj][1] = m
#print(ans)
#print(ks, ke)
ans = ''.join(ans)
print(len(ans))
if ans:
print(ans)
else:
print('-1')
``` | instruction | 0 | 70,002 | 15 | 140,004 |
No | output | 1 | 70,002 | 15 | 140,005 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
Submitted Solution:
```
n, m, k = map(int, input().split())
for i in range(2*k):
x, y = map(int, input().split())
num = 0
ans = (n - 1) * 'D' + (m - 1) * 'L' + (n - 1) * 'U'
num += 2*n + m - 3
for i in range(n):
if i % 2 == 0:
ans += 'R' * (m - 1)
else:
ans += 'L' * (m - 1)
num += m - 1
if i != n - 1:
ans += 'D'
num += 1
if num == 0:
print(1)
print('L')
else:
print(num)
print(ans)
``` | instruction | 0 | 70,003 | 15 | 140,006 |
No | output | 1 | 70,003 | 15 | 140,007 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has a rectangular Board of size n Γ m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sx_i-th row and sy_i-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (x, y) cell, then after the operation:
* left, its coordinates will be (x, y - 1);
* right, its coordinates will be (x, y + 1);
* down, its coordinates will be (x + 1, y);
* up, its coordinates will be (x - 1, y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2nm actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2nm actions.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 200) β the number of rows and columns of the board and the number of chips, respectively.
The next k lines contains two integers each sx_i, sy_i ( 1 β€ sx_i β€ n, 1 β€ sy_i β€ m) β the starting position of the i-th chip.
The next k lines contains two integers each fx_i, fy_i ( 1 β€ fx_i β€ n, 1 β€ fy_i β€ m) β the position that the i-chip should visit at least once.
Output
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters L, R, D, U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
Submitted Solution:
```
n,m,k = map(int,input().split())
for i in range(k):
a = input()
for i in range(k):
b = input()
n-=1
ans = n*"D"+m*"L"
count = 2*n
for i in range(m):
if i%2 == 0:
ans+=n*"U"
else:
ans+=n*"D"
ans+="R"
count+=n+1
print(count)
print(ans)
``` | instruction | 0 | 70,004 | 15 | 140,008 |
No | output | 1 | 70,004 | 15 | 140,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a 2 Γ n maze.
Imagine a maze that looks like a 2 Γ n rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two free maze cells, you need to determine the minimum time required to go from one cell to the other.
Unfortunately, the developed algorithm works well for only one request for finding the shortest path, in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to optimize the algorithm to find the shortest path. Write a program that will effectively respond to multiple requests to find the shortest path in a 2 Γ n maze.
Input
The first line contains two integers, n and m (1 β€ n β€ 2Β·105; 1 β€ m β€ 2Β·105) β the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains n characters, each character equals either '.' (empty cell), or 'X' (obstacle).
Each of the next m lines contains two integers vi and ui (1 β€ vi, ui β€ 2n) β the description of the i-th request. Numbers vi, ui mean that you need to print the value of the shortest path from the cell of the maze number vi to the cell number ui. We assume that the cells of the first line of the maze are numbered from 1 to n, from left to right, and the cells of the second line are numbered from n + 1 to 2n from left to right. It is guaranteed that both given cells are empty.
Output
Print m lines. In the i-th line print the answer to the i-th request β either the size of the shortest path or -1, if we can't reach the second cell from the first one.
Examples
Input
4 7
.X..
...X
5 1
1 3
7 7
1 4
6 1
4 7
5 7
Output
1
4
0
5
2
2
2
Input
10 3
X...X..X..
..X...X..X
11 7
7 18
18 10
Output
9
-1
3
Submitted Solution:
```
import itertools
n, m = tuple(map(int, str.split(input())))
xs = list(input() + input())
def find_start(xs, sx=0):
for x in range(sx, n):
for y in range(2):
if xs[x + y * n] == ".":
return x, y
return -1, -1
def pprint_field(xs):
print(
str.join(
"\n",
map(
lambda line: str.join(
"",
map(
lambda p: str(p[1]) if isinstance(p, tuple) else p,
line
)
),
(xs[:n], xs[n:])
)
)
)
x = 0
nseq = 0
while True:
x, y = find_start(xs)
if x == -1:
break
front = [(x, y)]
while front:
nfront = []
for x, y in front:
mi = None
for dx, dy in itertools.product((-1, 0, 1), repeat=2):
cx, cy = x + dx, y + dy
if abs(dx) + abs(dy) == 1 and 0 <= cx < n and 0 <= cy < 2:
p = xs[cx + cy * n]
if p == ".":
nfront.append((cx, cy))
elif isinstance(p, tuple):
mi = min(mi or p[1], p[1])
xs[x + y * n] = (nseq, mi + 1 if mi is not None else 0)
front = nfront
nseq += 1
# pprint_field(xs)
for _ in range(m):
v, u = tuple(map(int, str.split(input())))
v, u = xs[v - 1], xs[u - 1]
if v[0] != u[0]:
print(-1)
else:
print(abs(v[1] - u[1]))
``` | instruction | 0 | 70,171 | 15 | 140,342 |
No | output | 1 | 70,171 | 15 | 140,343 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.