message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. Submitted Solution: ``` #get inputs list_len = int(input()) list = input().split() dict = {} for i in list: if i in dict: dict[i] = dict.get(i) + 1 else: dict[i] = 1 if len(dict.keys()) >= 2: sorted_values = sorted(dict.values()) if sorted_values[-1] > sorted_values[-2]: sorted_values[-1] = sorted_values[-2] print(sum(sorted_values)) else: print("0") ```
instruction
0
26,736
12
53,472
No
output
1
26,736
12
53,473
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. Submitted Solution: ``` a=int(input()) b=list(map(int,input().split())) c=set(b) co=[] for i in c: co.append(b.count(i)) if len(set(co))<=1 or len(set(c))<=1: print(0) exit(0) count=0 v=[] for i in co: v.append(i) for i in range(len(v)): if v[i]<=1: co.remove(v[i]) count=min(co)*len(co) print(count) ```
instruction
0
26,737
12
53,474
No
output
1
26,737
12
53,475
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. Submitted Solution: ``` n = int(input()) arr = list(map(int,input().split())) for i in range(1): if n == 1 or len(set(arr)) == 1: print(0) break if n == 2: if arr[0] == arr[1]: print(0) break l = [] for i in set(arr): l.append([i,arr.count(i)]) l.sort(key = lambda d:d[1]) print(n - (l[-1][1] - l[-2][1])) ```
instruction
0
26,738
12
53,476
No
output
1
26,738
12
53,477
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. Submitted Solution: ``` n=int(input()) arr=list(map(int,input().split())) freq=[0]*(101) for i in arr: freq[i]+=1 freq2=[0]*(101) ans=0 k1=3 j=0 for i in range(n): freq2[arr[i]]+=1 if i>=1: freq3=[] for k in range(1,101): if freq2[k]>=1: freq3.append(freq2[k]) #print(freq3) if freq3.count(max(freq3))>=3: ans=max(ans,i+1) freq2=[0]*101 for j in range(n): freq2[arr[j]]+=1 freq3=[] for k in range(1,101): if freq[k]>=1:freq3.append(freq[k]-freq2[k]) #print(freq3,j+1) if max(freq3)>0 and freq3.count(max(freq3))>=2: ans=max(ans,n-j-1) #print() break print(ans) ```
instruction
0
26,739
12
53,478
No
output
1
26,739
12
53,479
Provide tags and a correct Python 3 solution for this coding contest problem. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
instruction
0
26,858
12
53,716
Tags: constructive algorithms, math, number theory Correct Solution: ``` n, k = map(int, input().split()) if k == n: print(-1) elif k == n - 1: print(*range(1, n + 1)) else: ans = [0] * n for i in range(k): ans[i + 1] = i + 2 for i in range(k, n - 1): ans[i + 1] = i + 3 ans[-1] = 1 ans[0] = k + 2 print(*ans) ```
output
1
26,858
12
53,717
Provide tags and a correct Python 3 solution for this coding contest problem. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
instruction
0
26,859
12
53,718
Tags: constructive algorithms, math, number theory Correct Solution: ``` import sys import math def gcd(a, b): c = max(a,b) d = min(a,b) r = c%d if r==0: return d return gcd(d,r) def lcm(a, b): def gcd_naive(a, b): c = max(a,b) d = min(a,b) r = c%d if r==0: return d return gcd_naive(d,r) return int(a*b/gcd_naive(a,b)) def fn(n,k): l=[0]*(n) if n<=k: print(-1) return 0 elif n==k+1: for i in range(n): l[i]=i+1 print(*l) return 0 else: for i in range(1,k+1): l[i]=i+1 l[0]=n for i in range(n-2,k,-1): l[i+1]=i+1 l[k+1]=1 print(*l) if __name__ == '__main__': input = sys.stdin.read() data = list(map(int, input.split())) n = data[0] k = data[1] (fn(n,k)) ```
output
1
26,859
12
53,719
Provide tags and a correct Python 3 solution for this coding contest problem. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
instruction
0
26,860
12
53,720
Tags: constructive algorithms, math, number theory Correct Solution: ``` n, k = list(map(int, input().split())) if k == n: print(-1) else: if 2+k <= n: print(2 + k, end=" ") else: print(1, end=" ") for i in range(2, 2+k): print(i, end=" ") i = 2 + k if i <= n: while i <= n-1: print(i+1, end=" ") i += 1 print(1, end="") print() ```
output
1
26,860
12
53,721
Provide tags and a correct Python 3 solution for this coding contest problem. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
instruction
0
26,861
12
53,722
Tags: constructive algorithms, math, number theory Correct Solution: ``` n,k = map(int,input().split()) if k<n: ans = [1] count = 0 for i in range(2,n+1): if count == k: ans.append(i+1) else: ans.append(i) count+=1 hare = set(ans) for i in range(1,n+1): if i not in hare: ans[0] = i ans[-1] = 1 break print(*ans) else: print(-1) ```
output
1
26,861
12
53,723
Provide tags and a correct Python 3 solution for this coding contest problem. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
instruction
0
26,862
12
53,724
Tags: constructive algorithms, math, number theory Correct Solution: ``` n, k = map(int, input().split()) if k == n: print(-1) else: bar = n - k print(' '.join(map(str, [bar] + list(range(1, bar)) + list(range(bar + 1, n + 1))))) ```
output
1
26,862
12
53,725
Provide tags and a correct Python 3 solution for this coding contest problem. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
instruction
0
26,863
12
53,726
Tags: constructive algorithms, math, number theory Correct Solution: ``` t = input() temp = t.split() n = int(temp[0]) k = int(temp[1]) if (k == n): print(-1) else: print(n-k,end =' ') for i in range(1,n-k): print(i,end=' ') for i in range(n-k+1, n+1): print(i,end=' ') ```
output
1
26,863
12
53,727
Provide tags and a correct Python 3 solution for this coding contest problem. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
instruction
0
26,864
12
53,728
Tags: constructive algorithms, math, number theory Correct Solution: ``` n, k = map(int, input().split()) if n == k: print(-1) elif n-1 == k: for i in range(n): print(i+1, end=' ') else: seq = [n] for i in range(k): seq.append(i+2) seq.append(1) for i in range(n-k-2): seq.append(i+k+2) for i in seq: print(i, end=' ') ```
output
1
26,864
12
53,729
Provide tags and a correct Python 3 solution for this coding contest problem. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
instruction
0
26,865
12
53,730
Tags: constructive algorithms, math, number theory Correct Solution: ``` n, k = map(int, input().split()) if n == k: print(-1) elif k == 0: p = 0 l =[] for i in range(1, n+1): if p%2==0: l.append(i+1) else: l.append(i-1) if i == n and l[i-1] == n+1: l[n-1]-=1 l[0], l[n-1] = l[n-1], l[0] p+=1 print(*l) else: l = [] for i in range(1,k+2): l.append(i) t = 0 for i in range(k+2, n+1): if t%2==0: l.append(i+1) else: l.append(i-1) if i == n and l[i-1] == n+1: l[n-1]-=1 l[0], l[n-1] = l[n-1], l[0] t+=1 print(*l) ```
output
1
26,865
12
53,731
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations. Submitted Solution: ``` def main(): n, k = map(int, input().split()) L = [i for i in range(1, n+1)] perms = n - k i = 0 while (perms > 1) and (perms > 0 and i < n - 1) : L[i], L[i+1] = L[i+1], L[i] perms -= 1 i += 1 if n == k: print(-1) else: print(*L) if __name__ == "__main__": main() ```
instruction
0
26,866
12
53,732
Yes
output
1
26,866
12
53,733
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations. Submitted Solution: ``` entrada = input().split() n = int(entrada[0]) k = int(entrada[1]) x = 1 if((n - k) % 2 == 0): x = 0 aux = [] for i in range(1,n+1): aux.append(i) for j in range(x,n-k,2): aux[j],aux[j+1] = aux[j+1],aux[j] if(n != k): print(*aux) else: print('-1') ```
instruction
0
26,867
12
53,734
Yes
output
1
26,867
12
53,735
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations. Submitted Solution: ``` # @author Nayara Souza # UFCG - Universidade Federal de Campina Grande # AA - Basico n, k = list(map(int, input().split())) s = '' s += str(n-k) + ' ' if n == k: print(-1) else: for i in range(1, n-k): s += str(i) + ' ' for i in range(n-k,n): s += str(i+1) + ' ' print(s.rstrip()) ```
instruction
0
26,868
12
53,736
Yes
output
1
26,868
12
53,737
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations. Submitted Solution: ``` n,k=map(int,input().split()) z=[] for i in range(1,n+1): z.append(i) m=n-k if n==k: print(-1) exit() if m%2!=0: for i in range(1,m-1,2): z[i],z[i+1]=z[i+1],z[i] else: for i in range(0,m-1,2): z[i],z[i+1]=z[i+1],z[i] print(*z) ```
instruction
0
26,869
12
53,738
Yes
output
1
26,869
12
53,739
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations. Submitted Solution: ``` inp = input().split() n = int(inp[0]) k = int(inp[1]) if k==0: for i in range(n): print(i+1,end=" ") exit(0) if n==0: print(-1) exit(0) if n==k: print(-1) exit(0) cnt =0 print(n,end=' ') cnt+=1 for i in range(2,k+2): print(i,end=' ') cnt+=1 if cnt<n: print(1,end=' ') cnt+=1 for i in range(k+2,n): print(i,end=' ') ```
instruction
0
26,870
12
53,740
No
output
1
26,870
12
53,741
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations. Submitted Solution: ``` '''n-1 good ones 1 bad one count for k if odd then hop from i=2 swapping neighbors if even then hop from i=1 swapping neighbors non-countable cases should be caught in first line''' import sys n,k = map(int,input().split()) if k==n or k==0: print(-1) sys.exit elements = [i+1 for i in range(n)] i = 1 if (n-k)%2 else 0 while i<n-k: elements[i],elements[i+1] = elements[i+1],elements[i] i+=2 print(' '.join([str(e) for e in elements])) ```
instruction
0
26,871
12
53,742
No
output
1
26,871
12
53,743
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations. Submitted Solution: ``` def isprime(n): # Corner cases if (n <= 1): return False if (n <= 3): return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0): return False i = 5 while (i * i <= n): if (n % i == 0 or n % (i + 2) == 0): return False i = i + 6 return True n,k=map(int,input().split()) if n<=k: print(-1) else: l=[1] for i in range(2,k+2): l.append(i) a=l[-1]+1 i=1+k while i<n: if isprime(a) and a!=i+1: l.append(a) i += 1 a+=1 print(*l) ```
instruction
0
26,872
12
53,744
No
output
1
26,872
12
53,745
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n. Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations. Submitted Solution: ``` n,k=map(int,input().split()) if n==k: print(-1) else: a=[] a.append(n-k) for i in range(1,k): a.append(i) for j in range(i+1,n): a.append(j) print(*a) ```
instruction
0
26,873
12
53,746
No
output
1
26,873
12
53,747
Provide tags and a correct Python 3 solution for this coding contest problem. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
instruction
0
26,900
12
53,800
Tags: dfs and similar, dsu, graphs, greedy, math, sortings Correct Solution: ``` def dfs(i, visited, g): indexes = [] visited.add(i) for j in g[i]: if j not in visited: indexes.extend(dfs(j, visited, g)) indexes.append(i) return indexes def solve(n, p, g): visited = set() result = [0] * n for i in range(n): if i not in visited: indexes = dfs(i, visited, g) values = [p[i] for i in indexes] indexes = sorted(indexes) values = sorted(values) for j in range(len(indexes)): result[indexes[j]] = values[j] return result def main(): n = int(input()) p = list(map(int, input().split())) swaps = [] for i in range(n): row = input() temp = [] for j in range(n): if row[j] == '1': temp.append(j) swaps.append(temp) result = solve(n, p, swaps) print(" ".join(map(str, result))) if __name__ == '__main__': main() ```
output
1
26,900
12
53,801
Provide tags and a correct Python 3 solution for this coding contest problem. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
instruction
0
26,901
12
53,802
Tags: dfs and similar, dsu, graphs, greedy, math, sortings Correct Solution: ``` def recursive_dfs(graph, start, path=[]): '''recursive depth first search from start''' path=path+[start] for node in graph[start]: if not node in path: path=recursive_dfs(graph, node, path) return path x = [] sa = int(input()) toed = list(map(int, input().split(' '))) for i in range(sa): x.append(input()) graph = {} for i in range(sa): graph[i] = [] marked = [False] * sa for bad in range(sa): for asdf in range(bad+1, sa): if x[bad][asdf] == '1': graph[bad].append(asdf) graph[asdf].append(bad) done = [] for i in range(sa): if not marked[i]: a = recursive_dfs(graph, i) done.append(a) for j in recursive_dfs(graph, i): marked[j] = True final = [0] * sa for i in done: o = [toed[a] for a in i] o.sort() i.sort() for a in range(len(i)): final[i[a]] = o[a] print(' '.join([str(i) for i in final])) ```
output
1
26,901
12
53,803
Provide tags and a correct Python 3 solution for this coding contest problem. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
instruction
0
26,902
12
53,804
Tags: dfs and similar, dsu, graphs, greedy, math, sortings Correct Solution: ``` n=int(input()) p=list(map(int, input().split())) a=[list(map(int, list(input()))) for _ in range(n)] v=[0]*n l=n+1 xx=[-1]*n def dfs(x): global l v[x]=1 if xx[x]==-1: l=min(x,l) for i in range(n): if a[x][i]==1 and v[i]==0: dfs(i) x = [0]*(n+1) for i in range(n): x[p[i]]=i for i in range(1,n+1): v=[0]*n l=n+1 dfs(x[i]) xx[l]=i print(' '.join(map(str,xx))) ```
output
1
26,902
12
53,805
Provide tags and a correct Python 3 solution for this coding contest problem. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
instruction
0
26,903
12
53,806
Tags: dfs and similar, dsu, graphs, greedy, math, sortings Correct Solution: ``` from collections import defaultdict n = int(input()) a = list(map(int, input().split())) adj = defaultdict(list) for i in range(n): line = [int(i) for i in input().strip()] for j in range(n): if line[j]: adj[a[i]].append(a[j]) v = [0]*(n+1) cmps = [] for i in range(n): if not v[a[i]]: cmp = [] q = [a[i]] while q: e = q.pop() if not v[e]: cmp.append(e) v[e] = 1 for j in adj[e]: if not v[j]: q.append(j) cmps.append(sorted(cmp)) labels = [0]*n ct = 0 for i in range(len(cmps)): for j in range(n): if a[j] in cmps[i]: labels[j] = ct ct += 1 ans = [0]*n for i in range(ct): q = sorted(cmps[i], reverse=True) for j in range(n): if labels[j] == i: ans[j] = q[-1] q.pop() print(*ans) ```
output
1
26,903
12
53,807
Provide tags and a correct Python 3 solution for this coding contest problem. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
instruction
0
26,904
12
53,808
Tags: dfs and similar, dsu, graphs, greedy, math, sortings Correct Solution: ``` from sys import stdin input=lambda : stdin.readline().strip() from math import ceil,sqrt,factorial,gcd from collections import deque n=int(input()) l=list(map(int,input().split())) graph={i:set() for i in range(n)} for i in range(n): s=input() for j in range(n): if s[j]=='1' and i!=j: graph[i].add(j) graph[j].add(i) visited=set() # print(graph) for i in range(n): if i not in visited: a=[] b=[] stack=[i] while stack: x=stack.pop() if x not in visited: b.append(x) a.append(l[x]) for j in graph[x]: stack.append(j) # print(j,x) if x in graph[j]: graph[j].remove(x) visited.add(x) a.sort() b.sort() for i in range(len(a)): l[b[i]]=a[i] print(*l) ```
output
1
26,904
12
53,809
Provide tags and a correct Python 3 solution for this coding contest problem. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
instruction
0
26,905
12
53,810
Tags: dfs and similar, dsu, graphs, greedy, math, sortings Correct Solution: ``` #!/usr/bin/env python3 n = int(input()) p = [int(x) for x in input().split()] a = [[int(c) for c in input()] for _ in range(n)] exchange = [] for i in range(n): s = set() s.add(i) for j in range(n): if a[i][j] == 1: s.add(j) if len(s) > 1: exchange.append(s) i = 0 while i < len(exchange) - 1: j = i + 1 while j < len(exchange): if exchange[i] & exchange[j]: exchange[i] |= exchange[j] exchange.pop(j) j = i + 1 else: j += 1 i += 1 exchange = [sorted(s) for s in exchange] for ex in exchange: for i in range(len(ex) - 1): for j in range(i + 1, len(ex)): if p[ex[i]] > p[ex[j]]: p[ex[i]], p[ex[j]] = p[ex[j]], p[ex[i]] print(" ".join(map(str, p))) ```
output
1
26,905
12
53,811
Provide tags and a correct Python 3 solution for this coding contest problem. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
instruction
0
26,906
12
53,812
Tags: dfs and similar, dsu, graphs, greedy, math, sortings Correct Solution: ``` import bisect,sys from collections import deque, namedtuple sys.setrecursionlimit(20000) N = 1050 par = [i for i in range(1050)] siz = [1]*N def find(i): if i == par[i]: return i par[i] = find(par[i]) return par[i] def merge(a,b): a = find(a) b = find(b) if siz[a] < siz[b]: a,b = b,a par[b] = a siz[a] += siz[b] def main(): n = int(input()) l = [int(i) for i in input().split()] a = [ [ord(c)-ord('0') for c in input()] for j in range(n) ] for i in range(n): for j in range(n): if a[i][j]: merge(i,j) d = [[] for i in range(n)] for i in range(n): a = find(i) bisect.insort(d[a],l[i]) for i in range(n): a = find(i) print(d[a][0],end=" ") d[a] = d[a][1:] if __name__ == "__main__": main() ```
output
1
26,906
12
53,813
Provide tags and a correct Python 3 solution for this coding contest problem. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.
instruction
0
26,907
12
53,814
Tags: dfs and similar, dsu, graphs, greedy, math, sortings Correct Solution: ``` import sys,os,io import math,bisect,operator inf,mod = float('inf'),10**9+7 # sys.setrecursionlimit(10 ** 6) from itertools import groupby,accumulate from heapq import heapify,heappop,heappush from collections import deque,Counter,defaultdict input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ Neo = lambda : list(map(int,input().split())) # test, = Neo() def find(i): if i == par[i]: return i par[i] = find(par[i]) return par[i] def Union(a,b): a = find(a) b = find(b) if rank[a] < rank[b]: a,b = b,a par[b] = a rank[a] += rank[b] n, = Neo() l = Neo() par = list(range(n)) rank = [0]*n for i in range(n): s = input() for j in range(n): if s[j]=='1': Union(i, j) s = set() for i in range(n): s.add(find(i)) dic = defaultdict(list) for i in range(n): j = find(i) dic[j].append(l[i]) for i in s: dic[i].sort() dic[i].reverse() ans = [] for i in range(n): j = find(i) ans.append(dic[j].pop()) print(*ans) ```
output
1
26,907
12
53,815
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n. Submitted Solution: ``` def dfs(node, g) : global visited, M, N for i in range(N) : if M[node][i] == '0' or visited[i] : continue visited[i] = True g.append(i) dfs(i, g) N = int(input()) P = list(map(int, input().split(' '))) M = [input() for i in range(N)] G = [] visited = [False]*N for i in range(N) : if visited[i] : continue visited[i] = True g = [i] dfs(i, g) g.sort() G.append(g) for g in G : for i in range(len(g)) : for j in range(i+1, len(g)) : if P[g[i]] > P[g[j]] : P[g[i]], P[g[j]] = P[g[j]], P[g[i]] ans = "" for i in range(len(P)) : ans += str(P[i]) + ' ' ans = ans[:-1] print(ans) ```
instruction
0
26,908
12
53,816
Yes
output
1
26,908
12
53,817
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n. Submitted Solution: ``` def dfs(v): pos.append(v) used[v]=1 for i in range(n): if s[v][i]=="1" and not used[i]: dfs(i) n = int(input()) a = list(map(int,input().split())) s = [input() for _ in range(n)] used = [0]*n for i in range(n): if not used[i]: pos = [] dfs(i) values = [a[i] for i in pos] pos.sort() values.sort() for i,j in enumerate(pos): a[j] = values[i] print(" ".join(str(x) for x in a)) # Made By Mostafa_Khaled ```
instruction
0
26,909
12
53,818
Yes
output
1
26,909
12
53,819
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n. Submitted Solution: ``` def dfs(i,visited,a,m,n): l=[i] visited[i]=True for j in range(n): if(m[i][j]==1 and not visited[j]): l.append(j) x=dfs(j,visited,a,m,n) l.extend(x) return l def main(): n=int(input()) a=list(map(int,input().split())) m=[list(map(int,list(input()))) for i in range(n)] visited=[False]*n # print(m) for i in range(n): if(not visited[i]): x=dfs(i,visited,a,m,n) # print(x) val=[a[i] for i in x] val.sort() x.sort() for j in range(len(x)): a[x[j]]=val[j] print(" ".join(list(map(str,a)))) main() ```
instruction
0
26,910
12
53,820
Yes
output
1
26,910
12
53,821
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n. Submitted Solution: ``` from sys import stdin input=stdin.readline from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members n=int(input()) p=list(map(int,input().split())) a=[list(input().rstrip()) for i in range(n)] uf=UnionFind(n) for i in range(n): for j in range(n): if a[i][j]=="1": uf.union(i,j) ans=[-1]*n for r in uf.roots(): m=uf.members(r) idx=[] z=[] for mm in m: idx.append(mm) z.append(p[mm]) idx.sort() z.sort() for j in range(len(z)): ans[idx[j]]=z[j] print(*ans) ```
instruction
0
26,911
12
53,822
Yes
output
1
26,911
12
53,823
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n. Submitted Solution: ``` n = int(input("")) permute = list(map(int, input("").split())) A = [[ j for j in list(map(int, input("")))] for i in range(n)] for i in range(n-1): min_element = permute[i] min_index = i for j in range(i+1, n): if permute[j] < min_element and A[i][j] == 1: min_index = j min_element = permute[j] permute[i], permute[min_index] = permute[min_index], permute[i] print(" ".join(map(str, permute))) ```
instruction
0
26,912
12
53,824
No
output
1
26,912
12
53,825
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n. Submitted Solution: ``` n = int(input())#5 a = input().split()#72345 s = [input() for i in range(n)] act=True i3=0 while act: act=False for i in range(0,n): for j in range(0,n): # print(s[i][j]) if s[i][j]=='1': # print(s[i][j]) if i<j: if int(a[i])>int(a[j]): #print(str(a[i])+" "+str(a[j])) b=a[j] a[j]=a[i] a[i]=b act=True oput='' #i3=i3+1 #for i2 in range(0,n): # oput=oput+' '+a[i2] #print(oput+" ###:"+str(i3)+" i:"+str(i)+" j:"+str(j)+" ai:"+str(a[i])+" aj:"+str(a[j])) else: if int(a[j])>int(a[i]): b=a[j] a[j]=a[i] a[i]=b act=True oput='' #i3=i3+1 #for i2 in range(0,n): # oput=oput+' '+a[i2] #print(oput+" ##:"+str(i3)+" i:"+str(i)+" j:"+str(j)) oput='' for i2 in range(0,n): oput=oput+' '+a[i2] print(oput) ```
instruction
0
26,913
12
53,826
No
output
1
26,913
12
53,827
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n. Submitted Solution: ``` __author__ = 'Rakshak.R.Hegde' """ Created on Dec 30 2014 PM 08:41 @author: Rakshak.R.Hegde """ n = int(input()) p = input().split() a = [input() for i in range(n)] changed = True while changed: changed = False for i in range(0, n): for j in range(i + 1, n): if p[j] < p[i] and a[i][j] == '1': p[i], p[j] = p[j], p[i] changed = True # print('i:{}; j:{}; p:{}'.format(i, m[0], p)) print(' '.join(p)) ```
instruction
0
26,914
12
53,828
No
output
1
26,914
12
53,829
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds. As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1. Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain. Input The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds. Output In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained. Examples Input 7 5 2 4 3 6 7 1 0001001 0000000 0000010 1000001 0000000 0010000 1001000 Output 1 2 4 3 6 7 5 Input 5 4 2 1 5 3 00100 00011 10010 01101 01010 Output 1 2 3 4 5 Note In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7). In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4). <image> A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n. Submitted Solution: ``` # 500B: New Year Permutation # Start Time: 11:46 p.m. 5-2-15 # End Time: 11:46 p.m. 5-2-15 def solve(n, c, g): swaps = set() cont = True for i in range(n): for j in range(n): if g[i][j] == 1 and i <= j: swaps.add((i, j)) while cont: cont = False for s in swaps: if c[s[0]] > c[s[1]]: c[s[0]], c[s[1]] = c[s[1]], c[s[0]] cont = True print(" ".join(map(str, c))) if __name__ == "__main__": elements = int(input()) values = list(map(int, input().split())) grid = list() for _ in range(elements): grid.append([i == "1" for i in input()]) solve(elements, values, grid) ```
instruction
0
26,915
12
53,830
No
output
1
26,915
12
53,831
Provide tags and a correct Python 3 solution for this coding contest problem. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
instruction
0
27,096
12
54,192
Tags: constructive algorithms, math Correct Solution: ``` import math from decimal import Decimal def na(): n = int(input()) b = [int(x) for x in input().split()] return n,b def nab(): n = int(input()) b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] return n,b,c def dv(): n, m = map(int, input().split()) return n,m def dva(): n, m = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] return n,m,b def eratosthenes(n): sieve = list(range(n + 1)) for i in sieve: if i > 1: for j in range(i + i, len(sieve), i): sieve[j] = 0 return sorted(set(sieve)) def nm(): n = int(input()) b = [int(x) for x in input().split()] m = int(input()) c = [int(x) for x in input().split()] return n,b,m,c def dvs(): n = int(input()) m = int(input()) return n, m n, q = map(int, input().split()) ans = [] for i in range(q): x = int(input()) ans.append(x) for i in ans: d = 2 * n - i while d % 2 == 0: d //= 2 print(n - d // 2) ```
output
1
27,096
12
54,193
Provide tags and a correct Python 3 solution for this coding contest problem. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
instruction
0
27,097
12
54,194
Tags: constructive algorithms, math Correct Solution: ``` import sys import bisect input=sys.stdin.readline #t=int(input()) t=1 mod=10**9+7 for _ in range(t): #n=int(input()) n,q=map(int,input().split()) #s=input() #l=list(map(int,input().split())) #pref=[[0 for j in range(3001)] for i in range(n+2)] for i in range(q): x=int(input()) if x%2!=0: print((x+1)//2) else: loop=(n-x//2) #print(111,x,loop) while 1: x+=loop if (x)%2!=0: print((x-1)//2+1) break #print(loop) loop//=2 ```
output
1
27,097
12
54,195
Provide tags and a correct Python 3 solution for this coding contest problem. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
instruction
0
27,098
12
54,196
Tags: constructive algorithms, math Correct Solution: ``` import sys input=sys.stdin.buffer.readline from math import ceil n,q=map(int,input().split()) for i in range(q): indx=int(input()) count =indx if indx %2==0: count =0 pro= n-indx//2 while pro %2==0: count +=pro pro //=2 count +=pro count +=indx print((count +1) //2) ```
output
1
27,098
12
54,197
Provide tags and a correct Python 3 solution for this coding contest problem. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
instruction
0
27,099
12
54,198
Tags: constructive algorithms, math Correct Solution: ``` from sys import* input=stdin.readline n,q=map(int,input().split()) for _ in range(q): i=int(input()) while i%2==0:i+=n-i//2 print((i+1)//2) ```
output
1
27,099
12
54,199
Provide tags and a correct Python 3 solution for this coding contest problem. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
instruction
0
27,100
12
54,200
Tags: constructive algorithms, math Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- n,q=map(int,input().split()) for _ in range(q): i=int(input()) while i%2==0:i+=n-i//2 print((i+1)//2) ```
output
1
27,100
12
54,201
Provide tags and a correct Python 3 solution for this coding contest problem. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
instruction
0
27,101
12
54,202
Tags: constructive algorithms, math Correct Solution: ``` import sys def query(n, a): while a % 2 == 0: a += n - a // 2 return a // 2 + 1 n, q = map(int, sys.stdin.readline().split()) sys.stdout.write("\n".join(str(query(n, int(input()))) for _ in range(q))) # arr = [int(sys.stdin.readline()) for _ in range(q)] # sys.stdout.write("\n".join(str(query(n, a)) for a in arr)) ```
output
1
27,101
12
54,203
Provide tags and a correct Python 3 solution for this coding contest problem. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
instruction
0
27,102
12
54,204
Tags: constructive algorithms, math Correct Solution: ``` import sys def query(n, a): while a % 2 == 0: a += n - a // 2 return a // 2 + 1 n, q = map(int, sys.stdin.readline().split()) arr = [int(sys.stdin.readline()) for _ in range(q)] # arr = [query(n, a) for a in arr] # sys.stdout.write(*arr) sys.stdout.write("\n".join(str(query(n, a)) for a in arr)) ```
output
1
27,102
12
54,205
Provide tags and a correct Python 3 solution for this coding contest problem. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
instruction
0
27,103
12
54,206
Tags: constructive algorithms, math Correct Solution: ``` import sys input = sys.stdin.readline n, q = list(map(int, input().split())) x = [int(input()) for i in range(q)] for num in x: num = 2 * n - num while True: if num % 2 == 0: num = num // 2 else: break print(n - num // 2) ```
output
1
27,103
12
54,207
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. Submitted Solution: ``` #!/usr/bin/env python3 import sys [n, q] = map(int, sys.stdin.readline().strip().split()) qis = [int(sys.stdin.readline().strip()) for _ in range(q)] def query(n, q): d = 2 * n - q while d % 2 == 0: d //= 2 return (n - d // 2) for qi in qis: print (query(n, qi)) ```
instruction
0
27,104
12
54,208
Yes
output
1
27,104
12
54,209
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. Submitted Solution: ``` # Codeforces Submission # User : sudoSieg # Time : 15:13:27 # Date : 21/10/2020 import io import os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n, q = map(int, input().split()) for _ in range(q): x = int(input()) for r in range(100): num = 2 * n - 2 ** r - x den = 2 ** (r + 1) if num % den == 0 and n - num//den >= 1 and n - num//den <= n: print(n - num//den) break ```
instruction
0
27,105
12
54,210
Yes
output
1
27,105
12
54,211
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. Submitted Solution: ``` # Codeforces Submission # User : sudoSieg # Time : 15:13:27 # Date : 21/10/2020 def move(x): a = x.pop() if a != None: for i in range(len(x) - 1, -1, -1): if x[i] == None: x[i] = a return x return x def gen(n): x = [] for i in range(1, 2 * n): if i % 2 == 1: x.append((i + 1)//2) else: x.append(None) return x def solve(x, n): print(*x) while len(x) != n: x = move(x) print(*x) return x import io import os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n, q = map(int, input().split()) for _ in range(q): x = int(input()) for r in range(100): num = 2 * n - 2 ** r - x den = 2 ** (r + 1) if num % den == 0 and num >= 0 and den > 0 and n - num//den >= 1 and n - num//den <= n: i = n - num//den print(i) break ```
instruction
0
27,106
12
54,212
Yes
output
1
27,106
12
54,213
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. Submitted Solution: ``` import sys def query(n, a): while not (a & 1): a += (n - a//2) return a+1 >> 1 n, q = map(int, sys.stdin.readline().split()) arr = [int(sys.stdin.readline()) for _ in range(q)] sys.stdout.write("\n".join(str(query(n, a)) for a in arr)) ```
instruction
0
27,107
12
54,214
Yes
output
1
27,107
12
54,215
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Fri Mar 9 11:00:32 2018 @author: Nikita """ N, Q = map(int, input().split()) def f(N, x): if N == 1: return(1) elif x <= (N + 1) // 2: return 2 * x - 1 else: if f(N // 2, x - (N + 1) // 2) == N // 2: return 2 else: return 2 * g(N // 2, x - (N + 1) // 2) + 2 def g(N, x): if N == 1: return(1) elif x <= (N + 1) // 2: return 2 * x - 1 else: return 2 * g(N // 2, x - (N + 1) // 2) for q in range(Q): x = int(input()) print(f(N, x)) ```
instruction
0
27,108
12
54,216
No
output
1
27,108
12
54,217
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: <image> You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes. Input The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. Output For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. Examples Input 4 3 2 3 4 Output 3 2 4 Input 13 4 10 5 4 8 Output 13 3 8 9 Note The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7]. Submitted Solution: ``` n, q = map(int, input().split()) A = [] s = 2*n - 1 i = 1 for j in range(1, s+1): if j == 2*i - 1: A.insert(j, i) i += 1 else: A.insert(j, '') for a in range(s-1, 0, -1): if A[a-1] == '': A[a-1] = A[a] A[a] = '' else: k = a-2 while k > 0: if A[k] == '': A[k] = A[a] A[a] = '' break else: k -= 1 for w in range(q): z = int(input()) print(A.index(z)+1) ```
instruction
0
27,109
12
54,218
No
output
1
27,109
12
54,219