message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.
The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).
The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).
Output
Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.
Examples
Input
5 4
1 3 5 7 9
6 4 2 8
Output
3 2 1 4
Input
5 5
1 2 1 2 5
3 1 4 1 5
Output
4 2 4 2 5 | instruction | 0 | 12,912 | 12 | 25,824 |
Tags: binary search, data structures, sortings, two pointers
Correct Solution:
```
from bisect import bisect_right
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
b = list(map(int, input().split()))
def f(k):
lo = 0
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if k < a[mid]:
hi = mid
else:
lo = mid + 1
return lo
x = []
for i in b:
x.append(f(i))
print(*x)
``` | output | 1 | 12,912 | 12 | 25,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.
The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).
The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).
Output
Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.
Examples
Input
5 4
1 3 5 7 9
6 4 2 8
Output
3 2 1 4
Input
5 5
1 2 1 2 5
3 1 4 1 5
Output
4 2 4 2 5 | instruction | 0 | 12,913 | 12 | 25,826 |
Tags: binary search, data structures, sortings, two pointers
Correct Solution:
```
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b_index = []
for i in range(len(b)):
b_index.append([b[i], i])
b_index.sort()
ans = [0]*len(b)
ptr = 0
for i in range(len(b)):
while ptr < n and a[ptr] <= b_index[i][0]:
ptr += 1
ans[b_index[i][1]] = ptr
for a in ans:
print(a, end=" ")
``` | output | 1 | 12,913 | 12 | 25,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.
The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).
The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).
Output
Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.
Examples
Input
5 4
1 3 5 7 9
6 4 2 8
Output
3 2 1 4
Input
5 5
1 2 1 2 5
3 1 4 1 5
Output
4 2 4 2 5 | instruction | 0 | 12,914 | 12 | 25,828 |
Tags: binary search, data structures, sortings, two pointers
Correct Solution:
```
x= input()
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = b.copy()
b.sort()
a.sort()
ans = 0
j = 0
i = 0
ot = {}
while j in range(len(b)) and i in range(len(a)):
if a[i]<=b[j]:
ans += 1
i += 1
else:
ot.update({b[j]: ans})
j += 1
for i in range(len(c)):
print(ot.get(c[i],len(a)), end = ' ')
``` | output | 1 | 12,914 | 12 | 25,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.
The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).
The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).
Output
Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.
Examples
Input
5 4
1 3 5 7 9
6 4 2 8
Output
3 2 1 4
Input
5 5
1 2 1 2 5
3 1 4 1 5
Output
4 2 4 2 5
Submitted Solution:
```
import bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics
def sin():
return input()
def ain():
return list(map(int, sin().split()))
def sain():
return input().split()
def iin():
return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
s = set()
for p in range(2, n+1):
if prime[p]:
s.add(p)
return s
def readTree(n, m):
adj = [deque([]) for _ in range(n+1)]
for _ in range(m):
u,v = ain()
adj[u].append(v)
adj[v].append(u)
return adj
def main():
n,m = ain()
a = ain()
b = ain()
i = 0
j = 0
d = deque()
x = b.copy()
a.sort()
b.sort()
count = 0
while i<n and j<m:
if b[j] > a[i]:
count += 1
i += 1
elif b[j] == a[i]:
count += 1
i += 1
elif b[j] < a[i]:
d.append(count)
j += 1
while j<m:
d.append(count)
j += 1
dic = {}
for i in range(m):
dic[b[i]] = d[i]
# print(x,dic)
for i in range(m):
b[i] = dic[x[i]]
print(*b)
# Fast IO Template starts
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")
if os.getcwd() == 'D:\\code':
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# Fast IO Template ends
if __name__ == "__main__":
main()
``` | instruction | 0 | 12,917 | 12 | 25,834 |
Yes | output | 1 | 12,917 | 12 | 25,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.
The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).
The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).
Output
Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.
Examples
Input
5 4
1 3 5 7 9
6 4 2 8
Output
3 2 1 4
Input
5 5
1 2 1 2 5
3 1 4 1 5
Output
4 2 4 2 5
Submitted Solution:
```
n = [int(b) for b in input().split()]
A = [int(b) for b in input().split()]
B = [int(b) for b in input().split()]
A.sort()
Bo = B.copy()
B.sort()
me = [0]
resp = {}
for i in B:
m = me[-1]
for j in range(me[-1], n[0]):
if i >= A[j]:
m += 1
else:
break
resp[i] = m
me.append(m)
del me[0]
str = ''
for i in Bo:
str += "{} ".format(resp[i])
print(str)
``` | instruction | 0 | 12,918 | 12 | 25,836 |
Yes | output | 1 | 12,918 | 12 | 25,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.
The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).
The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).
Output
Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.
Examples
Input
5 4
1 3 5 7 9
6 4 2 8
Output
3 2 1 4
Input
5 5
1 2 1 2 5
3 1 4 1 5
Output
4 2 4 2 5
Submitted Solution:
```
"""
Escuela Colombiana de Ingeniería Julio Garavito
Programación Imperativa Básica (PIMB) - Grupo 4
Daniel Felipe Hernández Mancipe
Arena 9 - Queries about less or equal elements
"""
from sys import stdin
def comparar(lista1, lista2):
"""
Pre: Requiere dos listas con elementos comparables.
Post: Retorna en un str, para cada elemento de la segunda lista(lista2), el número de elementos que son menores o iguales en la primera lista(lista1)al valor en cuestión de la segunda lista.
"""
lista1.sort()
for i in range(len(lista2)):
lista2[i].append(i)
lista2 = sorted(lista2, key=lambda lista2: lista2[0])
cont1, cont2, cont3 = 0, 0, 0
while cont1<len(lista2):
if cont1>0:
if lista2[cont1-1][0]==lista2[cont1][0]:
lista2[cont1].append(cont3)
else:
cont3 = 0
for i in range(cont2, len(lista1)):
if lista2[cont1][0]>=lista1[i]:
cont3 += 1
cont2 = i+1
else:
break
lista2[cont1].append(lista2[cont1-1][2]+cont3)
else:
for i in range(cont2, len(lista1)):
if lista2[cont1][0]>=lista1[i]:
cont3 += 1
cont2 = i+1
else:
break
lista2[cont1].append(cont3)
cont1 += 1
lista2 = sorted(lista2, key=lambda lista2: lista2[1])
rta = ''
for i in range(len(lista2)):
if i!=len(lista2)-1:
rta += str(lista2[i][2])+' '
else:
rta += str(lista2[i][2])
return rta
def main():
n1, n2 = stdin.readline().strip().split()
arr1 = [int(x) for x in stdin.readline().strip().split()]
arr2 = [[int(x)] for x in stdin.readline().strip().split()]
print(comparar(arr1, arr2))
main()
``` | instruction | 0 | 12,922 | 12 | 25,844 |
No | output | 1 | 12,922 | 12 | 25,845 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | instruction | 0 | 12,966 | 12 | 25,932 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n, m = map(int, input().split())
ans = n
for i in range(m):
x, y = map(int, input().split())
ans = min(ans, y - x + 1)
print(ans)
for i in range(n):
print(i % ans, end=" ")
print()
``` | output | 1 | 12,966 | 12 | 25,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | instruction | 0 | 12,967 | 12 | 25,934 |
Tags: constructive algorithms, greedy
Correct Solution:
```
data = list(map(int, input().split()))
n, m = data[0], data[1]
a = []
for i in range(m):
tmp = list(map(int, input().split()))
a.append(tmp)
# print(a)
min_len = 10**6
min_idx = 0
for i in range(len(a)):
if a[i][1] - a[i][0] + 1 < min_len:
min_len = a[i][1] - a[i][0] + 1
min_idx = i
print(min_len)
cnt = 0
for i in range(n):
print(cnt % min_len, end=' ')
cnt += 1
``` | output | 1 | 12,967 | 12 | 25,935 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | instruction | 0 | 12,968 | 12 | 25,936 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n, m = list(map(int, input().split()))
mi = 10**10
for i in range(m):
l, r = list(map(int, input().split()))
mi = min(r - l + 1, mi)
print(mi)
for i in range(n):
print(i % mi, end=" ")
``` | output | 1 | 12,968 | 12 | 25,937 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | instruction | 0 | 12,969 | 12 | 25,938 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import sys
import math
import collections
import heapq
import decimal
input=sys.stdin.readline
n,m=(int(i) for i in input().split())
d={}
k=n+1
for i in range(m):
a,b=(int(i) for i in input().split())
k=min(k,b-a+1)
l=[]
for i in range(n):
l.append(i%k)
print(k)
print(*l)
``` | output | 1 | 12,969 | 12 | 25,939 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | instruction | 0 | 12,970 | 12 | 25,940 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n,m = map(int,input().split())
ans = n+1
for i in range(m):
a,b = map(int,input().split())
ans = min(ans, b-a+1)
print(ans)
arr = [0] * n
put = 0
for i in range(n):
arr[i] = put
put += 1
if(put == ans):
put = 0
print(*arr)
``` | output | 1 | 12,970 | 12 | 25,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | instruction | 0 | 12,971 | 12 | 25,942 |
Tags: constructive algorithms, greedy
Correct Solution:
```
from sys import stdin
test = stdin.readlines()
n, m = map(int, test[0].split())
intervals = [[int(c) for c in test[i].split()] for i in range(1, m + 1)]
min_length = min(r - l + 1 for l, r in intervals)
ans = list(range(min_length)) * (n // min_length + 1)
ans = ans[:n]
print(min_length)
print(*ans)
``` | output | 1 | 12,971 | 12 | 25,943 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | instruction | 0 | 12,972 | 12 | 25,944 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
length, subs = list(map(int, input().strip().split()))
lol = 9999999999999999999999999999
for i in range(subs):
start,end = list(map(int, input().strip().split()))
if abs(start-end) + 1 < lol:
lol = abs(start-end) + 1
print(lol)
construct = []
count = 0
while count < length:
construct.append(str(count%lol))
count+=1
print(' '.join(construct))
``` | output | 1 | 12,972 | 12 | 25,945 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2. | instruction | 0 | 12,973 | 12 | 25,946 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
#
n,m = map(int, input().split())
mex = []
for i in range(m):
mex.append(list(map(int,input().split())))
ans = min([j-i+1 for i,j in mex])
print(ans)
lis = []
for i in range(n):
lis.append(str(i%ans))
print(' '.join(lis))
``` | output | 1 | 12,973 | 12 | 25,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
Submitted Solution:
```
n,m=list(map(int,input().split()))
mini=float('inf')
for i in range(m):
a,b=list(map(int,input().split()))
mini=min(mini,b-a+1)
a=[]
for i in range(n):
a.append(i%mini)
print(mini)
print(*a)
``` | instruction | 0 | 12,974 | 12 | 25,948 |
Yes | output | 1 | 12,974 | 12 | 25,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
Submitted Solution:
```
nm=[int(n) for n in input().split()]
n=nm[0]
m=nm[1]
ans=n+1
for i in range(m):
ab=[int(n) for n in input().split()]
a=ab[0]
b=ab[1]
ans=min(ans,b-a+1)
print(ans)
for i in range(n):
print((i)%ans,end=' ')
``` | instruction | 0 | 12,975 | 12 | 25,950 |
Yes | output | 1 | 12,975 | 12 | 25,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
Submitted Solution:
```
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
seg=[list(map(int,input().split())) for i in range(m)]
min_l=10**18
for l,r in seg:
min_l=min(min_l,r-l+1)
ans=[i for i in range(min_l)]*((n+min_l-1)//min_l)
ans=ans[:n]
print(min_l)
print(*ans)
``` | instruction | 0 | 12,976 | 12 | 25,952 |
Yes | output | 1 | 12,976 | 12 | 25,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
Submitted Solution:
```
[n, m] = [int(x) for x in input().split(" ")]
mex = n + 1
for i in range(m):
[l, r] = [int(x) for x in input().split(" ")]
if r - l + 1 < mex:
mex = r - l + 1
res = [""] * n
i = 0
for j in range(n):
res[j] = str(i)
i+=1
if i==mex:
i = 0
print(mex)
print(" ".join(res))
``` | instruction | 0 | 12,977 | 12 | 25,954 |
Yes | output | 1 | 12,977 | 12 | 25,955 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
Submitted Solution:
```
def solve(mex,subArr,arr):
if subArr==[]:
return arr;
u,v=subArr[0][0],subArr[0][1]
arr_new=arr[:]
included=set()
targets=[]
for i in range(u,v):
if arr[i]!=-1:
included.add(arr[i])
else:
targets.append(i)
for i in range(v-u):
if targets==[]:
break;
if i not in included:
arr_new[targets.pop(0)]=i
return solve(mex,subArr[1:],arr_new);
def main():
n,m=list(map(int,input().split()))
mex=-1
subArr=[]
for _ in range(m):
start,end=list(map(int,input().split()))
trial=end-start+1
if mex==-1 or trial<mex:
mex=trial
subArr.append([start-1,end])
subArr.sort(key=lambda x:x[1]-x[0])
## arr[corrS:corrE]=list(range(mex))
print(mex)
arr=solve(mex,subArr,[-1]*n)
for y in arr:
print(y,end=' ')
print()
main()
``` | instruction | 0 | 12,978 | 12 | 25,956 |
No | output | 1 | 12,978 | 12 | 25,957 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
Submitted Solution:
```
f=lambda : list(map(int,input().split()))
n,m=f()
lr=lambda x: x[1]-x[0]+1
sq=min(lr(f()) for _ in range(m))
print(sq)
x=' '.join([str(i%n) for i in range(n)])
print(x)
``` | instruction | 0 | 12,979 | 12 | 25,958 |
No | output | 1 | 12,979 | 12 | 25,959 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
Submitted Solution:
```
n, m = map(int, input().split())
L = [i for i in range(n)]
ans = 10000000000
for i in range(m):
a, b = map(int, input().split())
if b - a + 1 < ans:
ans = b - a + 1
for j in range(b - a + 1):
L[a + j - 1] = j
print(ans)
print(*L)
``` | instruction | 0 | 12,980 | 12 | 25,960 |
No | output | 1 | 12,980 | 12 | 25,961 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
Output
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
Examples
Input
5 3
1 3
2 5
4 5
Output
2
1 0 2 1 0
Input
4 2
1 4
2 4
Output
3
5 2 0 1
Note
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
Submitted Solution:
```
from sys import stdin
#n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
l1=0
r1=0
t=10**7
s=[]
for i in range(m):
l,r=map(int,stdin.readline().strip().split())
s.append([l,r])
s.sort()
for i in range(m):
l,r=s[i]
if(r-l<t):
l1=l
r1=r
ans=[0 for i in range(n)]
x=0
n1=r1-l1+1
for i in range(l1-1,l1+n-1):
ans[i%n]=x%n1
x+=1
print(r1-l1+1)
print(*ans)
#s=list(map(int,stdin.readline().strip().split()))
``` | instruction | 0 | 12,981 | 12 | 25,962 |
No | output | 1 | 12,981 | 12 | 25,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6. | instruction | 0 | 13,359 | 12 | 26,718 |
Tags: hashing, math, number theory
Correct Solution:
```
from fractions import Fraction
from collections import Counter
inf = 10**9+7
n = int(input())
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
d = []
zeros = 0
for i in range(n):
if a[i] != 0:
d.append(Fraction(-b[i], a[i]))
else:
if b[i] == 0:
zeros += 1
de = Counter(d)
if de == {}:
print(0+zeros)
else:
ans = max(list(de.values()))+zeros
print(ans)
``` | output | 1 | 13,359 | 12 | 26,719 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6. | instruction | 0 | 13,360 | 12 | 26,720 |
Tags: hashing, math, number theory
Correct Solution:
```
from decimal import getcontext, Decimal
getcontext().prec = 50
n = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
mp = {}
appendix = 0
for i in range(n):
if arr1[i] == arr2[i] == 0:
appendix += 1
elif arr1[i] != 0:
tmp = Decimal(arr2[i]) / Decimal(arr1[i])
mp.setdefault(tmp, 0)
mp[tmp] += 1
if len(mp):
print(max(mp.values()) + appendix)
else:
print(appendix)
``` | output | 1 | 13,360 | 12 | 26,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6. | instruction | 0 | 13,361 | 12 | 26,722 |
Tags: hashing, math, number theory
Correct Solution:
```
from fractions import gcd
n = int(input())
ai = list(map(int,input().split()))
bi = list(map(int,input().split()))
s = {}
num = 0
mod = 2 * 10**9 + 1
for i in range(n):
if ai[i] != 0:
temp = gcd(ai[i],bi[i])
try :
s[-ai[i] // temp + mod * (bi[i] // temp)] += 1
except :
s[-ai[i] // temp + mod * (bi[i] // temp)] = 1
elif bi[i] == ai[i]:
num += 1
ans = 0
for i in s:
ans = max(ans,s[i])
print(ans + num)
``` | output | 1 | 13,361 | 12 | 26,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6. | instruction | 0 | 13,362 | 12 | 26,724 |
Tags: hashing, math, number theory
Correct Solution:
```
from decimal import Decimal
from collections import *
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = []
d = defaultdict(int)
ans = 0
value = 0
for i in range(n):
if (a[i]==0 and b[i]!=0):
continue
if a[i]==0 and b[i]==0:
ans+=1
continue
val = Decimal(b[i])/Decimal(a[i])
d[val]+=1
value = max(value,d[val])
print(value+ans)
``` | output | 1 | 13,362 | 12 | 26,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6. | instruction | 0 | 13,363 | 12 | 26,726 |
Tags: hashing, math, number theory
Correct Solution:
```
from collections import Counter
from fractions import Fraction
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
D = []
indef_num = 0
for (a, b) in zip(A, B):
if a == 0:
if b == 0:
indef_num += 1
continue
D.append(Fraction(-b, a))
if not D:
print(indef_num)
else:
d = Counter(D).most_common()
print(D.count(d[0][0]) + indef_num)
``` | output | 1 | 13,363 | 12 | 26,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6. | instruction | 0 | 13,364 | 12 | 26,728 |
Tags: hashing, math, number theory
Correct Solution:
```
from math import gcd
def get_sign(_u, _b):
if (_u > 0 and _b > 0) or (_u < 0 and _b < 0):
return ""
else:
return "-"
if __name__ == '__main__':
input()
u = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
acc = 0
reduced = []
extra = 0
for i in range(len(u)):
_u = abs(u[i])
_b = abs(b[i])
sign = get_sign(u[i], b[i])
if _u == 0 or _b == 0:
if _u == 0 and _b == 0:
acc += 1
continue
elif _u != 0 and _b == 0:
extra += 1
continue
else:
continue
while True:
_gcd = gcd(_u, _b)
if _gcd == 1:
break
_u = _u // _gcd
_b = _b // _gcd
reduced.append(sign + str(_u) + ":" + str(_b))
m = {}
for i in range(len(reduced)):
if reduced[i] in m:
m[reduced[i]] += 1
else:
m[reduced[i]] = 1
_max = 0
for key in m:
if m[key] > _max:
_max = m[key]
print(max(acc + _max, acc + extra))
``` | output | 1 | 13,364 | 12 | 26,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6. | instruction | 0 | 13,365 | 12 | 26,730 |
Tags: hashing, math, number theory
Correct Solution:
```
n=int(input())
from decimal import *
getcontext().prec = 100
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
sm=0
d1=[]
d2=[]
cnt=0
for i in range(n):
if a[i]==0 and b[i]==0:
cnt+=1
continue
if a[i]==0 :
continue
d1.append((-Decimal(b[i]))/Decimal(a[i]))
from collections import Counter
c=Counter(d1)
maxi=0
for i in c:
maxi=max(maxi,c[i])
print(maxi+cnt)
``` | output | 1 | 13,365 | 12 | 26,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6. | instruction | 0 | 13,366 | 12 | 26,732 |
Tags: hashing, math, number theory
Correct Solution:
```
'''
i see two problems now:
is 1000000/17 = 2000000/34 something along that line, involving multiple decimals
solution is do sedsuonyangtam.
a = 0, b = 0
'''
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
d_nonneg, d_neg = {}, {}
m, v = 0, 0
for i in range(n):
if a[i] == 0 and b[i] == 0:
v = v + 1
if a[i] != 0:
aa, bb = a[i], b[i]
if bb == 0:
if (0, 1) not in d_nonneg:
d_nonneg[(0, 1)] = 1
else:
d_nonneg[(0, 1)] += 1
p = d_nonneg[(0, 1)]
if aa < 0 and bb < 0:
aa, bb = aa * (-1), bb * (-1)
if aa > 0 and bb > 0:
x = gcd(aa, bb)
aa, bb = aa // x, bb // x
if (aa, bb) not in d_nonneg:
d_nonneg[(aa, bb)] = 1
else:
d_nonneg[(aa, bb)] += 1
p = d_nonneg[(aa, bb)]
else:
if aa < 0:
aa = aa * (-1)
else:
bb = bb * (-1)
x = gcd(aa, bb)
aa, bb = aa // x, bb // x
if (aa, bb) not in d_neg:
d_neg[(aa, bb)] = 1
else:
d_neg[(aa, bb)] += 1
p = d_neg[(aa, bb)]
if p > m:
m = p
print(m + v)
``` | output | 1 | 13,366 | 12 | 26,733 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6.
Submitted Solution:
```
# https://codeforces.com/problemset/problem/1133/D
from sys import stdin, exit
from typing import List, Tuple
import math
stdin.readline() # ignore
a_arr = [int(v) for v in stdin.readline().rstrip().split()]
b_arr = [int(v) for v in stdin.readline().rstrip().split()]
def fractionize(numerator: int, denominator: int):
if denominator < 0:
numerator = -numerator
denominator = -denominator
elif denominator == 0:
raise ZeroDivisionError()
gcd = math.gcd(abs(numerator), denominator)
return numerator // gcd, denominator // gcd
class FractionN:
numerator: int
denominator: int
def __init__(self, numerator: int, denominator=1):
self.numerator, self.denominator = fractionize(numerator, denominator)
def __eq__(self, other):
return self.numerator == other.numerator and self.denominator == other.denominator
def __hash__(self):
return hash((self.numerator, self.denominator))
def __repr__(self):
return f"<Fraction({self.numerator} / {self.denominator})>"
frac_occurs = dict()
wildcards = 0
not_divisible = 0
for a, b in zip(a_arr, b_arr):
if a == 0 and b != 0:
not_divisible += 1
continue
elif a == 0 and b == 0:
wildcards += 1
continue
else:
# key = fractionize(-b, a)
key = FractionN(-b, a)
if key in frac_occurs:
frac_occurs[key] += 1
else:
frac_occurs[key] = 1
if wildcards == 0 and not_divisible == len(a_arr):
print(0)
else:
print((max(frac_occurs.values()) if len(frac_occurs) > 0 else 0) + wildcards)
``` | instruction | 0 | 13,368 | 12 | 26,736 |
Yes | output | 1 | 13,368 | 12 | 26,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[-99999999999999999999999999]*n
for i in range (n):
if(a[i]!=0):
c[i]= (-b[i]/a[i])
#print(c)
c.sort()
co=0
for i in range (n):
if(a[i]==0 and b[i]!=0):
co+=1
if(co==n):
print(0)
exit()
maxc=0
co=1
for i in range (1,n):
if(c[i]==c[i-1] and c[i]!=-99999999999999999999999999 ):
co+=1
if(co>maxc):
maxc=co
else:
co=1
for i in range (n):
if(a[i]==0 and b[i]==0):
maxc+=1
print(maxc)
``` | instruction | 0 | 13,372 | 12 | 26,744 |
No | output | 1 | 13,372 | 12 | 26,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b, each contains n integers.
You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i ∈ [1, n] let c_i := d ⋅ a_i + b_i.
Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in both arrays.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9).
The third line contains n integers b_1, b_2, ..., b_n (-10^9 ≤ b_i ≤ 10^9).
Output
Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
Examples
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d = -2.
In the second example, we may choose d = -1/13.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d = 6.
Submitted Solution:
```
import sys
from fractions import gcd
input = sys.stdin.readline
n = int(input())
arr1 = [int(i) for i in input().split()]
arr2 = [int(i) for i in input().split()]
arr3 = dict()
for i in range(len(arr1)):
if arr1[i] is not 0:
if arr2[i] is 0:
arr3["0"] = arr3.get("0",0)+1
else:
g = gcd(arr2[i],arr1[i])
s = "%d/%d"%(arr2[i]//g,arr1[i]//g)
arr3[s] = arr3.get(s,0) + 1
if arr1[i]==0 and arr2[i]==0:
arr3[0] = arr3.get(0,0) + 1
res = list(arr3.values())
if len(res)==0:
print(0)
else:
print(max(res))
``` | instruction | 0 | 13,374 | 12 | 26,748 |
No | output | 1 | 13,374 | 12 | 26,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n.
Let's call some subarray a_l, a_{l + 1}, ... , a_r of this array a subpermutation if it contains all integers from 1 to r-l+1 exactly once. For example, array a = [2, 2, 1, 3, 2, 3, 1] contains 6 subarrays which are subpermutations: [a_2 ... a_3], [a_2 ... a_4], [a_3 ... a_3], [a_3 ... a_5], [a_5 ... a_7], [a_7 ... a_7].
You are asked to calculate the number of subpermutations.
Input
The first line contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ n).
This array can contain the same integers.
Output
Print the number of subpermutations of the array a.
Examples
Input
8
2 4 1 3 4 2 1 2
Output
7
Input
5
1 1 2 1 2
Output
6
Note
There are 7 subpermutations in the first test case. Their segments of indices are [1, 4], [3, 3], [3, 6], [4, 7], [6, 7], [7, 7] and [7, 8].
In the second test case 6 subpermutations exist: [1, 1], [2, 2], [2, 3], [3, 4], [4, 4] and [4, 5]. | instruction | 0 | 13,391 | 12 | 26,782 |
Tags: brute force, data structures, divide and conquer, hashing, math
Correct Solution:
```
import sys
import math
input=sys.stdin.readline
#sys.setrecursionlimit(1000000)
mod=int(1000000007)
i=lambda :map(int,input().split())
n=int(input())
a=[int(x) for x in input().split()]
t=[[0]*21 for i in range(300005)]
for i in range(n):
t[i][0]=a[i]
def build(n):
for j in range(1,20):
for i in range(n):
if i+(1<<j)-1>n-1:
break;
t[i][j]=max(t[i][j-1],t[i+(1<<(j-1))][j-1])
def query(p,q):
p,q=int(p),int(q)
log=int(math.log2(q-p+1))
m=t[p][log]
n=t[q-(1<<log)+1][log]
return max(m,n)
b=[-1]*(n+2)
build(n)
max1=-1
ans=0
for i in range(n):
max1=max(max1,b[a[i]])
b[a[i]]=i
x=b[1]
while x>max1:
if x<=max1:
break
p=query(x,i)
if p==i-x+1:
ans+=1
x=b[p+1]
else:
x=i-p+1
print(ans)
``` | output | 1 | 13,391 | 12 | 26,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n.
Let's call some subarray a_l, a_{l + 1}, ... , a_r of this array a subpermutation if it contains all integers from 1 to r-l+1 exactly once. For example, array a = [2, 2, 1, 3, 2, 3, 1] contains 6 subarrays which are subpermutations: [a_2 ... a_3], [a_2 ... a_4], [a_3 ... a_3], [a_3 ... a_5], [a_5 ... a_7], [a_7 ... a_7].
You are asked to calculate the number of subpermutations.
Input
The first line contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ n).
This array can contain the same integers.
Output
Print the number of subpermutations of the array a.
Examples
Input
8
2 4 1 3 4 2 1 2
Output
7
Input
5
1 1 2 1 2
Output
6
Note
There are 7 subpermutations in the first test case. Their segments of indices are [1, 4], [3, 3], [3, 6], [4, 7], [6, 7], [7, 7] and [7, 8].
In the second test case 6 subpermutations exist: [1, 1], [2, 2], [2, 3], [3, 4], [4, 4] and [4, 5]. | instruction | 0 | 13,392 | 12 | 26,784 |
Tags: brute force, data structures, divide and conquer, hashing, math
Correct Solution:
```
#import sys
import math
#input=sys.stdin.readline
#sys.setrecursionlimit(1000000)
mod=int(1000000007)
i=lambda :map(int,input().split())
n=int(input())
a=[int(x) for x in input().split()]
t=[[0]*21 for i in range(300005)]
for i in range(n):
t[i][0]=a[i]
def build(n):
for j in range(1,20):
for i in range(n):
if i+(1<<j)-1>n-1:
break;
t[i][j]=max(t[i][j-1],t[i+(1<<(j-1))][j-1])
def query(p,q):
p,q=int(p),int(q)
log=int(math.log2(q-p+1))
m=t[p][log]
n=t[q-(1<<log)+1][log]
return max(m,n)
b=[-1]*(n+2)
build(n)
max1=-1
ans=0
for i in range(n):
max1=max(max1,b[a[i]])
b[a[i]]=i
x=b[1]
while x>max1:
if x<=max1:
break
p=query(x,i)
if p==i-x+1:
ans+=1
x=b[p+1]
else:
x=i-p+1
print(ans)
``` | output | 1 | 13,392 | 12 | 26,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n.
Let's call some subarray a_l, a_{l + 1}, ... , a_r of this array a subpermutation if it contains all integers from 1 to r-l+1 exactly once. For example, array a = [2, 2, 1, 3, 2, 3, 1] contains 6 subarrays which are subpermutations: [a_2 ... a_3], [a_2 ... a_4], [a_3 ... a_3], [a_3 ... a_5], [a_5 ... a_7], [a_7 ... a_7].
You are asked to calculate the number of subpermutations.
Input
The first line contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ n).
This array can contain the same integers.
Output
Print the number of subpermutations of the array a.
Examples
Input
8
2 4 1 3 4 2 1 2
Output
7
Input
5
1 1 2 1 2
Output
6
Note
There are 7 subpermutations in the first test case. Their segments of indices are [1, 4], [3, 3], [3, 6], [4, 7], [6, 7], [7, 7] and [7, 8].
In the second test case 6 subpermutations exist: [1, 1], [2, 2], [2, 3], [3, 4], [4, 4] and [4, 5].
Submitted Solution:
```
from sys import stdin
def inp():
return stdin.buffer.readline().rstrip().decode('utf8')
def itg():
return int(stdin.buffer.readline())
def mpint():
return map(int, stdin.buffer.readline().split())
# ############################## import
# ############################## main
# for __ in range(itg()):
n = itg()
arr = tuple(mpint())
ans = 0
def dfs(mx, start, end, st):
global ans
ans += 1
s = st.copy()
m = mx
for i in range(start - 1, -1, -1):
a = arr[i]
if a in s:
break
s.add(a)
m = max(m, a)
if a == mx + 1:
dfs(m, i, end, s)
s = st.copy()
m = mx
for i in range(end + 1, n):
a = arr[i]
if a in s:
break
s.add(a)
m = max(m, a)
if a == mx + 1:
dfs(m, start, i, s)
for index, num in enumerate(arr):
if num == 1:
dfs(1, index, index, {1})
print(ans)
# Please check!
``` | instruction | 0 | 13,393 | 12 | 26,786 |
No | output | 1 | 13,393 | 12 | 26,787 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1". | instruction | 0 | 13,438 | 12 | 26,876 |
Tags: constructive algorithms, sortings
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
m = (n + 1) // 2 - 1
for x, y in zip(a[m::-1], a[m + 1::]):
print(x, y, end=' ')
if n % 2:
print(a[0], end=' ')
print()
``` | output | 1 | 13,438 | 12 | 26,877 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1". | instruction | 0 | 13,439 | 12 | 26,878 |
Tags: constructive algorithms, sortings
Correct Solution:
```
import math
t=int(input())
for g in range(0,t):
n=int(input())
a=list(map(int,input().split(" ")))
a.sort()
if(n%2==0):
k=int(n/2)-1
else:
k=int(n/2)
b=[]
b.append(a[k])
i=k-1
j=k+1
while(i>=0 and j<=n-1):
b.append(a[j])
j=j+1
b.append(a[i])
i=i-1
if(n%2==0):
b.append(a[n-1])
for i in range(0,n):
print(b[i],end=" ")
print("")
``` | output | 1 | 13,439 | 12 | 26,879 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1". | instruction | 0 | 13,440 | 12 | 26,880 |
Tags: constructive algorithms, sortings
Correct Solution:
```
import sys
import math as mt
input=sys.stdin.buffer.readline
import bisect
t=int(input())
#tot=0
for __ in range(t):
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=[]
i=(n//2)-1
j=(n//2)+1
ans.append(a[n//2])
n1=n
n-=1
ch=0
if (n1-n//2-1)>n//2:
ch=1
while n:
if j>=n1:
ans.append(a[i])
i-=1
else:
if ch%2==0:
#print(ch,j,n,a[j])
ans.append(a[j])
j+=1
else:
ans.append(a[i])
i-=1
ch+=1
n-=1
print(*ans)
``` | output | 1 | 13,440 | 12 | 26,881 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1". | instruction | 0 | 13,441 | 12 | 26,882 |
Tags: constructive algorithms, sortings
Correct Solution:
```
t=int(input())
p=[]
for ikn in range(0,t):
n=int(input())
s=input().split( )
for i in range(0,n):
s[i]=int(s[i])
s.sort()
d=[]
g=0
while len(d)<len(s):
d.append(s[g])
d.append(s[n-g-1])
g+=1
if len(d)==n+1:
del(d[n])
d.reverse()
p.append(d)
for i in p:
for j in i:
print(j,end=' ')
print()
``` | output | 1 | 13,441 | 12 | 26,883 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1". | instruction | 0 | 13,442 | 12 | 26,884 |
Tags: constructive algorithms, sortings
Correct Solution:
```
for t in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
a.sort()
l=[]
for i in range(n):
if i%2:
l+=[a.pop(0)]
else:
l+=[a.pop()]
print(*l[::-1])
``` | output | 1 | 13,442 | 12 | 26,885 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1". | instruction | 0 | 13,443 | 12 | 26,886 |
Tags: constructive algorithms, sortings
Correct Solution:
```
t = int(input())
for q in range(t):
n=int(input())
arr = list(map(int, input().split()))
arr.sort()
ans=[]
if n%2==0:
for i in range(n//2):
ans.append(arr[n-i-1])
ans.append(arr[i])
else:
for i in range(n//2):
ans.append(arr[n - i - 1])
ans.append(arr[i])
ans.append(arr[n//2])
for k in range(len(ans)-1,-1,-1):
print(ans[k],end=' ')
print()
``` | output | 1 | 13,443 | 12 | 26,887 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1". | instruction | 0 | 13,444 | 12 | 26,888 |
Tags: constructive algorithms, sortings
Correct Solution:
```
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
b = [0] * n
j = 0
for i in range(n // 2, n):
b[j] = a[i]
if i != n - 1 or n % 2 == 0:
b[j + 1] = a[n - i - 1 - n % 2]
j += 2
print(*b)
``` | output | 1 | 13,444 | 12 | 26,889 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1". | instruction | 0 | 13,445 | 12 | 26,890 |
Tags: constructive algorithms, sortings
Correct Solution:
```
def solve():
n = int(input())
a = sorted([int(x) for x in input().split()])
l, r = 0, len(a) - 1
cur = 0
res = []
while(l <= r):
if(cur == 0):
res.append(str(a[l]))
l += 1
else:
res.append(str(a[r]))
r -= 1
cur = 1 - cur
print(" ".join(res[::-1]))
t = int(input())
for _ in range(t):
solve()
``` | output | 1 | 13,445 | 12 | 26,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1".
Submitted Solution:
```
testcases = int(input())
while(testcases):
testcases -= 1
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
while arr:
print(arr.pop(len(arr)//2),end = " ")
print("")
``` | instruction | 0 | 13,446 | 12 | 26,892 |
Yes | output | 1 | 13,446 | 12 | 26,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1".
Submitted Solution:
```
import sys
for _ in range(int(sys.stdin.readline())):
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
a.sort()
b = [None] * n
j = (n - 1) >> 1
for i in range(n):
b[i] = a[j]
if i & 1 == 0:
j += i + 1
else:
j -= i + 1
print(*b)
``` | instruction | 0 | 13,447 | 12 | 26,894 |
Yes | output | 1 | 13,447 | 12 | 26,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1".
Submitted Solution:
```
for _ in range(int(input())):
total_numbers = int(input())
numbers = list(map(int, input().split()))
numbers.sort()
final = []
for i in range(total_numbers//2):
final.append(numbers[i])
position = (-1)*(i+1)
final.append(numbers[position])
if total_numbers % 2 == 1:
middle = (total_numbers//2)
final.append(numbers[middle])
final.reverse()
print(" ".join(map(str, final)))
``` | instruction | 0 | 13,448 | 12 | 26,896 |
Yes | output | 1 | 13,448 | 12 | 26,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1".
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
i = n // 2 + n % 2
j = i + 1
p = []
for k in range(n // 2 + n % 2):
p.append(i)
p.append(j)
i -= 1
j += 1
if n % 2:
p.pop()
print(' '.join(str(a[i - 1]) for i in p))
``` | instruction | 0 | 13,449 | 12 | 26,898 |
Yes | output | 1 | 13,449 | 12 | 26,899 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1".
Submitted Solution:
```
def solution():
t = int(input())
for _ in range(t):
n = int(input())
nums = list(map(int,input().split()))
nums.sort()
if n % 2 == 0:
j = n // 2
i = j - 1
print(nums[i],end=" ")
print(nums[j], end="")
i -= 1
j += 1
else:
i = n // 2
print(nums[i],end="")
j = i + 1
i -= 1
while i >= 0:
print(" " + str(nums[i]), end="")
print(" " + str(nums[j]), end="")
i -= 1
j += 1
solution()
``` | instruction | 0 | 13,450 | 12 | 26,900 |
No | output | 1 | 13,450 | 12 | 26,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have array of n numbers a_{1}, a_{2}, …, a_{n}.
Rearrange these numbers to satisfy |a_{1} - a_{2}| ≤ |a_{2} - a_{3}| ≤ … ≤ |a_{n-1} - a_{n}|, where |x| denotes absolute value of x. It's always possible to find such rearrangement.
Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.
You have to answer independent t test cases.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases.
The first line of each test case contains single integer n (3 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}.
The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}).
Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.
Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a_{1} - a_{2}| = 0 ≤ |a_{2} - a_{3}| = 1 ≤ |a_{3} - a_{4}| = 2 ≤ |a_{4} - a_{5}| = 2 ≤ |a_{5} - a_{6}| = 10. There are other possible answers like "5 4 5 6 -2 8".
In the second test case, after given rearrangement, |a_{1} - a_{2}| = 1 ≤ |a_{2} - a_{3}| = 2 ≤ |a_{3} - a_{4}| = 4. There are other possible answers like "2 4 8 1".
Submitted Solution:
```
import re
def neg(x):
return abs(int(x[0])-int(x[1]))
def solve():
n=int(input())
stri=input()
temp=stri.split(" ")
res=[]
for i in range(0,n,2):
#print(i)
res.append([temp[i],temp[i+1]])
temp.clear()
#print(res)
#print(res)
res = sorted(res,key=lambda x: neg(x))
#print(res)
ans=""
for indx,i in enumerate(res):
print(i[0], end = " ")
print(i[1], end=" ")
t=int(input())
for i in range(t):
solve()
``` | instruction | 0 | 13,451 | 12 | 26,902 |
No | output | 1 | 13,451 | 12 | 26,903 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.