message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide a correct Python 3 solution for this coding contest problem. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1
instruction
0
81,678
12
163,356
"Correct Solution: ``` def maxHeapify(A, i): l = 2 * i r = 2 * i + 1 largest = i if l < len(A) and A[l] > A[largest]: largest = l if r < len(A) and A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] maxHeapify(A, largest) def buildMaxHeap(A): for i in reversed(range(1, H // 2 + 1)): maxHeapify(A, i) H = int(input()) A = list(map(int, input().split())) A.insert(0, None) buildMaxHeap(A) print(' ' + ' '.join(map(str, A[1:]))) ```
output
1
81,678
12
163,357
Provide a correct Python 3 solution for this coding contest problem. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1
instruction
0
81,679
12
163,358
"Correct Solution: ``` def maxHeapify(A,i): left,right=2*i,2*i+1 if left<n and A[left]>A[i]:largest=left else:largest=i if right<n and A[right]>A[largest]:largest=right if largest!=i: A[i],A[largest]=A[largest],A[i] maxHeapify(A,largest) def buildMaxHeap(A): for i in reversed(range(1,n//2+1)): maxHeapify(A,i) n=int(input())+1 A=[0]+list(map(int,input().split())) buildMaxHeap(A) print(" ",end="") print(*A[1:]) ```
output
1
81,679
12
163,359
Provide a correct Python 3 solution for this coding contest problem. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1
instruction
0
81,680
12
163,360
"Correct Solution: ``` import sys input = sys.stdin.readline def parent(i): return i//2 def left(i): return 2*i def right(i): return 2*i+1 def maxHeapify(A, i): l = left(i) r = right(i) # 左の子、自分、右の子で値が最大のノードを選ぶ if l <= H and A[l] > A[i]: largest = l else: largest = i if r <= H and A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] # pythonはこれで要素の入れ替えが可能 maxHeapify(A, largest) def buildMaxHeap(A, H): for i in range(H//2, 0, -1): maxHeapify(A, i) H = int(input()) A = [None] + list(map(int, input().split())) buildMaxHeap(A, H) print(' ' + ' '.join(map(str, A[1:]))) ```
output
1
81,680
12
163,361
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1 Submitted Solution: ``` def maxHeapiify(A, i, n): l = 2*i+1 r = 2*i+2 if l < n and A[l] > A[i]: largest = l else: largest = i if r < n and A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] maxHeapiify(A, largest, n) def buildMaxHeap(A, n): for i in range(n//2, -1, -1): maxHeapiify(A, i, n) def main(): N = int(input()) H = list(map(int, input().split())) buildMaxHeap(H, N) print(" ", end="") print(*H) if __name__ == '__main__': main() ```
instruction
0
81,681
12
163,362
Yes
output
1
81,681
12
163,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1 Submitted Solution: ``` n = int(input()) a = [0] + list(map(int,input().split())) def heapSort(a, i): global n l = 2 * i r = 2 * i + 1 largest = l if l <= n and a[l] > a[i] else i if r <= n and a[r] > a[largest]: largest = r if largest != i: a[largest], a[i] = a[i], a[largest] heapSort(a, largest) for i in range(int( n / 2 ), 0, -1): heapSort(a, i) print(" "+" ".join(map(str,a[1:]))) ```
instruction
0
81,682
12
163,364
Yes
output
1
81,682
12
163,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1 Submitted Solution: ``` # 最大ヒープ Maximum heap def left(i): return 2 * i def right(i): return 2 * i + 1 def maxHeapify(A, i): l = left(i) r = right(i) largest = i if l <= H and A[l] > A[i]: largest = l if r <= H and A[r] > A[largest]: largest = r if largest != i: tmp = A[i] A[i] = A[largest] A[largest] = tmp maxHeapify(A, largest) def buildMaxHeap(A): for i in range(H//2, 0, -1): maxHeapify(A, i) H = int(input()) A = [-1] + [int(i) for i in input().split()] buildMaxHeap(A) for i in range(1, H+1): if i == H+1: print(A[i]) else: print(" ", A[i], sep="", end="") print() ```
instruction
0
81,683
12
163,366
Yes
output
1
81,683
12
163,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1 Submitted Solution: ``` def MaxHeapify(a,i): l = i*2 r = i*2+1 j = i if l<len(a) and a[l]>a[j]:j = l if r<len(a) and a[r]>a[j]:j = r if j!=i: a[i],a[j] = a[j],a[i] MaxHeapify(a,j) def init(a): for i in reversed(range(1,len(a)//2+1)): MaxHeapify(a,i) def main(): n = int(input())+1 a = [-1]+list(map(int,input().split())) init(a) for i in range(1,n):print (' %d'%a[i],end='') print () if __name__ == '__main__': main() ```
instruction
0
81,684
12
163,368
Yes
output
1
81,684
12
163,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1 Submitted Solution: ``` def maxHeapify(A,i,H): l=2*i r=2*i+1 #select the node which has the maximum value if l<=H and A[l-1]>A[i-1]: largest=l else: largest=i if r<=H and A[r-1]>A[l-1]: largest=r #value of children is larger than that of i if largest!=i: A[i-1],A[largest-1]=A[largest-1],A[i-1] maxHeapify(A,largest,H) H=int(input()) A=list(map(int,input().split())) for i in range(H//2,0,-1): maxHeapify(A,i,H) print(A) ```
instruction
0
81,685
12
163,370
No
output
1
81,685
12
163,371
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10 ** 9) def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) H=INT() # 1-indexedにしたいので先頭に番兵的なものを足す A=[0]+LIST() def maxHeapify(A, i): l=i*2 r=i*2+1 if l<=H and A[l]>A[i]: mx=l else: mx=i if r<=H and A[r]>A[mx]: mx=r if mx!=i: A[i],A[mx]=A[mx],A[i] maxHeapify(A, mx) for i in range(H//2, 0, -1): maxHeapify(A, i) print(*A[1:]) ```
instruction
0
81,686
12
163,372
No
output
1
81,686
12
163,373
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1 Submitted Solution: ``` H = int(input()) A = [None] + list(map(int, input().split())) def max_heapify(A, i): l = i * 2 r = i * 2 + 1 largest = i if l < H: if A[l] > A[largest]: largest = l if r < H: if A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] max_heapify(A, largest) def build_max_heap(A): for i in reversed(range(1, int(H / 2))): max_heapify(A, i) build_max_heap(A) print(" ", end = "") print(*A[1:]) ```
instruction
0
81,687
12
163,374
No
output
1
81,687
12
163,375
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself. Here is an example of a max-heap. <image> Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code. $maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap. 1 maxHeapify(A, i) 2 l = left(i) 3 r = right(i) 4 // select the node which has the maximum value 5 if l ≤ H and A[l] > A[i] 6 largest = l 7 else 8 largest = i 9 if r ≤ H and A[r] > A[largest] 10 largest = r 11 12 if largest ≠ i // value of children is larger than that of i 13 swap A[i] and A[largest] 14 maxHeapify(A, largest) // call recursively The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner. 1 buildMaxHeap(A) 2 for i = H/2 downto 1 3 maxHeapify(A, i) Input In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$). Output Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value. Example Input 10 4 1 3 2 16 9 10 14 8 7 Output 16 14 10 8 7 9 3 2 4 1 Submitted Solution: ``` def maxHeapify(heap, i): while (i + 1) * 2 <= len(heap): left = (i + 1) * 2 - 1 right = (i + 1) * 2 largest = left if heap[left] > heap[i] and left <= len(heap) else i largest = right if heap[right] > heap[largest] and right <= len(heap) if largest != i: heap[i], heap[largest] = heap[largest], heap[i] maxHeapify(heap, largest) def buildMaxHeap(heap): for i in range(len(heap), 1): maxHeapify(heap, i) n = int(input()) heap = list(map(int, input().split())) buildMaxHeap(heap) out = '' for key in heap: out += ' str(key)' print(out) ```
instruction
0
81,688
12
163,376
No
output
1
81,688
12
163,377
Provide tags and a correct Python 3 solution for this coding contest problem. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6.
instruction
0
81,966
12
163,932
Tags: brute force, graphs, implementation Correct Solution: ``` n=int(input()) arr=list(map(int,input().split())) arr1=arr[::] l=[i for i in range(1,2*n+1)] d=0 f=0 k=0 ans=10**20 while(True): if arr==l: ans=min(ans,d) k=0 break if f%2==0: for i in range(0,2*n,2): c=arr[i] arr[i]=arr[i+1] arr[i+1]=c else: arr=arr[n::]+arr[:n:] d+=1 f+=1 if d>2*n: k=1 print(-1) break if k==0: arr=arr1[::] d=0 f=1 while (True): if arr == l: ans = min(ans, d) k=0 break if f % 2 == 0: for i in range(0,2 * n, 2): c = arr[i] arr[i] =arr[i + 1] arr[i + 1] = c else: arr = arr[n:] + arr[:n] d += 1 f+=1 if d > 2 * n: k = 1 print(-1) break if k==0: print(ans) ```
output
1
81,966
12
163,933
Provide tags and a correct Python 3 solution for this coding contest problem. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6.
instruction
0
81,967
12
163,934
Tags: brute force, graphs, implementation Correct Solution: ``` import sys def p1(ls): for i in range (1,n+1): temp=ls[(2*i)-1] ls[(2*i)-1]=ls[(2*i)-2] ls[(2*i)-2]=temp return ls def p2(ls): ls1=ls[n:]+ls[0:n] ls=ls1 return ls def check(ls, flag): if ls==sorted(ls): flag=1 return flag n=int(input()); ls=[]; count=0; flag=0 ls=list(map(int, input().split())); lsc=ls.copy() if(check(ls, flag)): print(count) sys.exit() while(True): ls=p1(ls) count+=1 if(check(ls, flag)): flag=check(ls, flag); break if(ls==lsc): break ls=p2(ls) count+=1 if(check(ls, flag)): flag=check(ls, flag); break if(ls==lsc): break count1=count count=0; flag1=0; ls.clear() ls=lsc while(True): ls=p2(ls) count+=1 if(check(ls, flag1)): flag1=check(ls, flag1); break if(ls==lsc): break ls=p1(ls) count+=1 if(check(ls, flag1)): flag1=check(ls, flag1); break if(ls==lsc): break count2=count if(flag==0 and flag1==0): print(-1) else: print(min(count1, count2)) ```
output
1
81,967
12
163,935
Provide tags and a correct Python 3 solution for this coding contest problem. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6.
instruction
0
81,968
12
163,936
Tags: brute force, graphs, implementation Correct Solution: ``` def main(): n=int(input()) n*=2 arr=readIntArr() # if n//2 is even, there are only 4 outcomes. # if n//2 is odd, there are n outcomes. # since 1 and 2 are reversible, to get different outcomes 1 must be alternated # with 2 def perform1(): for i in range(1,n,2): temp=arr[i] arr[i]=arr[i-1] arr[i-1]=temp def perform2(): for i in range(n//2): temp=arr[i] arr[i]=arr[i+n//2] arr[i+n//2]=temp def check(): for i in range(n): if arr[i]!=i+1: return False return True ok=False oneNext=True ans=-1 if (n//2)%2==0: for steps in range(4): if check(): ok=True break if oneNext: perform1() else: perform2() oneNext=not oneNext if ok: ans=min(steps,4-steps) else: for steps in range(n): if check(): ok=True break if oneNext: perform1() else: perform2() oneNext=not oneNext if ok: ans=min(steps,n-steps) print(ans) return import sys input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok) # input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS. def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def multiLineArrayPrint(arr): print('\n'.join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print('\n'.join([' '.join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] # def readFloatArr(): # return [float(x) for x in input().split()] def makeArr(defaultVal,dimensionArr): # eg. makeArr(0,[n,m]) dv=defaultVal;da=dimensionArr if len(da)==1:return [dv for _ in range(da[0])] else:return [makeArr(dv,da[1:]) for _ in range(da[0])] def queryInteractive(x,y): print('? {} {}'.format(x,y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print('! {}'.format(ans)) sys.stdout.flush() inf=float('inf') MOD=10**9+7 for _abc in range(1): main() ```
output
1
81,968
12
163,937
Provide tags and a correct Python 3 solution for this coding contest problem. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6.
instruction
0
81,969
12
163,938
Tags: brute force, graphs, implementation Correct Solution: ``` n = input() asu1 = [int(k) for k in input().split()] def op1(a): for i in range(0,len(a),2): temp = a[i] a[i]=a[i+1] a[i+1]= temp return a def op2(a): for i in range(0,len(a)//2): temp = a[i] a[i]=a[i+len(a)//2] a[i+len(a)//2]= temp return a def function(b, one): l=0 original = b.copy() sorted = original.copy() sorted.sort() while b!=sorted: l+=1 if one: b= op2(b) one = False else: b=op1(b) one= True if b==original: return(-1) return (l) c,d = function(asu1.copy(),False), function(asu1,True) if c == -1 and d ==-1: print(-1) else: if c == -1: print(d) elif d == -1: print(c) else: print(min(c,d)) ```
output
1
81,969
12
163,939
Provide tags and a correct Python 3 solution for this coding contest problem. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6.
instruction
0
81,970
12
163,940
Tags: brute force, graphs, implementation Correct Solution: ``` import sys def op1(n, x): for i in range(0,n): u = 2*i v = 2*i+1 t1 = x[u] x[u] = x[v] x[v] = t1 return x def op2(n,x): for i in range(0,n): u = i v = i+n t1 = x[u] x[u] = x[v] x[v] = t1 return x def cmp(n,a,b): for i in range(2*n): if a[i] != b[i]: return False return True def main(): n = int(input()) m = 2*n x = [i for i in range(m)] a = list(map(int,input().split())) for i in range(m): a[i] = a[i]-1 steps = 0 if cmp(n,a,x): print(steps) return 0 ans = m*3 for i in range(m): if i % 2 == 0: x = op1(n,x) if i % 2 == 1: x = op2(n,x) if cmp(n,a,x): steps = i + 1 ans = min(steps, ans) x = [i for i in range(m)] for i in range(m): if i % 2 == 1: x = op1(n,x) if i % 2 == 0: x = op2(n,x) if cmp(n,a,x): steps = i + 1 ans = min(steps, ans) if ans <= m: print(ans) else: print(-1) if __name__ == "__main__": input = sys.stdin.readline main() ```
output
1
81,970
12
163,941
Provide tags and a correct Python 3 solution for this coding contest problem. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6.
instruction
0
81,971
12
163,942
Tags: brute force, graphs, implementation Correct Solution: ``` import sys from collections import defaultdict # import logging # logging.root.setLevel(level=logging.INFO) memo = {} def search(cur_op,nums): global n,target state = tuple(nums) # print(state) if state in memo: return memo[state] memo[state] = float('inf') # try operate 1 l = [] for i in range(0,2*n-1,2): l.append(nums[i+1]) l.append(nums[i]) # try operate 2 new_list = [*nums[n:],*nums[:n]] memo[state] = min(memo[state], search(cur_op+1,new_list)+1) memo[state] = min(memo[state], search(cur_op+1,l)+1) return memo[state] n = int(sys.stdin.readline().strip()) nums = list(map(int,sys.stdin.readline().strip().split())) target = sorted(nums) memo[tuple(target)] = 0 search(0,nums) # print(memo) operation = memo[tuple(nums)] if operation == float('inf'): print(-1) else: print(operation) ```
output
1
81,971
12
163,943
Provide tags and a correct Python 3 solution for this coding contest problem. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6.
instruction
0
81,972
12
163,944
Tags: brute force, graphs, implementation Correct Solution: ``` def ii(): return int(input()) def li(): return [int(i) for i in input().split()] def op1(a,n): for i in range(1,2*n,2): a[i],a[i+1] = a[i+1],a[i] def op2(a,n): for i in range(1,n+1): # print(n,n+i,i,a) a[i],a[n+i] = a[n+i],a[i] def check(a): for i in range(1,len(a)): if a[i]!=i: return 0 return 1 n = ii() a = li() b = [-1] a.insert(0,-1) for i in range(1,2*n+1): b.append(a[i]) ans = -1 for i in range(n+1): if check(a) == 1: ans = i break if i%2 ==0: op1(a,n) else: op2(a,n) ans1 = -1 for i in range(n+1): if check(b) == 1: ans1 = i break if i%2 ==1: op1(b,n) else: op2(b,n) if ans == -1 and ans1 == -1: print(-1) elif ans==-1 and ans1!=-1: print(ans1) elif ans!=-1 and ans1==-1: print(ans) else: print(min(ans,ans1)) # 3 # 5 4 1 6 3 2 ```
output
1
81,972
12
163,945
Provide tags and a correct Python 3 solution for this coding contest problem. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6.
instruction
0
81,973
12
163,946
Tags: brute force, graphs, implementation Correct Solution: ``` n = int(input().strip()) nums = list(map(int, input().strip().split(" "))) def adj(arr): for i in range(n): tmp = arr[2*i] arr[2*i] = arr[2*i+1] arr[2*i+1] = tmp return arr def half(arr): for i in range(n): tmp = arr[i] arr[i] = arr[i+n] arr[i+n] = tmp return arr def check(arr): for i in range(2*n): if arr[i] != i+1: return False return True def solve(nums): poss = True for i in range(1, n): if (nums[2*i] - nums[0]) % 2 != 0: poss = False break if not poss: print(-1) return 0 if n % 2 == 0: cnt = 0 if nums[0] % 2 == 0: cnt += 1 nums = adj(nums) if nums[0] != 1: cnt += 1 nums = half(nums) if check(nums): print(cnt) else: print(-1) return 0 cnt = 0 pt = 0 while cnt < 2 * n: if pt == nums[pt] - 1: break pt = nums[pt] - 1 if cnt % 2 == 0: nums = half(nums) else: nums = adj(nums) cnt += 1 if check(nums): print(min(cnt, 2 * n - cnt)) else: print(-1) return 0 solve(nums) ```
output
1
81,973
12
163,947
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6. Submitted Solution: ``` n=int(input());a=*map(int,input().split()),;c=9e9 for j in[0,1]: b=list(a);d=0 while b[-1]<2*n:b=[b[n:]+b[:n],[b[i^1]for i in range(0,2*n)]][j];j^=1;d+=1 if b<list(range(1,2*n+2)):c=min(d,c) print([-1,c][c<9e9]) ```
instruction
0
81,974
12
163,948
Yes
output
1
81,974
12
163,949
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6. Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) l2=l.copy() c=l.copy() o=l.copy() c.sort() flag=1 cnt,var=0,0 # for i in range(0,(2*n)-1,2): # if l[i]>l[i+1]:var+=1 # if var>=(n//4):flag=1 # else:flag=0 while c!=l: # print("here") if flag: # print("here1") cnt+=1 flag=1-flag for i in range((2*n)-1): if i%2==0: temp=l[i] l[i]=l[i+1] l[i+1]=temp else : cnt+=1 flag=1-flag for i in range(n): temp=l[i] l[i]=l[n+i] l[n+i]=temp if o==l:break cnt2=cnt cnt=0 flag=0 while c!=l2: # print("here") if flag: # print("here1") cnt+=1 flag=1-flag for i in range((2*n)-1): if i%2==0: temp=l2[i] l2[i]=l2[i+1] l2[i+1]=temp else : cnt+=1 flag=1-flag for i in range(n): temp=l2[i] l2[i]=l2[n+i] l2[n+i]=temp if o==l2:break if c==l and c==l2: print(min(cnt2,cnt)) else:print("-1") ```
instruction
0
81,975
12
163,950
Yes
output
1
81,975
12
163,951
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6. Submitted Solution: ``` from __future__ import division, print_function import math import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def o1(l, n): for i in range(0, n+n, 2): l[i], l[i + 1] = l[i + 1], l[i] # print("o1",l) return l def o2(l, n): for i in range(0, n): l[i], l[n + i] = l[n + i], l[i] # print("o2",l) return l def main(): n = int(input()) l = list(map(int, input().split())) a1 = 0 a2 = 0 k1 = k2 = True l1 = list(l) l1.sort() l2 = list(l) while (l != l1): # print(l) if (a1 % 2==0): l = o1(l, n) else: l = o2(l, n) a1 += 1 if (a1 > 100000): k1 = False break # print(l) while (l2 != l1): # print(l2) if (a2 % 2==0): l2 = o2(l2, n) else: l2 = o1(l2, n) a2 += 1 if (a2 > 100000): k2 = False break # print(l,l2) if (k1 and k2): print(min(a1, a2)) else: print(-1) # 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") 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() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
81,976
12
163,952
Yes
output
1
81,976
12
163,953
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6. Submitted Solution: ``` def swap1(l): for i in range(0,len(l),2): a=l[i] l[i]=l[i+1] l[i+1]=a def swap2(l): for i in range(0,len(l)//2): a=l[i] l[i]=l[i+len(l)//2] l[i+len(l)//2]=a n = int(input()) l=list(map(int,input().split())) original=l.copy() s=l.copy() s.sort() count=0 # s is sorted while(True): if s==l: #print(count) break swap2(l) count+=1 if original==l: print("-1") count=-1 break if s==l: #print(count) break swap1(l) count+=1 if original==l: print("-1") count=-1 break #16 5 18 7 2 9 4 11 6 13 8 15 10 17 12 1 14 3 c=0 l=original.copy() if count==-1: exit(0) while(True): if s==l: #print(count) break swap1(l) c+=1 if original==l: print("-1") break if s==l: #print(count) break swap2(l) c+=1 if original==l: print("-1") break print(min(count,c)) ```
instruction
0
81,977
12
163,954
Yes
output
1
81,977
12
163,955
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6. Submitted Solution: ``` # cook your dish here n=int(input()) a=list(map(int,input().split())) b=a.copy() ans=10**7 ans0=10**7 if(a==sorted(a)): ans0=0 elif(a!=sorted(a)): ans=0 for i in range(n): ans+=1 if(i%2==0): for j in range(0,2*n,2): # print(j) a[j],a[j+1]=a[j+1],a[j] if(a==sorted(a)): break else: for j in range(n): a[j],a[j+n]=a[j+n],a[j] if(a==sorted(a)): break if(a!=sorted(a)):ans=10**7 # print(ans) ans1=10**7 if(1): ans1=0 a=[];a=b.copy() for i in range(n): ans1+=1 if(i%2==0): for j in range(n): a[j],a[j+n]=a[j+n],a[j] if(a==sorted(a)): break else: for j in range(0,2*n,2): a[j],a[j+1]=a[j+1],a[j] if(a==sorted(a)): break if(a!=sorted(a)):ans1=10**7 # print(ans1,ans0) if(a==sorted(a)):print(min(ans,ans1,ans0)) if(a!=sorted(a)):print(-1) # if(n==1000):print(min(a),max(a)) ```
instruction
0
81,978
12
163,956
No
output
1
81,978
12
163,957
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6. Submitted Solution: ``` #King's Task def sort1(list): for i in range(0, len(lst), 2): lst[i], lst[i + 1] = lst[i+1], lst[i] def sort2(list): for i in range(len(lst) // 2): lst[i], lst[n + i] = lst[n + i], lst[i] n = int(input()) lst = list(map(int, input().split())) old_lst = lst.copy() sorted_lst = old_lst.copy() sorted_lst.sort() itr = 0 while lst != sorted_lst: sort1(lst) itr += 1 if lst != sorted_lst: sort2(lst) itr += 1 if lst == old_lst: print(-1) break if lst == sorted_lst: print(itr) ```
instruction
0
81,979
12
163,958
No
output
1
81,979
12
163,959
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6. Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) c=l.copy() o=l.copy() c.sort() flag=1 cnt=0 while c!=l: # print("here") if flag: # print("here1") cnt+=1 flag=1-flag for i in range((2*n)-1): if i%2==0: temp=l[i] l[i]=l[i+1] l[i+1]=temp else : cnt+=1 flag=1-flag for i in range(n): temp=l[i] l[i]=l[n+i] l[n+i]=temp if o==l:break if c==l:print(cnt) else:print("-1") ```
instruction
0
81,980
12
163,960
No
output
1
81,980
12
163,961
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. 1. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. 2. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer n (1≤ n≤ 1000). The second line contains 2n integers p_i — the permutation of numbers from 1 to 2n. Output Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print -1. Examples Input 3 6 3 2 5 4 1 Output 3 Input 2 3 4 2 1 Output -1 Input 4 1 2 3 4 5 6 7 8 Output 0 Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: 3, 6, 5, 2, 1, 4. 2. Make operation 2: 2, 1, 4, 3, 6, 5. 3. Make operation 1: 1, 2, 3, 4, 5, 6. Submitted Solution: ``` #######puzzleVerma####### import sys import math mod = 10**9+7 LI=lambda:[int(k) for k in input().split()] input = lambda: sys.stdin.readline().rstrip() IN=lambda:int(input()) S=lambda:input() n=IN() li=LI() def swap1 (A): for i in range(len(A)//2): b = A[2*i] A[2*i] = A[2*i+1] A[2*i+1] = b return A def swap2 (A): n = len(A)//2 for i in range(n): b = A[n+i] A[n+i] = A[i] A[i] = b return A def is_sorted(A): for i in range (len(A)): if (A[i]!=i+1): return False return True ans1=0 ans2=0 li1=li.copy() li2=li.copy() while(not (is_sorted(li1)) and (not is_sorted(li2))): swap1(li1) ans1+=1 if is_sorted(li1): print(ans1) break swap2(li1) ans1+=1 if is_sorted(li1): print(ans1) break swap2(li2) ans2+=1 if is_sorted(li2): print(ans2) break swap1(li2) ans2+=1 if is_sorted(li2): print(ans2) break if ans1>n: print(-1) break ```
instruction
0
81,981
12
163,962
No
output
1
81,981
12
163,963
Provide tags and a correct Python 3 solution for this coding contest problem. DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x) = x mod p. Operation a mod b denotes taking a remainder after division a by b. However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after the i-th insertion, you should output i. If no conflict happens, just output -1. Input The first line contains two integers, p and n (2 ≤ p, n ≤ 300). Then n lines follow. The i-th of them contains an integer xi (0 ≤ xi ≤ 109). Output Output a single integer — the answer to the problem. Examples Input 10 5 0 21 53 41 53 Output 4 Input 5 5 0 1 2 3 4 Output -1
instruction
0
82,068
12
164,136
Tags: implementation Correct Solution: ``` p,n = map(int,input().split(" ")) arr = [] arrhash = [] temp = 0 for i in range(n) : arr.append(int(input())) for i in range(n) : if (arr[i] % p not in arrhash ) : arrhash.append(arr[i] % p) else : temp = i break if (temp == 0 ) : print(-1) else : print(temp+1) ```
output
1
82,068
12
164,137
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
instruction
0
82,227
12
164,454
Tags: dp, greedy, number theory Correct Solution: ``` from math import gcd def solve(): n = int(input()) a = list(map(int, input().split(' '))) if n == 1: return 0 g = gcd(a[0],a[1]) for i in range(2, len(a)): g = gcd(g, a[i]) if g == 1: break if g > 1: return 0 score = 0 i = 0 while i < len(a): if a[i] % 2 == 0: i += 1 continue if a[i] % 2 == 1: if i == len(a)-1: score += 2 break if a[i+1] % 2 == 1: score += 1 i += 2 else: score += 2 i += 2 return score x = solve() if x >= 0: print("YES") print(x) else: print("NO") ```
output
1
82,227
12
164,455
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
instruction
0
82,228
12
164,456
Tags: dp, greedy, number theory Correct Solution: ``` n=int(input()) a=input().split() for i in range(n): a[i]=int(a[i]) k=0 g=0 def gcd(c,d): if c%d==0: return d else: return gcd(d,c%d) while True: for i in range(n): g=gcd(g,a[i]) if g>1: k=0 break else: for i in range(n-1): if a[i]%2!=0 and a[i+1]%2!=0: k+=1 a[i]=a[i+1]=2 else: pass for i in range(n): if a[i]%2!=0: k+=2 a[i]=2 break print('YES') print(k) ```
output
1
82,228
12
164,457
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
instruction
0
82,229
12
164,458
Tags: dp, greedy, number theory Correct Solution: ``` from math import gcd, ceil from sys import exit from functools import reduce n = int(input()) numbers = list(map(int, input().strip().split())) cum_gcd = reduce(gcd, numbers, 0) if cum_gcd > 1: print('YES', 0, sep='\n') exit(0) numbers.append(0) numbers = [element % 2 for element in numbers] ans, cur_count = [0, 0] for index, val in enumerate(numbers): if val == 1: cur_count += 1 else: ans += ceil(cur_count/2) + cur_count % 2 cur_count = 0 print('YES', ans, sep='\n') ```
output
1
82,229
12
164,459
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
instruction
0
82,230
12
164,460
Tags: dp, greedy, number theory Correct Solution: ``` # -*- coding: utf-8 -*- """ Created on Sat Apr 22 15:27:31 2017 @author: CHITTARANJAN """ from fractions import gcd n=int(input()) arr=list(map(int,input().split())) d=gcd(arr[0],arr[1]) noftime=0 for i in range(2,n): d=gcd(d,arr[i]) if(d>1): print('YES\n0') exit() else: i=0 while(i<n): if(arr[i]%2==0): i=i+1 continue else: if(i==n-1): noftime+=2 break if(arr[i+1]%2==1): i=i+2 noftime+=1 else: i=i+2 noftime+=2 if(noftime>0): print('YES') print(noftime) else: print('NO') ```
output
1
82,230
12
164,461
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
instruction
0
82,231
12
164,462
Tags: dp, greedy, number theory Correct Solution: ``` from functools import reduce N = int( input() ) A = list( map( int, input().split() ) ) def gcd( a,b ): if b == 0: return a return gcd( b, a % b ) print( "YES" ) if reduce( gcd, A ) > 1: print( 0 ) else: ans = 0 for i in range( N - 1 ): while A[ i ] & 1: ans += 1 A[ i ], A[ i + 1 ] = A[ i ] - A[ i + 1 ], A[ i ] + A[ i + 1 ] if A[ N - 1 ] & 1: ans += 2 print( ans ) ```
output
1
82,231
12
164,463
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
instruction
0
82,232
12
164,464
Tags: dp, greedy, number theory Correct Solution: ``` from math import gcd def func(a): x=a[0] for i in range(1,len(a)): x=gcd(a[i],x) return x n=int(input()) a=[int(x) for x in input().split()] if func(a)>1: print('YES') print(0) else: a = [int(x)%2 for x in a] an=0 for i in range(n-1): if a[i]==1: if a[i+1]==1: an+=1 else: an+=2 a[i],a[i+1]=0,0 if a[-1]==1: an+=2 print('YES') print(an) ```
output
1
82,232
12
164,465
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
instruction
0
82,233
12
164,466
Tags: dp, greedy, number theory Correct Solution: ``` import bisect import decimal from decimal import Decimal import os from collections import Counter import bisect from collections import defaultdict import math import random import heapq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations from io import BytesIO, IOBase from itertools import accumulate # sys.setrecursionlimit(200000) # mod = 10**9+7 # mod = 998244353 decimal.getcontext().prec = 46 def primeFactors(n): prime = set() while n % 2 == 0: prime.add(2) n = n//2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: prime.add(i) n = n//i if n > 2: prime.add(n) return list(prime) def getFactors(n) : factors = [] i = 1 while i <= math.sqrt(n): if (n % i == 0) : if (n // i == i) : factors.append(i) else : factors.append(i) factors.append(n//i) i = i + 1 return factors def SieveOfEratosthenes(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 num = [] for p in range(2, n+1): if prime[p]: num.append(p) return num def lcm(a,b): return (a*b)//math.gcd(a,b) def sort_dict(key_value): return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0])) def list_input(): return list(map(int,input().split())) def num_input(): return map(int,input().split()) def string_list(): return list(input()) def decimalToBinary(n): return bin(n).replace("0b", "") def binaryToDecimal(n): return int(n,2) def solve(): n = int(input()) arr = list_input() prev = 0 for i in arr: prev = math.gcd(prev,i) if prev > 1: print('YES') print(0) return if n == 2: if arr[0]%2 != 0 and arr[1]%2 != 0: print('YES') print(1) elif arr[0]%2 == 0 and arr[1]%2 == 0: print('YES') print(0) else: print('YES') print(2) return count = 0 for i in range(1,n-1): if arr[i]%2 != 0: if arr[i-1]%2 != 0: count += 1 arr[i-1],arr[i] = abs(arr[i]-arr[i-1]),arr[i-1]+arr[i] elif arr[i+1]%2 != 0: count += 1 arr[i],arr[i+1] = abs(arr[i]-arr[i+1]),arr[i]+arr[i+1] for i in arr: if i%2 != 0: count += 2 print('YES') print(count) #t = int(input()) t = 1 for _ in range(t): solve() ```
output
1
82,233
12
164,467
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
instruction
0
82,234
12
164,468
Tags: dp, greedy, number theory Correct Solution: ``` from sys import stdin def gcd(a, b): if b: return gcd(b, a%b) else: return a n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) g = 0 for el in a: g = gcd(g, el) res = 0 if g == 1: for i in range(n-1): if a[i]%2 and a[i+1]%2: a[i], a[i+1] = a[i] - a[i+1], a[i] + a[i+1] res += 1 for i in range(n): if a[i]%2: res += 2 print("YES") print(res) ```
output
1
82,234
12
164,469
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1. Submitted Solution: ``` def gcd(a, b): if a == 0: return b if b == 0: return a return gcd(b, a % b) def gcd_of_list(a): a = a[::-1] partial_gcd = 0 while a: partial_gcd = gcd(partial_gcd, a.pop()) return partial_gcd def get_odd_chunk_lengths(a): a = a[::-1] chunk_lengths_arr = [] curr_chunk_length = 0 while a: next_num = a.pop() if next_num % 2 == 1: curr_chunk_length += 1 if next_num % 2 == 0 and curr_chunk_length != 0: chunk_lengths_arr.append(curr_chunk_length) curr_chunk_length = 0 if curr_chunk_length != 0: chunk_lengths_arr.append(curr_chunk_length) return chunk_lengths_arr def num_ops_to_evenify_odd_chunk(chunk_length): if chunk_length % 2 == 0: return chunk_length // 2 else: return chunk_length // 2 + 2 input() a = [int(i) for i in input().split()] if gcd_of_list(a) > 1: print("YES") print(0) else: print("YES") print(sum([num_ops_to_evenify_odd_chunk(chunk_length) for chunk_length in get_odd_chunk_lengths(a)])) ```
instruction
0
82,235
12
164,470
Yes
output
1
82,235
12
164,471
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1. Submitted Solution: ``` from math import gcd n = int(input()) a = [int(i) for i in input().split()] g = 0 cnt = 0 ans = 0 for v in a: g = gcd(g, v) if v & 1: cnt += 1 else: ans += (cnt // 2) + 2 * (cnt & 1) cnt = 0 ans += (cnt // 2) + 2 * (cnt & 1) print('YES') if g == 1: print(ans) else: print(0) ```
instruction
0
82,236
12
164,472
Yes
output
1
82,236
12
164,473
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1. Submitted Solution: ``` import math n = int(input()) lis = list(map(int,input().split())) gc=lis[0] c=0 for i in range(1,n): if math.gcd(gc,lis[i])==1: c=1 print("YES") if c==0: print("0") else: ans=0 c=0 for i in lis: if i%2!=0: c+=1 else: ans += c//2 + 2*(c%2) c=0 ans+=c//2 + 2*(c%2) print(ans) ```
instruction
0
82,237
12
164,474
Yes
output
1
82,237
12
164,475
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1. Submitted Solution: ``` from math import gcd from functools import reduce def main(): n = int(input()) a = list(map(int, input().split())) print('YES') if reduce(gcd, a) > 1: print(0) else: ans = 0 for i in range(n - 1): while a[i] % 2 != 0: a[i], a[i + 1] = a[i] - a[i + 1], a[i] + a[i + 1] ans += 1 while a[-1] % 2 != 0: a[-2], a[-1] = a[-2] - a[-1], a[-2] + a[-1] ans += 1 print(ans) main() ```
instruction
0
82,238
12
164,476
Yes
output
1
82,238
12
164,477
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1. Submitted Solution: ``` l = int(input()) a = list(map(int, input().split())) cnt = 0 odd = l % 2 == 1 for i in range(0, l-1): if a[i] % 2 != 0: if a[i+1] % 2 != 0: cnt += 1 a[i+1] = 2 else: cnt += 2 a[i+1] = 2 if a[-1] % 2 != 0: cnt += 2 print("YES") print(cnt) ```
instruction
0
82,239
12
164,478
No
output
1
82,239
12
164,479
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1. Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) d=-1 def gcd(a,b) : while a%b!=0 : ost=a%b a=b b=ost return b c=0 for i in range(n-1) : if d==-1 : d=gcd(l[i],l[i+1]) else : if d!=gcd(l[i],l[i+1]) : c=1 if c==0 and gcd(l[1],l[0])>1 : print('YES') print(0) exit() k=0 for i in range(n-2) : if l[i]%2==0 and l[i+1]%2==0 : k=k+0 if l[i]%2==0 and l[i+1]%2!=0 and l[i+2]%2==0 or l[i]%2!=0 and l[i+1]%2==0 : l[i+1]=2 k=k+2 l[i]=2 if l[i]%2!=0 and l[i+1]%2!=0 : k=k+1 l[i+1]=2 l[i]=2 if l[i+1]%2!=0 and l[i+2]!=0 : k=k+1 l[i+2]=2 l[i+1]=2 if l[-1]%2!=0 : if l[-2]%2==0 : k=k+2 else : k=k+1 if l[-2]%2!=0 : if l[-1]%2==0 : k=k+2 else : k=k+1 print('YES') print(k) ```
instruction
0
82,240
12
164,480
No
output
1
82,240
12
164,481
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1. Submitted Solution: ``` # your code goes here import math n=int(input()) a=list(map(int,input().split())) ans=a[0] for i in range(1,n): ans=math.gcd(ans,a[i]) if ans>1: print("YES") print(0) if ans==1: cnt=0 for i in range(n-1): if a[i]%2==0: continue else: if a[i+1]%2!=0: cnt+=1 i+=1 else: cnt+=2 i+=1 if(n%2): if a[n-1]%2: cnt+=2 print("YES") print(cnt) ```
instruction
0
82,241
12
164,482
No
output
1
82,241
12
164,483
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n). Input The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1. Submitted Solution: ``` from functools import reduce def gcd(a,b): if b==0: return a else: return gcd(b,a%b) def check(arr): if reduce(gcd,arr)==1: return False return True n=int(input()) new=list(map(int,input().split())) def main(new,n): if n==1: return 'NO' if check(new): return 'YES',0 arr=[x%2 for x in new] i=0 ans=0 while i<n: if i==n-1: if arr[i]==1: ans+=2 return 'YES',ans if arr[i]==1 and arr[i+1]==1: ans+=1 i+=2 continue if (arr[i]==1 and arr[i+1]==0) or (arr[i+1]==1 and arr[i]==0): ans+=2 i+=2 continue i+=2 return 'YES',ans ver,x=main(new,n) print(ver) if ver=='YES': print(x) ```
instruction
0
82,242
12
164,484
No
output
1
82,242
12
164,485
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them. Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority. In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ. Determine the array Ivan will obtain after performing all the changes. Input The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array. Output In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes. Examples Input 4 3 2 2 3 Output 2 1 2 4 3 Input 6 4 5 6 3 2 1 Output 0 4 5 6 3 2 1 Input 10 6 8 4 6 7 1 6 3 4 5 Output 3 2 8 4 6 7 1 9 3 10 5 Note In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to change anything because his array already is a permutation.
instruction
0
82,267
12
164,534
Tags: greedy, implementation, math Correct Solution: ``` n = int(input()) l = list(map(int, input().split())) cnt = [0] * 200001 pos = [0] * 200001 for i in range(n): cnt[l[i]] += 1 uk = 1 an = 0 for i in range(n): if cnt[l[i]] > 1: an += 1 while cnt[uk] != 0: uk += 1 if (uk > l[i] and pos[l[i]]) or uk < l[i]: cnt[l[i]] -= 1 cnt[uk] += 1 l[i] = uk else: pos[l[i]] = 1 an -= 1 print(an) print(' '.join(map(str, l))) ```
output
1
82,267
12
164,535
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them. Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority. In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ. Determine the array Ivan will obtain after performing all the changes. Input The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array. Output In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes. Examples Input 4 3 2 2 3 Output 2 1 2 4 3 Input 6 4 5 6 3 2 1 Output 0 4 5 6 3 2 1 Input 10 6 8 4 6 7 1 6 3 4 5 Output 3 2 8 4 6 7 1 9 3 10 5 Note In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to change anything because his array already is a permutation.
instruction
0
82,268
12
164,536
Tags: greedy, implementation, math Correct Solution: ``` # -*- coding: utf-8 -*- import math import collections import bisect import heapq import time import random """ created by shhuan at 2017/10/4 20:06 """ N = int(input()) M = [int(x) for x in input().split()] # t0 = time.time() # # N = 200000 # M = [random.randint(1, N) for _ in range(N)] S = [0 for i in range(N+1)] for v in M: S[v] += 1 changes = sum([S[i] - 1 for i in range(N+1) if S[i] > 1] or [0]) if changes == 0: print(changes) print(' '.join([str(x) for x in M])) exit(0) used = [0 for _ in range(N+1)] miss = collections.deque(sorted([x for x in range(1, N+1) if S[x] == 0])) for i in range(N): v = M[i] if used[v]: M[i] = miss.popleft() else: if S[v] > 1: if miss and miss[0] < v: M[i] = miss.popleft() S[v] -= 1 else: used[v] = 1 print(changes) print(' '.join([str(x) for x in M])) # print(time.time() - t0) ```
output
1
82,268
12
164,537
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them. Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority. In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ. Determine the array Ivan will obtain after performing all the changes. Input The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array. Output In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes. Examples Input 4 3 2 2 3 Output 2 1 2 4 3 Input 6 4 5 6 3 2 1 Output 0 4 5 6 3 2 1 Input 10 6 8 4 6 7 1 6 3 4 5 Output 3 2 8 4 6 7 1 9 3 10 5 Note In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to change anything because his array already is a permutation.
instruction
0
82,269
12
164,538
Tags: greedy, implementation, math Correct Solution: ``` n = int(input()) + 1 t = [0] + list(map(int, input().split())) s = [0] * n for j in t: s[j] += 1 p = [0] * n k = 1 for i, j in enumerate(t): if s[j] > 1: while s[k]: k += 1 if j > k or p[j]: t[i] = k s[j] -= 1 k += 1 else: p[j] = 1 print(s.count(0)) print(' '.join(map(str, t[1:]))) ```
output
1
82,269
12
164,539
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them. Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority. In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ. Determine the array Ivan will obtain after performing all the changes. Input The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array. Output In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes. Examples Input 4 3 2 2 3 Output 2 1 2 4 3 Input 6 4 5 6 3 2 1 Output 0 4 5 6 3 2 1 Input 10 6 8 4 6 7 1 6 3 4 5 Output 3 2 8 4 6 7 1 9 3 10 5 Note In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to change anything because his array already is a permutation.
instruction
0
82,270
12
164,540
Tags: greedy, implementation, math Correct Solution: ``` from collections import defaultdict as dc n=int(input()) a=[int(i) for i in input().split()] p=dc(int) q=dc(int) extra=dc(int) missing=[] for i in range(n): p[a[i]]=p[a[i]]+1 if p[a[i]]>1: extra[a[i]]=1 for i in range(1,n+1): if p[i]==0: missing.append(i) #print(extra,missing) l=0 #print(p,q) for i in range(n): if l>=len(missing): break if extra[a[i]]==1 and p[a[i]]>=1: if missing[l]>a[i] and p[a[i]]>1 and q[a[i]]==0: p[a[i]]=p[a[i]]-1 q[a[i]]=1 elif (p[a[i]]==1 and q[a[i]]==0): continue else: p[a[i]]=p[a[i]]-1 a[i]=missing[l] l=l+1 print(len(missing)) print(*a) ```
output
1
82,270
12
164,541
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them. Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority. In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ. Determine the array Ivan will obtain after performing all the changes. Input The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array. Output In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes. Examples Input 4 3 2 2 3 Output 2 1 2 4 3 Input 6 4 5 6 3 2 1 Output 0 4 5 6 3 2 1 Input 10 6 8 4 6 7 1 6 3 4 5 Output 3 2 8 4 6 7 1 9 3 10 5 Note In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to change anything because his array already is a permutation.
instruction
0
82,271
12
164,542
Tags: greedy, implementation, math Correct Solution: ``` def solve(printing): n = int(input()) nums = [int(st)-1 for st in input().split(" ")] numdupe = [0] * n dupeindex = [] dupeindexindv = {} missing = [] if printing: print("nums"); print(nums) for i in range(n): numdupe[nums[i]] += 1 for i in range(n): if numdupe[i] == 0: missing.append(i) if numdupe[nums[i]] >= 2: dupeindex.append(i) if nums[i] in dupeindexindv: dupeindexindv[nums[i]][1].append(i) else: dupeindexindv[nums[i]] = [0, [i], False] # left location, dupe indexs, if already located original for num in dupeindexindv: dupeindexindv[num][0] = len(dupeindexindv[num][1]) if printing: print("missing"); print(missing) print("dupeindexindv"); print(dupeindexindv) misslen = len(missing) misindex = 0 #answer = 0 for index in dupeindex: if misslen <= misindex: break elif dupeindexindv[nums[index]][0] == 1 and not dupeindexindv[nums[index]][2]: # one spot left but original not located yet. # locate original. dupeindexindv[nums[index]][0] -= 1 dupeindexindv[nums[index]][2] = True elif dupeindexindv[nums[index]][0] > 0: if dupeindexindv[nums[index]][2] or missing[misindex] < nums[index]: # num is smaller or original is already located. # locate missing number. dupeindexindv[nums[index]][0] -= 1 nums[index] = missing[misindex] misindex += 1 #answer += 1 else: # locate original dupeindexindv[nums[index]][0] -= 1 dupeindexindv[nums[index]][2] = True print(misslen) for num in nums: print(num+1, end = " ") solve(False) ```
output
1
82,271
12
164,543
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them. Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority. In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ. Determine the array Ivan will obtain after performing all the changes. Input The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array. Output In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes. Examples Input 4 3 2 2 3 Output 2 1 2 4 3 Input 6 4 5 6 3 2 1 Output 0 4 5 6 3 2 1 Input 10 6 8 4 6 7 1 6 3 4 5 Output 3 2 8 4 6 7 1 9 3 10 5 Note In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to change anything because his array already is a permutation.
instruction
0
82,272
12
164,544
Tags: greedy, implementation, math Correct Solution: ``` n=int(input()) d=[0]*n *l,=map(int,input().split()) for i in l:d[i-1]+=1 print(d.count(0)) p=[0]*n t=0 for i,j in enumerate(l): if d[j-1]>1: while d[t]>0:t+=1 if j>t+1 or p[j]: d[j-1]-=1 d[t]+=1 l[i]=t+1 else:p[j]=1 print(' '.join(map(str,l))) ```
output
1
82,272
12
164,545