message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). Submitted Solution: ``` #from math import * from bisect import * from collections import * from random import * from decimal import * from itertools import * from heapq import * import sys input=sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip('\n') def lis(): return list(map(int,input().split())) def ma(): return map(int,input().split()) t=1 p=998244353 fac=[1] for i in range(1,300010): fac.append((fac[-1]*i)%p) while(t): t-=1 n=inp() a=lis() a.sort() ha=fac[2*n] ha=ha*pow(fac[n],p-2,p)*pow(fac[n],p-2,p) ha=ha%p nn,pp=0,0 for i in range(2*n): if(i< n): nn+=ha*a[i] nn=nn%p else: pp+=ha*a[i] pp=pp%p print((pp-nn)%p) ```
instruction
0
85,207
12
170,414
Yes
output
1
85,207
12
170,415
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 a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). Submitted Solution: ``` # Enter your code here. Read input from STDIN. Print output to STDOUT# =============================================================================================== # importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase from itertools import * import bisect from heapq import * from math import ceil, floor from copy import * from collections import deque, defaultdict from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl from operator import * # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is returned # ============================================================================================== # fast I/O region BUFSIZE = 8192 from sys import stderr class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # inp = lambda: sys.stdin.readline().rstrip("\r\n") # =============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func #### END ITERATE RECURSION #### # =============================================================================================== # some shortcuts mod = 1000000007 def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def out(var): sys.stdout.write(str(var)) # for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) def nextline(): out("\n") # as stdout.write always print sring. def testcase(t): for p in range(t): solve() def pow(x, y, p): res = 1 # Initialize result x = x % p # Update x if it is more , than or equal to p if (x == 0): return 0 while (y > 0): if ((y & 1) == 1): # If y is odd, multiply, x with result res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res from functools import reduce def factors(n): return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))) def gcd(a, b): if a == b: return a while b > 0: a, b = b, a % b return a # discrete binary search # minimise: # def search(): # l = 0 # r = 10 ** 15 # # for i in range(200): # if isvalid(l): # return l # if l == r: # return l # m = (l + r) // 2 # if isvalid(m) and not isvalid(m - 1): # return m # if isvalid(m): # r = m + 1 # else: # l = m # return m # maximise: # def search(): # l = 0 # r = 10 ** 15 # # for i in range(200): # # print(l,r) # if isvalid(r): # return r # if l == r: # return l # m = (l + r) // 2 # if isvalid(m) and not isvalid(m + 1): # return m # if isvalid(m): # l = m # else: # r = m - 1 # return m ##############Find sum of product of subsets of size k in a array # ar=[0,1,2,3] # k=3 # n=len(ar)-1 # dp=[0]*(n+1) # dp[0]=1 # for pos in range(1,n+1): # dp[pos]=0 # l=max(1,k+pos-n-1) # for j in range(min(pos,k),l-1,-1): # dp[j]=dp[j]+ar[pos]*dp[j-1] # print(dp[k]) def prefix_sum(ar): # [1,2,3,4]->[1,3,6,10] return list(accumulate(ar)) def suffix_sum(ar): # [1,2,3,4]->[10,9,7,4] return list(accumulate(ar[::-1]))[::-1] def N(): return int(inp()) dx = [0, 0, 1, -1] dy = [1, -1, 0, 0] def YES(): print("YES") def NO(): print("NO") def Yes(): print("Yes") def No(): print("No") # ========================================================================================= from collections import defaultdict def numberOfSetBits(i): i = i - ((i >> 1) & 0x55555555) i = (i & 0x33333333) + ((i >> 2) & 0x33333333) return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24 # to find factorial and ncr N=300005 mod = 998244353 fac = [1, 1] finv = [1, 1] inv = [0, 1] for i in range(2, N + 1): fac.append((fac[-1] * i) % mod) inv.append(mod - (inv[mod % i] * (mod // i) % mod)) finv.append(finv[-1] * inv[-1] % mod) def comb(n, r): if n < r: return 0 else: return fac[n] * (finv[r] * finv[n - r] % mod) % mod def solve(): n=int(inp()) mod=998244353 ar=lis() ar.sort() s1=sum(ar) s2=sum(ar[:n]) t=s1-2*s2 t%=mod t*=comb(2*n,n) t%=mod print(t) solve() # testcase(int(inp())) ```
instruction
0
85,208
12
170,416
Yes
output
1
85,208
12
170,417
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 a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). Submitted Solution: ``` """ #If FastIO not needed, used this and don't forget to strip #import sys, math #input = sys.stdin.readline """ import os import sys from io import BytesIO, IOBase import heapq as h from bisect import bisect_left, bisect_right from types import GeneratorType BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") from collections import defaultdict as dd, deque as dq import math, string def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) MOD = 998244353 """ Sum of the differences of the elements when sorted in opposite ways A1 A2 A3 ... AN BN ... B3 B2 B1 f = |A1-BN| + ... Sum f over all partitions There are 2N choose N partitions so a lot The smallest element will always be A1 or B1 in whichever partition it is in Suppose we have [A1, A2, A3, A4] A1 A2 A4 A3 A1 A3 A4 A2 A1 A4 A3 A2 A2 A3 A4 A1 A2 A4 A3 A1 A3 A4 A2 A1 A4 is always added A3 is always added A2 is always subtracted A1 is always subtracted So the answer is (2N choose N)*sum(bigger half)-sum(smaller half) """ def solve(): N = getInt() A = getInts() A.sort() ans = 0 #print(A) for j in range(N): ans -= A[j] for j in range(N,2*N): ans += A[j] ans %= MOD curr = 1 for j in range(1,2*N+1): curr *= j curr %= MOD if j == N: n_fac = curr if j == 2*N: tn_fac = curr #print(ans,n_fac,tn_fac) div = pow(n_fac,MOD-2,MOD) return (ans*tn_fac*div*div) % MOD #for _ in range(getInt()): print(solve()) ```
instruction
0
85,209
12
170,418
Yes
output
1
85,209
12
170,419
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 a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). Submitted Solution: ``` import sys import math def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def MI(): return map(int, sys.stdin.readline().split()) def SI(): return sys.stdin.readline().strip() def FACT(n, mod): s = 1 facts = [1] for i in range(1,n+1): s*=i s%=mod facts.append(s) return facts[n] def C(n, k, mod): return (FACT(n,mod) * pow((FACT(k,mod)*FACT(n-k,mod))%mod,mod-2, mod))%mod n = II() a = LI() x = sorted(a[:n]) y = sorted(a[n:])[:][::-1] s = 0 mod = 998244353 for i in range(n): s+=abs(x[i]-y[i]) print(s*C(2*n,n, mod)%mod) ```
instruction
0
85,210
12
170,420
Yes
output
1
85,210
12
170,421
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 a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). Submitted Solution: ``` import itertools import math import sys import os from collections import defaultdict from heapq import heapify, heappush, heappop def is_debug(): return "PYPY3_HOME" not in os.environ def stdin_wrapper(): data = '''5 13 8 35 94 9284 34 54 69 123 846 ''' for line in data.split('\n'): yield line if not is_debug(): def stdin_wrapper(): while True: yield sys.stdin.readline() inputs = stdin_wrapper() def input_wrapper(): return next(inputs) def get_str(): if is_debug(): return input_wrapper() return input() def get(_type): if _type == str: return get_str() return _type(input_wrapper()) def get_arr(_type): return [_type(x) for x in input_wrapper().split()] def tuplerize(method): def wrap(*args, **kwargs): res = method(*args, **kwargs) if not isinstance(res, (tuple, list)): res = (res, ) return res return wrap ''' Solution ''' @tuplerize def solve(n, d): MOD = 998244353 def price(a,b): return sum([abs(x-y) for x, y in zip(sorted(a), reversed(sorted(b)))]) p = price(d[:len(d)//2], d[len(d)//2:]) f_bot = math.factorial(len(d)//2) % MOD f_top = f_bot for i in range(len(d)//2): f_top *= (len(d)//2 + i+1) % MOD f_top %= MOD return p * (f_top // (f_bot*f_bot)) % MOD n = get(int) a = get_arr(int) print(*solve(n, a)) ```
instruction
0
85,211
12
170,422
No
output
1
85,211
12
170,423
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 a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). Submitted Solution: ``` from math import comb n=int(input()) arr=list(map(int,input().split())) arr=sorted(arr) ans=0 hi=2 lo=1 for i in range(2,n+1): lo=(lo*i*i)%998244353 hi=hi*2*i*(2*i-1)%998244353 if lo==0: print(0) else: if hi%lo==0: t=hi//lo else: while hi%lo==0: hi=hi+998244353 hi=hi%lo t=hi//lo end=sum(arr[:n])%998244353 st=sum(arr[n:])%998244353 ans=t*(st-end)%998244353 print(ans) ```
instruction
0
85,212
12
170,424
No
output
1
85,212
12
170,425
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 a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). Submitted Solution: ``` def pwm(a, n, mod): ans = 1 while n: if n & 1: ans = ans*a % mod a = a*a % mod n >>= 1 return ans n = int(input()) l = [int(x) for x in input().split()] l.sort() a, b = l[:n], l[n:] ans = 0 mod = 998244353 dnom = 1 nom = 1 for i in range(n): ans += (b[i] % mod - a[i] % mod) % mod nom = nom * (i+1) % mod * (2*n - i) % mod dnom = dnom*(i+1) % mod * (i+1) % mod gg = (nom % mod)*pwm(dnom, mod-2, mod) % mod fans = ans*gg # print(ans) # print(gg) print(fans) ```
instruction
0
85,213
12
170,426
No
output
1
85,213
12
170,427
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 a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). Submitted Solution: ``` import sys import math import collections input=sys.stdin.readline mod=998244353 def fact(n): prod=1 for i in range(2,n+1): prod=(prod*i)%mod return prod def ncr(n): return((fact(2*n)//(fact(n)*fact(n)))%mod) n=int(input()) l=sorted([int(i) for i in input().split()]) s1=0 s2=0 for i in range(n): s1+=l[i] s2+=l[n+i] print(((s2-s1)*ncr(n))%mod) ```
instruction
0
85,214
12
170,428
No
output
1
85,214
12
170,429
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix: 1. Swap i-th and j-th rows of the matrix; 2. Swap i-th and j-th columns of the matrix. You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j. Input The first line contains an integer n (2 ≀ n ≀ 1000) β€” the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk (1 ≀ xk, yk ≀ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one. It is guaranteed that all positions are distinct. Output Print the description of your actions. These actions should transform the matrix to the described special form. In the first line you should print a non-negative integer m (m ≀ 105) β€” the number of actions. In each of the next m lines print three space-separated integers t, i, j (1 ≀ t ≀ 2, 1 ≀ i, j ≀ n, i β‰  j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, and i and j denote the numbers of rows or columns respectively. Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them. Examples Input 2 1 2 Output 2 2 1 2 1 1 2 Input 3 3 1 1 3 Output 3 2 2 3 1 1 3 1 1 2 Input 3 2 1 3 2 Output 0
instruction
0
85,273
12
170,546
Tags: constructive algorithms, greedy, math Correct Solution: ``` def find_all(s, c): index = s.index(c) while True: yield index try: index = s.index(c, index + 1) except ValueError: break n = int(input()) row, col, actions = [], [], [] available = list(range(1, n + 1)) for _ in range(n - 1): (r, c) = map(int, input().split(' ')) row.append(r) col.append(c) for cur in range(n, 1, -1): if not row: break # for column if cur in col: indices = list(find_all(col, cur)) zero = list(set(available) - set(col))[0] actions.append([2, cur, zero]) for i in indices: col[i] = zero # row try: idx = row.index(cur) indices = list(find_all(row, cur)) indices.reverse() for i in indices: del row[i], col[i] del available[-1] continue except ValueError: val = row[0] indices = list(find_all(row, val)) actions.append([1, row[0], cur]) indices.reverse() for i in indices: del row[i], col[i] del available[-1] if not actions: print(0) else: print(len(actions)) for move in actions: print(*move) ```
output
1
85,273
12
170,547
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix: 1. Swap i-th and j-th rows of the matrix; 2. Swap i-th and j-th columns of the matrix. You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j. Input The first line contains an integer n (2 ≀ n ≀ 1000) β€” the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk (1 ≀ xk, yk ≀ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one. It is guaranteed that all positions are distinct. Output Print the description of your actions. These actions should transform the matrix to the described special form. In the first line you should print a non-negative integer m (m ≀ 105) β€” the number of actions. In each of the next m lines print three space-separated integers t, i, j (1 ≀ t ≀ 2, 1 ≀ i, j ≀ n, i β‰  j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, and i and j denote the numbers of rows or columns respectively. Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them. Examples Input 2 1 2 Output 2 2 1 2 1 1 2 Input 3 3 1 1 3 Output 3 2 2 3 1 1 3 1 1 2 Input 3 2 1 3 2 Output 0
instruction
0
85,274
12
170,548
Tags: constructive algorithms, greedy, math Correct Solution: ``` import os from io import BytesIO, IOBase import sys def main(): n = int(input()) row = [0] * n col = [0] * n a = [[0 for j in range(n)] for i in range(n)] for i in range(n - 1): x, y = map(int, input().split()) row[x - 1] += 1 col[y - 1] += 1 a[x - 1][y - 1] = 1 ans = [] for i in range(n): zci = -1 # zero column index for j in range(n - i): if col[j] == 0: zci = j break if zci == -1: break if zci != (n - i - 1): ans.append([2, zci + 1, n - i]) col[n - i - 1], col[zci] = col[zci], col[n - i - 1] for j in range(n - i): a[j][zci], a[j][n - i - 1] = a[j][n - i - 1], a[j][zci] nzri = -1 # non zero row index for j in range(n - i): if row[j] != 0: nzri = j break if nzri == -1: break if nzri != (n - i - 1): ans.append([1, nzri + 1, n - i]) row[nzri], row[n - i - 1] = row[n - i - 1], row[nzri] for j in range(n - i): if a[nzri][j] == 1: col[j] -= 1 a[nzri][j], a[n - i - 1][j] = a[n - i - 1][j], a[nzri][j] print(len(ans)) for i in ans: print(*i) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
output
1
85,274
12
170,549
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix: 1. Swap i-th and j-th rows of the matrix; 2. Swap i-th and j-th columns of the matrix. You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j. Input The first line contains an integer n (2 ≀ n ≀ 1000) β€” the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk (1 ≀ xk, yk ≀ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one. It is guaranteed that all positions are distinct. Output Print the description of your actions. These actions should transform the matrix to the described special form. In the first line you should print a non-negative integer m (m ≀ 105) β€” the number of actions. In each of the next m lines print three space-separated integers t, i, j (1 ≀ t ≀ 2, 1 ≀ i, j ≀ n, i β‰  j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, and i and j denote the numbers of rows or columns respectively. Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them. Examples Input 2 1 2 Output 2 2 1 2 1 1 2 Input 3 3 1 1 3 Output 3 2 2 3 1 1 3 1 1 2 Input 3 2 1 3 2 Output 0
instruction
0
85,275
12
170,550
Tags: constructive algorithms, greedy, math Correct Solution: ``` # https://codeforces.com/problemset/problem/266/C n = int(input()) P = [list(map(int, input().split())) for _ in range(n-1)] col = set() row = set() sCol = {} sRow = {} for x, y in P: row.add(x) col.add(y) n_col = len(col) n_row = len(row) cur_col, cur_row = 1, n used_col = {} used_row = {} for c in sorted(col): if c > n_col: while cur_col in used_col: cur_col += 1 used_col[cur_col] = 1 sCol[c] = cur_col else: used_col[c] = 1 for r in sorted(row, reverse=True): if r < n - n_row + 1: while cur_row in used_row: cur_row -= 1 used_row[cur_row] = 1 sRow[r] = cur_row else: used_row[r] = 1 for i, [x, y] in enumerate(P): if x in sRow: P[i][0] = sRow[x] if y in sCol: P[i][1] = sCol[y] minCol = [float('inf') for _ in range(n_col+1)] minCol[0] = 0 for x, y in P: if x < minCol[y]: minCol[y] = x def sort(minCol): swap = [] pos = {} for col, val in enumerate(minCol): if col == 0: continue if val not in pos: pos[val] = set() pos[val].add(col) sortCol = sorted(minCol) for i, val in enumerate(sortCol): if i == 0: continue if val == minCol[i]: pos[val].remove(i) else: col_ = next(iter(pos[val])) pos[val].remove(col_) pos[minCol[i]].remove(i) pos[minCol[i]].add(col_) minCol[col_] = minCol[i] minCol[i] = val swap.append([2, i, col_]) #print(minCol) return swap swap = sort(minCol) ans = [[1, x, y] for x, y in sRow.items()] + [[2, x, y] for x, y in sCol.items()] + swap print(len(ans)) print('\n'.join([' '.join([str(x) for x in line]) for line in ans])) #2 #1 2 #3 #3 1 #1 3 #3 #2 1 #3 2 ```
output
1
85,275
12
170,551
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix: 1. Swap i-th and j-th rows of the matrix; 2. Swap i-th and j-th columns of the matrix. You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j. Input The first line contains an integer n (2 ≀ n ≀ 1000) β€” the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk (1 ≀ xk, yk ≀ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one. It is guaranteed that all positions are distinct. Output Print the description of your actions. These actions should transform the matrix to the described special form. In the first line you should print a non-negative integer m (m ≀ 105) β€” the number of actions. In each of the next m lines print three space-separated integers t, i, j (1 ≀ t ≀ 2, 1 ≀ i, j ≀ n, i β‰  j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, and i and j denote the numbers of rows or columns respectively. Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them. Examples Input 2 1 2 Output 2 2 1 2 1 1 2 Input 3 3 1 1 3 Output 3 2 2 3 1 1 3 1 1 2 Input 3 2 1 3 2 Output 0
instruction
0
85,276
12
170,552
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) a = [] b = [0] * (n+1) c = [10000] * (n+1) for i in range(0,n-1): p1,p2 = list(map(int,input().split())) a.append((p1,p2)) b[p1] = max(b[p1],p2) ans = [] for i in range(1,n+1): if b[i]==0: continue k = 0 for j in range(i,n+1): if b[j]==0: k=j break if k==0:break b[j]=b[i] b[i]=0 for j in range(0,n-1): if a[j][0]==i: a[j]=(k,a[j][1]) ans.append((1,i,k)) for i in a: c[i[1]]=min(c[i[1]],i[0]) for i in range(1,n+1): k=i for j in range(i+1,n+1): if c[j]<c[i]: i=j if (i==k): continue ans.append((2,i,k)) c[0]=c[i];c[i]=c[k];c[k]=c[0] print(len(ans)) for i in ans:print(i[0],i[1],i[2]) ```
output
1
85,276
12
170,553
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix: 1. Swap i-th and j-th rows of the matrix; 2. Swap i-th and j-th columns of the matrix. You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j. Input The first line contains an integer n (2 ≀ n ≀ 1000) β€” the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk (1 ≀ xk, yk ≀ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one. It is guaranteed that all positions are distinct. Output Print the description of your actions. These actions should transform the matrix to the described special form. In the first line you should print a non-negative integer m (m ≀ 105) β€” the number of actions. In each of the next m lines print three space-separated integers t, i, j (1 ≀ t ≀ 2, 1 ≀ i, j ≀ n, i β‰  j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, and i and j denote the numbers of rows or columns respectively. Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them. Examples Input 2 1 2 Output 2 2 1 2 1 1 2 Input 3 3 1 1 3 Output 3 2 2 3 1 1 3 1 1 2 Input 3 2 1 3 2 Output 0
instruction
0
85,277
12
170,554
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) a, b, c = [], [0] * (n+1), [int(2e9)] * (n+1) for i in range(n-1): p1, p2 = map(int, input().split()) a.append((p1, p2)) b[p1] = max(b[p1], p2) res = [] for i in range(1, n+1): if b[i] == 0: continue k = 0 for j in range(i, n+1): if b[j] == 0: k = j break else: break b[j], b[i] = b[i], 0 for j in range(n-1): if a[j][0] == i: a[j] = (k, a[j][1]) res.append((1, i, k)) for i in a: c[i[1]] = min(c[i[1]], i[0]) for i in range(1, n+1): k = i for j in range(i+1, n+1): if c[j] < c[i]: i = j if i == k: continue res.append((2, i, k)) c[0], c[i], c[k] = c[i], c[k], c[0] print(len(res)) for i in res: print(i[0], i[1], i[2]) ```
output
1
85,277
12
170,555
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
instruction
0
85,312
12
170,624
Tags: implementation Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) i = 1 aSorted = sorted(a) while i < n and a[i] >= a[i-1]: i += 1 a = a[i:] + a[:i] for j in range(n): if aSorted[j] != a[j]: print(-1) break else: print((n-i) % n) ```
output
1
85,312
12
170,625
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
instruction
0
85,313
12
170,626
Tags: implementation Correct Solution: ``` n = int(input()) s = list(map(int, input().split())) s1 = sorted(s) if s == s1: print(0) else: for i in range(n - 1): if s[i + 1] < s[i]: s = s[i + 1:] + s[:i + 1] if s == s1: print(n - (i + 1)) else: print(-1) break ```
output
1
85,313
12
170,627
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
instruction
0
85,314
12
170,628
Tags: implementation Correct Solution: ``` import sys n=int(input()) l1=list(map(int,input().split())) l3=l1.copy() l3.sort() for i in range(1,n): if l1[i-1]>l1[i]: l2=l1[i:]+l1[:i] if l3==l2: print(n-i) sys.exit() else : print(-1) sys.exit() print(0) ```
output
1
85,314
12
170,629
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
instruction
0
85,315
12
170,630
Tags: implementation Correct Solution: ``` n = int(input()) import sys def get_ints(): return list(map(int, sys.stdin.readline().strip().split())) from collections import deque arr = get_ints() dicts = {} flag = 0 maxnum = max(arr) items = deque(arr) flag = 0 def checkdecreasing(newarr): #print(newarr) prev = -1 for i in newarr: if i < prev: return False prev = i return True ansflag = 0 count = 0 for i in range(n ): if items[-1] == maxnum: flag = 1 curans = checkdecreasing(items) if curans: ansflag = 1 break items.rotate(1) count += 1 if ansflag: print(count) else: print(-1) ```
output
1
85,315
12
170,631
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
instruction
0
85,316
12
170,632
Tags: implementation Correct Solution: ``` """ Author: Sagar Pandey """ # ---------------------------------------------------Import Libraries--------------------------------------------------- import sys import time import os from math import sqrt, log, log2, ceil, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial from copy import copy, deepcopy from sys import exit, stdin, stdout from collections import Counter, defaultdict, deque from itertools import permutations import heapq from bisect import bisect_left as bl # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is r # ---------------------------------------------------Global Variables--------------------------------------------------- # sys.setrecursionlimit(100000000) mod = 1000000007 # ---------------------------------------------------Helper Functions--------------------------------------------------- iinp = lambda: int(sys.stdin.readline()) inp = lambda: sys.stdin.readline().strip() strl = lambda: list(inp().strip().split(" ")) intl = lambda: list(map(int, inp().split(" "))) mint = lambda: map(int, inp().split()) flol = lambda: list(map(float, inp().split(" "))) flush = lambda: stdout.flush() def permute(nums): def fun(arr, nums, cur, v): if len(cur) == len(nums): arr.append(cur.copy()) i = 0 while i < len(nums): if v[i]: i += 1 continue else: cur.append(nums[i]) v[i] = 1 fun(arr, nums, cur, v) cur.pop() v[i] = 0 i += 1 # while i<len(nums) and nums[i]==nums[i-1]:i+=1 # Uncomment for unique permutations return arr res = [] nums.sort() v = [0] * len(nums) return fun(res, nums, [], v) def subsets(res, index, arr, cur): res.append(cur.copy()) for i in range(index, len(arr)): cur.append(arr[i]) subsets(res, i + 1, arr, cur) cur.pop() return res def sieve(N): root = int(sqrt(N)) primes = [1] * (N + 1) primes[0], primes[1] = 0, 0 for i in range(2, root + 1): if primes[i]: for j in range(i * i, N + 1, i): primes[j] = 0 return primes def bs(arr, l, r, x): if x < arr[0] or x > arr[len(arr) - 1]: return -1 while l <= r: mid = l + (r - l) // 2 if arr[mid] == x: return mid elif arr[mid] < x: l = mid + 1 else: r = mid - 1 return -1 def isPrime(n): if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False p = int(sqrt(n)) for i in range(5, p + 1, 6): if n % i == 0 or n % (i + 2) == 0: return False return True # -------------------------------------------------------Functions------------------------------------------------------ def fun(arr, mid, l): n = len(arr) if mid < arr[0] or mid < l - arr[-1]: return False for i in range(n - 1): if not arr[i] + mid >= arr[i + 1] - mid: return False return True def solve(): n=iinp() arr=intl() i=0 while i<n-1 and arr[i+1]>=arr[i]: i+=1 if i==n-1: print(0) return ans=arr[i+1:] ans.extend(arr[:i+1]) # print(ans,i) if sorted(arr)==ans: print(n-i-1) else: print(-1) # -------------------------------------------------------Main Code------------------------------------------------------ start_time = time.time() # for _ in range(iinp()): solve() # print("--- %s seconds ---" % (time.time() - start_time)) ```
output
1
85,316
12
170,633
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
instruction
0
85,317
12
170,634
Tags: implementation Correct Solution: ``` n = int(input()) lst = list(map(int, input().split())) ans = 0 c = 0 for i in range(1, n): if lst[i] < lst[i-1]: ans = n-i c += 1 if (c == 1 and lst[-1] <= lst[0]) or c == 0: print(ans) else: print(-1) ```
output
1
85,317
12
170,635
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
instruction
0
85,318
12
170,636
Tags: implementation Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) b = -1 for i in range(1,n): if a[i-1]>a[i]: b = i break if b==-1: print(0) else: c = a[b:]+a[:b] flag = 0 for i in range(1,n): if c[i]<c[i-1]: flag = 1 break if flag == 1: print(-1) else: print(n-b) ```
output
1
85,318
12
170,637
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
instruction
0
85,319
12
170,638
Tags: implementation Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): n = I() a = LI() aa = a + a t = [] for i in range(n*2-1): if aa[i] > aa[i+1]: t.append(i) if len(t) < 2: return 0 if len(t) > 2: return -1 return n - t[0] - 1 print(main()) ```
output
1
85,319
12
170,639
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` n = input() n = int(n) d = [int(x) for x in input().split(' ')] e = 0 f = 0 for i in range(1, n): if d[i-1] <= d[i]: if f == 0 or ( f == 1 and d[i] <= d[0]): pass else: e = -1 break else: if (f == 0) and (d[0] >= d[i]): f = 1 e = i else: e = -1 break #print(e) if e == -1: print(-1) else: print((n-e)%n) ```
instruction
0
85,320
12
170,640
Yes
output
1
85,320
12
170,641
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` n = int(input()) ls = list(map(int, input().split())) if ls == sorted(ls): print(0) elif ls == sorted(ls, reverse=True): if n == 2: print(1) else: print(-1) else: counter = 0 b = False for i in range(1, n): if i == n-1 and ls[-1] > ls[0]: counter = -1 break if ls[i] < ls[i-1] and not b: counter += 1 b = True elif not b and ls[i] < ls[i-1]: counter = -1 break elif b: if ls[i] >= ls[i-1]: counter += 1 else: counter = -1 break print(counter) ```
instruction
0
85,321
12
170,642
Yes
output
1
85,321
12
170,643
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) d=0 for i in range(1,n): if l[i]<l[i-1]: d=i break san=l[i:]+l[:i] if d!=0: l.sort() if l==san: print (n-d) else: print (-1) else: print(0) ```
instruction
0
85,322
12
170,644
Yes
output
1
85,322
12
170,645
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` #from collections import deque as dq n = int(input()) l = list(map(int,input().split())) d=0 r=0 for i in range(n-1): if l[i+1]<l[i]: d=i+1 break l = l[d:]+l[:d] #print(l,d) r = n-d if l==sorted(l): print(r%n) else: print(-1) ```
instruction
0
85,323
12
170,646
Yes
output
1
85,323
12
170,647
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` # # "main" # # " . . . " # # # Created by Sergey Yaksanov at 29.09.2019 # Copyright Β© 2019 Yakser. All rights reserved. n = int(input()) a = list(map(int,input().split())) sorted_a = sorted(a) k = 0 a0 = a[0] while a != sorted_a and k < n: a.insert(0,a[n-1]) del(a[n-1]) k += 1 if k >= n: k = -1 print(k) ```
instruction
0
85,324
12
170,648
No
output
1
85,324
12
170,649
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` #author : dokueki import sys INT_MAX = sys.maxsize INT_MIN = -(sys.maxsize)-1 sys.setrecursionlimit(10**5) mod = 1000000007 def IOE(): sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") def main(): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) x = sorted(a) if x == a: print(0) else: for i in range(int(10e5)+2): a = [a[-1]] + a[:n - 1] if a == x: break print(i+1 if i != int(10e5)+2 else -1) if __name__ == "__main__": try: IOE() except: pass main() ```
instruction
0
85,325
12
170,650
No
output
1
85,325
12
170,651
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) #print(a) n=len(a) min_index=a.index(min(a)) k=min_index #print(f"This is min index: {min_index}") ans=0 count=0 #print(a[(k+1)%(n)]) #print(a[k%n]) while(count<n-1): if(a[k%n] > a[(k+1)%(n)]): ans=-1 break k=k+1 count=count+1 #print(f"This is the value of ans: {ans}") if(ans!=0): print(-1) else: if(a[0]>a[n-1]): print(n-min_index) else: print(0) ```
instruction
0
85,326
12
170,652
No
output
1
85,326
12
170,653
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` ## necessary imports import sys input = sys.stdin.readline # import random # from math import log2, log, ceil # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp ## gcd function def gcd(a,b): if a == 0: return b return gcd(b%a, a) ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b # find function def find(x, link): while(x != link[x]): x = link[x] return x # the union function which makes union(x,y) # of two nodes x and y def union(x, y, size, link): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e6 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve() def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())) ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################# ---------------- TEMPLATE ENDS HERE ---------------- ################# n = int(input()); a = int_array(); if a == sorted(a, reverse= True): print(n- a.count(max(a))); exit(); new = []; initial = a[0]; while(len(a) > 0 and initial >= a[-1]): new.append(a.pop()); this = new + a; if this == sorted(this): print(len(new)) else: print(-1); ```
instruction
0
85,327
12
170,654
No
output
1
85,327
12
170,655
Provide tags and a correct Python 3 solution for this coding contest problem. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1
instruction
0
85,369
12
170,738
Tags: binary search, two pointers Correct Solution: ``` from bisect import bisect_left n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) sum_a, sum_b = sum(a), sum(b) delta = sum_b - sum_a ans = abs(delta) ans_swap = [] for i in range(n): for j in range(m): if abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j])) < ans: ans = abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j])) ans_swap = [(i+1, j+1)] d = dict() for i in range(m): for j in range(i+1, m): d[b[i]+b[j]] = (i+1, j+1) minf, inf = -10**13, 10**13 val = [minf, minf] + sorted(d.keys()) + [inf, inf] for i in range(n): for j in range(i+1, n): ap = a[i] + a[j] req = (delta + ap*2) >> 1 k = bisect_left(val, req) for k in range(k-1, k+2): if abs(delta + ap*2 - val[k]*2) < ans: ans = abs(delta + ap*2 - val[k]*2) ans_swap = [(i+1, d[val[k]][0]), (j+1, d[val[k]][1])] print(ans) print(len(ans_swap)) for x, y in ans_swap: print(x, y) ```
output
1
85,369
12
170,739
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Submitted Solution: ``` #import bisect def mindf(a,b): k=0 s1=sum(a) s2=sum(b) min=abs(s1-s2) for i in range(len(a)): for j in range(len(b)): s22=s2-b[j]+a[i] s11=s1-a[i]+b[j] df=abs(s22-s11) if df<=min: min=df t1=i t2=j k=1 if k==0: t1=-1 t2=-1 else: t=a[t1] a[t1]=b[t2] b[t2]=t return min,t1,t2,a,b n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] w=sum(a)-sum(b) if (sum(a)-sum(b))==0: j=0 min=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min1,t1,t2,a,b=mindf(a,b) if min1==1: min=min1 j=1 t3=-1 t4=-1 elif min1==(w): min=min1 j=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min,t3,t4,a,b=mindf(a,b) '''if min==min1: min=min1 j=1 t3=-1 t4=-1''' #else: j=2 print(min) print(j) if (t1+1)!=0 and (t2+1)!=0: print(t1+1,t2+1) if (t3+1)!=0 and (t4+1)!=0: print(t3+1,t4+1) ```
instruction
0
85,370
12
170,740
No
output
1
85,370
12
170,741
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Submitted Solution: ``` import bisect def mindf(a,b): k=0 s1=sum(a) s2=sum(b) min=abs(s1-s2) for i in range(len(a)): for j in range(len(b)): s22=s2-b[j]+a[i] s11=s1-a[i]+b[j] df=abs(s22-s11) if df<=min: min=df t1=i t2=j k=1 if k==0: t1=-1 t2=-1 else: t=a[t1] a[t1]=b[t2] b[t2]=t return min,t1,t2,a,b def mins(a): c=[] if len(a)==1: c=a else: for i in range(len(a)-1): for j in range(i+1,len(a)): c.append(a[i]+a[j]) return c n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] w=sum(a)-sum(b) c=mins(a) d=mins(b) if (sum(a)-sum(b))==0: j=0 min=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min1,t1,t2,a,b=mindf(a,b) if min1==1: min=min1 j=1 t3=-1 t4=-1 elif min1==(w): min=min1 j=0 t1=-1 t2=-1 t3=-1 t4=-1 else: if len(c)<=1 or len(d)<=1: min=min1 j=1 t3=-1 t4=-1 else: #min,t3,t4,a,b=mindf(a,b) min=min1 j=2 c.sort() for i in range(len(c)): c[i]=c[i]*2 for i in range(len(d)): x=w+2*(d[i]) if bisect.bisect(c,x)==0: v=c[0] else: v=c[bisect.bisect(c,x)-1] z=abs(x-v) if z<min: min=z if min==min1: j=1 print(min) print(j) if (t1+1)!=0 and (t2+1)!=0: print(t1+1,t2+1) #if (t3+1)!=0 and (t4+1)!=0: #print(t3+1,t4+1) ```
instruction
0
85,371
12
170,742
No
output
1
85,371
12
170,743
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Submitted Solution: ``` #import bisect def mindf(a,b): k=0 s1=sum(a) s2=sum(b) min=abs(s1-s2) for i in range(len(a)): for j in range(len(b)): s22=s2-b[j]+a[i] s11=s1-a[i]+b[j] df=abs(s22-s11) if df<min: min=df t1=i t2=j k=1 if k==0: t1=-1 t2=-1 else: t=a[t1] a[t1]=b[t2] b[t2]=t return min,t1,t2,a,b n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] if (sum(a)-sum(b))==0: j=0 min=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min1,t1,t2,a,b=mindf(a,b) if min1==1: min=min1 j=1 t3=-1 t4=-1 elif min1==(sum(a)-sum(b)): min=min1 j=0 t3=-1 t4=-1 else: min,t3,t4,a,b=mindf(a,b) if min==min1: min=min1 j=1 t3=-1 t4=-1 else: j=2 print(min) print(j) if (t1+1)!=0 and (t2+1)!=0: print(t1+1,t2+1) if (t3+1)!=0 and (t4+1)!=0: print(t3+1,t4+1) ```
instruction
0
85,372
12
170,744
No
output
1
85,372
12
170,745
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Submitted Solution: ``` import bisect def mindf(a,b): k=0 s1=sum(a) s2=sum(b) min=abs(s1-s2) for i in range(len(a)): for j in range(len(b)): s22=s2-b[j]+a[i] s11=s1-a[i]+b[j] df=abs(s22-s11) if df<=min: min=df t1=i t2=j k=1 if k==0: t1=-1 t2=-1 else: t=a[t1] a[t1]=b[t2] b[t2]=t return min,t1,t2,a,b def mins(a): c=[] if len(a)==1: c=a else: for i in range(len(a)-1): for j in range(i+1,len(a)): c.append(a[i]+a[j]) return c n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] w=sum(a)-sum(b) c=mins(a) d=mins(b) if (sum(a)-sum(b))==0: j=0 min=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min1,t1,t2,a,b=mindf(a,b) if min1==1: min=min1 j=1 t3=-1 t4=-1 elif min1==(w): min=min1 j=0 t1=-1 t2=-1 t3=-1 t4=-1 else: if len(c)<=1 or len(d)<=1: min=min1 j=1 t3=-1 t4=-1 else: if n<1000: min,t3,t4,a,b=mindf(a,b) t3=-1 t4=-1 min=min1 j=2 c.sort() for i in range(len(c)): c[i]=c[i]*2 for i in range(len(d)): x=w+2*(d[i]) if bisect.bisect(c,x)==0: v=c[0] else: v=c[bisect.bisect(c,x)-1] z=abs(x-v) if z<min: min=z if min==min1: j=1 print(min) print(j) if (t1+1)!=0 and (t2+1)!=0: print(t1+1,t2+1) if (t3+1)!=0 and (t4+1)!=0: print(t3+1,t4+1) ```
instruction
0
85,373
12
170,746
No
output
1
85,373
12
170,747
Provide tags and a correct Python 3 solution for this coding contest problem. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1
instruction
0
85,445
12
170,890
Tags: constructive algorithms, divide and conquer Correct Solution: ``` import sys n, k = map(int, input().split()) k = [k-1] ans = [-1]*n def set_n(l, r, rev, cur): if not rev: for i in range(l, r): ans[i] = cur cur += 1 else: for i in range(r-1, l-1, -1): ans[i] = cur cur += 1 return cur def rec(l, r, cur): # print(l, r, k, ans) if r - l == 1: set_n(l, r, 0, cur) return if k[0] < 2: set_n(l, r, 0, cur) return k[0] -= 2 mid = (l + r) >> 1 rec(mid, r, cur) rec(l, mid, cur+(r - mid)) rec(0, n, 1) if k[0] != 0: print(-1) else: print(*ans) ```
output
1
85,445
12
170,891
Provide tags and a correct Python 3 solution for this coding contest problem. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1
instruction
0
85,446
12
170,892
Tags: constructive algorithms, divide and conquer Correct Solution: ``` def gbpx(i,j): global k if(k==1): return else: if(j-i==1): return else: k-=2 mid=(i+j)//2 a=s[mid-1] b=s[mid] s[mid-1]=b s[mid]=a gbpx(i,mid) gbpx(mid,j) n,k=map(int,input().split()) s=[i for i in range(1,n+1)] if(k%2==0 or k>n*2):print(-1) else: gbpx(0,n) for i in range(0,n): print('%d ' %(s[i]),end='') ```
output
1
85,446
12
170,893
Provide tags and a correct Python 3 solution for this coding contest problem. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1
instruction
0
85,447
12
170,894
Tags: constructive algorithms, divide and conquer Correct Solution: ``` import random, math from copy import deepcopy as dc calls = 1 # Function to call the actual solution def solution(n, k): if k % 2 == 0 or k > (2 * n) - 1: return -1 li = [i for i in range(1, n+1)] def mergeCount(li, s, e, k): global calls if calls >= k or s >= e-1: return mid = (s + e)//2 calls += 2 if mid-1 >= s: li[mid], li[mid-1] = li[mid-1], li[mid] mergeCount(li, s, mid, k) mergeCount(li, mid, e, k) mergeCount(li, 0, n, k) return li # Function to take input def input_test(): n, k = map(int, input().strip().split(" ")) out = solution(n, k) if out != -1: print(' '.join(list(map(str, out)))) else: print(out) input_test() # test()s ```
output
1
85,447
12
170,895
Provide tags and a correct Python 3 solution for this coding contest problem. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1
instruction
0
85,448
12
170,896
Tags: constructive algorithms, divide and conquer Correct Solution: ``` def create_list(n, num_of_calls): if n == 1: return [1], 0 if n == 2: return [2, 1], 2 if num_of_calls == 2: return list(range(2, n // 2 + 2)) + [1] +\ list(range(n // 2 + 2, n + 1)), 2 list1, num_of_calls1 = create_list(n // 2, num_of_calls - 2) if num_of_calls1 == num_of_calls - 2: return list1 + list(range(n // 2 + 1, n + 1)), num_of_calls list2, num_of_calls2 = create_list((n + 1) // 2, num_of_calls - num_of_calls1 - 2) return list1 + [x + n // 2 for x in list2], \ num_of_calls1 + num_of_calls2 + 2 def main(): n, k = [int(x) for x in input().split()] if k % 2 != 1: print(-1) return if k == 1: print(' '.join([str(x) for x in range(1, n + 1)])) return num_list, num_of_calls = create_list(n, k - 1) if num_of_calls != k - 1: print(-1) return print(' '.join([str(x) for x in num_list])) if __name__ == '__main__': main() ```
output
1
85,448
12
170,897
Provide tags and a correct Python 3 solution for this coding contest problem. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1
instruction
0
85,449
12
170,898
Tags: constructive algorithms, divide and conquer Correct Solution: ``` from sys import stdin, setrecursionlimit, stdout #setrecursionlimit(1000000) #use "python" instead of "pypy" to avoid MLE from collections import deque from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin from heapq import heapify, heappop, heappush, heapreplace, heappushpop from bisect import bisect_right, bisect_left def ii(): return int(stdin.readline()) def fi(): return float(stdin.readline()) def mi(): return map(int, stdin.readline().split()) def fmi(): return map(float, stdin.readline().split()) def li(): return list(mi()) def si(): return stdin.readline().rstrip() def lsi(): return list(si()) mod=1000000007 res=['NO', 'YES'] ####################################################################################### ########################### M Y F U N C T I O N S ########################### ####################################################################################### def merge(l, r): global k if k: m=(l+r)//2 if m and l+1!=r: a[m-1], a[m]=a[m], a[m-1] #print(a, m) k-=2 merge(l, m) merge(m, r) return return ####################################################################################### ########################### M A I N P R O G R A M ########################### ####################################################################################### test=1 test_case=1 while test<=test_case: test+=1 n, k=mi() if not k%2 or k>=n+n: print(-1) exit() k-=1 a=[i+1 for i in range(n)] merge(0, n) print(*a) ```
output
1
85,449
12
170,899
Provide tags and a correct Python 3 solution for this coding contest problem. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1
instruction
0
85,450
12
170,900
Tags: constructive algorithms, divide and conquer Correct Solution: ``` ##Right most (mid) not taking as inclusive size, call_no = map(int, input().split()) list_val = [i for i in range(1,size+1)] def modify(list_val,l,r): global count if count == call_no or r-l<2: return mid = (l+r)//2 list_val[mid-1],list_val[mid]=list_val[mid],list_val[mid-1] #swap code count+=2 modify(list_val,l,mid) modify(list_val,mid,r) if call_no == 1: str_val =(" ").join(map(str,list_val)) print(str_val) elif call_no%2 == 0: print("-1") else: count = 1 modify(list_val,0,len(list_val)) if count < call_no: ##count<call_no would fail 8 test case Reason:::: print("-1") else: print((" ").join(map(str,list_val))) ```
output
1
85,450
12
170,901
Provide tags and a correct Python 3 solution for this coding contest problem. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1
instruction
0
85,451
12
170,902
Tags: constructive algorithms, divide and conquer Correct Solution: ``` def swap(m,n): temp= arr[m] arr[m]=arr[n] arr[n]=temp def swap_and_modify(arr,l,r,k): global count if count>=k or r-l<2: return else: m=int((l+r)/2) swap(m,m-1) count+=2 swap_and_modify(arr,l,m,k) swap_and_modify(arr,m,r,k) n,k=input().split() n=int(n) k=int(k) arr=[] global count count=1 for i in range(1,n+1): arr.append(i) swap_and_modify(arr,0,n,k) if count!=k: print(-1) else: for i in arr: print(i,end=" ") ```
output
1
85,451
12
170,903
Provide tags and a correct Python 3 solution for this coding contest problem. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1
instruction
0
85,452
12
170,904
Tags: constructive algorithms, divide and conquer Correct Solution: ``` n,k=map(int,input().split()) if not k&1:exit(print(-1)) k-=1 a=[int(i+1) for i in range(n)] def f(l,r): global k if k<2or r-l<2:return k-=2 m=(l+r)//2 a[m],a[m-1]=a[m-1],a[m] f(l,m) f(m,r) f(0,n) if k:exit(print(-1)) for i in a: print(int(i),end=' ') ```
output
1
85,452
12
170,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1 Submitted Solution: ``` #Bhargey Mehta (Sophomore) #DA-IICT, Gandhinagar import sys, math, queue, bisect #sys.stdin = open("input.txt", "r") MOD = 10**9+7 sys.setrecursionlimit(1000000) n, k = map(int, input().split()) k -= 1 a = [-1 for i in range(n)] c = 1 def merge(l, r, k): global a, c if l >= r or k%2 != 0: return False if k == 0: for i in range(l, r): a[i] = c c += 1 return True k -= 2 m = (l+r)//2 if (k//2)%2 == 0: if merge(m, r, k//2): return merge(l, m, k//2) else: return False else: if merge(m, r, k//2+1): return merge(l, m, k//2-1) else: return False if not merge(0, n, k): print(-1) exit() print(*a) ```
instruction
0
85,453
12
170,906
Yes
output
1
85,453
12
170,907
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1 Submitted Solution: ``` from sys import stdin, stdout n, k = map(int, stdin.readline().split()) value = n cnt = k ans = [] def generate(l, r): global cnt, value if l == r: ans.append(value) value -= 1 return 0 if not cnt: for i in range(value - (r - l), value + 1): ans.append(i) value -= (r - l + 1) return 0 cnt -= 2 middle = (l + r + 1) >> 1 generate(l, middle - 1) generate(middle, r) cnt -= 1 generate(0, n - 1) if len(ans) != n or cnt: stdout.write('-1') else: stdout.write(' '.join(list(map(str, ans)))) ```
instruction
0
85,454
12
170,908
Yes
output
1
85,454
12
170,909
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1 Submitted Solution: ``` import os import sys import copy num = 0 a = None # sys.stdin = open(os.path.join(os.path.dirname(__file__),'35.in')) k = None def solve(): global k,a n, k = map(lambda x:int(x), input().split()) savek = copy.deepcopy(k) # print(a) a = [_+1 for _ in range(n)] def mgsort(l, r): global num,a,k mid = (l+r) >> 1 if r - l == 1 or k == 1: return k -= 2 a[l:mid] , a[mid:r] = a[mid:r] , a[l:mid] mgsort(l, mid) mgsort(mid, r) if k % 2 != 0: mgsort(0, n) if k == 1: for i in a: print(i,sep=' ',end=' ') else: print(-1) solve() ```
instruction
0
85,455
12
170,910
Yes
output
1
85,455
12
170,911
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1 Submitted Solution: ``` n,k=map(int,input().split()) a=[i+1 for i in range(n)] if not k&1:exit(print(-1)) k-=1 def f(l,r): global k if(k<2 or r-l<2): return k-=2 m=(l+r)//2 a[m-1],a[m]=a[m],a[m-1] f(l,m) f(m,r) f(0,n) if not k==0:exit(print(-1)) for i in a: print(int(i),end=" ") ```
instruction
0
85,456
12
170,912
Yes
output
1
85,456
12
170,913
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1 Submitted Solution: ``` from sys import stdin, stdout n, k = map(int, stdin.readline().split()) value = n cnt = k ans = [] def generate(l, r): global cnt, value cnt -= 1 if l == r: ans.append(value) value -= 1 return 0 if not cnt: for i in range(value - (r - l), value + 1): ans.append(i) value -= (r - l + 1) return 0 middle = (l + r + 1) >> 1 generate(l, middle - 1) if not cnt: for i in range(1, value + 1): ans.append(i) return 0 generate(middle, r) generate(0, n - 1) if len(ans) != n: stdout.write('-1') else: stdout.write(' '.join(list(map(str, ans)))) ```
instruction
0
85,457
12
170,914
No
output
1
85,457
12
170,915
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1 Submitted Solution: ``` n, k = map(int, input().split()) if k >= n: print("-1") elif k == 1: print(*list(range(1, n+1))) else: l = list(range(1, n+1)) print(*(sorted(l[:k-1], reverse = True)+l[k-1:])) ```
instruction
0
85,458
12
170,916
No
output
1
85,458
12
170,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1 Submitted Solution: ``` # your code goes here size, call_no = map(int, input().split()) list_val = [i for i in range(1,size+1)] #print(list_val) def modify(list_val,l,r): global count if count == call_no or r-l<2: return mid = (l+r)//2 temp = list_val[mid] list_val[mid] = list_val[mid-1] list_val[mid-1] = temp count+=2 modify(list_val,l,mid) modify(list_val,mid+1,r) if call_no == 1: str_val = " ".join(str(x) for x in list_val) print(str_val) elif call_no%2 == 0: print("-1") else: count = 1 modify(list_val,0,len(list_val)-1) if count < call_no: print("-1") else: print(" ".join(str(x) for x in list_val)) ```
instruction
0
85,459
12
170,918
No
output
1
85,459
12
170,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: 1. If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≀ i < r - 1 a[i] ≀ a[i + 1]), then end the function call; 2. Let <image>; 3. Call mergesort(a, l, mid); 4. Call mergesort(a, mid, r); 5. Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n). The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort β€” mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted. Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k. Help Ivan to find an array he wants! Input The first line contains two numbers n and k (1 ≀ n ≀ 100000, 1 ≀ k ≀ 200000) β€” the size of a desired permutation and the number of mergesort calls required to sort it. Output If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them. Examples Input 3 3 Output 2 1 3 Input 4 1 Output 1 2 3 4 Input 5 6 Output -1 Submitted Solution: ``` def unsort(a, left, right, k): if k[0] < 1 or left + 1 == right: return k[0] -= 2 mid = (left + right) // 2 tmp = a[mid - 1] a[mid - 1] = a[mid] a[mid] = tmp unsort(a, left, mid, k) unsort(a, mid, right, k) def main(): k = [1] n, k[0] = (int(i) for i in input().split()) if k[0] % 2 == 0: print('-1') return a = [i for i in range(1, n + 1)] k[0] -= 1 unsort(a, 0, n, k) print(*a) if __name__ == '__main__': main() ```
instruction
0
85,460
12
170,920
No
output
1
85,460
12
170,921