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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ashish has n elements arranged in a line.
These elements are represented by two integers a_i β the value of the element and b_i β the type of the element (there are only two possible types: 0 and 1). He wants to sort the elements in non-decreasing values of a_i.
He can perform the following operation any number of times:
* Select any two elements i and j such that b_i β b_j and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of a_i after performing any number of operations.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n (1 β€ n β€ 500) β the size of the arrays.
The second line contains n integers a_i (1 β€ a_i β€ 10^5) β the value of the i-th element.
The third line containts n integers b_i (b_i β \{0, 1\}) β the type of the i-th element.
Output
For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
Input
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1
Output
Yes
Yes
Yes
No
Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions 1 and 2, then swap elements at positions 2 and 3.
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements i and j such that b_i β b_j. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 3 and 4, then elements at positions 1 and 2.
Submitted Solution:
```
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int,input().split()))
brr = list(map(int,input().split()))
cnt0=0
for i in brr:
if(i==0):
cnt0+=1
if(arr== sorted(arr)):
print("Yes")
else:
if(cnt0==n or cnt0==0):
print("No")
else:
print("Yes")
``` | instruction | 0 | 14,338 | 12 | 28,676 |
Yes | output | 1 | 14,338 | 12 | 28,677 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ashish has n elements arranged in a line.
These elements are represented by two integers a_i β the value of the element and b_i β the type of the element (there are only two possible types: 0 and 1). He wants to sort the elements in non-decreasing values of a_i.
He can perform the following operation any number of times:
* Select any two elements i and j such that b_i β b_j and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of a_i after performing any number of operations.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n (1 β€ n β€ 500) β the size of the arrays.
The second line contains n integers a_i (1 β€ a_i β€ 10^5) β the value of the i-th element.
The third line containts n integers b_i (b_i β \{0, 1\}) β the type of the i-th element.
Output
For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
Input
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1
Output
Yes
Yes
Yes
No
Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions 1 and 2, then swap elements at positions 2 and 3.
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements i and j such that b_i β b_j. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 3 and 4, then elements at positions 1 and 2.
Submitted Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
T = list(map(int, input().split()))
ordered = True
last = 0
for i in range(n):
if A[i] < last:
ordered = False
last = A[i]
if ordered:
print('Yes')
continue
print('Yes' if len(set(T)) == 2 else 'No')
``` | instruction | 0 | 14,339 | 12 | 28,678 |
Yes | output | 1 | 14,339 | 12 | 28,679 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ashish has n elements arranged in a line.
These elements are represented by two integers a_i β the value of the element and b_i β the type of the element (there are only two possible types: 0 and 1). He wants to sort the elements in non-decreasing values of a_i.
He can perform the following operation any number of times:
* Select any two elements i and j such that b_i β b_j and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of a_i after performing any number of operations.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n (1 β€ n β€ 500) β the size of the arrays.
The second line contains n integers a_i (1 β€ a_i β€ 10^5) β the value of the i-th element.
The third line containts n integers b_i (b_i β \{0, 1\}) β the type of the i-th element.
Output
For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
Input
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1
Output
Yes
Yes
Yes
No
Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions 1 and 2, then swap elements at positions 2 and 3.
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements i and j such that b_i β b_j. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 3 and 4, then elements at positions 1 and 2.
Submitted Solution:
```
import sys
input = lambda: sys.stdin.readline().strip("\r\n")
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if (len(set(b)) == 1 and sorted(a) == a) or len(set(b)) == 2:
print("YES")
else:
print("NO")
``` | instruction | 0 | 14,340 | 12 | 28,680 |
Yes | output | 1 | 14,340 | 12 | 28,681 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ashish has n elements arranged in a line.
These elements are represented by two integers a_i β the value of the element and b_i β the type of the element (there are only two possible types: 0 and 1). He wants to sort the elements in non-decreasing values of a_i.
He can perform the following operation any number of times:
* Select any two elements i and j such that b_i β b_j and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of a_i after performing any number of operations.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n (1 β€ n β€ 500) β the size of the arrays.
The second line contains n integers a_i (1 β€ a_i β€ 10^5) β the value of the i-th element.
The third line containts n integers b_i (b_i β \{0, 1\}) β the type of the i-th element.
Output
For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
Input
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1
Output
Yes
Yes
Yes
No
Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions 1 and 2, then swap elements at positions 2 and 3.
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements i and j such that b_i β b_j. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 3 and 4, then elements at positions 1 and 2.
Submitted Solution:
```
t=int(input())
while t:
t-=1
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
li=[]
mi=[]
for i in a:
li.append(i)
li.sort()
if li==a:
print('YES')
else:
for i in range(len(li)):
if a.index(li[i])==i:
pass
else:
if b[i]!=b[a.index(li[i])]:
mi.append(a[a.index(li[i])])
print('YES')
break
else:
print('NO')
``` | instruction | 0 | 14,341 | 12 | 28,682 |
No | output | 1 | 14,341 | 12 | 28,683 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ashish has n elements arranged in a line.
These elements are represented by two integers a_i β the value of the element and b_i β the type of the element (there are only two possible types: 0 and 1). He wants to sort the elements in non-decreasing values of a_i.
He can perform the following operation any number of times:
* Select any two elements i and j such that b_i β b_j and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of a_i after performing any number of operations.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n (1 β€ n β€ 500) β the size of the arrays.
The second line contains n integers a_i (1 β€ a_i β€ 10^5) β the value of the i-th element.
The third line containts n integers b_i (b_i β \{0, 1\}) β the type of the i-th element.
Output
For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
Input
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1
Output
Yes
Yes
Yes
No
Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions 1 and 2, then swap elements at positions 2 and 3.
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements i and j such that b_i β b_j. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 3 and 4, then elements at positions 1 and 2.
Submitted Solution:
```
t = int(input())
while(t > 0):
n = int(input())
a = [0]*510
b = [0]*510
x = 0
y = 0
a = list(map(int,input().split()))
b = list(map(int,input().split()))
for i in range(n):
if i != 0:
if a[i] < a[i-1]:
x = x + 1
for i in range(n):
if b[i] != b[0]:
y = y + 1
if y > 1 and x == 0:
print("yes")
else:
print("no")
t = t - 1
``` | instruction | 0 | 14,342 | 12 | 28,684 |
No | output | 1 | 14,342 | 12 | 28,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ashish has n elements arranged in a line.
These elements are represented by two integers a_i β the value of the element and b_i β the type of the element (there are only two possible types: 0 and 1). He wants to sort the elements in non-decreasing values of a_i.
He can perform the following operation any number of times:
* Select any two elements i and j such that b_i β b_j and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of a_i after performing any number of operations.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n (1 β€ n β€ 500) β the size of the arrays.
The second line contains n integers a_i (1 β€ a_i β€ 10^5) β the value of the i-th element.
The third line containts n integers b_i (b_i β \{0, 1\}) β the type of the i-th element.
Output
For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
Input
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1
Output
Yes
Yes
Yes
No
Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions 1 and 2, then swap elements at positions 2 and 3.
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements i and j such that b_i β b_j. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 3 and 4, then elements at positions 1 and 2.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
if b.count(1) > 0 and b.count(0) > 0:
print('YES')
else:
print('NO')
``` | instruction | 0 | 14,343 | 12 | 28,686 |
No | output | 1 | 14,343 | 12 | 28,687 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ashish has n elements arranged in a line.
These elements are represented by two integers a_i β the value of the element and b_i β the type of the element (there are only two possible types: 0 and 1). He wants to sort the elements in non-decreasing values of a_i.
He can perform the following operation any number of times:
* Select any two elements i and j such that b_i β b_j and swap them. That is, he can only swap two elements of different types in one move.
Tell him if he can sort the elements in non-decreasing values of a_i after performing any number of operations.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n (1 β€ n β€ 500) β the size of the arrays.
The second line contains n integers a_i (1 β€ a_i β€ 10^5) β the value of the i-th element.
The third line containts n integers b_i (b_i β \{0, 1\}) β the type of the i-th element.
Output
For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.
You may print each letter in any case (upper or lower).
Example
Input
5
4
10 20 20 30
0 1 0 1
3
3 1 2
0 1 1
4
2 2 4 8
1 1 1 1
3
5 15 4
0 0 0
4
20 10 100 50
1 0 0 1
Output
Yes
Yes
Yes
No
Yes
Note
For the first case: The elements are already in sorted order.
For the second case: Ashish may first swap elements at positions 1 and 2, then swap elements at positions 2 and 3.
For the third case: The elements are already in sorted order.
For the fourth case: No swap operations may be performed as there is no pair of elements i and j such that b_i β b_j. The elements cannot be sorted.
For the fifth case: Ashish may swap elements at positions 3 and 4, then elements at positions 1 and 2.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
l1 = list(map(int,input().split()))
l2 = list(map(int,input().split()))
r1 = []
r0 = []
for i in range(n):
if l2[i] == 1:
r1.append(l1[i])
else:
r0.append(l1[i])
c = 1
for i in range(1,len(r1)):
if r1[i] < r1[i-1]:
print("No")
c = 0
break
if c == 0:
continue
for i in range(1,len(r0)):
if r0[i] < r0[i-1]:
print("No")
c = 0
break
if c == 0:
continue
print("Yes")
``` | instruction | 0 | 14,344 | 12 | 28,688 |
No | output | 1 | 14,344 | 12 | 28,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1 | instruction | 0 | 14,345 | 12 | 28,690 |
Tags: greedy
Correct Solution:
```
t = int(input())
while t:
n = int(input())
l = [int(x) for x in input().split()]
assert len(l) == 2*n
c = [0]*2*n
o = []
for i in range(2*n):
if c[i]==1:
o.append(str(l[i]))
continue
for j in range(i+1, 2*n):
if l[i]==l[j]:
c[j]=1
print(" ".join(o))
t-=1
``` | output | 1 | 14,345 | 12 | 28,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1 | instruction | 0 | 14,346 | 12 | 28,692 |
Tags: greedy
Correct Solution:
```
import sys
def input():
return sys.stdin.readline().rstrip()
def input_split():
return [int(i) for i in input().split()]
testCases = int(input())
answers = []
for _ in range(testCases):
#take input
n = int(input())
arr = input_split()
found = [False for i in range(n+1)]
ans = []
for a in arr:
if not found[a]:
ans.append(a)
found[a] = True
answers.append(ans)
for ans in answers:
print(*ans, sep = ' ')
``` | output | 1 | 14,346 | 12 | 28,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1 | instruction | 0 | 14,347 | 12 | 28,694 |
Tags: greedy
Correct Solution:
```
n=int(input())
for _ in range(n):
m=int(input())
ar=list(map(int,input().split()))
x=[]
for i in ar:
if i not in x:
x.append(i)
p=len(x)
for i in range(p):
print(x[i],end=' ')
``` | output | 1 | 14,347 | 12 | 28,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1 | instruction | 0 | 14,348 | 12 | 28,696 |
Tags: greedy
Correct Solution:
```
t = int(input())
for i in range(t):
length = input()
inp = input()
inp_split = inp.split(" ")
numbers = [int(num) for num in inp_split]
check = []
for i in numbers:
if i not in check:
check.append(i)
final1 = [str(i) for i in check]
final = " ".join(final1)
print(final)
``` | output | 1 | 14,348 | 12 | 28,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1 | instruction | 0 | 14,349 | 12 | 28,698 |
Tags: greedy
Correct Solution:
```
t = int(input())
for _ in range(t):
s = set()
n = int(input())
a = list(map(int,input().split()))
for i in a:
if i not in s:
s.add(i)
print(i,end = ' ')
print()
``` | output | 1 | 14,349 | 12 | 28,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1 | instruction | 0 | 14,350 | 12 | 28,700 |
Tags: greedy
Correct Solution:
```
c=int(input())
for j in range(c):
n=int(input())
l=list(map(int,input().split()))
d=list(dict.fromkeys(l))
for i in d:
if i in l:
print(i,end=" ")
print()
``` | output | 1 | 14,350 | 12 | 28,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1 | instruction | 0 | 14,351 | 12 | 28,702 |
Tags: greedy
Correct Solution:
```
t = int(input())
#the number of test cases
for k in range(t):
n = int(input())
#the length of permutation
a = list(map(int, input().split()))
p = []
#the oiginal permuaion
for i in range(n * 2):
if a[i] not in p:
p.append(a[i])
for i in range(n):
print(p[i], end=" ")
print()
``` | output | 1 | 14,351 | 12 | 28,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1 | instruction | 0 | 14,352 | 12 | 28,704 |
Tags: greedy
Correct Solution:
```
for T in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
c=[0 for i in range(n+1)]
ans=[]
for i in arr:
if c[i]==0:
ans.append(str(i))
c[i]=1
print(' '.join(ans))
``` | output | 1 | 14,352 | 12 | 28,705 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1
Submitted Solution:
```
count = int(input())
for i in range(count):
n = int(input())
test = [int(j) for j in input().split()]
answer = []
for j in test:
if j not in answer:
answer.append(j)
for j in answer:
print(j, end = " ")
print()
``` | instruction | 0 | 14,353 | 12 | 28,706 |
Yes | output | 1 | 14,353 | 12 | 28,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
lst = list(map(int, input().split()))
myset = set()
sol = []
for item in lst:
if item not in myset:
myset.add(item)
sol.append(item)
for item in sol:
print(item, end=' ')
print()
``` | instruction | 0 | 14,354 | 12 | 28,708 |
Yes | output | 1 | 14,354 | 12 | 28,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1
Submitted Solution:
```
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = [int(item) for item in input().split()]
visited = [0] * (n + 1)
ans = []
for item in a:
if visited[item] == 1:
ans.append(item)
else:
visited[item] += 1
print(*ans)
``` | instruction | 0 | 14,355 | 12 | 28,710 |
Yes | output | 1 | 14,355 | 12 | 28,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1
Submitted Solution:
```
def permut(n,a):
l=[]
for i in range(2*n):
if len(l)==n:
break
if a[i] not in l:
l.append(a[i])
for i in l:
print(i,end=" ")
t=int(input())
for i in range(t):
n=int(input())
a=[int(j) for j in input().split()]
permut(n,a)
print()
``` | instruction | 0 | 14,356 | 12 | 28,712 |
Yes | output | 1 | 14,356 | 12 | 28,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1
Submitted Solution:
```
t=int(input())
for o in range(t):
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
if a[i] not in b:
b.append(a[i])
print(*b)
``` | instruction | 0 | 14,357 | 12 | 28,714 |
No | output | 1 | 14,357 | 12 | 28,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1
Submitted Solution:
```
t=int(input())
print('**********')
while t>0:
t-=1
n=int(input())
arr=list(map(str,input().split()))
num=set()
ans=''
for i in arr:
if i not in num:
num.add(i)
ans+=i
print(ans)
``` | instruction | 0 | 14,358 | 12 | 28,716 |
No | output | 1 | 14,358 | 12 | 28,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1
Submitted Solution:
```
t=int(input())
for i in range(t):
n=int(input())
lst=list(map(int,input().strip().split()))[:n]
res=[]
for ele in lst:
if ele not in res:
res.append(ele)
print(*res)
``` | instruction | 0 | 14,359 | 12 | 28,718 |
No | output | 1 | 14,359 | 12 | 28,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.
There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].
For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 400) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of permutation. The second line of the test case contains 2n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
Example
Input
5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1
Output
1 2
1 3 4 2
1 2 3 4 5
1 2 3
2 3 4 1
Submitted Solution:
```
t = int(input())
li = []
for k in range(t):
n = int(input())
permutation = map(int, input().split())
for j in permutation:
if j not in li:
li.append(j)
for l in li:
print(l, end=' ')
``` | instruction | 0 | 14,360 | 12 | 28,720 |
No | output | 1 | 14,360 | 12 | 28,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i-th problem is denoted by an integer a_i.
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n. The cost of a permutation is defined as the number of indices i (1β€ i<n) where |p_{i+1}-p_i|>1. You have the requirement that a_{p_i}β a_{p_{i+1}} for all 1β€ i< n.
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
Input
The first line contains a single integer t (1β€ tβ€ 10^4) β the number of test cases.
The first line of the description of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of problems in the contest.
The next line contains n integers a_1,a_2,β¦ a_n (1 β€ a_i β€ n) β the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, if there are no permutations that satisfy the required condition, print -1. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
Example
Input
4
6
2 1 2 3 1 1
5
1 1 1 2 2
8
7 7 2 7 7 1 8 7
10
1 2 3 4 1 1 2 3 4 1
Output
1
3
-1
2
Note
In the first test case, let p=[5, 4, 3, 2, 1, 6]. The cost is 1 because we jump from p_5=1 to p_6=6, and |6-1|>1. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1.
In the second test case, let p=[1,5,2,4,3]. The cost is 3 because |p_2-p_1|>1, |p_3-p_2|>1, and |p_4-p_3|>1. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3.
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is -1. | instruction | 0 | 14,369 | 12 | 28,738 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
def main():
n = int(input())
alst = list(map(int, input().split()))
cnt = [0 for _ in range(n + 1)]
for a in alst:
cnt[a] += 1
if max(cnt) > (n + 1) / 2:
print(-1)
return
ans = 0
end = [0 for _ in range(n + 1)]
for i in range(n - 1):
if alst[i] == alst[i + 1]:
end[alst[i]] += 1
end[alst[i + 1]] += 1
ans += 1
end[alst[0]] += 1
end[alst[-1]] += 1
print(ans + max(0, max(end) - ans - 2))
for _ in range(int(input())):
main()
``` | output | 1 | 14,369 | 12 | 28,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i-th problem is denoted by an integer a_i.
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n. The cost of a permutation is defined as the number of indices i (1β€ i<n) where |p_{i+1}-p_i|>1. You have the requirement that a_{p_i}β a_{p_{i+1}} for all 1β€ i< n.
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
Input
The first line contains a single integer t (1β€ tβ€ 10^4) β the number of test cases.
The first line of the description of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of problems in the contest.
The next line contains n integers a_1,a_2,β¦ a_n (1 β€ a_i β€ n) β the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, if there are no permutations that satisfy the required condition, print -1. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
Example
Input
4
6
2 1 2 3 1 1
5
1 1 1 2 2
8
7 7 2 7 7 1 8 7
10
1 2 3 4 1 1 2 3 4 1
Output
1
3
-1
2
Note
In the first test case, let p=[5, 4, 3, 2, 1, 6]. The cost is 1 because we jump from p_5=1 to p_6=6, and |6-1|>1. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1.
In the second test case, let p=[1,5,2,4,3]. The cost is 3 because |p_2-p_1|>1, |p_3-p_2|>1, and |p_4-p_3|>1. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3.
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is -1. | instruction | 0 | 14,370 | 12 | 28,740 |
Tags: constructive algorithms, greedy
Correct Solution:
```
def calc(A):
N = len(A)
if N == 1:return 0
X = [0] * N
for a in A:X[a] += 1
if max(X) > (N + 1) // 2:return -1
Y = [0] * N;Y[A[0]] += 1;Y[A[-1]] += 1
for a, b in zip(A, A[1:]):
if a == b:Y[a] += 2
su, ma = sum(Y), max(Y);cc = su - ma
return su // 2 - 1 + max(ma - cc - 2, 0) // 2
for _ in range(int(input())):N = int(input());print(calc([int(a) - 1 for a in input().split()]))
``` | output | 1 | 14,370 | 12 | 28,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i-th problem is denoted by an integer a_i.
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n. The cost of a permutation is defined as the number of indices i (1β€ i<n) where |p_{i+1}-p_i|>1. You have the requirement that a_{p_i}β a_{p_{i+1}} for all 1β€ i< n.
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
Input
The first line contains a single integer t (1β€ tβ€ 10^4) β the number of test cases.
The first line of the description of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of problems in the contest.
The next line contains n integers a_1,a_2,β¦ a_n (1 β€ a_i β€ n) β the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, if there are no permutations that satisfy the required condition, print -1. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
Example
Input
4
6
2 1 2 3 1 1
5
1 1 1 2 2
8
7 7 2 7 7 1 8 7
10
1 2 3 4 1 1 2 3 4 1
Output
1
3
-1
2
Note
In the first test case, let p=[5, 4, 3, 2, 1, 6]. The cost is 1 because we jump from p_5=1 to p_6=6, and |6-1|>1. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1.
In the second test case, let p=[1,5,2,4,3]. The cost is 3 because |p_2-p_1|>1, |p_3-p_2|>1, and |p_4-p_3|>1. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3.
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is -1. | instruction | 0 | 14,371 | 12 | 28,742 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n=int(input())
A=list(map(int,input().split()))
C=[0]*(n+1)
for a in A:
C[a]+=1
MAX=-1
MAXI=-1
for i in range(n+1):
if C[i]>MAX:
MAX=C[i]
MAXI=i
if MAX>(n+1)//2:
print(-1)
continue
NOW=A[0]
T=[]
for i in range(n-1):
if A[i]==A[i+1]:
T.append((NOW,A[i]))
NOW=A[i+1]
T.append((NOW,A[-1]))
#print(T)
D=[0]*(n+1)
OTHER=[0]*(n+1)
for x,y in T:
if x==y:
D[x]+=1
OTHER[x]+=1
else:
OTHER[x]+=1
OTHER[y]+=1
TUIKA=0
LEN=len(T)
for i in range(n+1):
if D[i]>LEN-OTHER[i]+1:
TUIKA+=D[i]-(LEN-OTHER[i]+1)
print(LEN-1+TUIKA)
``` | output | 1 | 14,371 | 12 | 28,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i-th problem is denoted by an integer a_i.
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n. The cost of a permutation is defined as the number of indices i (1β€ i<n) where |p_{i+1}-p_i|>1. You have the requirement that a_{p_i}β a_{p_{i+1}} for all 1β€ i< n.
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
Input
The first line contains a single integer t (1β€ tβ€ 10^4) β the number of test cases.
The first line of the description of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of problems in the contest.
The next line contains n integers a_1,a_2,β¦ a_n (1 β€ a_i β€ n) β the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, if there are no permutations that satisfy the required condition, print -1. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
Example
Input
4
6
2 1 2 3 1 1
5
1 1 1 2 2
8
7 7 2 7 7 1 8 7
10
1 2 3 4 1 1 2 3 4 1
Output
1
3
-1
2
Note
In the first test case, let p=[5, 4, 3, 2, 1, 6]. The cost is 1 because we jump from p_5=1 to p_6=6, and |6-1|>1. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1.
In the second test case, let p=[1,5,2,4,3]. The cost is 3 because |p_2-p_1|>1, |p_3-p_2|>1, and |p_4-p_3|>1. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3.
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is -1. | instruction | 0 | 14,372 | 12 | 28,744 |
Tags: constructive algorithms, greedy
Correct Solution:
```
from collections import Counter
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
if max(Counter(A).values()) > (n + 1) // 2: print(-1)
else:
k = 0
cnt = Counter()
cnt[A[0]] += 1
cnt[A[-1]] += 1
for i in range(n - 1):
if A[i] == A[i + 1]:
cnt[A[i]] += 2
k += 1
print(k + max(0, max(cnt.values()) - (k + 2)))
``` | output | 1 | 14,372 | 12 | 28,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i-th problem is denoted by an integer a_i.
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n. The cost of a permutation is defined as the number of indices i (1β€ i<n) where |p_{i+1}-p_i|>1. You have the requirement that a_{p_i}β a_{p_{i+1}} for all 1β€ i< n.
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
Input
The first line contains a single integer t (1β€ tβ€ 10^4) β the number of test cases.
The first line of the description of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of problems in the contest.
The next line contains n integers a_1,a_2,β¦ a_n (1 β€ a_i β€ n) β the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, if there are no permutations that satisfy the required condition, print -1. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
Example
Input
4
6
2 1 2 3 1 1
5
1 1 1 2 2
8
7 7 2 7 7 1 8 7
10
1 2 3 4 1 1 2 3 4 1
Output
1
3
-1
2
Note
In the first test case, let p=[5, 4, 3, 2, 1, 6]. The cost is 1 because we jump from p_5=1 to p_6=6, and |6-1|>1. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1.
In the second test case, let p=[1,5,2,4,3]. The cost is 3 because |p_2-p_1|>1, |p_3-p_2|>1, and |p_4-p_3|>1. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3.
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is -1. | instruction | 0 | 14,373 | 12 | 28,746 |
Tags: constructive algorithms, greedy
Correct Solution:
```
T, = map(int, input().split())
for _ in range(T):
N, = map(int, input().split())
X = list(map(int, input().split()))
d = [0] * (N+1)
for x in X:
d[x] += 1
if max(d) > (N+1)//2:
print(-1)
continue
L = []
stack = []
for i in range(N):
if len(stack) == 0 or stack[-1] != X[i]:
stack.append(X[i])
else:
L.append(stack)
stack = [X[i]]
if stack:
L.append(stack)
cnt = [0]*(N+1)
for st in L:
if st[0] == st[-1]:
cnt[st[0]] += 1
mx, mxi = 0, 0
for i in range(N+1):
if cnt[i] > mx:
mx, mxi = cnt[i], i
tmp = 0
for st in L:
if st[0] != mxi and st[-1] != mxi:
tmp += 1
# print(len(L)-1, mx, tmp)
print(len(L)-1 + max(0, mx - tmp - 1))
``` | output | 1 | 14,373 | 12 | 28,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i-th problem is denoted by an integer a_i.
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n. The cost of a permutation is defined as the number of indices i (1β€ i<n) where |p_{i+1}-p_i|>1. You have the requirement that a_{p_i}β a_{p_{i+1}} for all 1β€ i< n.
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
Input
The first line contains a single integer t (1β€ tβ€ 10^4) β the number of test cases.
The first line of the description of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of problems in the contest.
The next line contains n integers a_1,a_2,β¦ a_n (1 β€ a_i β€ n) β the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, if there are no permutations that satisfy the required condition, print -1. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
Example
Input
4
6
2 1 2 3 1 1
5
1 1 1 2 2
8
7 7 2 7 7 1 8 7
10
1 2 3 4 1 1 2 3 4 1
Output
1
3
-1
2
Note
In the first test case, let p=[5, 4, 3, 2, 1, 6]. The cost is 1 because we jump from p_5=1 to p_6=6, and |6-1|>1. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1.
In the second test case, let p=[1,5,2,4,3]. The cost is 3 because |p_2-p_1|>1, |p_3-p_2|>1, and |p_4-p_3|>1. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3.
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is -1. | instruction | 0 | 14,374 | 12 | 28,748 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
N = n
a = list(map(int,input().split()))
a = [a[i]-1 for i in range(n)]
E = []
cnt = [0 for i in range(n)]
L = 0
for i in range(1,n):
if a[i]==a[i-1]:
cnt[a[i-1]] += 1
cnt[a[L]] += 1
E.append((L,i-1))
L = i
cnt[a[n-1]] += 1
cnt[a[L]] += 1
E.append((L,n-1))
idx = -1
M = max(cnt)
for i in range(n):
if cnt[i]==M:
idx = i
break
k = len(E)
cut = 0
for L,R in E:
for i in range(L,R):
if a[i]!=idx and a[i+1]!=idx:
cut += 1
#print(k,M,cut)
ans = -1
if k+1>=M:
ans = k-1
else:
need = M-(k+1)
if need<=cut:
ans = M - 2
#print("ANS")
print(ans)
``` | output | 1 | 14,374 | 12 | 28,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i-th problem is denoted by an integer a_i.
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n. The cost of a permutation is defined as the number of indices i (1β€ i<n) where |p_{i+1}-p_i|>1. You have the requirement that a_{p_i}β a_{p_{i+1}} for all 1β€ i< n.
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
Input
The first line contains a single integer t (1β€ tβ€ 10^4) β the number of test cases.
The first line of the description of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of problems in the contest.
The next line contains n integers a_1,a_2,β¦ a_n (1 β€ a_i β€ n) β the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, if there are no permutations that satisfy the required condition, print -1. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
Example
Input
4
6
2 1 2 3 1 1
5
1 1 1 2 2
8
7 7 2 7 7 1 8 7
10
1 2 3 4 1 1 2 3 4 1
Output
1
3
-1
2
Note
In the first test case, let p=[5, 4, 3, 2, 1, 6]. The cost is 1 because we jump from p_5=1 to p_6=6, and |6-1|>1. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1.
In the second test case, let p=[1,5,2,4,3]. The cost is 3 because |p_2-p_1|>1, |p_3-p_2|>1, and |p_4-p_3|>1. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3.
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is -1. | instruction | 0 | 14,375 | 12 | 28,750 |
Tags: constructive algorithms, greedy
Correct Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
cnt = 0
aCnt = [0] * n
for i in range(n):
aCnt[a[i] - 1] += 1
flag = True
for elem in aCnt:
if elem > n - (n // 2):
flag = False
if not flag:
print(-1)
continue
endCountDouble = [0] * (n + 1)
endCountSingle = [0] * (n + 1)
last = a[0]
lastInd = 0
for i in range(n-1):
if a[i] == a[i + 1]:
cnt += 1
if last == a[i]:
endCountDouble[last] += 1
else:
endCountSingle[last] += 1
endCountSingle[a[i]] += 1
last = a[i]
lastInd = i
if last == a[n-1]:
endCountDouble[last] += 1
else:
endCountSingle[last] += 1
endCountSingle[a[n-1]] += 1
last = a[n-1]
segCount = cnt + 1
adjCount = 0
whattoAdj = 0
for i in range(n + 1):
if endCountDouble[i] > (segCount - endCountSingle[i]) - (segCount - endCountSingle[i]) // 2:
adjCount = endCountDouble[i] - ((segCount - endCountSingle[i]) - (segCount - endCountSingle[i]) // 2)
whattoAdj = i
if adjCount != 0:
badPair = endCountDouble[whattoAdj]
goodPair = segCount - endCountSingle[whattoAdj] - endCountDouble[whattoAdj]
adjAble = 0
for i in range(n - 1):
if a[i] != i and a[i + 1] != i:
adjAble += 1
if adjAble >= badPair - goodPair - 1:
print(cnt + badPair - goodPair - 1)
else:
print(-1)
else:
print(cnt)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | output | 1 | 14,375 | 12 | 28,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i-th problem is denoted by an integer a_i.
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n. The cost of a permutation is defined as the number of indices i (1β€ i<n) where |p_{i+1}-p_i|>1. You have the requirement that a_{p_i}β a_{p_{i+1}} for all 1β€ i< n.
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
Input
The first line contains a single integer t (1β€ tβ€ 10^4) β the number of test cases.
The first line of the description of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of problems in the contest.
The next line contains n integers a_1,a_2,β¦ a_n (1 β€ a_i β€ n) β the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, if there are no permutations that satisfy the required condition, print -1. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
Example
Input
4
6
2 1 2 3 1 1
5
1 1 1 2 2
8
7 7 2 7 7 1 8 7
10
1 2 3 4 1 1 2 3 4 1
Output
1
3
-1
2
Note
In the first test case, let p=[5, 4, 3, 2, 1, 6]. The cost is 1 because we jump from p_5=1 to p_6=6, and |6-1|>1. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1.
In the second test case, let p=[1,5,2,4,3]. The cost is 3 because |p_2-p_1|>1, |p_3-p_2|>1, and |p_4-p_3|>1. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3.
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is -1. | instruction | 0 | 14,376 | 12 | 28,752 |
Tags: constructive algorithms, greedy
Correct Solution:
```
# region fastio # from https://codeforces.com/contest/1333/submission/75948789
import sys, io, os
BUFSIZE = 8192
class FastIO(io.IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = io.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(io.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")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#endregion
T = int(input())
for _ in range(T):
N = int(input())
A = list(map(int, input().split()))
Counts = [0] * (N+1)
for a in A:
Counts[a] += 1
ma = max(Counts)
if ma * 2 - 1 > N:
print(-1)
continue
Cons = [0] * (N+1)
for a1, a2 in zip(A, A[1:]):
if a1 == a2:
Cons[a1] += 1
ans1 = sum(Cons)
ma = -1
ama = -101010101
for i, cons in enumerate(Cons):
if ma < cons:
ama = i
ma = cons
a_old = A[0]
left = A[0]
C = [0, 0, 0]
for a in A[1:]+[A[-1]]:
if a == a_old:
right = a
C[(left==ama)+(right==ama)] += 1
left = a
a_old = a
C[2] -= C[0]
if C[2] <= 1:
ans = ans1
else:
ans = ans1 + C[2] - 1
print(ans)
``` | output | 1 | 14,376 | 12 | 28,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x. | instruction | 0 | 14,576 | 12 | 29,152 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import itertools
import math
def main():
n, k = map(int, input().split())
a = 1
b = n
res = []
for i in range(k - 1):
if i % 2:
res.append(a)
a += 1
else:
res.append(b)
b -= 1
if k % 2:
res += range(b, a - 1, -1)
else:
res += range(a, b + 1)
print(" ".join(map(str, res)))
if __name__ == "__main__":
main()
``` | output | 1 | 14,576 | 12 | 29,153 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x. | instruction | 0 | 14,577 | 12 | 29,154 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n, k = map(int, input().split())
res = []
for i in range(1, k+2):
if i % 2 == 1:
res.append((i+1) // 2)
else:
res.append(k + 2 - (i//2))
res += list(range(k+2, n+1))
print(*res)
``` | output | 1 | 14,577 | 12 | 29,155 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x. | instruction | 0 | 14,578 | 12 | 29,156 |
Tags: constructive algorithms, greedy
Correct Solution:
```
from sys import exit
n,k = map(int,input().split())
if n<=2:
print(*[i for i in range(1,n+1)])
exit()
else:
d = [1]
for i in range(n+1):
if k<=0:
break
if i%2==0:
d.append(k+d[-1])
k-=1
else:
d.append(d[-1]-k)
k-=1
if len(d)==n:
print(*d)
else:
r = set(d)
print(*d,*[i for i in range(1,n+1) if i not in r])
``` | output | 1 | 14,578 | 12 | 29,157 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x. | instruction | 0 | 14,579 | 12 | 29,158 |
Tags: constructive algorithms, greedy
Correct Solution:
```
entry = input().split()
p = int(entry[0])
k = int(entry[1])
forward = 1
backward = k + 1
answer = []
for controller in range((k//2) + 1):
answer.append(str(forward))
if forward != backward:
answer.append(str(backward))
forward += 1
backward -= 1
for filler in range(k+2, p+1):
answer.append(str(filler))
print(' '.join(answer))
``` | output | 1 | 14,579 | 12 | 29,159 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x. | instruction | 0 | 14,580 | 12 | 29,160 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n,k = input().split()
answer = []
answer.append(1)
appendVar = 1
lowVar = 2
highVar = 1 + int(k)
for i in range(1, int(k) + 1):
if i % 2 == 1:
appendVar = highVar
highVar -= 1
answer.append(appendVar)
else:
appendVar = lowVar
lowVar += 1
answer.append(appendVar)
for i in range(int(k)+2, int(n) + 1):
answer.append(i)
print(*answer)
``` | output | 1 | 14,580 | 12 | 29,161 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x. | instruction | 0 | 14,581 | 12 | 29,162 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n,k=input().split()
n=int(n)
k=int(k)-1
b=[]
s=(k+3)//2
b.append(s)
i=0
if k%2==0:
i=1
else: i=-1
while k>-1:
s=s+i
b.append(s)
if i>0:
i=-1*i-1
else: i=-1*i+1
k-=1
while s<n:
s+=1
b.append(s)
for d in range(len(b)):
print(b[d],end=' ')
``` | output | 1 | 14,581 | 12 | 29,163 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x. | instruction | 0 | 14,582 | 12 | 29,164 |
Tags: constructive algorithms, greedy
Correct Solution:
```
f = input()
n , k = map(int , f.split(' '))
x = n
y = 1
for i in range(k):
if(i % 2 == 0):
print(x, end=" ")
x -= 1
else:
print(y, end=" ")
y += 1
bas = 0
t = 0
if k % 2 == 0:
bas = y
t = 1
else:
bas = x
t = -1
for i in range(n - k):
print(bas, end = " ")
bas += t
``` | output | 1 | 14,582 | 12 | 29,165 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x. | instruction | 0 | 14,583 | 12 | 29,166 |
Tags: constructive algorithms, greedy
Correct Solution:
```
# Description of the problem can be found at http://codeforces.com/problemset/problem/482/A
n, k = map(int, input().split())
l_n = list()
h = n
l = 1
for i in range(n):
if i < k:
if i % 2 == 0:
l_n.append(i // 2 + 1)
l = i // 2 + 1
else:
l_n.append(n - i // 2)
h = n - i // 2
else:
if i % 2 == 1:
l_n.extend(l + x for x in range(1, n + 1 - i))
if i % 2 == 0:
l_n.extend(h - x for x in range(1, n + 1 - i))
break
print(" ".join(str(x) for x in l_n))
``` | output | 1 | 14,583 | 12 | 29,167 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x.
Submitted Solution:
```
n,k = map(int,input().split())
a = [i for i in range(1,n+1)]
ans = []
l = 0
r = n-1
while l<r:
ans.append(a[l])
ans.append(a[r])
l+=1
r-=1
if l<=r:
ans.append(a[l])
ans = ans[:k]
if k%2==1:
ans += a[k//2+1:k//2+1 + n-k]
else:
ans += a[k//2:k//2 + n-k][::-1]
print(*ans)
``` | instruction | 0 | 14,584 | 12 | 29,168 |
Yes | output | 1 | 14,584 | 12 | 29,169 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x.
Submitted Solution:
```
a, b = map(int, input().split(' '))
for i in range(1, a - b + 1):
print(i)
curr = i
if b % 2 == 0:
for i in range(b, 0, -1):
if i % 2 == 0:
curr = curr + i
else:
curr = curr - i
print(curr)
else:
for i in range(b, 0, -1):
if i % 2 == 1:
curr = curr + i
else:
curr = curr - i
print(curr)
``` | instruction | 0 | 14,585 | 12 | 29,170 |
Yes | output | 1 | 14,585 | 12 | 29,171 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x.
Submitted Solution:
```
#l1 = int(input())
l2 = input().split()
x = int(l2[0])
y = int(l2[1])
c = x - y
d = True;
for i in range (1, x - y + 1):
print(str(i) + " ", end = '')
v = y
while (v >= 1):
if (d):
print(str(c+v) + " ", end = '')
c += v;
d = False;
else:
print(str(c-v) + " ", end = '')
c -= v;
d = True;
v -= 1
``` | instruction | 0 | 14,586 | 12 | 29,172 |
Yes | output | 1 | 14,586 | 12 | 29,173 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x.
Submitted Solution:
```
'''input
3 2
'''
n, k = map(int, input().split())
cur1 = 1
cur2 = n
temp = 0
tt = 0
for i in range(n):
if i < k:
if (i & 1) == 0:
print(cur1, end = ' ')
cur1 += 1
temp = cur1
tt = 1
else:
print(cur2, end = ' ')
cur2 -= 1
temp = cur2
tt = -1
else:
if tt == 1:
print(cur1, end = ' ')
cur1 += 1
else:
print(cur2, end = ' ')
cur2 -= 1
print()
``` | instruction | 0 | 14,587 | 12 | 29,174 |
Yes | output | 1 | 14,587 | 12 | 29,175 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x.
Submitted Solution:
```
n,k=map(int,input().split())
s=''
for i in range(k//2):
s+=str(i+1)+' '
s+=str(n-i)+' '
if k!=1 and k%2: s+=[n-k//2]
for i in range(k//2+1,n-(-(-k//2))+1):
s+=str(i)+' '
print(s)
``` | instruction | 0 | 14,588 | 12 | 29,176 |
No | output | 1 | 14,588 | 12 | 29,177 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x.
Submitted Solution:
```
import sys
n, k = map(int, input().split())
if k == 1:
for i in range(1, n+1):
print(i, end=' ')
sys.exit();
for i in range(0, k//2):
print(i+1, n-i, end=' ')
if k%2:
z = 0
for i in range(n-k//2, k//2+2, -1):
z = i
print(i, end=' ')
print(z-2, z-1)
else:
for i in range(n-k//2, k//2, -1):
print(i, end=' ')
``` | instruction | 0 | 14,589 | 12 | 29,178 |
No | output | 1 | 14,589 | 12 | 29,179 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x.
Submitted Solution:
```
n,k = map(int,input().split())
ll = [str(i) for i in range(1,n+1)]
print(' '.join([ll[0]] + [ll[k]] + ll[1:k] + ll[k+1:]))
``` | instruction | 0 | 14,590 | 12 | 29,180 |
No | output | 1 | 14,590 | 12 | 29,181 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 β€ k < n β€ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Examples
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of number x.
Submitted Solution:
```
# python3
n, k = [int(x) for x in input().split()]
begin = 1
if k == 1:
for x in range(1, n+1):
print(x, end=" ")
else:
# k = 2, +2 -1: 1 3 2
# k = 3, +3 -2 + 1: 1 4 2 3
# k = 4, +4 -3 + 2 -1: 1 5 2 4 3
for y in range(n-k-1):
print(begin+y, end=" ")
diff = n-k
high = n
while high >= diff + 1:
print(diff, end=" ")
print(high, end=" ")
diff += 1
high -= 1
print(high)
# if diff == high and k % 2 != 0:
# print(high)
# for z in range((k+1)/2):
# # Difference continues to decrease by 1
# print(diff + z, end=" ")
# print(n - z, end=" ")
# if k % 2 == 0:
# print((diff+n)//2)
``` | instruction | 0 | 14,591 | 12 | 29,182 |
No | output | 1 | 14,591 | 12 | 29,183 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integer numbers.
Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i.
You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability.
Input
The first line of the input contains one integer n (2 β€ n β€ 10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^5) β elements of the array a.
Output
Print one integer β the minimum possible instability of the array if you have to remove exactly one element from the array a.
Examples
Input
4
1 3 3 7
Output
2
Input
2
1 100000
Output
0
Note
In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2.
In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. | instruction | 0 | 15,122 | 12 | 30,244 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = sorted([*map(int, input().split())])
print (min(a[-1] - a[1], a[-2] - a[0]))
``` | output | 1 | 15,122 | 12 | 30,245 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integer numbers.
Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i.
You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability.
Input
The first line of the input contains one integer n (2 β€ n β€ 10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^5) β elements of the array a.
Output
Print one integer β the minimum possible instability of the array if you have to remove exactly one element from the array a.
Examples
Input
4
1 3 3 7
Output
2
Input
2
1 100000
Output
0
Note
In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2.
In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. | instruction | 0 | 15,123 | 12 | 30,246 |
Tags: implementation
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split()))
arr.sort()
left=arr[1]-arr[0]
right=arr[-1]-arr[-2]
if(right>left):
print(arr[-2]-arr[0])
elif(left>right):
print(arr[-1]-arr[1])
elif(left==right):
print(arr[-1]-arr[1])
``` | output | 1 | 15,123 | 12 | 30,247 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integer numbers.
Let instability of the array be the following value: max_{i = 1}^{n} a_i - min_{i = 1}^{n} a_i.
You have to remove exactly one element from this array to minimize instability of the resulting (n-1)-elements array. Your task is to calculate the minimum possible instability.
Input
The first line of the input contains one integer n (2 β€ n β€ 10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^5) β elements of the array a.
Output
Print one integer β the minimum possible instability of the array if you have to remove exactly one element from the array a.
Examples
Input
4
1 3 3 7
Output
2
Input
2
1 100000
Output
0
Note
In the first example you can remove 7 then instability of the remaining array will be 3 - 1 = 2.
In the second example you can remove either 1 or 100000 then instability of the remaining array will be 100000 - 100000 = 0 and 1 - 1 = 0 correspondingly. | instruction | 0 | 15,124 | 12 | 30,248 |
Tags: implementation
Correct Solution:
```
x = int(input())
m = []
for i in input().split():
m.append(int(i))
min = m[0]
max = m[0]
for i in range(x):
if m[i] < min:
min = m[i]
if m[i] > max:
max = m[i]
m.remove(min)
x -= 1
min2 = m[0]
for i in range(x):
if min2 > m[i]:
min2 = m[i]
m.append(min)
m.remove(max)
max2 = m[0]
for i in range(x):
if max2 < m[i]:
max2 = m[i]
r = max - min
if max2 - min < r:
r = max2 - min
if max - min2 < r:
r = max - min2
print(r)
``` | output | 1 | 15,124 | 12 | 30,249 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.