message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide tags and a correct Python 3 solution for this coding contest problem. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
instruction
0
41,663
12
83,326
Tags: binary search, data structures, two pointers Correct Solution: ``` n = int(input()) A = [int(x) for x in input().split()] s = {A[0]} left = 1 segments = [] for i in range(1, n): if A[i] in s: segments.append((left, i+1)) left = i+2 s.clear() else: s.add(A[i]) if len(segments) == 0: print(-1) else: segments[-1] = (segments[-1][0], n) print(len(segments)) for l, r in segments: print(l, r) ```
output
1
41,663
12
83,327
Provide tags and a correct Python 3 solution for this coding contest problem. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
instruction
0
41,664
12
83,328
Tags: binary search, data structures, two pointers Correct Solution: ``` n = int(input()) cnt = 0 gems = list(map(int, input().split())) pearls = set() for i in range(n): if gems[i] not in pearls: pearls.add(gems[i]) else: cnt += 1 pearls = set() if cnt: print(cnt) first = 0 second = 0 pearls = set() for i in range(n): if gems[i] not in pearls: pearls.add(gems[i]) else: if second: print(first + 1, second + 1) first = second + 1 second = i pearls = set() print(first + 1, n) else: print('-1') ```
output
1
41,664
12
83,329
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1 Submitted Solution: ``` import sys #import random from bisect import bisect_right as rb from collections import deque #sys.setrecursionlimit(10**6) from queue import PriorityQueue from math import * input_ = lambda: sys.stdin.readline().strip("\r\n") ii = lambda : int(input_()) il = lambda : list(map(int, input_().split())) ilf = lambda : list(map(float, input_().split())) ip = lambda : input_() fi = lambda : float(input_()) ap = lambda ab,bc,cd : ab[bc].append(cd) li = lambda : list(input_()) pr = lambda x : print(x) prinT = lambda x : print(x) f = lambda : sys.stdout.flush() mod = 10**9 + 7 n,k = il() a = il() b = [0 for i in range (10**6 + 5)] ans = [] l = -1 t = 0 j = 0 for i in range (n) : if (b[a[i]] == 0) : t += 1 b[a[i]] += 1 if (t > k) : while t>k : b[a[j]] -= 1 if (b[a[j]] == 0) : t -= 1 j += 1 if (i-j > l) : ans = [j+1,i+1] l = i-j print(*ans) ```
instruction
0
41,665
12
83,330
Yes
output
1
41,665
12
83,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1 Submitted Solution: ``` ans = [] s = set() l = 1 n = input() for i, e in enumerate(map(int, input().split()), 1): if e in s: s = set() ans += [(l, i)] l = i + 1 else: s.add(e) if ans: print(len(ans)) ans[-1] = ans[-1][0], n for a, b in ans: print(a, b) else: print(-1) ```
instruction
0
41,666
12
83,332
Yes
output
1
41,666
12
83,333
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1 Submitted Solution: ``` import sys count = int(input()) perls = list(input().split(' ')) if len(perls)<2: print('-1') sys,exit() start=0 cur = 0 flag=0 slide=set() goodlist=list() for i in perls: if i in slide: goodlist.append([start+1,cur+1]) start=cur+1 slide.clear() flag=1 else: slide.add(i) flag=0 cur+=1 if len(goodlist)==0: print('-1') sys.exit() if flag==0: goodlist[-1][1]=len(perls) print(len(goodlist)) for i in goodlist: print('{} {}'.format(i[0],i[1])) ```
instruction
0
41,667
12
83,334
Yes
output
1
41,667
12
83,335
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1 Submitted Solution: ``` n = int(input()) a = list(map(int,input().split())) m = {} start = 0 count = 0 x = [] y = [] for i in range(n): num = a[i] if num in m: count += 1 x.append(start+1) y.append(i+1) start = i+1 m = {} else: m[num] = 1 if (len(x)==0): print(-1) else: print(len(x)) y[len(y)-1] = n for i in range(len(x)): print(x[i],y[i]) ```
instruction
0
41,668
12
83,336
Yes
output
1
41,668
12
83,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1 Submitted Solution: ``` n = int(input()) arr = list(map(int,input().split())) if len(arr)==len(set(arr)): print (-1) else: seg = 0 ans = [] i=0 while i<n: j=i while j<n: if len(arr[i:j+1]) > len(set(arr[i:j+1])): if len(set(arr[j:n+1]))==len(arr[j:n+1]): ans.append((i+1,n)) else: ans.append((i+1,j+1)) i=j+1 seg+=1 break j+=1 i+=1 print (seg) for i in ans: for j in i: print (j,end=' ') print() ```
instruction
0
41,669
12
83,338
No
output
1
41,669
12
83,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1 Submitted Solution: ``` import sys inputNum=input() inputTypes=input().split(' ') stack1=[] stack2=[] stack3=[] for i in range(len(inputTypes)): if len(stack1)==0: stack1.append(inputTypes[i]) stack2.append(str(i+1)) else: if inputTypes[i] in stack1: stack2.append(str(i+1)) stack3.append(' '.join(stack2)) stack1.clear() stack2.clear() else: stack1.append(inputTypes[i]) #stack2.append(str(i+1)) if len(stack1)!=0: print(-1) sys.exit() else: print(len(stack3)) for x in stack3: print(x) ```
instruction
0
41,670
12
83,340
No
output
1
41,670
12
83,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1 Submitted Solution: ``` import sys n = int(sys.stdin.readline()) arr = list(map(int, sys.stdin.readline().split())) idx = 0 cr = [] i = 1 while i < len(arr): if arr[i] in arr[idx:i]: cr.append([idx + 1, i + 1]) idx = i + 1 i += 1 i += 1 if cr: if cr[-1][1] != i + 1: cr[-1][1] = i if not cr: sys.stdout.write("-1\n") else: sys.stdout.write(f"{len(cr)}\n") for i in range(len(cr)): sys.stdout.write(f"{cr[i][0]} {cr[i][1]}\n") ```
instruction
0
41,671
12
83,342
No
output
1
41,671
12
83,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k. The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a. Output Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1 Submitted Solution: ``` n = int(input()) index = 1 memory = [] counter = 0 segments = [(0, 0)] for number in input().split(): if number not in memory: memory.append(number) else: counter += 1 segments.append((segments[-1][1] + 1, index)) memory = [] index += 1 if counter != 0: print(counter) segments.remove((0, 0)) for item in segments: print(' '.join(map(str, item))) else: print(-1) ```
instruction
0
41,672
12
83,344
No
output
1
41,672
12
83,345
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
instruction
0
41,704
12
83,408
Tags: constructive algorithms, dfs and similar Correct Solution: ``` #-*- coding:utf-8 -*- #!/usr/bin/python3 def dfs(x): if vis[p[x]] == False: vis[p[x]] = True dfs(p[x]) n = int(input()) p = list(map(int, input().split())) b = list(map(int, input().split())) vis = [False] * n num = 0 for i in range(0, n): p[i] = p[i] - 1 for i in range(0, n): if vis[i] == False: while(vis[i] == False): vis[i] = True i = p[i] num = num + 1 if num != 1: ans = num else: ans = 0 num = 0 for bx in b: if bx == 1: num = num + 1 if num % 2 == 0: ans = ans + 1 print(ans) ```
output
1
41,704
12
83,409
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
instruction
0
41,705
12
83,410
Tags: constructive algorithms, dfs and similar Correct Solution: ``` n=int(input()) A = list(map(int,input().split())) B = list(map(int,input().split())) ans =0 sums1 =0 for j in range(n): if B[j] == 1: sums1+=1 if sums1 % 2==0: ans=1 visited = [0] * n cou =0 for j in range(n): if visited[j] == 0: visited[j] = 1 while True: if visited[A[j]-1] == 0: visited[A[j]-1] = 1 j = A[j]-1 else: cou += 1 break if cou !=1: ans +=cou print(ans) ```
output
1
41,705
12
83,411
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
instruction
0
41,706
12
83,412
Tags: constructive algorithms, dfs and similar Correct Solution: ``` n = int(input()) p = [int(i) for i in input().split()] b = [int(i) for i in input().split()] ans = 0 visited = [False for i in range(n)] for i in range(n): if visited[i]: continue ans += 1 while not visited[i]: visited[i] = True i = p[i] - 1 if ans == 1: ans = 0 ans += (sum(b) + 1) % 2 print(ans) ```
output
1
41,706
12
83,413
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
instruction
0
41,707
12
83,414
Tags: constructive algorithms, dfs and similar Correct Solution: ``` n = int(input()) p = list(map(int, input().split())) b = list(map(int, input().split())) cnt_cyc = 0 vis = [0] * n for i in range(n): if vis[i] == 0: vis[i] = 1 nxt = p[i]-1 while vis[nxt] == 0: vis[nxt] = 1 nxt = p[nxt]-1 cnt_cyc += 1 res = (b.count(1)+1) % 2 print(res if cnt_cyc == 1 else res + cnt_cyc) ```
output
1
41,707
12
83,415
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
instruction
0
41,708
12
83,416
Tags: constructive algorithms, dfs and similar Correct Solution: ``` n = int(input()) p = [[0, 1] for i in range(n)] b = [0 for i in range(n)] ans = 0 s = input().split() for i in range(n): p[i][0] = int(s[i]) - 1 s = input().split() for i in range(n): b[i] = int(s[i]) temp = 0 cyc = 0 for i in range(n): if p[i][1] == 0: continue cyc += 1 temp = i while p[temp][1] == 1: p[temp][1] = 0 temp = p[temp][0] if cyc > 1: ans = cyc if sum(b) % 2 == 0: ans += 1 print(str(ans)) ```
output
1
41,708
12
83,417
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
instruction
0
41,709
12
83,418
Tags: constructive algorithms, dfs and similar Correct Solution: ``` from collections import deque def read(type=int): return type(input()) def read_arr(type=int): return [type(token) for token in input().split()] def num_cycles(P): V = [False] * len(P) i = 0 for u in P: if not V[u]: while not V[u]: V[u] = True u = P[u] i += 1 return i def runB(): n = read() P = read_arr() P = [a-1 for a in P] B = read_arr() extra = (sum(B) + 1) % 2 cycles = num_cycles(P) ans = extra + (cycles if cycles > 1 else 0) print(ans) runB() ```
output
1
41,709
12
83,419
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
instruction
0
41,710
12
83,420
Tags: constructive algorithms, dfs and similar Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) permutation = list(map(int, input().split())) go = set(range(1,n+1)) reached = set() ans = 0 while go: x = go.pop() while x not in reached: reached.add(x) x = permutation[x-1] if x in go: go.remove(x) ans += 1 if ans == 1: ans = 0 permutation = input().split() if permutation.count('1') % 2 == 0: ans += 1 print(ans)# ```
output
1
41,710
12
83,421
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1.
instruction
0
41,711
12
83,422
Tags: constructive algorithms, dfs and similar Correct Solution: ``` n = int(input()) p = list(map(lambda x: int(x)-1, input().split())) b = list(map(int, input().split())) cp = 0 z = [True]*n old = -1 nn = n while nn: cp += 1 cur = next(i for i in range(old + 1, n) if z[i]) old = cur while(z[cur]): z[cur] = False nn -= 1 cur = p[cur] if cp == 1: cp = 0 cb = 1 - (sum(b) & 1) print(cb + cp) ```
output
1
41,711
12
83,423
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` class Graph: def __init__(self,n): self.nodes=n self.edge_mapping=[] for x in range(self.nodes+1): self.edge_mapping.append([]) def add_edge(self,src,dest): if self.edge_mapping[src]==None: self.edge_mapping[src]=[dest] else: self.edge_mapping[src].append(dest) def dfs_inner(self,nd,mark_list): stack=[] stack.append(nd) while len(stack)!=0: node=stack.pop() mark_list[node]=1 for n in self.edge_mapping[node]: if mark_list[n]==0: stack.append(n) def dfs(self): mark_list=[0]*(self.nodes+1) ret=0; for i in range(1,self.nodes+1): if mark_list[i]!=1: self.dfs_inner(i,mark_list) ret+=1 return ret def printg(self): for z in self.edge_mapping: print(z) if __name__ == '__main__': n=int(input()) g=Graph(n) p_list=list(map(int,input().split())) s_list=list(map(int,input().split())) for x in range(len(p_list)): g.add_edge(p_list[x],x+1) ans=0 trav=g.dfs() if trav!=1: ans+=trav s=0 for x in s_list: s+=x # print(ans) if s%2==0: print(ans+1) else: print(ans) ```
instruction
0
41,712
12
83,424
Yes
output
1
41,712
12
83,425
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` n = int(input()) p = map(int, input().split()) p = [x - 1 for x in p] b = map(int, input().split()) s = int(sum(b)) c = 0 done = set() for i in range(n): if i not in done: c += 1 j = i while j not in done: done.add(j) j = p[j] modif = 0 if c > 1: modif = c res = modif + (s + 1) % 2 print(res) ```
instruction
0
41,713
12
83,426
Yes
output
1
41,713
12
83,427
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` import sys input = sys.stdin.readline N = int(input()) permu = list(map(int, input().split())) permu = [i-1 for i in permu] blist = list(map(int, input().split())) cyclefind = [False for i in range(N)] cyclenum = 0 for i in range(N): if cyclefind[i] == False: cyclenum+=1 q = i while cyclefind[q] != True: cyclefind[q] = True q = permu[q] if cyclenum == 1: cyclenum = 0 if blist.count(1) % 2 == 0: cyclenum+=1 print(cyclenum) ```
instruction
0
41,714
12
83,428
Yes
output
1
41,714
12
83,429
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` amount = int(input()) movement = list(map(int, input().split())) reachable = [False]*amount flip = sum(list(map(int, input().split()))) changesneeded = 0 flipneeded = 0 if flip % 2 == 0: flipneeded = 1 for i in range(amount): if not reachable[i]: changesneeded += 1 point = i while True: if reachable[point]: break reachable[point] = True point = movement[point]-1 if changesneeded > 1: print(changesneeded+flipneeded) else: print(flipneeded) ```
instruction
0
41,715
12
83,430
Yes
output
1
41,715
12
83,431
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` import sys sys.setrecursionlimit(200000000) def can_reach(point): if not reachable[point]: reachable[point] = True can_reach(movement[point]-1) return 1 else: return 0 amount = int(input()) movement = list(map(int, input().split())) reachable = [False]*amount flip = sum(list(map(int, input().split()))) changesneeded = 0 flipneeded = 0 if flip % 2 == 0: flipneeded = 1 for i in range(amount): adder = 0 if not reachable[i]: changesneeded += adder point = i while True: if reachable[point]: break reachable[point] = True point = movement[point]-1 if changesneeded > 1: print(changesneeded+flipneeded) else: print(flipneeded) ```
instruction
0
41,716
12
83,432
No
output
1
41,716
12
83,433
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` if __name__ == '__main__': n, = map(int, input().split()) p = list(map(lambda x: int(x)-1, input().split())) swaps = sum(map(int, input().split())) res = 1 - (swaps % 2) visited = [False for _ in range(n)] res -= 2 for i in range(n): if visited[i]: continue visited[i] = True j = p[i] while j != i: visited[j] = True j = p[j] res += 2 print(res) ```
instruction
0
41,717
12
83,434
No
output
1
41,717
12
83,435
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` class Graph: def __init__(self,n): self.nodes=n self.edge_mapping=[None]*(self.nodes+1) def add_edge(self,src,dest): if self.edge_mapping[src]==None: self.edge_mapping[src]=[dest] else: self.edge_mapping[src].append(dest) def dfs_inner(self,node,mark_list): mark_list[node]=1 ret=1 for n in self.edge_mapping[node]: if mark_list[n]==0: ret+=self.dfs_inner(n,mark_list) return ret def dfs(self,start_node): mark_list=[0]*(self.nodes+1) return self.dfs_inner(start_node,mark_list) def printg(self): for z in self.edge_mapping: print(z) if __name__ == '__main__': n=int(input()) g=Graph(n) p_list=list(map(int,input().split())) s_list=list(map(int,input().split())) for x in range(len(p_list)): g.add_edge(p_list[x],x+1) ans=0 trav=g.dfs(1) ans+=min(trav,n-trav) s=0 for x in s_list: s+=x # print(ans) if s%2==0: print(ans+1) else: print(ans) ```
instruction
0
41,718
12
83,436
No
output
1
41,718
12
83,437
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` class Graph: def __init__(self,n): self.nodes=n self.edge_mapping=[] for x in range(self.nodes+1): self.edge_mapping.append([]) def add_edge(self,src,dest): if self.edge_mapping[src]==None: self.edge_mapping[src]=[dest] else: self.edge_mapping[src].append(dest) def dfs_inner(self,node,mark_list): mark_list[node]=1 try: for n in self.edge_mapping[node]: if mark_list[n]==0: self.dfs_inner(n,mark_list) except: # print(node) pass def dfs(self): mark_list=[0]*(self.nodes+1) ret=0; for i in range(1,self.nodes+1): if mark_list[i]!=1: self.dfs_inner(i,mark_list) ret+=1 return ret def printg(self): for z in self.edge_mapping: print(z) if __name__ == '__main__': n=int(input()) g=Graph(n) p_list=list(map(int,input().split())) s_list=list(map(int,input().split())) for x in range(len(p_list)): g.add_edge(p_list[x],x+1) ans=0 trav=g.dfs() if trav!=1: ans+=trav s=0 for x in s_list: s+=x # print(ans) if s%2==0: print(ans+1) else: print(ans) ```
instruction
0
41,719
12
83,438
No
output
1
41,719
12
83,439
Provide a correct Python 3 solution for this coding contest problem. You are given N integers A_1, A_2, ..., A_N. Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number. Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}. Find the median of this list, S_{2^{N-1}}. Constraints * 1 \leq N \leq 2000 * 1 \leq A_i \leq 2000 * All input values are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the median of the sorted list of the sums of all non-empty subsequences of A. Examples Input 3 1 2 1 Output 2 Input 1 58 Output 58
instruction
0
41,909
12
83,818
"Correct Solution: ``` #!/usr/bin/env python3 import math input() A = list(map(int, input().split())) dp = 1 for x in A: dp |= dp << x h = math.ceil(sum(A) / 2) print(h + bin(dp >> h)[::-1].index('1')) ```
output
1
41,909
12
83,819
Provide a correct Python 3 solution for this coding contest problem. You are given N integers A_1, A_2, ..., A_N. Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number. Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}. Find the median of this list, S_{2^{N-1}}. Constraints * 1 \leq N \leq 2000 * 1 \leq A_i \leq 2000 * All input values are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the median of the sorted list of the sums of all non-empty subsequences of A. Examples Input 3 1 2 1 Output 2 Input 1 58 Output 58
instruction
0
41,912
12
83,824
"Correct Solution: ``` """ 連続でない部分列問題… 中央値問題でよくあるのは、その数字以上の数がいくつ出てくるかで二分探索 部分問題:N個ある数列Aから1つ以上取って総和がK以上になる取り方はいくつあるか →普通にdpするとできるがO(N^2*A)で無理(つーかほぼ答え) 中央値と平均値の関係は? →今回はすべてのAが1/2の確率で選ばれるので、Aの総和/2 が全てのSの平均値となる →だからなに・・・ =======解説をみた======= Aのすべての合計をTとすると、前半はT/2以下で、後半はT/2以上(補集合の関係から) 求めるのは後半の一番小さい数字→T/2以上で登場する最小値 boolのDPになった! bitsetを使えばオーダーレベルの改善になる https://w.atwiki.jp/vivid_turtle/pages/40.html ここに書かれている例に帰着する """ import sys N = int(input()) A = list(map(int,input().split())) asum = sum(A) now = 1 for i in range(N): now = (now<<A[i]) | now for i in range((asum+1)//2 , asum+1): if (1<<i) & now > 0: print (i) sys.exit() ```
output
1
41,912
12
83,825
Provide a correct Python 3 solution for this coding contest problem. You are given N integers A_1, A_2, ..., A_N. Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number. Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}. Find the median of this list, S_{2^{N-1}}. Constraints * 1 \leq N \leq 2000 * 1 \leq A_i \leq 2000 * All input values are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the median of the sorted list of the sums of all non-empty subsequences of A. Examples Input 3 1 2 1 Output 2 Input 1 58 Output 58
instruction
0
41,915
12
83,830
"Correct Solution: ``` def main(): _,a=open(0) *a,=map(int,a.split()) d=1 for i in a:d|=d<<i s=0--sum(a)//2 d>>=s print((d&-d).bit_length()+s-1) main() ```
output
1
41,915
12
83,831
Provide a correct Python 3 solution for this coding contest problem. You are given N integers A_1, A_2, ..., A_N. Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number. Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}. Find the median of this list, S_{2^{N-1}}. Constraints * 1 \leq N \leq 2000 * 1 \leq A_i \leq 2000 * All input values are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the median of the sorted list of the sums of all non-empty subsequences of A. Examples Input 3 1 2 1 Output 2 Input 1 58 Output 58
instruction
0
41,916
12
83,832
"Correct Solution: ``` n, arr, dp = int(input()), list(map(int, input().split())), 1 for a in arr: dp |= dp << a ofs = (sum(arr) + 1) // 2 dp >>= ofs print((dp & -dp).bit_length() + ofs - 1) ```
output
1
41,916
12
83,833
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given N integers A_1, A_2, ..., A_N. Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number. Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}. Find the median of this list, S_{2^{N-1}}. Constraints * 1 \leq N \leq 2000 * 1 \leq A_i \leq 2000 * All input values are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the median of the sorted list of the sums of all non-empty subsequences of A. Examples Input 3 1 2 1 Output 2 Input 1 58 Output 58 Submitted Solution: ``` from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce INF = float('inf') def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return sys.stdin.readline().strip() def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 n = I() A = LI() dp = 1 for a in A: dp |= dp << a b = bin(dp) s = sum(A) for i in range((s + 1)//2, s + 1): if 1 << i & dp: print(i) break ```
instruction
0
41,920
12
83,840
Yes
output
1
41,920
12
83,841
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array which is initially empty. You need to perform n operations of the given format: * "a l r k": append a to the end of the array. After that count the number of integer pairs x, y such that l ≤ x ≤ y ≤ r and \operatorname{mex}(a_{x}, a_{x+1}, …, a_{y}) = k. The elements of the array are numerated from 1 in the order they are added to the array. To make this problem more tricky we don't say your real parameters of the queries. Instead your are given a', l', r', k'. To get a, l, r, k on the i-th operation you need to perform the following: * a := (a' + lans) mod(n + 1), * l := (l' + lans) mod{i} + 1, * r := (r' + lans) mod{i} + 1, * if l > r swap l and r, * k := (k' + lans) mod(n + 1), where lans is the answer to the previous operation, initially lans is equal to zero. i is the id of the operation, operations are numbered from 1. The \operatorname{mex}(S), where S is a multiset of non-negative integers, is the smallest non-negative integer which does not appear in the set. For example, \operatorname{mex}(\{2, 2, 3\}) = 0 and \operatorname{mex} (\{0, 1, 4, 1, 6\}) = 2. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array. The next n lines contain the description of queries. Each of them n lines contains four non-negative integers a', l', r', k' (0, ≤ a', l', r', k' ≤ 10^9), describing one operation. Output For each query print a single integer — the answer to this query. Examples Input 5 0 0 0 1 0 1 0 5 5 2 1 0 5 2 1 0 2 4 3 3 Output 1 1 2 6 3 Input 5 2 0 0 2 2 0 1 1 0 0 2 0 3 2 2 0 0 2 3 0 Output 0 0 3 0 0 Note For the first example the decoded values of a, l, r, k are the following: a_1=0,l_1=1,r_1=1,k_1=1 a_2=1,l_2=1,r_2=2,k_2=0 a_3=0,l_3=1,r_3=3,k_3=1 a_4=1,l_4=1,r_4=4,k_4=2 a_5=2,l_5=1,r_5=5,k_5=3 For the second example the decoded values of a, l, r, k are the following: a_1=2,l_1=1,r_1=1,k_1=2 a_2=2,l_2=1,r_2=2,k_2=1 a_3=0,l_3=1,r_3=3,k_3=0 a_4=0,l_4=2,r_4=2,k_4=3 a_5=0,l_5=3,r_5=4,k_5=0 Submitted Solution: ``` print("Hello, World") ```
instruction
0
42,165
12
84,330
No
output
1
42,165
12
84,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array which is initially empty. You need to perform n operations of the given format: * "a l r k": append a to the end of the array. After that count the number of integer pairs x, y such that l ≤ x ≤ y ≤ r and \operatorname{mex}(a_{x}, a_{x+1}, …, a_{y}) = k. The elements of the array are numerated from 1 in the order they are added to the array. To make this problem more tricky we don't say your real parameters of the queries. Instead your are given a', l', r', k'. To get a, l, r, k on the i-th operation you need to perform the following: * a := (a' + lans) mod(n + 1), * l := (l' + lans) mod{i} + 1, * r := (r' + lans) mod{i} + 1, * if l > r swap l and r, * k := (k' + lans) mod(n + 1), where lans is the answer to the previous operation, initially lans is equal to zero. i is the id of the operation, operations are numbered from 1. The \operatorname{mex}(S), where S is a multiset of non-negative integers, is the smallest non-negative integer which does not appear in the set. For example, \operatorname{mex}(\{2, 2, 3\}) = 0 and \operatorname{mex} (\{0, 1, 4, 1, 6\}) = 2. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array. The next n lines contain the description of queries. Each of them n lines contains four non-negative integers a', l', r', k' (0, ≤ a', l', r', k' ≤ 10^9), describing one operation. Output For each query print a single integer — the answer to this query. Examples Input 5 0 0 0 1 0 1 0 5 5 2 1 0 5 2 1 0 2 4 3 3 Output 1 1 2 6 3 Input 5 2 0 0 2 2 0 1 1 0 0 2 0 3 2 2 0 0 2 3 0 Output 0 0 3 0 0 Note For the first example the decoded values of a, l, r, k are the following: a_1=0,l_1=1,r_1=1,k_1=1 a_2=1,l_2=1,r_2=2,k_2=0 a_3=0,l_3=1,r_3=3,k_3=1 a_4=1,l_4=1,r_4=4,k_4=2 a_5=2,l_5=1,r_5=5,k_5=3 For the second example the decoded values of a, l, r, k are the following: a_1=2,l_1=1,r_1=1,k_1=2 a_2=2,l_2=1,r_2=2,k_2=1 a_3=0,l_3=1,r_3=3,k_3=0 a_4=0,l_4=2,r_4=2,k_4=3 a_5=0,l_5=3,r_5=4,k_5=0 Submitted Solution: ``` import copy def mex(k): for i in range(k) : if q.count(i)==0 : return 0 return 1 lans=0 n=int(input()) w=[] o=[] for i in range(n) : a,l,r,k=map(int,input().split()) a=(a+lans)%(n+1) l=(l+lans)%(i+1)+1 r=(r+lans)%(i+1)+1 k=(k+lans)%(n+1) if l>r : x=l l=r r=x w.append(a) j=0 total=0 ok=1 o=[] for i in range(k): if w.count(i)==0 : total=0 ok=0 break if ok == 1 : for i in range(len(w)) : q=copy.deepcopy(o) for j in range(i,len(w)): q.append(w[j]) if q.count(k)==0 and mex(k): total+=1 print(total) lans=total ```
instruction
0
42,166
12
84,332
No
output
1
42,166
12
84,333
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array which is initially empty. You need to perform n operations of the given format: * "a l r k": append a to the end of the array. After that count the number of integer pairs x, y such that l ≤ x ≤ y ≤ r and \operatorname{mex}(a_{x}, a_{x+1}, …, a_{y}) = k. The elements of the array are numerated from 1 in the order they are added to the array. To make this problem more tricky we don't say your real parameters of the queries. Instead your are given a', l', r', k'. To get a, l, r, k on the i-th operation you need to perform the following: * a := (a' + lans) mod(n + 1), * l := (l' + lans) mod{i} + 1, * r := (r' + lans) mod{i} + 1, * if l > r swap l and r, * k := (k' + lans) mod(n + 1), where lans is the answer to the previous operation, initially lans is equal to zero. i is the id of the operation, operations are numbered from 1. The \operatorname{mex}(S), where S is a multiset of non-negative integers, is the smallest non-negative integer which does not appear in the set. For example, \operatorname{mex}(\{2, 2, 3\}) = 0 and \operatorname{mex} (\{0, 1, 4, 1, 6\}) = 2. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array. The next n lines contain the description of queries. Each of them n lines contains four non-negative integers a', l', r', k' (0, ≤ a', l', r', k' ≤ 10^9), describing one operation. Output For each query print a single integer — the answer to this query. Examples Input 5 0 0 0 1 0 1 0 5 5 2 1 0 5 2 1 0 2 4 3 3 Output 1 1 2 6 3 Input 5 2 0 0 2 2 0 1 1 0 0 2 0 3 2 2 0 0 2 3 0 Output 0 0 3 0 0 Note For the first example the decoded values of a, l, r, k are the following: a_1=0,l_1=1,r_1=1,k_1=1 a_2=1,l_2=1,r_2=2,k_2=0 a_3=0,l_3=1,r_3=3,k_3=1 a_4=1,l_4=1,r_4=4,k_4=2 a_5=2,l_5=1,r_5=5,k_5=3 For the second example the decoded values of a, l, r, k are the following: a_1=2,l_1=1,r_1=1,k_1=2 a_2=2,l_2=1,r_2=2,k_2=1 a_3=0,l_3=1,r_3=3,k_3=0 a_4=0,l_4=2,r_4=2,k_4=3 a_5=0,l_5=3,r_5=4,k_5=0 Submitted Solution: ``` print("Hello world") ```
instruction
0
42,167
12
84,334
No
output
1
42,167
12
84,335
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array which is initially empty. You need to perform n operations of the given format: * "a l r k": append a to the end of the array. After that count the number of integer pairs x, y such that l ≤ x ≤ y ≤ r and \operatorname{mex}(a_{x}, a_{x+1}, …, a_{y}) = k. The elements of the array are numerated from 1 in the order they are added to the array. To make this problem more tricky we don't say your real parameters of the queries. Instead your are given a', l', r', k'. To get a, l, r, k on the i-th operation you need to perform the following: * a := (a' + lans) mod(n + 1), * l := (l' + lans) mod{i} + 1, * r := (r' + lans) mod{i} + 1, * if l > r swap l and r, * k := (k' + lans) mod(n + 1), where lans is the answer to the previous operation, initially lans is equal to zero. i is the id of the operation, operations are numbered from 1. The \operatorname{mex}(S), where S is a multiset of non-negative integers, is the smallest non-negative integer which does not appear in the set. For example, \operatorname{mex}(\{2, 2, 3\}) = 0 and \operatorname{mex} (\{0, 1, 4, 1, 6\}) = 2. Input The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array. The next n lines contain the description of queries. Each of them n lines contains four non-negative integers a', l', r', k' (0, ≤ a', l', r', k' ≤ 10^9), describing one operation. Output For each query print a single integer — the answer to this query. Examples Input 5 0 0 0 1 0 1 0 5 5 2 1 0 5 2 1 0 2 4 3 3 Output 1 1 2 6 3 Input 5 2 0 0 2 2 0 1 1 0 0 2 0 3 2 2 0 0 2 3 0 Output 0 0 3 0 0 Note For the first example the decoded values of a, l, r, k are the following: a_1=0,l_1=1,r_1=1,k_1=1 a_2=1,l_2=1,r_2=2,k_2=0 a_3=0,l_3=1,r_3=3,k_3=1 a_4=1,l_4=1,r_4=4,k_4=2 a_5=2,l_5=1,r_5=5,k_5=3 For the second example the decoded values of a, l, r, k are the following: a_1=2,l_1=1,r_1=1,k_1=2 a_2=2,l_2=1,r_2=2,k_2=1 a_3=0,l_3=1,r_3=3,k_3=0 a_4=0,l_4=2,r_4=2,k_4=3 a_5=0,l_5=3,r_5=4,k_5=0 Submitted Solution: ``` def mex(l): m = 0 for i in l: if i == m: m += 1 return m def find_mex(mas, l, r, k): lis = mas[l-1:r] count = 0 lis.sort() for i in range(len(lis)): if mex(lis[i:]) == k: count += len(lis)-i return count def holy_diver(): lans = 0 results = [] mas = [] req = int(input()) for i in range(1, req+1): s = input().split() a = (int(s[0]) + lans)%(req+1) l = (int(s[1]) + lans)%i+1 r = (int(s[2]) + lans)%i+1 if l > r : tmp = r r = l l = tmp k = (int(s[3]) + lans)%(req+1) mas.append(a) res = find_mex(mas, l, r, k) results.append(res) lans = res for i in results: print(i) holy_diver() ```
instruction
0
42,168
12
84,336
No
output
1
42,168
12
84,337
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero. You have to process two types of queries to this array: * 1 x y — increase a_x by y; * 2 x y — compute ∑_{i ∈ R(x, y)} a_i, where R(x, y) is the set of all integers from 1 to 500000 which have remainder y modulo x. Can you process all the queries? Input The first line contains one integer q (1 ≤ q ≤ 500000) — the number of queries. Then q lines follow, each describing a query. The i-th line contains three integers t_i, x_i and y_i (1 ≤ t_i ≤ 2). If t_i = 1, then it is a query of the first type, 1 ≤ x_i ≤ 500000, and -1000 ≤ y_i ≤ 1000. If t_i = 2, then it it a query of the second type, 1 ≤ x_i ≤ 500000, and 0 ≤ y_i < x_i. It is guaranteed that there will be at least one query of type 2. Output For each query of type 2 print one integer — the answer to it. Example Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
instruction
0
42,173
12
84,346
Tags: brute force, data structures, implementation Correct Solution: ``` import sys readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): q = ni() n = 5 * 10** 5 a = [0]*(n + 1) m = 720 rem = [[0]*m for _ in range(m)] for _ in range(q): t, x, y = nm() if t == 1: a[x] += y for p in range(1, m): rem[p][x%p] += y else: if x < m: print(rem[x][y]) else: print(sum(a[i] for i in range(y, n+1, x))) if __name__ == '__main__': solve() # T = ni() # for _ in range(T): # solve() ```
output
1
42,173
12
84,347
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero. You have to process two types of queries to this array: * 1 x y — increase a_x by y; * 2 x y — compute ∑_{i ∈ R(x, y)} a_i, where R(x, y) is the set of all integers from 1 to 500000 which have remainder y modulo x. Can you process all the queries? Input The first line contains one integer q (1 ≤ q ≤ 500000) — the number of queries. Then q lines follow, each describing a query. The i-th line contains three integers t_i, x_i and y_i (1 ≤ t_i ≤ 2). If t_i = 1, then it is a query of the first type, 1 ≤ x_i ≤ 500000, and -1000 ≤ y_i ≤ 1000. If t_i = 2, then it it a query of the second type, 1 ≤ x_i ≤ 500000, and 0 ≤ y_i < x_i. It is guaranteed that there will be at least one query of type 2. Output For each query of type 2 print one integer — the answer to it. Example Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
instruction
0
42,174
12
84,348
Tags: brute force, data structures, implementation Correct Solution: ``` import sys from array import array # noqa: F401 import typing as Tp # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') def main(): q = int(input()) n = 500000 m = 600 a = array('i', [0]) * (n + 1) dp = [array('i', [0]) * (i + 1) for i in range(m + 1)] ans = [] for _ in range(q): t, x, y = map(int, input().split()) if t == 1: a[x] += y for i in range(1, m + 1): dp[i][x % i] += y else: if x > m: ans.append(sum(a[i] for i in range(y, n + 1, x))) else: ans.append(dp[x][y]) sys.stdout.buffer.write(('\n'.join(map(str, ans)) + '\n').encode('utf-8')) if __name__ == '__main__': main() ```
output
1
42,174
12
84,349
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero. You have to process two types of queries to this array: * 1 x y — increase a_x by y; * 2 x y — compute ∑_{i ∈ R(x, y)} a_i, where R(x, y) is the set of all integers from 1 to 500000 which have remainder y modulo x. Can you process all the queries? Input The first line contains one integer q (1 ≤ q ≤ 500000) — the number of queries. Then q lines follow, each describing a query. The i-th line contains three integers t_i, x_i and y_i (1 ≤ t_i ≤ 2). If t_i = 1, then it is a query of the first type, 1 ≤ x_i ≤ 500000, and -1000 ≤ y_i ≤ 1000. If t_i = 2, then it it a query of the second type, 1 ≤ x_i ≤ 500000, and 0 ≤ y_i < x_i. It is guaranteed that there will be at least one query of type 2. Output For each query of type 2 print one integer — the answer to it. Example Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
instruction
0
42,175
12
84,350
Tags: brute force, data structures, implementation Correct Solution: ``` import sys n = 500001 sqrt = int(0.75 * n**0.5) data = [0]*(n) ans = [[]] out = [] for i in range(1, sqrt): ans.append([0]*i) j = int(sys.stdin.readline()) qus = sys.stdin.readlines() for qu in qus: q = [int(i) for i in qu.split()] if q[0] == 1: x = q[1] y = q[2] data[x] += y for i in range(1, sqrt): ans[i][x%i] += y else: if q[1] < sqrt: out.append(str(ans[q[1]][q[2]])) else: sm = 0 for i in range(q[2], n, q[1]): sm += data[i] out.append(str(sm)) #out.append(str(sum([data[i] for i in range(q[2], n, q[1])]))) sys.stdout.write('\n'.join(out) + '\n') ```
output
1
42,175
12
84,351
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero. You have to process two types of queries to this array: * 1 x y — increase a_x by y; * 2 x y — compute ∑_{i ∈ R(x, y)} a_i, where R(x, y) is the set of all integers from 1 to 500000 which have remainder y modulo x. Can you process all the queries? Input The first line contains one integer q (1 ≤ q ≤ 500000) — the number of queries. Then q lines follow, each describing a query. The i-th line contains three integers t_i, x_i and y_i (1 ≤ t_i ≤ 2). If t_i = 1, then it is a query of the first type, 1 ≤ x_i ≤ 500000, and -1000 ≤ y_i ≤ 1000. If t_i = 2, then it it a query of the second type, 1 ≤ x_i ≤ 500000, and 0 ≤ y_i < x_i. It is guaranteed that there will be at least one query of type 2. Output For each query of type 2 print one integer — the answer to it. Example Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
instruction
0
42,177
12
84,354
Tags: brute force, data structures, implementation Correct Solution: ``` import sys n = 500001 sqrt = int(0.75 * n**0.5) data = [0]*(n) ans = [[]] out = [] for i in range(1, sqrt): ans.append([0]*i) j = int(sys.stdin.readline()) qus = sys.stdin.readlines() for qu in qus: q = [int(i) for i in qu.split()] if q[0] == 1: x = q[1] y = q[2] data[x] += y for i in range(1, sqrt): ans[i][x%i] += y else: if q[1] < sqrt: out.append(str(ans[q[1]][q[2]])) else: out.append(str(sum([data[i] for i in range(q[2], n, q[1])]))) sys.stdout.write('\n'.join(out) + '\n') ```
output
1
42,177
12
84,355
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero. You have to process two types of queries to this array: * 1 x y — increase a_x by y; * 2 x y — compute ∑_{i ∈ R(x, y)} a_i, where R(x, y) is the set of all integers from 1 to 500000 which have remainder y modulo x. Can you process all the queries? Input The first line contains one integer q (1 ≤ q ≤ 500000) — the number of queries. Then q lines follow, each describing a query. The i-th line contains three integers t_i, x_i and y_i (1 ≤ t_i ≤ 2). If t_i = 1, then it is a query of the first type, 1 ≤ x_i ≤ 500000, and -1000 ≤ y_i ≤ 1000. If t_i = 2, then it it a query of the second type, 1 ≤ x_i ≤ 500000, and 0 ≤ y_i < x_i. It is guaranteed that there will be at least one query of type 2. Output For each query of type 2 print one integer — the answer to it. Example Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
instruction
0
42,178
12
84,356
Tags: brute force, data structures, implementation Correct Solution: ``` import sys n = 500001 sqrt = int(0.75 * n**0.5) data = [0]*(n) ans = [[]] # out = [] for i in range(1, sqrt): ans.append([0]*i) j = int(sys.stdin.readline()) qus = sys.stdin.readlines() for qu in qus: q = [int(i) for i in qu.split()] if q[0] == 1: x = q[1] y = q[2] data[x] += y for i in range(1, sqrt): ans[i][x%i] += y else: if q[1] < sqrt: sys.stdout.write(str(ans[q[1]][q[2]]) + "\n") else: sm = 0 for i in range(q[2], n, q[1]): sm += data[i] sys.stdout.write(str(sm) + '\n') # out.append(str(sm)) #out.append(str(sum([data[i] for i in range(q[2], n, q[1])]))) # sys.stdout.write('\n'.join(out) + '\n') ```
output
1
42,178
12
84,357
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero. You have to process two types of queries to this array: * 1 x y — increase a_x by y; * 2 x y — compute ∑_{i ∈ R(x, y)} a_i, where R(x, y) is the set of all integers from 1 to 500000 which have remainder y modulo x. Can you process all the queries? Input The first line contains one integer q (1 ≤ q ≤ 500000) — the number of queries. Then q lines follow, each describing a query. The i-th line contains three integers t_i, x_i and y_i (1 ≤ t_i ≤ 2). If t_i = 1, then it is a query of the first type, 1 ≤ x_i ≤ 500000, and -1000 ≤ y_i ≤ 1000. If t_i = 2, then it it a query of the second type, 1 ≤ x_i ≤ 500000, and 0 ≤ y_i < x_i. It is guaranteed that there will be at least one query of type 2. Output For each query of type 2 print one integer — the answer to it. Example Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
instruction
0
42,179
12
84,358
Tags: brute force, data structures, implementation Correct Solution: ``` import sys n = 500001 sqrt = int(0.75 * n**0.5) data = [0]*(n) ans = [[]] # out = [] for i in range(1, sqrt): ans.append([0]*i) j = int(sys.stdin.readline()) qus = sys.stdin.readlines() for qu in qus: q = [int(i) for i in qu.split()] if q[0] == 1: x = q[1] y = q[2] data[x] += y for i in range(1, sqrt): ans[i][x%i] += y else: if q[1] < sqrt: sys.stdout.write(str(ans[q[1]][q[2]])) else: sm = 0 for i in range(q[2], n, q[1]): sm += data[i] sys.stdout.write(str(sm)) # out.append(str(sm)) #out.append(str(sum([data[i] for i in range(q[2], n, q[1])]))) sys.stdout.write("\n") # sys.stdout.write('\n'.join(out) + '\n') ```
output
1
42,179
12
84,359
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero. You have to process two types of queries to this array: * 1 x y — increase a_x by y; * 2 x y — compute ∑_{i ∈ R(x, y)} a_i, where R(x, y) is the set of all integers from 1 to 500000 which have remainder y modulo x. Can you process all the queries? Input The first line contains one integer q (1 ≤ q ≤ 500000) — the number of queries. Then q lines follow, each describing a query. The i-th line contains three integers t_i, x_i and y_i (1 ≤ t_i ≤ 2). If t_i = 1, then it is a query of the first type, 1 ≤ x_i ≤ 500000, and -1000 ≤ y_i ≤ 1000. If t_i = 2, then it it a query of the second type, 1 ≤ x_i ≤ 500000, and 0 ≤ y_i < x_i. It is guaranteed that there will be at least one query of type 2. Output For each query of type 2 print one integer — the answer to it. Example Input 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 Output 4 4 0
instruction
0
42,180
12
84,360
Tags: brute force, data structures, implementation Correct Solution: ``` import sys readline = sys.stdin.buffer.readline ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) def solve(): q = ni() n = 5 * 10**5 a = [0]*(n + 1) m = 710 rem = [[0]*m for _ in range(m)] for _ in range(q): t, x, y = nm() if t == 1: a[x] += y for p in range(1, m): rem[p][x%p] += y else: if x < m: print(rem[x][y]) else: print(sum(a[y::x])) if __name__ == '__main__': solve() # T = ni() # for _ in range(T): # solve() ```
output
1
42,180
12
84,361
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
instruction
0
42,189
12
84,378
Tags: brute force, combinatorics, implementation Correct Solution: ``` from itertools import permutations n, k = list(map(int, input().split(" "))) permutacoes = [] for i in range(n): x = list(input()) permuts = [''.join(x) for x in list(permutations(x))] permuts = list(map(int, permuts)) permutacoes.append(permuts) menor_dif = 10000000000 for i in range(len(permutacoes[0])): p = [] for j in range(len(permutacoes)): aux = permutacoes[j][i] p.append(aux) dif = max(p) - min(p) if dif < menor_dif: menor_dif = dif print(menor_dif) ```
output
1
42,189
12
84,379
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
instruction
0
42,190
12
84,380
Tags: brute force, combinatorics, implementation Correct Solution: ``` from itertools import permutations n, k = map(int, input().split()) min_diff = 10**9 tab = [] for _ in range(n): tab += [list(input())] for digit_map in permutations(list(range(k))): perm_nums = [] for num in tab: perm_nums += [int(''.join([num[i] for i in digit_map]))] perm_nums.sort() min_diff = min([min_diff, perm_nums[-1] - perm_nums[0]]) print(min_diff) ```
output
1
42,190
12
84,381
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
instruction
0
42,191
12
84,382
Tags: brute force, combinatorics, implementation Correct Solution: ``` #! /usr/bin/python from itertools import permutations n, k = map(int, input().split()) N = [] for i in range(n): N.append(permutations(input().strip())) ans = 1000000000 try: while True: X = [] for i in range(n): X.append(int("".join(next(N[i])))) diff = max(X) - min(X) if diff < ans: ans = diff except StopIteration: print(ans) ```
output
1
42,191
12
84,383
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
instruction
0
42,192
12
84,384
Tags: brute force, combinatorics, implementation Correct Solution: ``` import itertools as it n, k = [int(i) for i in input().split()] a = [list(map(int, input())) for i in range(n)] p = [10 ** j for j in range(k)][::-1] m = 99999999999999 mmx, mmn = 0, 9999999999 for i in it.permutations(p): mx, mn = 0, 9999999999 for j in a: s = sum([k * t for k, t in zip(i, j)]) mx = mx if mx > s else s mn = mn if mn < s else s if mx - mn > m: break d = mx - mn if d < m: m = d print(m) ```
output
1
42,192
12
84,385
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
instruction
0
42,193
12
84,386
Tags: brute force, combinatorics, implementation Correct Solution: ``` import math import itertools n, k = map(int, input().split()) numbers = [] for i in range(n): numbers.append(input()) permutations = [] for number in numbers: curr = [int(''.join(perm)) for perm in itertools.permutations(number)] permutations.append(curr) possibilities = [[] for i in range(math.factorial(k))] for i in range(n): for j in range(math.factorial(k)): possibilities[j].append(permutations[i][j]) minimum = 99999999 for poss in possibilities: minimum = min(max(poss) - min(poss), minimum) print(minimum) ```
output
1
42,193
12
84,387
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
instruction
0
42,194
12
84,388
Tags: brute force, combinatorics, implementation Correct Solution: ``` from itertools import permutations n,k=map(int,input().split()) ar=[input() for i in range(n)] ans=float('inf') for p in permutations(range(k)): mx=0 mn=float('inf') for e in ar: cur=0 for i in p: cur=10*cur+(int(e[i])) mx=max(mx,cur) mn=min(mn,cur) ans=min(ans,mx-mn) print(ans) ```
output
1
42,194
12
84,389
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
instruction
0
42,195
12
84,390
Tags: brute force, combinatorics, implementation Correct Solution: ``` from itertools import * n,k=map(int,input().split()) a=[] for i in range(n): a.append([i for i in input()]) l=[] for i in range(k): l.append(i) perm=permutations(l) ans=10**9 for now in perm: curr=list(now) mx=0 mn=10**9 for i in a: d="" for j in range(k): d+=i[curr[j]] mx=max(mx,int(d)) mn=min(mn,int(d)) ans=min(ans,mx-mn) print(ans) ```
output
1
42,195
12
84,391
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits. Output Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule. Examples Input 6 4 5237 2753 7523 5723 5327 2537 Output 2700 Input 3 3 010 909 012 Output 3 Input 7 5 50808 36603 37198 44911 29994 42543 50156 Output 20522 Note In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits). In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.
instruction
0
42,196
12
84,392
Tags: brute force, combinatorics, implementation Correct Solution: ``` n,d = [int(x) for x in input().split()] li = [] for i in range(n): s = input() li.append(s) st = "" for i in range(0,d): st += str(i) from itertools import permutations max_diff = 10**9 for i in permutations(st): ans = [] for j in i: ans.append(int(j)) aa = [] for j in li: k = "" for m in ans: k += j[m] aa.append(int(k)) mam,mim = max(aa),min(aa) max_diff = min(mam-mim,max_diff) print(max_diff) ```
output
1
42,196
12
84,393