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.
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with 2Γ n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer β the maximum amount of bishwocks that can be placed onto the given board.
Examples
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
Submitted Solution:
```
line1 = input()
line2 = input()
boards = [['' for x in range(len(line1))] for y in range(2)]
for i in range(len(line1)):
boards[0][i] = line1[i]
boards[1][i] = line2[i]
count = 0
for x in range(len(line1)-1):
if 3 <= boards[0][x:x+2].count('0') + boards[1][x:x+2].count('0'):
count += 1
if boards[0][x] =='0':
if boards[0][x+1] == '0':
boards[0][x] = 'X'
boards[1][x] = 'X'
if boards[1][x] =='0':
boards[1][x+1] = 'X'
else:
boards[1][x] = 'X'
else:
boards[0][x] = 'X'
boards[1][x] = 'X'
boards[1][x+1] = 'X'
else:
boards[0][x+1] = 'X'
boards[1][x] = 'X'
boards[1][x+1] = 'X'
print(count)
``` | instruction | 0 | 54,544 | 15 | 109,088 |
No | output | 1 | 54,544 | 15 | 109,089 |
Provide a correct Python 3 solution for this coding contest problem.
You have full control over a robot that walks around in a rectangular field paved with square tiles like a chessboard. There are m columns of tiles from west to east, and n rows of tiles from south to north (1 <= m, n <= 100). Each tile is given a pair of coordinates, such as (i, j), where 1 <= i <= m and 1 <= j <= n.
Your robot is initially on the center of the tile at (1, 1), that is, one at the southwest corner of the field, facing straight north. It can move either forward or backward, or can change its facing direction by ninety degrees at a time, according to a command you give to it, which is one of the following.
* `FORWARD` k
Go forward by k tiles to its facing direction (1 <= k < 100).
* `BACKWARD` k
Go backward by k tiles, without changing its facing direction (1 <= k < 100).
* `RIGHT`
Turn to the right by ninety degrees.
* `LEFT`
Turn to the left by ninety degrees.
* `STOP`
Stop.
While executing either a "`FORWARD`" or a "`BACKWARD`" command, the robot may bump against the wall surrounding the field. If that happens, the robot gives up the command execution there and stands at the center of the tile right in front of the wall, without changing its direction.
After finishing or giving up execution of a given command, your robot will stand by for your next command.
Input
The input consists of one or more command sequences. Each input line has at most fifty characters.
The first line of a command sequence contains two integer numbers telling the size of the field, the first number being the number of columns and the second being the number of rows. There might be white spaces (blanks and/or tabs) before, in between, or after the two numbers. Two zeros as field size indicates the end of input.
Each of the following lines of a command sequence contains a command to the robot. When a command has an argument, one or more white spaces are put between them. White spaces may also appear before and after the command and/or its argument.
A command sequence is terminated by a line containing a "`STOP`" command. The next command sequence, if any, starts from the next line.
Output
The output should be one line for each command sequence in the input. It should contain two numbers i and j of the coordinate pair (i, j), in this order, of the tile on which your robot stops. Two numbers should be separated by one white spaces.
Example
Input
6 5
FORWARD 3
RIGHT
FORWARD 5
LEFT
BACKWARD 2
STOP
3 1
FORWARD 2
STOP
0 0
Output
6 2
1 1 | instruction | 0 | 54,726 | 15 | 109,452 |
"Correct Solution:
```
moves = [
{'F': lambda i, j, x: (i, min(n, j + x)), 'B': lambda i, j, x: (i, max(1, j - x))},
{'F': lambda i, j, x: (min(m, i + x), j), 'B': lambda i, j, x: (max(1, i - x), j)},
{'F': lambda i, j, x: (i, max(1, j - x)), 'B': lambda i, j, x: (i, min(n, j + x))},
{'F': lambda i, j, x: (max(1, i - x), j), 'B': lambda i, j, x: (min(m, i + x), j)}]
turns = {'R': lambda i: (i + 1) % 4, 'L': lambda i: (i - 1) % 4}
while True:
m, n = map(int, input().split())
if not m:
break
i, j = 1, 1
cci = 0
while True:
cmd = input()
ch = cmd[0]
if ch in 'FB':
i, j = moves[cci][ch](i, j, int(cmd.split()[1]))
elif ch in 'RL':
cci = turns[ch](cci)
else:
print(i, j)
break
``` | output | 1 | 54,726 | 15 | 109,453 |
Provide a correct Python 3 solution for this coding contest problem.
You have full control over a robot that walks around in a rectangular field paved with square tiles like a chessboard. There are m columns of tiles from west to east, and n rows of tiles from south to north (1 <= m, n <= 100). Each tile is given a pair of coordinates, such as (i, j), where 1 <= i <= m and 1 <= j <= n.
Your robot is initially on the center of the tile at (1, 1), that is, one at the southwest corner of the field, facing straight north. It can move either forward or backward, or can change its facing direction by ninety degrees at a time, according to a command you give to it, which is one of the following.
* `FORWARD` k
Go forward by k tiles to its facing direction (1 <= k < 100).
* `BACKWARD` k
Go backward by k tiles, without changing its facing direction (1 <= k < 100).
* `RIGHT`
Turn to the right by ninety degrees.
* `LEFT`
Turn to the left by ninety degrees.
* `STOP`
Stop.
While executing either a "`FORWARD`" or a "`BACKWARD`" command, the robot may bump against the wall surrounding the field. If that happens, the robot gives up the command execution there and stands at the center of the tile right in front of the wall, without changing its direction.
After finishing or giving up execution of a given command, your robot will stand by for your next command.
Input
The input consists of one or more command sequences. Each input line has at most fifty characters.
The first line of a command sequence contains two integer numbers telling the size of the field, the first number being the number of columns and the second being the number of rows. There might be white spaces (blanks and/or tabs) before, in between, or after the two numbers. Two zeros as field size indicates the end of input.
Each of the following lines of a command sequence contains a command to the robot. When a command has an argument, one or more white spaces are put between them. White spaces may also appear before and after the command and/or its argument.
A command sequence is terminated by a line containing a "`STOP`" command. The next command sequence, if any, starts from the next line.
Output
The output should be one line for each command sequence in the input. It should contain two numbers i and j of the coordinate pair (i, j), in this order, of the tile on which your robot stops. Two numbers should be separated by one white spaces.
Example
Input
6 5
FORWARD 3
RIGHT
FORWARD 5
LEFT
BACKWARD 2
STOP
3 1
FORWARD 2
STOP
0 0
Output
6 2
1 1 | instruction | 0 | 54,727 | 15 | 109,454 |
"Correct Solution:
```
def numchange(boo):
if boo:return 1
else: return -1
while True:
m,n = map(int,input().split())
if m==0: break
yaxis = 1
xaxis = 1
rotation = 0
while True:
lnr = input()
if lnr == "STOP": break
elif lnr == "RIGHT": rotation = (rotation+1)%4
elif lnr == "LEFT": rotation = (rotation+3)%4
else:
movequeue = lnr.split()
moveamount = int(movequeue[1]) * numchange((rotation//2)==0) * numchange(movequeue[0]=="FORWARD")
if rotation%2==0:
yaxis += moveamount
if yaxis <= 0:
yaxis = 1
elif yaxis > n:
yaxis = n
else:
xaxis += moveamount
if xaxis <= 0:
xaxis = 1
elif xaxis > m:
xaxis = m
print(xaxis,yaxis)
``` | output | 1 | 54,727 | 15 | 109,455 |
Provide a correct Python 3 solution for this coding contest problem.
You have full control over a robot that walks around in a rectangular field paved with square tiles like a chessboard. There are m columns of tiles from west to east, and n rows of tiles from south to north (1 <= m, n <= 100). Each tile is given a pair of coordinates, such as (i, j), where 1 <= i <= m and 1 <= j <= n.
Your robot is initially on the center of the tile at (1, 1), that is, one at the southwest corner of the field, facing straight north. It can move either forward or backward, or can change its facing direction by ninety degrees at a time, according to a command you give to it, which is one of the following.
* `FORWARD` k
Go forward by k tiles to its facing direction (1 <= k < 100).
* `BACKWARD` k
Go backward by k tiles, without changing its facing direction (1 <= k < 100).
* `RIGHT`
Turn to the right by ninety degrees.
* `LEFT`
Turn to the left by ninety degrees.
* `STOP`
Stop.
While executing either a "`FORWARD`" or a "`BACKWARD`" command, the robot may bump against the wall surrounding the field. If that happens, the robot gives up the command execution there and stands at the center of the tile right in front of the wall, without changing its direction.
After finishing or giving up execution of a given command, your robot will stand by for your next command.
Input
The input consists of one or more command sequences. Each input line has at most fifty characters.
The first line of a command sequence contains two integer numbers telling the size of the field, the first number being the number of columns and the second being the number of rows. There might be white spaces (blanks and/or tabs) before, in between, or after the two numbers. Two zeros as field size indicates the end of input.
Each of the following lines of a command sequence contains a command to the robot. When a command has an argument, one or more white spaces are put between them. White spaces may also appear before and after the command and/or its argument.
A command sequence is terminated by a line containing a "`STOP`" command. The next command sequence, if any, starts from the next line.
Output
The output should be one line for each command sequence in the input. It should contain two numbers i and j of the coordinate pair (i, j), in this order, of the tile on which your robot stops. Two numbers should be separated by one white spaces.
Example
Input
6 5
FORWARD 3
RIGHT
FORWARD 5
LEFT
BACKWARD 2
STOP
3 1
FORWARD 2
STOP
0 0
Output
6 2
1 1 | instruction | 0 | 54,728 | 15 | 109,456 |
"Correct Solution:
```
while True:
col, row = map(int, input().split())
if col == 0:
break
else:
robot = [1, 1]
direction = "up"
while True:
str = input()
if str == "STOP":
print(robot[0], robot[1])
break
elif str == "LEFT":
if direction == "up":
direction = "left"
elif direction == "left":
direction = "down"
elif direction == "down":
direction = "right"
else: # right
direction = "up"
elif str == "RIGHT":
if direction == "up":
direction = "right"
elif direction == "left":
direction = "up"
elif direction == "down":
direction = "left"
else: # right
direction = "down"
else:
di, step = str.split()
step = int(step)
if direction == "up":
if di == "FORWARD":
robot[1] = min(robot[1] + step, row)
else:
robot[1] = max(robot[1] - step, 1)
elif direction == "left":
if di == "FORWARD":
robot[0] = max(robot[0] - step, 1)
else:
robot[0] = min(robot[0] + step, col)
elif direction == "down":
if di == "FORWARD":
robot[1] = max(robot[1] - step, 1)
else:
robot[1] = min(robot[1] + step, row)
else: # right
if di == "FORWARD":
robot[0] = min(robot[0] + step, col)
else:
robot[0] = max(robot[0] - step, 1)
``` | output | 1 | 54,728 | 15 | 109,457 |
Provide a correct Python 3 solution for this coding contest problem.
You have full control over a robot that walks around in a rectangular field paved with square tiles like a chessboard. There are m columns of tiles from west to east, and n rows of tiles from south to north (1 <= m, n <= 100). Each tile is given a pair of coordinates, such as (i, j), where 1 <= i <= m and 1 <= j <= n.
Your robot is initially on the center of the tile at (1, 1), that is, one at the southwest corner of the field, facing straight north. It can move either forward or backward, or can change its facing direction by ninety degrees at a time, according to a command you give to it, which is one of the following.
* `FORWARD` k
Go forward by k tiles to its facing direction (1 <= k < 100).
* `BACKWARD` k
Go backward by k tiles, without changing its facing direction (1 <= k < 100).
* `RIGHT`
Turn to the right by ninety degrees.
* `LEFT`
Turn to the left by ninety degrees.
* `STOP`
Stop.
While executing either a "`FORWARD`" or a "`BACKWARD`" command, the robot may bump against the wall surrounding the field. If that happens, the robot gives up the command execution there and stands at the center of the tile right in front of the wall, without changing its direction.
After finishing or giving up execution of a given command, your robot will stand by for your next command.
Input
The input consists of one or more command sequences. Each input line has at most fifty characters.
The first line of a command sequence contains two integer numbers telling the size of the field, the first number being the number of columns and the second being the number of rows. There might be white spaces (blanks and/or tabs) before, in between, or after the two numbers. Two zeros as field size indicates the end of input.
Each of the following lines of a command sequence contains a command to the robot. When a command has an argument, one or more white spaces are put between them. White spaces may also appear before and after the command and/or its argument.
A command sequence is terminated by a line containing a "`STOP`" command. The next command sequence, if any, starts from the next line.
Output
The output should be one line for each command sequence in the input. It should contain two numbers i and j of the coordinate pair (i, j), in this order, of the tile on which your robot stops. Two numbers should be separated by one white spaces.
Example
Input
6 5
FORWARD 3
RIGHT
FORWARD 5
LEFT
BACKWARD 2
STOP
3 1
FORWARD 2
STOP
0 0
Output
6 2
1 1 | instruction | 0 | 54,729 | 15 | 109,458 |
"Correct Solution:
```
# AOJ 1104: Where's Your Robot?
# Python3 2018.7.14 bal4u
# N,E,S,W -> 0,1,2,3
f = [[0,1],[1,0],[0,-1],[-1,0]]
b = [[0,-1],[-1,0],[0,1],[1,0]]
while True:
w, h = map(int, input().split())
if w == 0: break
r, c, d = 1, 1, 0
while True:
buf = input().split()
if buf[0] == "STOP": break
elif buf[0] == "RIGHT": d = (d+1)%4
elif buf[0] == "LEFT": d = (d+3)%4
else:
if buf[0] == "FORWARD":
r2 = r + int(buf[1])*f[d][1]
c2 = c + int(buf[1])*f[d][0]
else: # BACKWARD k
r2 = r + int(buf[1])*b[d][1]
c2 = c + int(buf[1])*b[d][0]
if r2 < 1: r2 = 1
if r2 > h: r2 = h
if c2 < 1: c2 = 1
if c2 > w: c2 = w
r, c = r2, c2
print(c, r)
``` | output | 1 | 54,729 | 15 | 109,459 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have full control over a robot that walks around in a rectangular field paved with square tiles like a chessboard. There are m columns of tiles from west to east, and n rows of tiles from south to north (1 <= m, n <= 100). Each tile is given a pair of coordinates, such as (i, j), where 1 <= i <= m and 1 <= j <= n.
Your robot is initially on the center of the tile at (1, 1), that is, one at the southwest corner of the field, facing straight north. It can move either forward or backward, or can change its facing direction by ninety degrees at a time, according to a command you give to it, which is one of the following.
* `FORWARD` k
Go forward by k tiles to its facing direction (1 <= k < 100).
* `BACKWARD` k
Go backward by k tiles, without changing its facing direction (1 <= k < 100).
* `RIGHT`
Turn to the right by ninety degrees.
* `LEFT`
Turn to the left by ninety degrees.
* `STOP`
Stop.
While executing either a "`FORWARD`" or a "`BACKWARD`" command, the robot may bump against the wall surrounding the field. If that happens, the robot gives up the command execution there and stands at the center of the tile right in front of the wall, without changing its direction.
After finishing or giving up execution of a given command, your robot will stand by for your next command.
Input
The input consists of one or more command sequences. Each input line has at most fifty characters.
The first line of a command sequence contains two integer numbers telling the size of the field, the first number being the number of columns and the second being the number of rows. There might be white spaces (blanks and/or tabs) before, in between, or after the two numbers. Two zeros as field size indicates the end of input.
Each of the following lines of a command sequence contains a command to the robot. When a command has an argument, one or more white spaces are put between them. White spaces may also appear before and after the command and/or its argument.
A command sequence is terminated by a line containing a "`STOP`" command. The next command sequence, if any, starts from the next line.
Output
The output should be one line for each command sequence in the input. It should contain two numbers i and j of the coordinate pair (i, j), in this order, of the tile on which your robot stops. Two numbers should be separated by one white spaces.
Example
Input
6 5
FORWARD 3
RIGHT
FORWARD 5
LEFT
BACKWARD 2
STOP
3 1
FORWARD 2
STOP
0 0
Output
6 2
1 1
Submitted Solution:
```
moves = [
{'F': lambda i, j, x: (i, min(n, j + x)), 'B': lambda i, j, x: (i, max(1, j - x))},
{'F': lambda i, j, x: (min(m, i + x), j), 'B': lambda i, j, x: (max(1, i - x), j)},
{'F': lambda i, j, x: (i, max(1, j - x)), 'B': lambda i, j, x: (i, min(n, j + x))},
{'F': lambda i, j, x: (max(m, i - x), j), 'B': lambda i, j, x: (min(1, i + x), j)}]
turns = {'R': lambda i: (i + 1) % 4, 'L': lambda i: (i - 1) % 4}
while True:
m, n = map(int, input().split())
if not m:
break
i, j = 1, 1
cci = 0
while True:
cmd = input()
ch = cmd[0]
if ch in 'FB':
i, j = moves[cci][ch](i, j, int(cmd.split()[1]))
elif ch in 'RL':
cci = turns[ch](cci)
else:
print(i, j)
break
``` | instruction | 0 | 54,730 | 15 | 109,460 |
No | output | 1 | 54,730 | 15 | 109,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image> | instruction | 0 | 54,812 | 15 | 109,624 |
Tags: constructive algorithms, implementation
Correct Solution:
```
import sys,math
from collections import deque,defaultdict
import operator as op
from functools import reduce
#sys.setrecursionlimit(10**6)
I=sys.stdin.readline
#s="abcdefghijklmnopqrstuvwxyz"
"""
x_move=[-1,0,1,0,-1,1,1,-1]
y_move=[0,1,0,-1,1,1,-1,-1]
"""
def ii():
return int(I().strip())
def li():
return list(map(int,I().strip().split()))
def mi():
return map(int,I().strip().split())
def ncr(n, r):
r = min(r, n-r)
numer = reduce(op.mul, range(n, n-r, -1), 1)
denom = reduce(op.mul, range(1, r+1), 1)
return numer // denom
def gcd(x, y):
while y:
x, y = y, x % y
return x
def valid(row,col,rows,cols,rcross,lcross):
return rows[row]==0 and cols[col]==0 and rcross[col+row]==0 and lcross[col-row]==0
def div(n):
tmp=[]
for i in range(2,int(n**.5)+1):
if n%i==0:
cnt=0
while(n%i==0):
n=n//i
cnt+=1
tmp.append((i,cnt))
if n>1:
tmp.append((n,1))
return tmp
def isPrime(n):
if n<=1:
return False
elif n<=2:
return True
else:
flag=True
for i in range(2,int(n**.5)+1):
if n%i==0:
flag=False
break
return flag
def s(b):
cnt=0
while b>0:
tmp=b%10
cnt+=tmp
b=b//10
return cnt
def main():
ans=""
col=[(1,1),(3,1)]
row=[(1,2),(2,2),(3,2),(4,2)]
ans=[]
s=I().strip()
cntC=0
cntR=0
for i in s:
if i=='0':
ans.append(col[cntC%2])
cntC+=1
else:
ans.append(row[cntR%4])
cntR+=1
for i in ans:
print(*i)
if __name__ == '__main__':
main()
``` | output | 1 | 54,812 | 15 | 109,625 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image> | instruction | 0 | 54,813 | 15 | 109,626 |
Tags: constructive algorithms, implementation
Correct Solution:
```
#double underscore makes a class variable or a class method private
mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
l=[[0]*4 for i in range(4)]
s=si()
hor = [[1, 2], [2, 2], [3, 2], [4, 2]]
ver = [[1, 1],[3, 1]]
o,z=0,0
for i in s:
if i=='1':
print(*hor[o%4])
o+=1
else:
print(*ver[z%2])
z+=1
``` | output | 1 | 54,813 | 15 | 109,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image> | instruction | 0 | 54,814 | 15 | 109,628 |
Tags: constructive algorithms, implementation
Correct Solution:
```
s=input()
i=0
j=0
k=0
while i<len(s):
if s[i]=='0':
if j==0:
print("1 1")
j=1
else:
print("3 1")
j=0
else:
if k==0:
print("4 3")
k=1
else:
print("4 1")
k=0
i+=1
``` | output | 1 | 54,814 | 15 | 109,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image> | instruction | 0 | 54,815 | 15 | 109,630 |
Tags: constructive algorithms, implementation
Correct Solution:
```
__author__ = 'Esfandiar'
s = input()
Len = len(s)
f=ff=0
for i in range(Len):
if s[i] == '0':
if f==0:
print(1,1)
f=1
else:
print(3,1)
f=0
else:
if ff==0:
print(4,3)
ff=1
else:
print(4,1)
ff=0
``` | output | 1 | 54,815 | 15 | 109,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image> | instruction | 0 | 54,816 | 15 | 109,632 |
Tags: constructive algorithms, implementation
Correct Solution:
```
from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353
def inp(): return stdin.readline().strip()
def iinp(): return int(inp())
def out(var, end="\n"): stdout.write(str(var)+"\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def smp(): return map(str, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)]
def remadd(x, y): return 1 if x%y else 0
def ceil(a,b): return (a+b-1)//b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def isprime(x):
if x<=1: return False
if x in (2, 3): return True
if x%2 == 0: return False
for i in range(3, int(sqrt(x))+1, 2):
if x%i == 0: return False
return True
s = inp()
a = 1
b = 1
for i in s:
if i=='0':
print(1, a)
a += 1
else:
print(3, b)
b += 2
a = (a-1)%4+1
b = (b-1)%4+1
``` | output | 1 | 54,816 | 15 | 109,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image> | instruction | 0 | 54,817 | 15 | 109,634 |
Tags: constructive algorithms, implementation
Correct Solution:
```
s = input()
zero, one = 0, 0
for si in s:
if si=='0':
print(1, zero%4+1)
zero += 1
else:
print(4, one%2*2+1)
one += 1
``` | output | 1 | 54,817 | 15 | 109,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image> | instruction | 0 | 54,818 | 15 | 109,636 |
Tags: constructive algorithms, implementation
Correct Solution:
```
s=input()
twos=0
fours=0
for i in s:
if i=='0':
if twos==1:
print(3,1)
twos=0
else:
print(1,1)
twos=1
else:
print(fours+1,2)
fours+=1
fours%=4
``` | output | 1 | 54,818 | 15 | 109,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image> | instruction | 0 | 54,819 | 15 | 109,638 |
Tags: constructive algorithms, implementation
Correct Solution:
```
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque
def r():
return int(input())
def rm():
return map(int,input().split())
def rl():
return list(map(int,input().split()))
'''A'''
s = input()
n = len(s)
zero = False
one = False
for i in range(n):
if s[i]=='0':
if zero:
print(3,1)
else:
print(1,1)
zero = not zero
else:
if one:
print(4,1)
else:
print(4,3)
one = not one
``` | output | 1 | 54,819 | 15 | 109,639 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image>
Submitted Solution:
```
s=input();print(*('%d %d'%((1,s[:i].count('0')%4+1)if s[i]=='0'else(3,s[:i].count('1')%2*2+1))for i in range(len(s))),sep='\n')
``` | instruction | 0 | 54,820 | 15 | 109,640 |
Yes | output | 1 | 54,820 | 15 | 109,641 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image>
Submitted Solution:
```
p = [0, 0]
for d in input():
c = int(d)
print(p[c] + 1, c * 2 + 1)
p[c] = (p[c] + 2 - c) % 4
``` | instruction | 0 | 54,821 | 15 | 109,642 |
Yes | output | 1 | 54,821 | 15 | 109,643 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image>
Submitted Solution:
```
# Author Name: Ajay Meena
# Codeforce : https://codeforces.com/profile/majay1638
import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout
# -------------- INPUT FUNCTIONS ------------------
def get_ints_in_variables(): return map(
int, sys.stdin.readline().strip().split())
def get_int(): return int(sys.stdin.readline())
def get_ints_in_list(): return list(
map(int, sys.stdin.readline().strip().split()))
def get_list_of_list(n): return [list(
map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def get_string(): return sys.stdin.readline().strip()
# -------- SOME CUSTOMIZED FUNCTIONS-----------
def myceil(x, y): return (x + y - 1) // y
# -------------- SOLUTION FUNCTION ------------------
def clearGridCol(grid):
for i in range(4):
sm = 0
for j in range(4):
sm += grid[j][i]
if sm == 4:
for j in range(4):
grid[j][i] = 0
def clearGridRow(grid):
for i in range(4):
sm = 0
for j in range(4):
sm += grid[i][j]
if sm == 4:
for j in range(4):
grid[i][j] = 0
def Solution():
# Write Your Code Here
s = get_string()
cnt0 = 0
cnt1 = 0
for c in s:
if c == "0":
if cnt0 == 0:
print(1, 1)
cnt0 += 1
elif cnt0 == 1:
print(3, 1)
cnt0 += 1
if cnt0 == 2:
cnt0 = 0
else:
if cnt1 == 0:
print(4, 3)
cnt1 += 1
elif cnt1 == 1:
print(4, 1)
cnt1 += 1
if cnt1 == 2:
cnt1 = 0
def main():
# Take input Here and Call solution function
Solution()
# calling main Function
if __name__ == '__main__':
main()
``` | instruction | 0 | 54,822 | 15 | 109,644 |
Yes | output | 1 | 54,822 | 15 | 109,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image>
Submitted Solution:
```
s = input()
v1, h1 = -1, -1
for i in range(len(s)):
if s[i] == '0':
if v1 == -1:
print(1, 1)
v1 = 1
else:
print(3, 1)
v1 = -1
else:
if h1 == -1:
print(4, 3)
h1 = 1
else:
print(4, 1)
h1 = -1
``` | instruction | 0 | 54,823 | 15 | 109,646 |
Yes | output | 1 | 54,823 | 15 | 109,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image>
Submitted Solution:
```
# Author Name: Ajay Meena
# Codeforce : https://codeforces.com/profile/majay1638
import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout
# -------------- INPUT FUNCTIONS ------------------
def get_ints_in_variables(): return map(
int, sys.stdin.readline().strip().split())
def get_int(): return int(sys.stdin.readline())
def get_ints_in_list(): return list(
map(int, sys.stdin.readline().strip().split()))
def get_list_of_list(n): return [list(
map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def get_string(): return sys.stdin.readline().strip()
# -------- SOME CUSTOMIZED FUNCTIONS-----------
def myceil(x, y): return (x + y - 1) // y
# -------------- SOLUTION FUNCTION ------------------
def clearGridCol(grid):
for i in range(4):
sm = 0
for j in range(4):
sm += grid[j][i]
if sm == 4:
for j in range(4):
grid[j][i] = 0
def clearGridRow(grid):
for i in range(4):
sm = 0
for j in range(4):
sm += grid[i][j]
if sm == 4:
for j in range(4):
grid[i][j] = 0
def Solution():
# Write Your Code Here
s = get_string()
cnt0 = 0
cnt1 = 0
for c in s:
if c == "0":
if cnt0 == 0:
print(1, 1)
cnt0 += 1
elif cnt0 == 1:
print(3, 1)
cnt0 += 1
if cnt0 == 2:
cnt0 = 0
else:
if cnt1 == 0:
print(4, 4)
cnt1 += 1
elif cnt1 == 1:
print(4, 2)
cnt1 += 1
if cnt1 == 2:
cnt1 = 0
def main():
# Take input Here and Call solution function
Solution()
# calling main Function
if __name__ == '__main__':
main()
``` | instruction | 0 | 54,824 | 15 | 109,648 |
No | output | 1 | 54,824 | 15 | 109,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image>
Submitted Solution:
```
#double underscore makes a class variable or a class method private
mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
l=[[0]*4 for i in range(4)]
s=si()
hor = [[1, 2], [2, 2], [3, 2], [4, 3]]
ver = [[1, 1],[3, 1]]
o,z=0,0
for i in s:
if i=='1':
print(*hor[o%4])
o+=1
else:
print(*ver[z%2])
z+=1
``` | instruction | 0 | 54,825 | 15 | 109,650 |
No | output | 1 | 54,825 | 15 | 109,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image>
Submitted Solution:
```
s = input()
has_h = False
has_v = False
for c in s:
if c == "0":
if has_v:
print(4, 1)
else:
print(4, 3)
has_v = not has_v
else:
if has_h:
print(3, 1)
else:
print(1, 1)
``` | instruction | 0 | 54,826 | 15 | 109,652 |
No | output | 1 | 54,826 | 15 | 109,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a 4x4 grid. You play a game β there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.
Input
The only line contains a string s consisting of zeroes and ones (1 β€ |s| β€ 1000). Zero describes vertical tile, one describes horizontal tile.
Output
Output |s| lines β for each tile you should output two positive integers r,c, not exceeding 4, representing numbers of smallest row and column intersecting with it.
If there exist multiple solutions, print any of them.
Example
Input
010
Output
1 1
1 2
1 4
Note
Following image illustrates the example after placing all three tiles:
<image> Then the first row is deleted: <image>
Submitted Solution:
```
a = [0 , 0]
for i in map(int,input()):
print(a[0]+1,i*2+1)
a[0]=(a[0]+2-i)%4
``` | instruction | 0 | 54,827 | 15 | 109,654 |
No | output | 1 | 54,827 | 15 | 109,655 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image> | instruction | 0 | 54,860 | 15 | 109,720 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import math
from collections import Counter
#n=int(input())
#l=list(map(int,input().split()))
n=int(input())
#a,b,c=map(int,input().split())
#l=list(map(int,input().split()))
#a,b,c,d=map(int,input().split())
#l=[]
#for i in range(n):
# l+=[list(map(int,input().split()))]
a=((n+2)//2)
l=[[0]*a for i in range(a)]
print(a)
if n<=a:
for i in range(1,n+1):
print(1,i)
else:
for i in range(1,a+1):
print(1,i)
for i in range(2,2+n-a):
print(i,a)
``` | output | 1 | 54,860 | 15 | 109,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image> | instruction | 0 | 54,861 | 15 | 109,722 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int( input() )
i, j = 1, 1
print( n // 2 + 1 )
for qwq in range( n ):
print( str( i ) + ' ' + str( j ) )
if qwq & 1 == 0: i += 1
else: j += 1
``` | output | 1 | 54,861 | 15 | 109,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image> | instruction | 0 | 54,862 | 15 | 109,724 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
m = (n+2)//2
ans = []
for i in range(1, n+1):
if i <= m:
r = 1
c = i
else:
r = m
c = m-(n-i)
ans.append((r, c))
print(m)
for i in range(n):
print(*ans[i])
``` | output | 1 | 54,862 | 15 | 109,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image> | instruction | 0 | 54,863 | 15 | 109,726 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import math
n=int(input())
a=math.ceil((n+1)/2)
print(a)
for i in range(1,a+1):
print('1 '+str(i))
for i in range(2, n-a+2):
print(str(i)+' '+str(a))
``` | output | 1 | 54,863 | 15 | 109,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image> | instruction | 0 | 54,864 | 15 | 109,728 |
Tags: constructive algorithms, greedy
Correct Solution:
```
N = int(input())
tmp = N
ans = 0
for b in range(1, 1000):
if b + b - 1 >= N:
ans = b
break
print(ans)
for i in range(1, ans + 1):
print(1, i)
tmp -= 1
if tmp == 0:
exit()
for j in range(2, ans + 1):
print(j, ans)
tmp -= 1
if tmp == 0:
exit()
``` | output | 1 | 54,864 | 15 | 109,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image> | instruction | 0 | 54,865 | 15 | 109,730 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
print((n + 2) // 2)
row = col = 1
di, dj = 0, 1
for k in range(n):
print(row, col, sep=" ")
row += di
col += dj
di, dj = dj, di
``` | output | 1 | 54,865 | 15 | 109,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image> | instruction | 0 | 54,866 | 15 | 109,732 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
m = (n+2)//2
print(m)
for i in range(m):
print(1, i+1)
for i in range(n-m):
print(i+2, m)
``` | output | 1 | 54,866 | 15 | 109,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image> | instruction | 0 | 54,867 | 15 | 109,734 |
Tags: constructive algorithms, greedy
Correct Solution:
```
#!/usr/bin/env python3
import atexit
import io
import sys
from math import ceil
_I_B = sys.stdin.read().splitlines()
input = iter(_I_B).__next__
_O_B = io.StringIO()
sys.stdout = _O_B
@atexit.register
def write():
sys.__stdout__.write(_O_B.getvalue())
def main():
n=int(input())
m=1 + n//2
print(m)
c=m
while c:
print(c,1)
c-=1
c=2
while c<m:
print(1,c)
c+=1
if n!=1 and n&1:
print(1,m)
if __name__=='__main__':
main()
``` | output | 1 | 54,867 | 15 | 109,735 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image>
Submitted Solution:
```
n = int(input())
m = n//2 + 1
print(m)
for i in range(1,m+1):
print(1,i)
for i in range(m+1,n+1):
print(m,i-n+m)
``` | instruction | 0 | 54,868 | 15 | 109,736 |
Yes | output | 1 | 54,868 | 15 | 109,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image>
Submitted Solution:
```
import math
n=int(input())
m=math.ceil((n+1)/2.0)
print(m)
cnt=0
for i in range(m):
print(1,i+1)
cnt+=1
for j in range(m-1):
if cnt==n:
break
else:
print(j+2,i+1)
cnt+=1
``` | instruction | 0 | 54,869 | 15 | 109,738 |
Yes | output | 1 | 54,869 | 15 | 109,739 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image>
Submitted Solution:
```
from math import *
from collections import *
import sys
sys.setrecursionlimit(10**9)
n = int(input())
s = n//2 + 1
print(s)
ct = 0
x = 1
y = 1
for i in range(n):
print(x,y)
if(x < s):
x += 1
else:
y += 1
``` | instruction | 0 | 54,870 | 15 | 109,740 |
Yes | output | 1 | 54,870 | 15 | 109,741 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image>
Submitted Solution:
```
n=int(input())
m=n//2+1
print(n//2+1)
for i in range(1,m+1):
print(1,i)
for i in range(m+1,n+1):
print(i-m+1,m)
``` | instruction | 0 | 54,871 | 15 | 109,742 |
Yes | output | 1 | 54,871 | 15 | 109,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image>
Submitted Solution:
```
n = int(input())
if n == 1:
print(1)
print("1 1")
exit()
if n == 2:
print(2)
print("1 1")
print("1 2")
exit()
if n % 2 == 1:
print(n // 2 + 1)
for i in range(1, n // 2 + 2):
print(f"1 {i}")
for i in range(1, n // 2 + 1):
print(f"{n // 2 + 1} {i}")
else:
print(n // 2)
for i in range(1, n // 2 + 1):
print(f"1 {i}")
for i in range(1, n // 2 + 1):
print(f"{n // 2} {i}")
``` | instruction | 0 | 54,872 | 15 | 109,744 |
No | output | 1 | 54,872 | 15 | 109,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image>
Submitted Solution:
```
n=int(input())
if n==2:
print(2)
print(1,1)
print(1,2)
elif n==4:
print(3)
print(1,1)
print(1,3)
print(3,1)
print(3,3)
``` | instruction | 0 | 54,873 | 15 | 109,746 |
No | output | 1 | 54,873 | 15 | 109,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image>
Submitted Solution:
```
import math
b= int(input())
x1 = math.ceil((b+1)/2)
print(x1)
i=0
x=1
y=1
while i<b:
print(x," ",y)
if i>(x1-1):
y=y+1
else:
x=x+1
i=i+1
``` | instruction | 0 | 54,874 | 15 | 109,748 |
No | output | 1 | 54,874 | 15 | 109,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nauuo is a girl who loves playing chess.
One day she invented a game by herself which needs n chess pieces to play on a mΓ m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|β₯|i-j|. Here |x| means the absolute value of x.
However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.
She wants to find the smallest chessboard on which she can put n pieces according to the rules.
She also wonders how to place the pieces on such a chessboard. Can you help her?
Input
The only line contains a single integer n (1β€ nβ€ 1000) β the number of chess pieces for the game.
Output
The first line contains a single integer β the minimum value of m, where m is the length of sides of the suitable chessboard.
The i-th of the next n lines contains two integers r_i and c_i (1β€ r_i,c_iβ€ m) β the coordinates of the i-th chess piece.
If there are multiple answers, print any.
Examples
Input
2
Output
2
1 1
1 2
Input
4
Output
3
1 1
1 3
3 1
3 3
Note
In the first example, you can't place the two pieces on a 1Γ1 chessboard without breaking the rule. But you can place two pieces on a 2Γ2 chessboard like this:
<image>
In the second example, you can't place four pieces on a 2Γ2 chessboard without breaking the rule. For example, if you place the pieces like this:
<image>
then |r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1, |1-3|=2, 1<2; and |r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2, |1-4|=3, 2<3. It doesn't satisfy the rule.
However, on a 3Γ3 chessboard, you can place four pieces like this:
<image>
Submitted Solution:
```
import math
n=int(input())
if n==1:
print(1)
print(1,1)
exit()
dim=1
while (2*dim-2)<n:
dim+=1
cc=0
x=1
y=1
print(dim)
cc=0
while 1:
if cc==n:
break
print(x,y)
if x<dim:
x+=1
cc+=1
continue
else:
y+=1
cc+=1
``` | instruction | 0 | 54,875 | 15 | 109,750 |
No | output | 1 | 54,875 | 15 | 109,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR". | instruction | 0 | 55,700 | 15 | 111,400 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
from collections import deque
def solve(arr):
dict = {'R':0,'L':0,'U':0,'D':0}
for char in arr:
dict[char] += 1
dict['R'] = min(dict['R'], dict['L'])
dict['L'] = min(dict['R'], dict['L'])
dict['U'] = min(dict['U'], dict['D'])
dict['D'] = min(dict['U'], dict['D'])
if dict['U'] == 0:
dict['R'] = min(dict['R'], 1)
dict['L'] = dict['R']
if dict['R'] == 0:
dict['U'] = min(dict['U'], 1)
dict['D'] = dict['U']
result = 'R'*dict['R']+'U'*dict['U']+'L'*dict['L']+'D'*dict['D']
return str(len(result))+'\n'+ result +'\n'
if __name__ == "__main__":
line = input().split(' ')
output = ''
for i in range(int(line[0])):
arr = input()
output += solve(arr)
print(output)
``` | output | 1 | 55,700 | 15 | 111,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR". | instruction | 0 | 55,701 | 15 | 111,402 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
for _ in range(int(input())):
s = input()
i = 0
ans = ''
ls = {'U': 0, 'D': 0, 'L': 0, 'R': 0}
for i in s:
ls[i] += 1
if ls['U'] > ls['D']:
ls['U'] = ls['D']
else:
ls['D'] = ls['U']
if ls['L'] > ls['R']:
ls['L'] = ls['R']
else:
ls['R'] = ls['L']
if ls['U'] == 0 and ls['L'] == 0:
print(0)
print('')
continue
if ls['U']==0 or ls['L']==0:
if ls['U']==0:
print(2)
print('LR')
continue
else:
print(2)
print('UD')
continue
else:
ans +=ls['U']*'U'
ans +=ls['L']*'L'
ans +=ls['D']*'D'
ans +=ls['R']*'R'
print(len(ans))
print(ans)
``` | output | 1 | 55,701 | 15 | 111,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR". | instruction | 0 | 55,702 | 15 | 111,404 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
ans1 = []
ans2 = []
for _ in range(int(input())):
str1 = input()
u=str1.count("U")
d=str1.count("D")
l=str1.count("L")
r=str1.count("R")
ud = 0
lr = 0
if u>d: ud = d
else: ud = u
if l>r: lr = r
else: lr = l
if lr == 0 and ud != 0: ud = 1
if ud == 0 and lr != 0: lr = 1
ans1.append(ud*2+lr*2)
ans2.append("U"*ud+"R"*lr+"D"*ud+"L"*lr)
for i in range(len(ans1)):
print(ans1[i])
print(ans2[i])
``` | output | 1 | 55,702 | 15 | 111,405 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR". | instruction | 0 | 55,703 | 15 | 111,406 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
q = int(input())
for w in range(q):
s = str(input())
n = len(s)
x, y = 0, 0
ans = ""
was = []
if n == 1:
print(0)
continue
sCheck = list(set(s))
if len(sCheck) == 1:
print(0)
continue
if len(sCheck) == 2 and (((sCheck[0] == "U" and sCheck[1] == "D") or (sCheck[1] == "U" and sCheck[0] == "D")) or ((sCheck[1] == "R" and sCheck[0] == "L") or (sCheck[0] == "R" and sCheck[1] == "L"))):
print(2)
print(str(sCheck[0]) + str(sCheck[1]))
continue
countL = 0
countR = 0
countD = 0
countU = 0
for i in range(n):
if s[i] == "L":
countL += 1
elif s[i] == "R":
countR += 1
elif s[i] == "U":
countU += 1
else:
countD += 1
'''
if (countL != 0 and countR == 0 and countU == 0 and countD == 0) or (countL == 0 and countR != 0 and countU == 0 and countD == 0) or (countL == 0 and countR == 0 and countU != 0 and countD == 0) or (countL == 0 and countR == 0 and countU == 0 and countD != 0):
print(0)
continue
if ((countL != 0 and countR == 0) and (countD != 0 and countD == 0)) or ((countL == 0 and countR != 0) and (countD == 0 and countD != 0)):
print(0)
continue
'''
minLR = min(countL, countR)
minUD = min(countU, countD)
if minLR == 0 and minUD == 0:
print(0)
continue
if minLR == 0:
print(2)
print("UD")
continue
if minUD == 0:
print(2)
print("LR")
continue
for i in range(minUD):
ans += "U"
y += 1
for i in range(minLR):
ans += "L"
x += 1
for i in range(minUD):
ans += "D"
y -= 1
for i in range(minLR):
ans += "R"
x -= 1
if x == 0 and y == 0:
print(len(ans))
for i in range(len(ans)):
print(ans[i], end="")
print()
else:
print(0)
``` | output | 1 | 55,703 | 15 | 111,407 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR". | instruction | 0 | 55,704 | 15 | 111,408 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
q = int(input())
for _ in range(q):
s = input()
d = {}
for c in ['L', 'R', 'U', 'D']: d[c] = 0
for c in s:
d[c] += 1
if(0 not in d.values()):
rem = 2*(min(d['L'], d['R']) + min(d['U'], d['D']))
print(rem)
for i in range(min(d['L'], d['R'])): print('L', end='')
for i in range(min(d['U'], d['D'])): print('D', end='')
for i in range(min(d['L'], d['R'])): print('R', end='')
for i in range(min(d['U'], d['D'])): print('U', end='')
print()
else:
if(d['U'] == 0 or d['D'] == 0):
if(d['L']>=1 and d['R']>=1):
print(2)
print('LR')
else:
print(0)
print()
else:
if(d['U']>=1 and d['D']>=1):
print(2)
print('UD')
else:
print(0)
print()
``` | output | 1 | 55,704 | 15 | 111,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR". | instruction | 0 | 55,705 | 15 | 111,410 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
t = int(input())
for _ in range(t):
instructions = list(input())
r = instructions.count('R')
l = instructions.count('L')
u = instructions.count('U')
d = instructions.count('D')
diff1 = r - l
if diff1 > 0:
r = l
else:
l = r
diff2 = u - d
if diff2 > 0:
u = d
else:
d = u
if r == 0 and d == 0:
print(0)
elif r > 0 and d > 0:
print(2 * r + 2 * d)
elif u==0:
r = 1
l = 1
print(2)
else:
u = 1
d = 1
print(2)
print("R" * r + "U" * u + "L" * l + "D" * d)
``` | output | 1 | 55,705 | 15 | 111,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR". | instruction | 0 | 55,706 | 15 | 111,412 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
s = input()[:-1]
cntU = s.count("U")
cntD = s.count("D")
cntL = s.count("L")
cntR = s.count("R")
minLR = min(cntR, cntL)
minUD = min(cntU, cntD)
if minLR > 0 and minUD > 0:
ans = minLR * 2 + minUD * 2
string = "R" * minLR + "U" * minUD + "L" * minLR + "D" * minUD
elif minLR == 0:
ans = min(minUD, 1) * 2
string = "U" * min(minUD, 1) + "D" * min(minUD, 1)
elif minUD == 0:
ans = min(minLR, 1) * 2
string = "R" * min(minLR, 1) + "L" * min(minLR, 1)
else:
ans = 0
string = ""
print(ans)
print(string)
``` | output | 1 | 55,706 | 15 | 111,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR". | instruction | 0 | 55,707 | 15 | 111,414 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
import sys
import math
t = int(input())
for _ in range(t):
s = input()
l, r, d, u = 0, 0, 0, 0
for i in range(len(s)):
if s[i] == 'L':
l += 1
if s[i] == 'R':
r += 1
if s[i] == 'D':
d += 1
if s[i] == 'U':
u += 1
# print(l, r, d, u)
L, R, D, U = min(l, r), min(l, r), min(d, u), min(d, u)
if R != 0 and U != 0:
print(L + R + D + U)
ans = ''
for i in range(U):
ans += 'U'
for i in range(R):
ans += 'R'
for i in range(D):
ans += 'D'
for i in range(L):
ans += 'L'
print(ans)
else:
if R == 0 and U == 0:
print(0)
print('')
if R >= 1:
print(2)
print('LR')
if D >= 1:
print(2)
print('UD')
``` | output | 1 | 55,707 | 15 | 111,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
Submitted Solution:
```
for _ in range(int(input())):
s = input()
d = s.count("D")
u = s.count("U")
r = s.count("R")
l = s.count("L")
x = min(d, u)
d=x
u=x
y=min(r,l)
r=y
l=y
ans = "L"*l+"U"*u+"R"*r+"D"*d
if d == 0 and u == 0:
if l > 1 and r > 1:
ans = "LR"
elif l == 0 and r == 0:
if u > 1 and d > 1:
ans = "UD"
print(len(ans))
print(ans)
``` | instruction | 0 | 55,708 | 15 | 111,416 |
Yes | output | 1 | 55,708 | 15 | 111,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
Submitted Solution:
```
q=int(input())
for i in range(q):
s=str(input())
l,r,u,d=0,0,0,0
for t in s:
if t=='L':l+=1
elif t=='U':u+=1
elif t=='D':d+=1
else:r+=1
k=min(l,r);d=min(u,d)
if d==0:
if k!=0:
print(2)
print('LR')
else:
print(0)
elif k==0:
if d!=0:
print(2)
print('UD')
else:
print(0)
else:
print(k*2+d*2)
print('L'*k+'U'*d+'R'*k+'D'*d)
``` | instruction | 0 | 55,709 | 15 | 111,418 |
Yes | output | 1 | 55,709 | 15 | 111,419 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
Submitted Solution:
```
t=int(input())
for _ in range(t):
s=input()
u=s.count('U')
d=s.count('D')
l=s.count('L')
r=s.count('R')
t1=min(u,d)
t2=min(l,r)
if(t1!=0 and t2!=0):
ans=t1+t1+t2+t2
st=''
if(t1>0):
st=st+('U'*t1)
if(t2>0):
st=st+('L'*t2)
if(t1>0):
st=st+('D'*t1)
if(t2>0):
st=st+('R'*t2)
print(ans)
print(st)
elif(t1==0 and t2!=0):
ans=2
st='LR'
print(ans)
print(st)
elif(t1!=0 and t2==0):
ans=2
st='UD'
print(ans)
print(st)
else:
ans=0
st=''
print(ans)
print(st)
``` | instruction | 0 | 55,710 | 15 | 111,420 |
Yes | output | 1 | 55,710 | 15 | 111,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
Submitted Solution:
```
t=int(input())
while t:
t-=1
s=input()
n=len(s)
d=0
u=0
r=0
l=0
for i in range(n):
if s[i]=="R":
r+=1
if s[i]=="L":
l+=1
if s[i]=="U":
u+=1
if s[i]=="D":
d+=1
if min(r,l)==0 and min(u,d)>0:
print(2)
print("DU")
elif min(u,d)==0 and min(r,l)>0:
print(2)
print("RL")
elif min(r,l)==0 and min(u,d)==0:
print(0)
print()
else:
x=min(l,r)
y=min(u,d)
print(2*x+2*y)
print("U"*y+"R"*x+"D"*y+"L"*x)
``` | instruction | 0 | 55,711 | 15 | 111,422 |
Yes | output | 1 | 55,711 | 15 | 111,423 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.