message stringlengths 2 19.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image> | instruction | 0 | 15,547 | 15 | 31,094 |
Tags: data structures, math
Correct Solution:
```
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial
INT_MIN=float("-infinity")
INT_MAX=float("infinity")
n,m=map(int,input().split())
row=set()
col=set()
su=n*n
for i in range(m):
a,b=map(int,input().split())
if a not in row:
su-=n-len(col)
row.add(a)
# print(su)
if b not in col:
su-=n-len(row)
col.add(b)
print(su,end=' ')
``` | output | 1 | 15,547 | 15 | 31,095 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image> | instruction | 0 | 15,548 | 15 | 31,096 |
Tags: data structures, math
Correct Solution:
```
n,m = map(int, input().split())
all_x = set()
all_y = set()
ans = []
for i in range(m):
x,y = map(int, input().split())
all_x.add(x)
all_y.add(y)
ans.append((n - len(all_x)) * (n - len(all_y)))
print(*ans)
``` | output | 1 | 15,548 | 15 | 31,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image>
Submitted Solution:
```
n, m = map(int, input().split())
v = [0]*(10**5 + 1)
k1 = k2 = 0
h = [0]*(10**5 + 1)
ans = n * n
for i in range(m):
x, y = map(int, input().split())
if not v[x] or not h[y]:
if v[x]:
ans -= n - k1
elif h[y]:
ans -= n - k2
else:
ans -= 2*n-k1-k2-1
if not v[x]:
v[x] = 1
k1 += 1
if not h[y]:
h[y] = 1
k2 += 1
print(ans)
``` | instruction | 0 | 15,549 | 15 | 31,098 |
Yes | output | 1 | 15,549 | 15 | 31,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image>
Submitted Solution:
```
def main():
n, m = map(int, input().split())
row_remains = n
col_remains = n
row_used = dict()
col_used = dict()
ans = list()
for i in range(m):
x, y = map(int, input().split())
if x not in row_used:
row_used[x] = True
row_remains -= 1
if y not in col_used:
col_used[y] = True
col_remains -= 1
ans.append(row_remains * col_remains)
print(' '.join(str(x) for x in ans))
if __name__ == '__main__':
main()
``` | instruction | 0 | 15,550 | 15 | 31,100 |
Yes | output | 1 | 15,550 | 15 | 31,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image>
Submitted Solution:
```
s,rooks=map(int,input().split())
columns,rows=[0]*s,[0]*s
cN,cR=0,0
taken=0
for i in range(rooks):
a,b=map(int,input().split())
if columns[a-1]==0:
cN+=1
taken+=s-cR
columns[a-1]=1
if rows[b-1]==0:
cR+=1
taken+=s-cN
rows[b-1]=1
print(s**2-taken,end=' ')
``` | instruction | 0 | 15,551 | 15 | 31,102 |
Yes | output | 1 | 15,551 | 15 | 31,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image>
Submitted Solution:
```
board_size,rooks=map(int,(input().split()))
valid_squares=board_size**2
row_set=set()
col_set=set()
soln=[]
for i in range (rooks):
row,col=map(int,input().split())
if row not in row_set and col not in col_set:
valid_squares-=(2*board_size)-len(row_set)-len(col_set)-1
elif row not in row_set:
valid_squares-=board_size-len(col_set)
elif col not in col_set:
valid_squares-=board_size-len(row_set)
row_set.add(row)
col_set.add(col)
soln.append(valid_squares)
print(*soln)
``` | instruction | 0 | 15,552 | 15 | 31,104 |
Yes | output | 1 | 15,552 | 15 | 31,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image>
Submitted Solution:
```
# -*- coding: utf-8 -*-
# Baqir Khan
# Software Engineer (Backend)
n, m = map(int, input().split())
safe = n * n
ans = []
rows = set()
cols = set()
while m:
m -= 1
x, y = map(int, input().split())
if x not in rows and y not in cols:
safe -= (n + n - len(rows) - len(cols) - 1)
rows.add(x)
cols.add(y)
if x in rows and y not in cols:
safe -= (n - len(rows))
cols.add(x)
if x not in rows and y in cols:
safe -= (n - len(cols))
rows.add(x)
ans.append(safe)
print(" ".join([str(a) for a in ans]))
``` | instruction | 0 | 15,553 | 15 | 31,106 |
No | output | 1 | 15,553 | 15 | 31,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image>
Submitted Solution:
```
n, m = [int(i) for i in input().split()]
x_list = []
y_list = []
for i in range(m):
p, q = [int(i) for i in input().split()]
if i == 0:
x, y = n, n
c = x * y
if p in x_list:
print(c - y, end=' ')
x -= 1
y_list.append(q)
elif q in y_list:
print(c - x, end=' ')
y -= 1
x_list.append(p)
else:
print(c - (x + y - 1), end=' ')
x_list.append(p)
y_list.append(q)
x -= 1
y -= 1
``` | instruction | 0 | 15,554 | 15 | 31,108 |
No | output | 1 | 15,554 | 15 | 31,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image>
Submitted Solution:
```
n,m=map(int,input().split())
col=set([])
row=set([])
s=n*n
l=[]
for i in range(m):
x,y=map(int,input().split())
if x not in col and y not in row:
s-=2*n-1-len(row)-len(col)
elif y not in row and x in col:
s-=n-len(row)
elif x not in col and y in row:
s-=n-len(col)
l.append(s)
col.add(x)
row.add(y)
print(*l)
``` | instruction | 0 | 15,555 | 15 | 31,110 |
No | output | 1 | 15,555 | 15 | 31,111 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Output
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
Examples
Input
3 3
1 1
3 1
2 2
Output
4 2 0
Input
5 2
1 5
5 1
Output
16 9
Input
100000 1
300 400
Output
9999800001
Note
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
<image>
Submitted Solution:
```
n, m = list(map(int, input().split()))
v = []
h = []
nb = n ** 2
st = n * 2 - 1
for i in range(0, m):
y, x = list(map(int, input().split()))
y-=1
x-=1
if y not in v and x not in h:
nb -= st
st -= 2
elif (y not in v and x in h) or (y in v and x not in h):
nb -= (st - 1)
if n%2==0:
st -= 1
else:
pass
if nb<0:
nb=0
print((nb), end=" ")
v.append(y)
h.append(x)
``` | instruction | 0 | 15,556 | 15 | 31,112 |
No | output | 1 | 15,556 | 15 | 31,113 |
Provide a correct Python 3 solution for this coding contest problem.
Let us consider a grid of squares with N rows and N columns. You want to put some domino pieces on this grid. Each domino piece covers two squares that have a common side. Each square can be covered by at most one piece.
For each row of the grid, let's define its quality as the number of domino pieces that cover at least one square in this row. We define the quality of each column similarly.
Find a way to put at least one domino piece on the grid so that the quality of every row is equal to the quality of every column, or determine that such a placement doesn't exist.
Constraints
* 2 \le N \le 1000
Input
Input is given from Standard Input in the following format:
N
Output
If the required domino placement doesn't exist, print a single integer `-1`.
Otherwise, output your placement as N strings of N characters each. If a square is not covered, the corresponding character must be `.` (a dot). Otherwise, it must contain a lowercase English letter. Squares covered by the same domino piece must contain the same letter. If two squares have a common side but belong to different pieces, they must contain different letters.
Examples
Input
6
Output
aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b
Input
2
Output
-1 | instruction | 0 | 15,731 | 15 | 31,462 |
"Correct Solution:
```
n=int(input());f=["aabbc","hi..c","hi..d","g.jjd","gffee"];x=list("cdcd");a=[n*["."]for _ in range(n)];z={2:[-1],3:["abb","a.c","ddc"],5:["aabbc","hi..c","hi..d","g.jjd","gffee"],7:["..abc..","..abc..","aax..aa","bbx..bb","cc.yycc","..abc..","..abc.."]}
if n in z:print(*z[n]);exit()
if n%2:
for i in range(5):
for j in range(5):a[-i-1][-j-1]=f[i][j]
n-=5
for i in range(0,n,2):a[i][i],a[i][i+1],a[i+1][i],a[i+1][i+1]=list("aabb")
if n%4:
for i in range(0,n,2):a[i][(i+2)%n],a[i][(i+3)%n],a[i+1][(i+2)%n],a[i+1][(i+3)%n]=x
else:
for i in range(0,n,2):a[n-i-2][i],a[n-i-2][i+1],a[n-i-1][i],a[n-i-1][i+1]=x
for i in a:print("".join(i))
``` | output | 1 | 15,731 | 15 | 31,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. | instruction | 0 | 15,967 | 15 | 31,934 |
Tags: binary search, two pointers
Correct Solution:
```
from bisect import bisect_left
def readint():
return int(input())
def readline():
return [int(c) for c in input().split()]
# similar to 45311982
def main():
MAX = 10**9
n, m = readline()
v = sorted([readint() for _ in range(n)])
h = []
for _ in range(m):
x1, x2, _ = readline()
if x1 == 1:
h.append(x2)
h.sort()
lh = len(h)
if lh == 0:
print(0)
elif n == 0:
print(lh - bisect_left(h, MAX))
else:
mn = n + lh - bisect_left(h, MAX)
for i in range(n):
mn = min(mn, lh - bisect_left(h, v[i]) + i)
print(mn)
if __name__ == '__main__':
main()
``` | output | 1 | 15,967 | 15 | 31,935 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. | instruction | 0 | 15,968 | 15 | 31,936 |
Tags: binary search, two pointers
Correct Solution:
```
#n=int(input())
n,m=map(int,input().split())
vert=[]
for i in range(n):
v=int(input())
vert.append(v)
horz=[]
for i in range(m):
x1,x2,y=map(int,input().split())
if x1==1:
horz.append(x2)
vert.sort()
horz.sort()
vert.append(1000000000)
def next(k,a,x):
while k<len(a) and a[k]<x:
k+=1
return k
num=next(0,horz,vert[0])
ans=len(horz)-num
for i in range(1,len(vert)):
num2=next(num,horz,vert[i])
t=i+len(horz)-num2
if t<ans: ans=t
num=num2
print(ans)
``` | output | 1 | 15,968 | 15 | 31,937 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. | instruction | 0 | 15,969 | 15 | 31,938 |
Tags: binary search, two pointers
Correct Solution:
```
n, m = map(int, input().split())
ver = [int(input()) for _ in range(n)]
ver.append(10 ** 9)
hor = []
for _ in range(m):
x1, x2, y = map(int, input().split())
if x1 == 1:
hor.append(x2)
hor.sort()
ver.sort()
j = 0
ans = 10 ** 18
for i in range(n + 1):
while j < len(hor) and ver[i] > hor[j]:
j += 1
ans = min(ans, i + len(hor) - j)
if j == len(hor):
break
print(ans)
``` | output | 1 | 15,969 | 15 | 31,939 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. | instruction | 0 | 15,970 | 15 | 31,940 |
Tags: binary search, two pointers
Correct Solution:
```
n,m = map(int, input().split())
vertical_blocks = []
for i in range(n):
vertical_blocks.append(int(input()))
vertical_blocks.append(10**9)
horizontal_blocks = []
for i in range(m):
x1,x2,y = map(int, input().split())
if x1 == 1:
horizontal_blocks.append(x2)
vertical_blocks.sort()
horizontal_blocks.sort()
pointer,res = 0,n+m
for i in range(len(vertical_blocks)):
while pointer<len(horizontal_blocks) and horizontal_blocks[pointer]<vertical_blocks[i]:
pointer += 1
res = min(res,i+len(horizontal_blocks)-pointer)
print (res)
``` | output | 1 | 15,970 | 15 | 31,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. | instruction | 0 | 15,971 | 15 | 31,942 |
Tags: binary search, two pointers
Correct Solution:
```
n, m = map(int, input().split())
V = []
for i in range(n):
V.append(int(input()))
V.sort()
V.append(10 ** 9)
n += 1
X2 = []
for i in range(m):
x1, x2, y = map(int, input().split())
if x1 == 1:
X2.append(x2)
X2.sort()
k = len(X2)
i = 0
j = 0
ans = 10 ** 9 + 7
c = 0
while i < n:
while j < k:
if X2[j] < V[i]:
c += 1
j += 1
else:
break
ans = min(ans, k - c + i)
i += 1
print(ans)
``` | output | 1 | 15,971 | 15 | 31,943 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. | instruction | 0 | 15,972 | 15 | 31,944 |
Tags: binary search, two pointers
Correct Solution:
```
n,m=[int(x) for x in input().split()]
v=[]
h=[]
for i in range(n):
x=int(input())
v.append(x)
for i in range(m):
x,y,z=[int(x) for x in input().split()]
if x==1:
h.append(y)
h.sort()
v.sort()
m=len(h)
n=len(v)
if n==0 or v[n-1]!=1000000000:
v.append(1000000000)
n+=1
mina=9999999999999
j=0
for i in range(n):
while(j<m and h[j]<v[i]):
j+=1
#print(i+m-j)
mina=min(mina,i+m-j)
print(mina)
``` | output | 1 | 15,972 | 15 | 31,945 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. | instruction | 0 | 15,973 | 15 | 31,946 |
Tags: binary search, two pointers
Correct Solution:
```
from collections import namedtuple
import sys
HS = namedtuple('HS', 'x1 x2 y')
n, m = [int(w) for w in input().split()]
vs = [int(input()) for _ in range(n)]
hs = [HS(*[int(w) for w in input().split()]) for _ in range(m)]
vs.sort()
hr = len([s for s in hs if s.x1 == 1 and s.x2 == 10**9])
hs = [s.x2 for s in hs if s.x1 == 1 and s.x2 < 10**9]
hs.sort()
r = hc = len(hs)
hi = vi = 0
for hi in range(hc):
while vi < n and hs[hi] >= vs[vi]:
vi += 1
c = (hc - hi - 1) + vi
if c < r:
r = c
print(r + hr)
``` | output | 1 | 15,973 | 15 | 31,947 |
Provide tags and a correct Python 3 solution for this coding contest problem.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. | instruction | 0 | 15,974 | 15 | 31,948 |
Tags: binary search, two pointers
Correct Solution:
```
import bisect
n, m = map(int, input().split())
ar1 = [1] + [int(input()) for _ in range(n)]
ar1.append(10 ** 9)
ar1.sort()
ar2 = [list(map(int, input().split())) for _ in range(m)]
kek = list()
for x in ar2:
j1 = bisect.bisect_left(ar1, x[0])
j2 = bisect.bisect_right(ar1, x[1])
if x[0] == 1:
kek.append(j2)
res = [0] * (len(ar1) + 1)
res[0] = len(kek)
for x in kek:
res[x] -= 1
for i in range(1, len(res)):
res[i] += res[i - 1]
min_ = float('inf')
for i in range(1, len(res) - 1):
min_ = min(min_, res[i] + i - 1)
print(min_)
``` | output | 1 | 15,974 | 15 | 31,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.
Submitted Solution:
```
import sys
n,m=map(int,sys.stdin.readline().split())
X=[int(sys.stdin.readline()) for i in range(n)]
Y=[list(map(int,sys.stdin.readline().split())) for i in range(m)]
Z=[]
ANS=0
for y in Y:
if y[0]==1 and y[1]==10**9:
ANS+=1
elif y[0]==1:
Z.append(y[1])
X.sort(reverse=True)
Z.sort(reverse=True)
XCOUNT=[0]*n#X[i]より大きいZの個数
i=0
j=0
l=len(Z)
X.append(0)
Z.append(0)
while i<l+1 and j<n:
if Z[i]>=X[j]:
i+=1
else:
XCOUNT[j]=i
j+=1
count=n
XCOUNT.reverse()
for i in range(n):
if count>i+XCOUNT[i]:
count=i+XCOUNT[i]
print(count+ANS)
``` | instruction | 0 | 15,975 | 15 | 31,950 |
Yes | output | 1 | 15,975 | 15 | 31,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.
Submitted Solution:
```
m, n = map(int, input().split())
a = [int(input()) for q in range(m)]
a.append(10**9)
s = []
for q in range(n):
f, g, d = map(int, input().split())
if f == 1:
s.append(g)
a.sort()
s.sort()
q1 = 0
min1 = float('inf')
for q2 in range(len(a)):
while q1 < len(s) and a[q2] > s[q1]:
q1 += 1
if min1 > q2+len(s)-q1:
min1 = q2+len(s)-q1
if q1 == len(s):
break
print(min1)
``` | instruction | 0 | 15,976 | 15 | 31,952 |
Yes | output | 1 | 15,976 | 15 | 31,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.
Submitted Solution:
```
import sys
import math
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65
MAXRIGHT = 10 ** 9
def getInt():
return int(sys.stdin.readline().rstrip())
def getInts():
return map(int, sys.stdin.readline().rstrip().split(" "))
def getString():
return sys.stdin.readline().rstrip()
def printOutput(ans):
sys.stdout.write()
pass
def solve(v, h):
vertdict = []
h.sort()
v.sort()
hcur = 0
for vert in v:
while hcur < len(h) and h[hcur][1] < vert:
hcur += 1
vertdict.append(len(h) - hcur)
minmoves = vertdict[0]
for i in range(len(vertdict)):
minmoves = min(minmoves, vertdict[i] + i)
return minmoves
def readinput():
v, h = getInts()
vert = []
horiz = []
for _ in range(v):
vert.append(getInt())
vert.append(MAXRIGHT)
for _ in range(h):
x1, x2, y = getInts()
if x1 == 1:
horiz.append((x1, x2, y))
print(solve(vert, horiz))
readinput()
``` | instruction | 0 | 15,977 | 15 | 31,954 |
Yes | output | 1 | 15,977 | 15 | 31,955 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.
Submitted Solution:
```
from bisect import bisect
n, m = map(int, input().split())
vv = sorted([int(input()) for _ in range(n)])
hh = [0] * n
rr = 0
for _ in range(m):
one, x, _ = map(int, input().split())
if one == 1:
if x == 1000000000:
rr += 1
else:
ind = bisect(vv, x)
if ind:
hh[ind-1] += 1
r = n
s = 0
#print(*vv)
#print(*hh)
for i, h in reversed(list(enumerate(hh))):
s += h
#print("~", r, s)
r = min(r, s+i)
print(r+rr)
``` | instruction | 0 | 15,978 | 15 | 31,956 |
Yes | output | 1 | 15,978 | 15 | 31,957 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.
Submitted Solution:
```
import bisect
n, m = map(int, input().split())
ar1 = [1] + [int(input()) for _ in range(n)]
ar1.append(10 ** 9)
ar2 = [list(map(int, input().split())) for _ in range(m)]
kek = list()
for x in ar2:
j1 = bisect.bisect_left(ar1, x[0])
j2 = bisect.bisect_right(ar1, x[1])
if j2 - j1 > 1 and j1 == 0:
kek.append(j2)
res = [0] * (len(ar1) + 1)
res[0] = len(kek)
for x in kek:
res[x] -= 1
for i in range(1, len(res)):
res[i] += res[i - 1]
min_ = float('inf')
for i in range(1, len(res) - 1):
min_ = min(min_, res[i] + i - 1)
print(min_)
``` | instruction | 0 | 15,979 | 15 | 31,958 |
No | output | 1 | 15,979 | 15 | 31,959 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.
Submitted Solution:
```
n,m=[int(x) for x in input().split()]
v=[]
h=[]
for i in range(n):
x=int(input())
v.append(x)
for i in range(m):
x,y,z=[int(x) for x in input().split()]
if x==1:
h.append(y)
m=len(h)
n=len(v)
h.sort()
v.sort()
if m==0:
print(0)
exit()
mina=9999999999999
j=0
for i in range(m):
while(j<n and h[i]<v[j]):
j+=1
#print(i+m-j)
mina=min(mina,i+n-j)
print(mina)
``` | instruction | 0 | 15,980 | 15 | 31,960 |
No | output | 1 | 15,980 | 15 | 31,961 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.
Submitted Solution:
```
n,m = map(int,input().split())
x = [0]*(n+1)
for i in range(n):
x[i] = int(input())
x[n] = 1000000000
vert = []
for i in range(m):
x1,x2,y = map(int,input().split())
if x1 == 1:
vert.append(x2)
vert.sort()
cur = 0
minicount = 100000000
k = len(vert)
for i in range(n+1):
while cur < k:
if x[i] <= vert[cur]:
break
cur += 1
minicount = min(minicount,k-cur+i)
print(minicount)
``` | instruction | 0 | 15,981 | 15 | 31,962 |
No | output | 1 | 15,981 | 15 | 31,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, for each cell of the chessboard you can assign the coordinates (x,y), where x is the column number and y is the row number.
Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates (1,1). But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number 10^9).
Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
* Vertical. Each of these is defined by one number x. Such spells create an infinite blocking line between the columns x and x+1.
* Horizontal. Each of these is defined by three numbers x_1, x_2, y. Such spells create a blocking segment that passes through the top side of the cells, which are in the row y and in columns from x_1 to x_2 inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
<image> An example of a chessboard.
Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell (r_0,c_0) into the cell (r_1,c_1) only under the condition that r_1 = r_0 or c_1 = c_0 and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
Input
The first line contains two integers n and m (0 ≤ n,m ≤ 10^5) — the number of vertical and horizontal spells.
Each of the following n lines contains one integer x (1 ≤ x < 10^9) — the description of the vertical spell. It will create a blocking line between the columns of x and x+1.
Each of the following m lines contains three integers x_1, x_2 and y (1 ≤ x_{1} ≤ x_{2} ≤ 10^9, 1 ≤ y < 10^9) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number y, in columns from x_1 to x_2 inclusive.
It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
Output
In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell (1,1) to at least one cell in the row with the number 10^9
Examples
Input
2 3
6
8
1 5 6
1 9 4
2 4 2
Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6
Output
1
Input
0 2
1 1000000000 4
1 1000000000 2
Output
2
Input
0 0
Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5
Output
2
Note
In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
<image> Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home.
In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
<image> Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home.
In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
<image> Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home.
In the fourth sample, we have no spells, which means that we do not need to remove anything.
In the fifth example, we can remove the first vertical and third horizontal spells.
<image> Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.
Submitted Solution:
```
#!/usr/bin/env python3
import sys
v, h = list(map(lambda x: int(x), sys.stdin.readline().split(' ')))
vs = []
hs = []
for i in range(v):
vs.append(int(sys.stdin.readline()))
for i in range(h):
hs.append(list(map(lambda x: int(x), sys.stdin.readline().split(' '))))
def sort_y(val):
return val[2]
hs.sort(key=sort_y)
hsl = len(hs)
vsl = len(vs)
def find_path(hi, vi, c):
if hi == hsl:
return c
if vi < vsl:
if hs[hi][0] == 1 and hs[hi][1] >= vs[vi]:
ch = find_path(hi + 1, vi, c + 1)
cv = find_path(hi, vi + 1, c + 1)
return min(ch, cv)
else:
return find_path(hi + 1, vi, c)
else:
return find_path(hi + 1, vi, c + 1 if hs[0][0] == 1 and hs[0][1] == 1000000000 else c)
if v == 100000:
print(hsl)
else:
print(find_path(0, 0, 0))
``` | instruction | 0 | 15,982 | 15 | 31,964 |
No | output | 1 | 15,982 | 15 | 31,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC. | instruction | 0 | 16,146 | 15 | 32,292 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = [input() for i in range(n)]
ans = 0
for i in range(n):
if a[i][m-1] == 'R':
ans += 1
ans += a[-1].count("D")
print(ans)
``` | output | 1 | 16,146 | 15 | 32,293 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC. | instruction | 0 | 16,147 | 15 | 32,294 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t=int(input())
for i in range(t):
n,m=map(int,input().split())
a=[]
for j in range(n):
b=input()
a.append(b)
if j==n-1:
d=b.count('D')
c=[a[j][m-1] for j in range(n)]
r=c.count("R")
print(r+d)
``` | output | 1 | 16,147 | 15 | 32,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC. | instruction | 0 | 16,148 | 15 | 32,296 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
n,m = Ri()
lis = []
for i in range(n):
lis.append(ri())
cnt = 0
for i in range(0, n-1):
if lis[i][m-1] == 'R':
cnt+=1
for i in range(0, m-1):
if lis[n-1][i] == 'D':
cnt+=1
print(cnt)
``` | output | 1 | 16,148 | 15 | 32,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC. | instruction | 0 | 16,149 | 15 | 32,298 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import math
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
cnt=0
for i in range(n):
a=input()
if(i!=n-1):
if(a[-1]!="D"):
cnt+=1
else:
cnt+=a.count("D")
#print(cnt)
print(cnt)
``` | output | 1 | 16,149 | 15 | 32,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC. | instruction | 0 | 16,150 | 15 | 32,300 |
Tags: brute force, greedy, implementation
Correct Solution:
```
from collections import Counter
def solve(n,m) :
step=0
for i in range(n):
st=input()
if(st[m-1]=='R'):
step +=1
if(i==(n-1)):
for j in range(m):
if(st[j]=='D'):
step +=1
return step
if __name__ == "__main__":
t=int(input())
ans=[]
for i in range (t):
[n,m]=list(map(int,input().split()))
ans.append(solve(n,m))
print(*ans,sep="\n")
``` | output | 1 | 16,150 | 15 | 32,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC. | instruction | 0 | 16,151 | 15 | 32,302 |
Tags: brute force, greedy, implementation
Correct Solution:
```
# -*- coding: utf-8 -*-
#hamza = payaso
for _ in range(int(input())):
n,m = map(int,input().split())
cont = 0
for i in range(n):
s = input()
if s[m-1] == 'R': cont += 1
cont += s.count('D')
print(cont)
``` | output | 1 | 16,151 | 15 | 32,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC. | instruction | 0 | 16,152 | 15 | 32,304 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t=int(input())
for q in range(0,t):
n,m=map(int,input().split())
a=[]
for i in range(0,n):
b=input()
b=list(b)
a.append(b)
count=0
for i in range(0,n-1):
if a[i][m-1]=='R':
count+=1
for i in range(0,m-1):
if a[n-1][i]=='D':
count+=1
print(count)
``` | output | 1 | 16,152 | 15 | 32,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC. | instruction | 0 | 16,153 | 15 | 32,306 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t = int(input())
for q in range(t):
n, m = map(int, input().split())
matr = []
for i in range(n):
matr.append(input())
kol = 0
for j in range(m - 1):
if matr[n - 1][j] != "R":
kol += 1
for i in range(n - 1):
if matr[i][m - 1] != "D":
kol += 1
print(kol)
``` | output | 1 | 16,153 | 15 | 32,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC.
Submitted Solution:
```
for i in range(int(input())):
n,m = map(int,input().split())
a = []
ans=0
for i in range(n):
s = input()
a.append(list(s))
for j in range(n):
if j == n-1:
r = a[j].count('D')
ans+=r
else:
if a[j][-1]=='R':
ans+=1
print(ans)
``` | instruction | 0 | 16,154 | 15 | 32,308 |
Yes | output | 1 | 16,154 | 15 | 32,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC.
Submitted Solution:
```
for ii in range(int(input())):
n, m = map(int, input().split())
ans=0
for jj in range(n):
x=input()
if x[-1]=='R':
ans+=1
print(ans+x.count('D'))
``` | instruction | 0 | 16,155 | 15 | 32,310 |
Yes | output | 1 | 16,155 | 15 | 32,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC.
Submitted Solution:
```
from sys import stdin
ip = stdin.readline
t= int(ip())
while t:
t-=1
n,m = map(int, ip().split())
a = []
for _ in range(n):
a.append(list(ip()))
ans = 0
for i in range(n-1):
if not a[i][m-1]=='D':
ans+=1
for i in range(m-1):
if not a[n-1][i]=='R':
ans+=1
print(ans)
``` | instruction | 0 | 16,156 | 15 | 32,312 |
Yes | output | 1 | 16,156 | 15 | 32,313 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC.
Submitted Solution:
```
t = int(input())
for f in range(t):
b = []
c=[]
n,m = input().split()
zamen =0
for i in range(int(n)):
a = str(input())
if i == int(n)-1:
for j in range(int(m) - 1):
c.append(a[j])
b.append(a[int(m)-1])
for elem in b:
if elem =='R':
zamen +=1
for elem in c:
if elem =='D':
zamen +=1
print(zamen)
``` | instruction | 0 | 16,157 | 15 | 32,314 |
Yes | output | 1 | 16,157 | 15 | 32,315 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC.
Submitted Solution:
```
def solve(n, m, arr):
cnt = 0
for i in range(n - 1):
cnt = arr[i].count('R')
print(cnt + arr[n - 1].count('D'))
t = int(input())
while t != 0:
n1, m1 = map(int, input().split(' '))
s = []
for i in range(n1):
s.append(input())
solve(n1, m1, s)
t -= 1
``` | instruction | 0 | 16,158 | 15 | 32,316 |
No | output | 1 | 16,158 | 15 | 32,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC.
Submitted Solution:
```
n = 4
f = 1
mod = 10**9 + 7
for i in range(2, n+1):
f *= i
f %= mod
f = (f - pow(2, n-1, mod) + mod) % mod
print(f)
``` | instruction | 0 | 16,159 | 15 | 32,318 |
No | output | 1 | 16,159 | 15 | 32,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC.
Submitted Solution:
```
# When in doubt, collections.Counter()
# input()
# int(input())
# input().split()
# list(map(int, input().split()))
# for _ in range(t):
import math
import collections
import bisect
def arrPrint(a):
return " ".join([str(i) for i in a])
def gridPrint(a):
return "\n".join([" ".join([str(j) for j in a[i]]) for i in range(len(a))])
def isPalindrome(s):
for i in range(len(s)//2):
if not s[i] == s[-i-1]:
return False
return True
def primeCheck(n):
for i in range(2, int(math.sqrt(n))+1):
if n%i == 0:
return False
return True
def solve(n, m, grid):
ans = 0
for i in range(n-1):
if not grid[i][-1] == "D":
ans += 1
for i in range(m-1):
if not grid[-1][i] == "R":
ans += 1
return ans
t = int(input())
grid = []
for _ in range(t):
n, m = list(map(int, input().split()))
for _ in range(n):
grid.append(input())
print(solve(n, m, grid))
``` | instruction | 0 | 16,160 | 15 | 32,320 |
No | output | 1 | 16,160 | 15 | 32,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a conveyor belt represented using a grid consisting of n rows and m columns. The cell in the i-th row from the top and the j-th column from the left is labelled (i,j).
Every cell, except (n,m), has a direction R (Right) or D (Down) assigned to it. If the cell (i,j) is assigned direction R, any luggage kept on that will move to the cell (i,j+1). Similarly, if the cell (i,j) is assigned direction D, any luggage kept on that will move to the cell (i+1,j). If at any moment, the luggage moves out of the grid, it is considered to be lost.
There is a counter at the cell (n,m) from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell (i,j), any luggage placed in this cell should eventually end up in the cell (n,m).
This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.
Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
The following n lines each contain m characters. The j-th character in the i-th line, a_{i,j} is the initial direction of the cell (i, j). Please note that a_{n,m}= C.
Output
For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.
Example
Input
4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C
Output
1
3
9
0
Note
In the first case, just changing the direction of (2,3) to D is enough.
You can verify that the resulting belt is functional. For example, if we place any luggage at (2,2), it first moves to (3,2) and then to (3,3).
In the second case, we have no option but to change the first 3 cells from D to R making the grid equal to RRRC.
Submitted Solution:
```
for _ in range(int(input())):
a,b = map(int,input().split())
res = []
c = 0
for i in range(a):
l = input().split()
res.append(l)
if i==a-1:
for k in l:
if k=='D':
c+=1
else:
if l[-1]=='R':
c+=1
print(c)
``` | instruction | 0 | 16,161 | 15 | 32,322 |
No | output | 1 | 16,161 | 15 | 32,323 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given four different integer points p_1, p_2, p_3 and p_4 on XY grid.
In one step you can choose one of the points p_i and move it in one of four directions by one. In other words, if you have chosen point p_i = (x, y) you can move it to (x, y + 1), (x, y - 1), (x + 1, y) or (x - 1, y).
Your goal to move points in such a way that they will form a square with sides parallel to OX and OY axes (a square with side 0 is allowed).
What is the minimum number of steps you need to make such a square?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
Each test case consists of four lines. Each line contains two integers x and y (0 ≤ x, y ≤ 10^9) — coordinates of one of the points p_i = (x, y).
All points are different in one test case.
Output
For each test case, print the single integer — the minimum number of steps to make a square.
Example
Input
3
0 2
4 2
2 0
2 4
1 0
2 0
4 0
6 0
1 6
2 2
2 5
4 1
Output
8
7
5
Note
In the first test case, one of the optimal solutions is shown below:
<image> Each point was moved two times, so the answer 2 + 2 + 2 + 2 = 8.
In the second test case, one of the optimal solutions is shown below:
<image> The answer is 3 + 1 + 0 + 3 = 7.
In the third test case, one of the optimal solutions is shown below:
<image> The answer is 1 + 1 + 2 + 1 = 5. | instruction | 0 | 16,178 | 15 | 32,356 |
Tags: brute force, constructive algorithms, flows, geometry, greedy, implementation, math, ternary search
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
from itertools import permutations
def prog():
for _ in range(int(input())):
ans = 10**10
points = []
for i in range(4):
x,y = map(int,input().split())
points.append([x,y])
for perm in permutations([0,1,2,3]):
if (points[perm[0]][0] > points[perm[1]][0]) or \
(points[perm[2]][0] > points[perm[3]][0]) or \
(points[perm[0]][1] > points[perm[2]][1]) or \
(points[perm[1]][1] > points[perm[3]][1]):
continue
amt_to_rectangle = abs(points[perm[1]][1] - points[perm[0]][1]) + \
abs(points[perm[3]][1] - points[perm[2]][1]) + \
abs(points[perm[2]][0] - points[perm[0]][0]) + \
abs(points[perm[3]][0] - points[perm[1]][0])
wmax = max(points[perm[2]][1],points[perm[3]][1]) - \
min(points[perm[0]][1],points[perm[1]][1])
lmax = max(points[perm[1]][0],points[perm[3]][0]) - \
min(points[perm[0]][0],points[perm[2]][0])
wmin = max(0, min(points[perm[2]][1],points[perm[3]][1]) - \
max(points[perm[0]][1],points[perm[1]][1]))
lmin = max(0, min(points[perm[1]][0],points[perm[3]][0]) - \
max(points[perm[0]][0],points[perm[2]][0]))
if lmin > wmax:
ans = min(ans, amt_to_rectangle + 2*(lmin-wmax))
elif wmin > lmax:
ans = min(ans, amt_to_rectangle + 2*(wmin-lmax))
else:
ans = min(ans,amt_to_rectangle)
print(ans)
prog()
``` | output | 1 | 16,178 | 15 | 32,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given four different integer points p_1, p_2, p_3 and p_4 on XY grid.
In one step you can choose one of the points p_i and move it in one of four directions by one. In other words, if you have chosen point p_i = (x, y) you can move it to (x, y + 1), (x, y - 1), (x + 1, y) or (x - 1, y).
Your goal to move points in such a way that they will form a square with sides parallel to OX and OY axes (a square with side 0 is allowed).
What is the minimum number of steps you need to make such a square?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
Each test case consists of four lines. Each line contains two integers x and y (0 ≤ x, y ≤ 10^9) — coordinates of one of the points p_i = (x, y).
All points are different in one test case.
Output
For each test case, print the single integer — the minimum number of steps to make a square.
Example
Input
3
0 2
4 2
2 0
2 4
1 0
2 0
4 0
6 0
1 6
2 2
2 5
4 1
Output
8
7
5
Note
In the first test case, one of the optimal solutions is shown below:
<image> Each point was moved two times, so the answer 2 + 2 + 2 + 2 = 8.
In the second test case, one of the optimal solutions is shown below:
<image> The answer is 3 + 1 + 0 + 3 = 7.
In the third test case, one of the optimal solutions is shown below:
<image> The answer is 1 + 1 + 2 + 1 = 5. | instruction | 0 | 16,179 | 15 | 32,358 |
Tags: brute force, constructive algorithms, flows, geometry, greedy, implementation, math, ternary search
Correct Solution:
```
def read_int():
return int(input())
def read_ints():
return map(int, input().split(' '))
def solve(x, y):
xcost = x[1] - x[0] + x[3] - x[2]
ycost = y[1] - y[0] + y[3] - y[2]
xlo = max(0, x[2] - x[1])
xhi = x[3] - x[0]
ylo = max(0, y[2] - y[1])
yhi = y[3] - y[0]
ans = xcost + ycost
if ylo > xhi:
ans += 2 * (ylo - xhi)
elif xlo > yhi:
ans += 2 * (xlo - yhi)
return ans
t = read_int()
for case_num in range(t):
x = []
y = []
for i in range(4):
xi, yi = read_ints()
x.append((xi, i))
y.append((yi, i))
x.sort()
y.sort()
xl = set([x[0][1], x[1][1]])
yl = set([y[0][1], y[1][1]])
ans = int(1e18)
if len(xl.intersection(yl)) != 1:
ans = min(solve([x[0][0], x[2][0], x[1][0], x[3][0]],
[y[0][0], y[1][0], y[2][0], y[3][0]]), solve([x[0][0], x[1][0], x[2][0], x[3][0]],
[y[0][0], y[2][0], y[1][0], y[3][0]]))
else:
ans = solve([x[0][0], x[1][0], x[2][0], x[3][0]],
[y[0][0], y[1][0], y[2][0], y[3][0]])
print(ans)
``` | output | 1 | 16,179 | 15 | 32,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given four different integer points p_1, p_2, p_3 and p_4 on XY grid.
In one step you can choose one of the points p_i and move it in one of four directions by one. In other words, if you have chosen point p_i = (x, y) you can move it to (x, y + 1), (x, y - 1), (x + 1, y) or (x - 1, y).
Your goal to move points in such a way that they will form a square with sides parallel to OX and OY axes (a square with side 0 is allowed).
What is the minimum number of steps you need to make such a square?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
Each test case consists of four lines. Each line contains two integers x and y (0 ≤ x, y ≤ 10^9) — coordinates of one of the points p_i = (x, y).
All points are different in one test case.
Output
For each test case, print the single integer — the minimum number of steps to make a square.
Example
Input
3
0 2
4 2
2 0
2 4
1 0
2 0
4 0
6 0
1 6
2 2
2 5
4 1
Output
8
7
5
Note
In the first test case, one of the optimal solutions is shown below:
<image> Each point was moved two times, so the answer 2 + 2 + 2 + 2 = 8.
In the second test case, one of the optimal solutions is shown below:
<image> The answer is 3 + 1 + 0 + 3 = 7.
In the third test case, one of the optimal solutions is shown below:
<image> The answer is 1 + 1 + 2 + 1 = 5. | instruction | 0 | 16,180 | 15 | 32,360 |
Tags: brute force, constructive algorithms, flows, geometry, greedy, implementation, math, ternary search
Correct Solution:
```
def move(ps):
for i in range(3):
for j in range(i+1,4):
if ps[j] < ps[i]:
x = ps[i]
ps[i] = ps[j]
ps[j] = x
ps1 = ps[:]
if ps[0][1] > ps[1][1]:
x = ps[0]
ps[0] = ps[1]
ps[1] = x
if ps[2][1] > ps[3][1]:
x = ps[2]
ps[2] = ps[3]
ps[3] = x
dx1 = abs(ps[1][0]-ps[0][0])
dx2 = abs(ps[3][0]-ps[2][0])
dx_max = max(ps[3][0],ps[2][0])-min(ps[0][0],ps[1][0])
dx_min = max(0, min(ps[3][0],ps[2][0])-max(ps[0][0],ps[1][0]))
dy1 = abs(ps[3][1]-ps[1][1])
dy2 = abs(ps[2][1]-ps[0][1])
dy_max = max(ps[3][1],ps[1][1])-min(ps[2][1], ps[0][1])
dy_min = max(0, min(ps[3][1],ps[1][1])-max(ps[2][1], ps[0][1]))
move1 = dx1 + dx2 + dy1 + dy2 + 2 * max(0, max(dy_min, dx_min)-min(dx_max, dy_max))
ps = ps1
x = ps[1]
ps[1] = ps[2]
ps[2] = x
if ps[0][1] > ps[1][1]:
x = ps[0]
ps[0] = ps[1]
ps[1] = x
if ps[2][1] > ps[3][1]:
x = ps[2]
ps[2] = ps[3]
ps[3] = x
dx1 = abs(ps[1][0]-ps[0][0])
dx2 = abs(ps[3][0]-ps[2][0])
dx_max = max(ps[3][0],ps[2][0])-min(ps[0][0],ps[1][0])
dx_min = max(0, min(ps[3][0],ps[2][0])-max(ps[0][0],ps[1][0]))
dy1 = abs(ps[3][1]-ps[1][1])
dy2 = abs(ps[2][1]-ps[0][1])
dy_max = max(ps[3][1],ps[1][1])-min(ps[2][1], ps[0][1])
dy_min = max(0, min(ps[3][1],ps[1][1])-max(ps[2][1], ps[0][1]))
move2 = dx1 + dx2 + dy1 + dy2 + 2 * max(0, max(dy_min, dx_min)-min(dx_max, dy_max))
return min(move1, move2)
for i in range(int(input())):
ps = []
for j in range(4):
x,y = map(int, input().split())
ps.append([x,y])
steps = str(int(move(ps)))
print(steps)
``` | output | 1 | 16,180 | 15 | 32,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given four different integer points p_1, p_2, p_3 and p_4 on XY grid.
In one step you can choose one of the points p_i and move it in one of four directions by one. In other words, if you have chosen point p_i = (x, y) you can move it to (x, y + 1), (x, y - 1), (x + 1, y) or (x - 1, y).
Your goal to move points in such a way that they will form a square with sides parallel to OX and OY axes (a square with side 0 is allowed).
What is the minimum number of steps you need to make such a square?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
Each test case consists of four lines. Each line contains two integers x and y (0 ≤ x, y ≤ 10^9) — coordinates of one of the points p_i = (x, y).
All points are different in one test case.
Output
For each test case, print the single integer — the minimum number of steps to make a square.
Example
Input
3
0 2
4 2
2 0
2 4
1 0
2 0
4 0
6 0
1 6
2 2
2 5
4 1
Output
8
7
5
Note
In the first test case, one of the optimal solutions is shown below:
<image> Each point was moved two times, so the answer 2 + 2 + 2 + 2 = 8.
In the second test case, one of the optimal solutions is shown below:
<image> The answer is 3 + 1 + 0 + 3 = 7.
In the third test case, one of the optimal solutions is shown below:
<image> The answer is 1 + 1 + 2 + 1 = 5. | instruction | 0 | 16,181 | 15 | 32,362 |
Tags: brute force, constructive algorithms, flows, geometry, greedy, implementation, math, ternary search
Correct Solution:
```
import sys;input=sys.stdin.readline
from itertools import permutations
def fx(a, b, c, d):
return min(b, c) - max(a, d), max(b, c) - min(a, d), abs(b-c)+abs(a-d)
T, = map(int, input().split())
for _ in range(T):
X = []
for _ in range(4):
x, y = map(int, input().split())
X.append((x, y))
R = 10**18
for pp in permutations(list(range(4)), 4):
(ax, ay), (bx, by), (cx, cy), (dx, dy) = [X[p] for p in pp]
k1, k2, ll = fx(ax, bx, cx, dx)
k3, k4, llb = fx(ay, cy, dy, by)
for k in [k1, k2, k3, k4, 0]:
if k < 0:
continue
xx = ll + 2*max(k-k2, k1-k, 0)
yy = llb + 2*max(k-k4, k3-k, 0)
R = min(R, xx+yy)
print(R)
``` | output | 1 | 16,181 | 15 | 32,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given four different integer points p_1, p_2, p_3 and p_4 on XY grid.
In one step you can choose one of the points p_i and move it in one of four directions by one. In other words, if you have chosen point p_i = (x, y) you can move it to (x, y + 1), (x, y - 1), (x + 1, y) or (x - 1, y).
Your goal to move points in such a way that they will form a square with sides parallel to OX and OY axes (a square with side 0 is allowed).
What is the minimum number of steps you need to make such a square?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
Each test case consists of four lines. Each line contains two integers x and y (0 ≤ x, y ≤ 10^9) — coordinates of one of the points p_i = (x, y).
All points are different in one test case.
Output
For each test case, print the single integer — the minimum number of steps to make a square.
Example
Input
3
0 2
4 2
2 0
2 4
1 0
2 0
4 0
6 0
1 6
2 2
2 5
4 1
Output
8
7
5
Note
In the first test case, one of the optimal solutions is shown below:
<image> Each point was moved two times, so the answer 2 + 2 + 2 + 2 = 8.
In the second test case, one of the optimal solutions is shown below:
<image> The answer is 3 + 1 + 0 + 3 = 7.
In the third test case, one of the optimal solutions is shown below:
<image> The answer is 1 + 1 + 2 + 1 = 5. | instruction | 0 | 16,182 | 15 | 32,364 |
Tags: brute force, constructive algorithms, flows, geometry, greedy, implementation, math, ternary search
Correct Solution:
```
T = int(input());ans = [0]*T
for t in range(T):
X,Y = [0]*4,[0]*4;A = [0]*4
for i in range(4):X[i],Y[i] = map(int, input().split());A[i] = [X[i], Y[i]]
X.sort(); Y.sort(); A.sort();cnt = 0
for i in range(2):
rank = 1
for j in range(4):
if A[i][1] < A[j][1]:rank += 1
if rank<=2:cnt += 1
if cnt!=1:ans[t] += min(Y[2]-Y[1], X[2]-X[1])*2
x_min = X[2]-X[1]; x_max = X[3]-X[0];y_min = Y[2]-Y[1]; y_max = Y[3]-Y[0]
if x_max<y_min:ans[t] += (X[3]-X[2])+(X[1]-X[0]);ans[t] += (Y[3]-Y[2])+(Y[1]-Y[0])+2*(y_min-x_max)
elif y_max<x_min:ans[t] += (X[3]-X[2])+(X[1]-X[0])+2*(x_min-y_max);ans[t] += (Y[3]-Y[2])+(Y[1]-Y[0])
else:ans[t] += (X[3]-X[2])+(X[1]-X[0]);ans[t] += (Y[3]-Y[2])+(Y[1]-Y[0])
print(*ans, sep='\n')
``` | output | 1 | 16,182 | 15 | 32,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given four different integer points p_1, p_2, p_3 and p_4 on XY grid.
In one step you can choose one of the points p_i and move it in one of four directions by one. In other words, if you have chosen point p_i = (x, y) you can move it to (x, y + 1), (x, y - 1), (x + 1, y) or (x - 1, y).
Your goal to move points in such a way that they will form a square with sides parallel to OX and OY axes (a square with side 0 is allowed).
What is the minimum number of steps you need to make such a square?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
Each test case consists of four lines. Each line contains two integers x and y (0 ≤ x, y ≤ 10^9) — coordinates of one of the points p_i = (x, y).
All points are different in one test case.
Output
For each test case, print the single integer — the minimum number of steps to make a square.
Example
Input
3
0 2
4 2
2 0
2 4
1 0
2 0
4 0
6 0
1 6
2 2
2 5
4 1
Output
8
7
5
Note
In the first test case, one of the optimal solutions is shown below:
<image> Each point was moved two times, so the answer 2 + 2 + 2 + 2 = 8.
In the second test case, one of the optimal solutions is shown below:
<image> The answer is 3 + 1 + 0 + 3 = 7.
In the third test case, one of the optimal solutions is shown below:
<image> The answer is 1 + 1 + 2 + 1 = 5. | instruction | 0 | 16,183 | 15 | 32,366 |
Tags: brute force, constructive algorithms, flows, geometry, greedy, implementation, math, ternary search
Correct Solution:
```
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def main():
from itertools import permutations
t = int(input())
ans = ['0'] * t
def calc(points, x1, y1, x2, y2):
return (
abs(points[0][0] - x1) + abs(points[0][1] - y1)
+ abs(points[1][0] - x2) + abs(points[1][1] - y1)
+ abs(points[2][0] - x1) + abs(points[2][1] - y2)
+ abs(points[3][0] - x2) + abs(points[3][1] - y2)
)
eps = 1e-8
for ti in range(t):
points = [tuple(map(float, input().split())) for _ in range(4)]
x_set = sorted(set(x for x, _ in points))
y_set = sorted(set(y for _, y in points))
perm = tuple(permutations(points))
res = min(
calc(points, points[i][0], points[i][1], points[i][0], points[i][1])
for i in range(4)
)
for pts in perm:
for i, x1 in enumerate(x_set):
for x2 in x_set[i + 1:]:
cost = abs(pts[0][0] - x1) + abs(pts[1][0] - x2) + abs(pts[2][0] - x1) + abs(pts[3][0] - x2)
if cost >= res:
continue
for y1 in y_set:
for y2 in (y1 - (x2 - x1), y1 + (x2 - x1)):
res = min(
res,
cost + abs(pts[0][1] - y1) + abs(pts[1][1] - y1) + abs(pts[2][1] - y2) + abs(pts[3][1] - y2)
)
for i, y1 in enumerate(y_set):
for y2 in y_set[i + 1:]:
cost = abs(pts[0][1] - y1) + abs(pts[1][1] - y2) + abs(pts[2][1] - y1) + abs(pts[3][1] - y2)
if cost >= res:
continue
for x1 in x_set:
for x2 in (x1 - (y2 - y1), x1 + (y2 - y1)):
res = min(
res,
cost + abs(pts[0][0] - x1) + abs(pts[1][0] - x1) + abs(pts[2][0] - x2) + abs(pts[3][0] - x2)
)
ans[ti] = str(int(res + eps))
sys.stdout.buffer.write(('\n'.join(ans) + '\n').encode('utf-8'))
if __name__ == '__main__':
main()
``` | output | 1 | 16,183 | 15 | 32,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given four different integer points p_1, p_2, p_3 and p_4 on XY grid.
In one step you can choose one of the points p_i and move it in one of four directions by one. In other words, if you have chosen point p_i = (x, y) you can move it to (x, y + 1), (x, y - 1), (x + 1, y) or (x - 1, y).
Your goal to move points in such a way that they will form a square with sides parallel to OX and OY axes (a square with side 0 is allowed).
What is the minimum number of steps you need to make such a square?
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
Each test case consists of four lines. Each line contains two integers x and y (0 ≤ x, y ≤ 10^9) — coordinates of one of the points p_i = (x, y).
All points are different in one test case.
Output
For each test case, print the single integer — the minimum number of steps to make a square.
Example
Input
3
0 2
4 2
2 0
2 4
1 0
2 0
4 0
6 0
1 6
2 2
2 5
4 1
Output
8
7
5
Note
In the first test case, one of the optimal solutions is shown below:
<image> Each point was moved two times, so the answer 2 + 2 + 2 + 2 = 8.
In the second test case, one of the optimal solutions is shown below:
<image> The answer is 3 + 1 + 0 + 3 = 7.
In the third test case, one of the optimal solutions is shown below:
<image> The answer is 1 + 1 + 2 + 1 = 5. | instruction | 0 | 16,184 | 15 | 32,368 |
Tags: brute force, constructive algorithms, flows, geometry, greedy, implementation, math, ternary search
Correct Solution:
```
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
def cal(xy):
res=inf
for _ in range(2):
xy.sort()
for i in [0, 2]:
if xy[i][1] > xy[i+1][1]: xy[i], xy[i+1] = xy[i+1], xy[i]
xmax = max(xy[2][0], xy[3][0])-min(xy[1][0], xy[0][0])
xmin = min(xy[2][0], xy[3][0])-max(xy[1][0], xy[0][0])
ymax = max(xy[1][1], xy[3][1])-min(xy[0][1], xy[2][1])
ymin = min(xy[1][1], xy[3][1])-max(xy[0][1], xy[2][1])
cur = (max(max(ymin,xmin)-min(xmax,ymax), 0)*2
+abs(xy[0][0]-xy[1][0])
+abs(xy[2][0]-xy[3][0])
+abs(xy[0][1]-xy[2][1])
+abs(xy[1][1]-xy[3][1]))
# print(cur,xmin,xmax,ymin,ymax,xy,cur)
res=min(res,cur)
xy=[[y,x] for x,y in xy]
return res
for t in range(II()):
xy = LLI(4)
ans = cal(xy)
print(ans)
``` | output | 1 | 16,184 | 15 | 32,369 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.