message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m.
instruction
0
104,622
12
209,244
Tags: constructive algorithms, greedy, math Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict #threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase #sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=300006, func=lambda a, b: min(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b:max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] <=key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] > k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False class Trie: def __init__(self): self.root = self.getNode() def getNode(self): return TrieNode() def _charToIndex(self, ch): return ord(ch) - ord('a') def insert(self, key): pCrawl = self.root length = len(key) for level in range(length): index = self._charToIndex(key[level]) if not pCrawl.children[index]: pCrawl.children[index] = self.getNode() pCrawl = pCrawl.children[index] pCrawl.isEndOfWord = True def search(self, key): pCrawl = self.root length = len(key) for level in range(length): index = self._charToIndex(key[level]) if not pCrawl.children[index]: return False pCrawl = pCrawl.children[index] return pCrawl != None and pCrawl.isEndOfWord #-----------------------------------------trie--------------------------------- class Node: def __init__(self, data): self.data = data self.count=0 self.left = None # left node for 0 self.right = None # right node for 1 class BinaryTrie: def __init__(self): self.root = Node(0) def insert(self, pre_xor): self.temp = self.root for i in range(31, -1, -1): val = pre_xor & (1 << i) if val: if not self.temp.right: self.temp.right = Node(0) self.temp = self.temp.right self.temp.count+=1 if not val: if not self.temp.left: self.temp.left = Node(0) self.temp = self.temp.left self.temp.count += 1 self.temp.data = pre_xor def query(self, xor): self.temp = self.root for i in range(31, -1, -1): val = xor & (1 << i) if not val: if self.temp.left and self.temp.left.count>0: self.temp = self.temp.left elif self.temp.right: self.temp = self.temp.right else: if self.temp.right and self.temp.right.count>0: self.temp = self.temp.right elif self.temp.left: self.temp = self.temp.left self.temp.count-=1 return xor ^ self.temp.data #-------------------------bin trie------------------------------------------- for ik in range(int(input())): n,k=map(int,input().split()) a=list(map(int,input().split())) if k==1: s=set(a) if len(s)!=1: print(-1) else: print(1) else: ans=0 while(sum(a)!=0): ans+=1 cou=0 s=set() last=0 for i in range(n): if cou<k: if a[i] not in s: cou+=1 s.add(a[i]) last=a[i] a[i] = 0 elif cou==k and a[i] in s: a[i]=0 else: a[i]-=last print(ans) ```
output
1
104,622
12
209,245
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m.
instruction
0
104,623
12
209,246
Tags: constructive algorithms, greedy, math Correct Solution: ``` import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from math import ceil t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) if len(set(a)) <= k: print(1) else: m = -1 n = len(set(a)) for i in range(1, 101): if 1+ceil((n-1)/i) <= k: m = i break print(m) ```
output
1
104,623
12
209,247
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m.
instruction
0
104,624
12
209,248
Tags: constructive algorithms, greedy, math Correct Solution: ``` import math from sys import stdin from sys import setrecursionlimit setrecursionlimit(100000) def put(): return map(int, stdin.readline().split()) for _ in range(int(input())): # n=int(input()) n,k=put() l=(list(put())) if(len(set(l))<=k): print(1) elif(len(set(l))>1 and k==1): print(-1) else: if((len(set(l))-k)%(k-1)==0): print(1+(len(set(l))-k)//(k-1)) else: print(2+(len(set(l))-k)//(k-1)) ```
output
1
104,624
12
209,249
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m.
instruction
0
104,625
12
209,250
Tags: constructive algorithms, greedy, math Correct Solution: ``` from math import ceil t = int(input()) for _ in range(t): n, k = map(int, input().split()) A = set(map(int, input().split())) uniq = len(A) if k == 1: if uniq == 1: print(1) else: print(-1) else: print(1 + ceil(max(0, uniq - k) / (k - 1))) ```
output
1
104,625
12
209,251
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m.
instruction
0
104,626
12
209,252
Tags: constructive algorithms, greedy, math Correct Solution: ``` import math for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) if k == 1: if len(set(a)) == 1: print(1) else: print(-1) continue count = 0 c = 1 flag = True for i in range(n-1): if a[i] != a[i+1]: count += 1 print(max((count+k-2)//(k-1), 1)) ```
output
1
104,626
12
209,253
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m.
instruction
0
104,627
12
209,254
Tags: constructive algorithms, greedy, math Correct Solution: ``` from sys import stdin,stdout import math,bisect from collections import Counter,deque,defaultdict L=lambda:list(map(int, stdin.readline().strip().split())) M=lambda:map(int, stdin.readline().strip().split()) I=lambda:int(stdin.readline().strip()) S=lambda:stdin.readline().strip() C=lambda:stdin.readline().strip().split() def pr(a):return(" ".join(list(map(str,a)))) #_________________________________________________# def solve(): n,k = M() a = L() x = len(set(a)) if k>=x: print(1) elif k==1: print(-1) else: print(math.ceil((x-1)/(k-1))) for _ in range(I()): solve() ```
output
1
104,627
12
209,255
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m.
instruction
0
104,628
12
209,256
Tags: constructive algorithms, greedy, math Correct Solution: ``` for _ in range(int(input())): n,k = map(int,input().split(' ')) ans = 0 arr = [int(num) for num in input().split(' ')] if k==1 and len(set(arr))!=1: ans = -1 elif arr[0]==0 and len(set(arr))==1: ans = 1 else: while (arr[0] !=0 or len(set(arr))!=1): i = 0 a = set() a.add(arr[0]) while i<n and len(set(a))<=k: a.add(arr[i]) if len(a)>k: break else: arr[i] = 0 i = i + 1 for j in range(i,n): arr[j] = arr[j] - arr[i-1] ans = ans + 1 print(ans) ```
output
1
104,628
12
209,257
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m. Submitted Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') 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") ALPHA='abcdefghijklmnopqrstuvwxyz/' M=998244353 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() for _ in range(Int()): n,k=value() a=array() dist=len(set(a)) if(k==1): if(dist==1): print(1) else: print(-1) else: ans=0 cur=sorted(set(a)) # print(cur) while(True): ans+=1 d=1 ok=False cur[0]=0 for i in range(1,len(cur)): if(cur[i]!=cur[i-1]): d+=1 if(d==k):key=cur[i] if(d<=k): cur[i]=0 else: ok=True cur[i]=key if(not ok): break print(ans) ```
instruction
0
104,629
12
209,258
Yes
output
1
104,629
12
209,259
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m. Submitted Solution: ``` import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') def ceil(x, y=1): return int(-(-x // y)) import math from collections import defaultdict for _ in range(ri()): n,k=ria() l=ria() s=set(l) if k==1 and len(s)>1: wi(-1) elif k==1 and len(s)==1: wi(1) else: x=len(s) if x<=k: wi(1) else: x-=k k-=1 wi(1+math.ceil(x/k)) ```
instruction
0
104,630
12
209,260
Yes
output
1
104,630
12
209,261
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m. Submitted Solution: ``` import sys import itertools as it import bisect as bi import math as mt import collections as cc input=sys.stdin.readline S=lambda :list(input().split()) I=lambda:list(map(int,input().split())) for tc in range(int(input())): n,k=I() l=I() nn=len(set(l)) if nn>1 and k-1==0: print(-1) elif k>=nn: print(1) else: print(1+(nn-2)//(k-1)) ```
instruction
0
104,631
12
209,262
Yes
output
1
104,631
12
209,263
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m. Submitted Solution: ``` from collections import * from itertools import * from bisect import * def inp(): return int(input()) def arrinp(): return [int(x) for x in input().split()] def main(): t = inp() for _ in range(t): n,k = arrinp() A = arrinp() distinct = set(A) curr = len(distinct) if(k<=1 and curr>k): print(-1) continue if(curr<=k): print(1) continue m = 0 while(len(distinct)>=k): m += 1 temp = list(distinct) #print(*temp, 'value m:', m) sub = temp[k-1] distinct = set() n = len(temp) l = n-1 while(l>=k-1): distinct.add(temp[l]-sub) l -=1 #print(distinct, 'after 1 list generated') if(len(distinct)>1): m+=1 print(m) if __name__ == '__main__': main() ```
instruction
0
104,632
12
209,264
Yes
output
1
104,632
12
209,265
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m. Submitted Solution: ``` import sys try:sys.stdin,sys.stdout=open('in.txt','r'),open('out.txt','w') except:pass ii1=lambda:int(sys.stdin.readline().strip()) # for interger is1=lambda:sys.stdin.readline().strip() # for str iia=lambda:list(map(int,sys.stdin.readline().strip().split())) # for List[int] isa=lambda:sys.stdin.readline().strip().split() # for List[str] mod=int(1e9 + 7);from collections import *;from math import * ###################### Start Here ###################### for _ in range(ii1()): n ,k= iia() arr = iia() arr_set=set(arr) # diff=[] # for i in range(n-1): # diff.append(arr[i+1]-arr[i]) # diff_len=len(set(diff)) # if 0 in diff:diff_len-=1 set_len = len(arr_set) if k==1 and set_len==2: print(-1) continue if set_len==1: print(1) continue if k>=set_len: print(1) if set_len>k: print(2) continue ```
instruction
0
104,633
12
209,266
No
output
1
104,633
12
209,267
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m. Submitted Solution: ``` import sys import math input = lambda: sys.stdin.readline().rstrip() for _ in range(int(input())): n,k=map(int,input().split()) l=[int(x) for x in input().split()] a=len(set(l)) arr=[1,2,3,4,5] if(l==arr and k==3): print(2) elif(k<a and k==1): print(-1) elif(a==k): print(1) else: print(a-k) ```
instruction
0
104,634
12
209,268
No
output
1
104,634
12
209,269
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m. Submitted Solution: ``` for aww in range(int(input())): a,b=[*map(int, input().split())] c=[*map(int, input().split())] d=set(c) if(b==1): if(len(d)>1): print(-1) else: print(1) else: x=c[0] e=0 f=0 for i,j in enumerate(c): if(x!=c[i]): x=c[i] e+=1 if(e==b): f+=1 e=0 if(e>0): f+=1 print(f) ```
instruction
0
104,635
12
209,270
No
output
1
104,635
12
209,271
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a non-decreasing array of non-negative integers a_1, a_2, …, a_n. Also you are given a positive integer k. You want to find m non-decreasing arrays of non-negative integers b_1, b_2, …, b_m, such that: * The size of b_i is equal to n for all 1 ≀ i ≀ m. * For all 1 ≀ j ≀ n, a_j = b_{1, j} + b_{2, j} + … + b_{m, j}. In the other word, array a is the sum of arrays b_i. * The number of different elements in the array b_i is at most k for all 1 ≀ i ≀ m. Find the minimum possible value of m, or report that there is no possible m. Input The first line contains one integer t (1 ≀ t ≀ 100): the number of test cases. The first line of each test case contains two integers n, k (1 ≀ n ≀ 100, 1 ≀ k ≀ n). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_1 ≀ a_2 ≀ … ≀ a_n ≀ 100, a_n > 0). Output For each test case print a single integer: the minimum possible value of m. If there is no such m, print -1. Example Input 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 Output -1 1 2 2 2 1 Note In the first test case, there is no possible m, because all elements of all arrays should be equal to 0. But in this case, it is impossible to get a_4 = 1 as the sum of zeros. In the second test case, we can take b_1 = [3, 3, 3]. 1 is the smallest possible value of m. In the third test case, we can take b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] and b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]. It's easy to see, that a_i = b_{1, i} + b_{2, i} for all i and the number of different elements in b_1 and in b_2 is equal to 3 (so it is at most 3). It can be proven that 2 is the smallest possible value of m. Submitted Solution: ``` mod = 10**9 + 7 def solve(): ans = -1 n, k = map(int, input().split()) a = list(map(int, input().split())) d = {} for x in a: d[x] = 1 if k == 1: if len(d) != 1: ans = -1 else: ans = 1 else: l = len(d) ans = 1 l -= k ans += (l + k - 2) // (k - 1) print(ans) t = int(input()) while t > 0: solve() t -= 1 ```
instruction
0
104,636
12
209,272
No
output
1
104,636
12
209,273
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two tables A and B of size n Γ— m. We define a sorting by column as the following: we choose a column and reorder the rows of the table by the value in this column, from the rows with the smallest value to the rows with the largest. In case there are two or more rows with equal value in this column, their relative order does not change (such sorting algorithms are called stable). You can find this behavior of sorting by column in many office software for managing spreadsheets. Petya works in one, and he has a table A opened right now. He wants to perform zero of more sortings by column to transform this table to table B. Determine if it is possible to do so, and if yes, find a sequence of columns to sort by. Note that you do not need to minimize the number of sortings. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1500) β€” the sizes of the tables. Each of the next n lines contains m integers a_{i,j} (1 ≀ a_{i, j} ≀ n), denoting the elements of the table A. Each of the next n lines contains m integers b_{i, j} (1 ≀ b_{i, j} ≀ n), denoting the elements of the table B. Output If it is not possible to transform A into B, print -1. Otherwise, first print an integer k (0 ≀ k ≀ 5000) β€” the number of sortings in your solution. Then print k integers c_1, …, c_k (1 ≀ c_i ≀ m) β€” the columns, by which Petya needs to perform a sorting. We can show that if a solution exists, there is one in no more than 5000 sortings. Examples Input 2 2 2 2 1 2 1 2 2 2 Output 1 1 Input 3 3 2 3 2 1 3 3 1 1 2 1 1 2 1 3 3 2 3 2 Output 2 1 2 Input 2 2 1 1 2 1 2 1 1 1 Output -1 Input 4 1 2 2 2 1 1 2 2 2 Output 1 1 Note Consider the second example. After the sorting by the first column the table becomes $$$\begin{matrix} 1&3&3\\\ 1&1&2\\\ 2&3&2. \end{matrix}$$$ After the sorting by the second column the table becomes $$$\begin{matrix} 1&1&2\\\ 1&3&3\\\ 2&3&2, \end{matrix}$$$ and this is what we need. In the third test any sorting does not change anything, because the columns are already sorted. Submitted Solution: ``` def check(a,b): for i in range(len(a)): if(a[i]!=b[i]): return(False) return(True) def sort(a): if(len(a)<=1): return(a) else: pivot=a.pop() left=[] right=[] for i in a: if(i<pivot): left.append(i) else: right.append(i) return(sort(left)+[pivot]+sort(right)) a,b=map(int,input().split()) c=[] d=[] count=0 z=0 out=[] for i in range(a): x=[int(x) for x in input().split()] c.append(x) for j in range(a): x=[int(x) for x in input().split()] d.append(x) for i in range(b): fi=[] sec=[] ram=0 for j in range(a): fi.append(c[j][i]) sec.append(d[j][i]) if(not(check(fi,sec))): fi=sort(fi) ram=1 if(ram==1): if(check(fi,sec)): count+=1 out.append(i+1) else: z=1 break if(z==0): print(count) print(*out,sep=" ") else: print(-1) ```
instruction
0
104,681
12
209,362
No
output
1
104,681
12
209,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two tables A and B of size n Γ— m. We define a sorting by column as the following: we choose a column and reorder the rows of the table by the value in this column, from the rows with the smallest value to the rows with the largest. In case there are two or more rows with equal value in this column, their relative order does not change (such sorting algorithms are called stable). You can find this behavior of sorting by column in many office software for managing spreadsheets. Petya works in one, and he has a table A opened right now. He wants to perform zero of more sortings by column to transform this table to table B. Determine if it is possible to do so, and if yes, find a sequence of columns to sort by. Note that you do not need to minimize the number of sortings. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1500) β€” the sizes of the tables. Each of the next n lines contains m integers a_{i,j} (1 ≀ a_{i, j} ≀ n), denoting the elements of the table A. Each of the next n lines contains m integers b_{i, j} (1 ≀ b_{i, j} ≀ n), denoting the elements of the table B. Output If it is not possible to transform A into B, print -1. Otherwise, first print an integer k (0 ≀ k ≀ 5000) β€” the number of sortings in your solution. Then print k integers c_1, …, c_k (1 ≀ c_i ≀ m) β€” the columns, by which Petya needs to perform a sorting. We can show that if a solution exists, there is one in no more than 5000 sortings. Examples Input 2 2 2 2 1 2 1 2 2 2 Output 1 1 Input 3 3 2 3 2 1 3 3 1 1 2 1 1 2 1 3 3 2 3 2 Output 2 1 2 Input 2 2 1 1 2 1 2 1 1 1 Output -1 Input 4 1 2 2 2 1 1 2 2 2 Output 1 1 Note Consider the second example. After the sorting by the first column the table becomes $$$\begin{matrix} 1&3&3\\\ 1&1&2\\\ 2&3&2. \end{matrix}$$$ After the sorting by the second column the table becomes $$$\begin{matrix} 1&1&2\\\ 1&3&3\\\ 2&3&2, \end{matrix}$$$ and this is what we need. In the third test any sorting does not change anything, because the columns are already sorted. Submitted Solution: ``` # C. Matrix Sorting: https://codeforces.com/contest/1500/problem/C # glob_x = """ # 2 2 2 1 # 2 2 1 2 # 1 2 2 2 # 2 1 2 2 # 2 2 1 2 # 2 1 2 2 # 1 2 2 2 # 2 2 2 1 # """ # def get_ab(glob_x): # glob_x = glob_x[1:-1].split('\n') # n = len(glob_x) // 2 # a, b = [], [] # for i in range(n): # a.append(glob_x[i].split(' ')) # for i in range(n, 2 * n): # b.append(glob_x[i].split(' ')) # m = len(a[0]) # return n, m, a, b # print(get_ab(glob_x)) def main(): n, m = list(map(lambda x: int(x), str(input()).split(' '))) u = {} states = {} end = '' a, b = [], [] for i in range(n): t = input() a.append(t.split(' ')) if t not in u: u[t] = 1 else: u[t] += 1 for i in range(n): t = input() b.append(t.split(' ')) if t not in u: print(-1) return else: u[t] -= 1 if u[t] == 0: del u[t] # n, m, a, b = get_ab(glob_x) end = get_str(b) s = [] ans = bt(a, s, end, states) if not ans: print(-1) return def is_solved(a, s, end): if len(s) > 5000: return False if get_str(a) == end: return True return False def get_str(a): return '$'.join(map(lambda x: ' '.join(x), a)) def bt(a, s, end, states): # print(len(states), states, '\n') if is_solved(a, s, end): print(len(s)) print(' '.join(map(lambda x: str(x), s))) return True for i in range(len(a[0])): # print('#' * len(s) + str(i)) if len(s) == 0: s.append(4) else: s.append(i + 1) new_a = sort(a, i + 1) new_str = get_str(new_a) added = False if not new_str in states: states[new_str] = True added = True if bt(new_a, s, end, states): return True if added: del states[new_str] s.pop() # print("-" * 20) return False def sort(a, i): return sorted(a, key=lambda x: x[i - 1]) if __name__ == "__main__": main() ```
instruction
0
104,682
12
209,364
No
output
1
104,682
12
209,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two tables A and B of size n Γ— m. We define a sorting by column as the following: we choose a column and reorder the rows of the table by the value in this column, from the rows with the smallest value to the rows with the largest. In case there are two or more rows with equal value in this column, their relative order does not change (such sorting algorithms are called stable). You can find this behavior of sorting by column in many office software for managing spreadsheets. Petya works in one, and he has a table A opened right now. He wants to perform zero of more sortings by column to transform this table to table B. Determine if it is possible to do so, and if yes, find a sequence of columns to sort by. Note that you do not need to minimize the number of sortings. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1500) β€” the sizes of the tables. Each of the next n lines contains m integers a_{i,j} (1 ≀ a_{i, j} ≀ n), denoting the elements of the table A. Each of the next n lines contains m integers b_{i, j} (1 ≀ b_{i, j} ≀ n), denoting the elements of the table B. Output If it is not possible to transform A into B, print -1. Otherwise, first print an integer k (0 ≀ k ≀ 5000) β€” the number of sortings in your solution. Then print k integers c_1, …, c_k (1 ≀ c_i ≀ m) β€” the columns, by which Petya needs to perform a sorting. We can show that if a solution exists, there is one in no more than 5000 sortings. Examples Input 2 2 2 2 1 2 1 2 2 2 Output 1 1 Input 3 3 2 3 2 1 3 3 1 1 2 1 1 2 1 3 3 2 3 2 Output 2 1 2 Input 2 2 1 1 2 1 2 1 1 1 Output -1 Input 4 1 2 2 2 1 1 2 2 2 Output 1 1 Note Consider the second example. After the sorting by the first column the table becomes $$$\begin{matrix} 1&3&3\\\ 1&1&2\\\ 2&3&2. \end{matrix}$$$ After the sorting by the second column the table becomes $$$\begin{matrix} 1&1&2\\\ 1&3&3\\\ 2&3&2, \end{matrix}$$$ and this is what we need. In the third test any sorting does not change anything, because the columns are already sorted. Submitted Solution: ``` l,c=map(int, input().split()) A=list() nA=list() B=list() tb=list() cpt=0 i=0 for s in range(l*2): if s<l: A.append(list(map(int,input().split()))[:c]) elif s>=l: B.append(list(map(int, input().split()))[:c]) if A == B: cpt=c nA=[x+1 for x in range(c)] else: while i <c: tem = list() for index,z in enumerate(A): tem.append((z[i],index)) for ind,z in enumerate(B): tb.append((z[i],ind)) stem=sorted(tem, key= lambda x:x[0]) if tem !=stem: cpt+=1 nA.append(i+1) for w in range(len(stem)): u=stem[w][1] A[w][i:]=A[u][i:] i+=1 print(cpt) if cpt>0 else print(-1) print(' '.join(str(z) for z in nA)) if cpt>0 else print('') ```
instruction
0
104,683
12
209,366
No
output
1
104,683
12
209,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two tables A and B of size n Γ— m. We define a sorting by column as the following: we choose a column and reorder the rows of the table by the value in this column, from the rows with the smallest value to the rows with the largest. In case there are two or more rows with equal value in this column, their relative order does not change (such sorting algorithms are called stable). You can find this behavior of sorting by column in many office software for managing spreadsheets. Petya works in one, and he has a table A opened right now. He wants to perform zero of more sortings by column to transform this table to table B. Determine if it is possible to do so, and if yes, find a sequence of columns to sort by. Note that you do not need to minimize the number of sortings. Input The first line contains two integers n and m (1 ≀ n, m ≀ 1500) β€” the sizes of the tables. Each of the next n lines contains m integers a_{i,j} (1 ≀ a_{i, j} ≀ n), denoting the elements of the table A. Each of the next n lines contains m integers b_{i, j} (1 ≀ b_{i, j} ≀ n), denoting the elements of the table B. Output If it is not possible to transform A into B, print -1. Otherwise, first print an integer k (0 ≀ k ≀ 5000) β€” the number of sortings in your solution. Then print k integers c_1, …, c_k (1 ≀ c_i ≀ m) β€” the columns, by which Petya needs to perform a sorting. We can show that if a solution exists, there is one in no more than 5000 sortings. Examples Input 2 2 2 2 1 2 1 2 2 2 Output 1 1 Input 3 3 2 3 2 1 3 3 1 1 2 1 1 2 1 3 3 2 3 2 Output 2 1 2 Input 2 2 1 1 2 1 2 1 1 1 Output -1 Input 4 1 2 2 2 1 1 2 2 2 Output 1 1 Note Consider the second example. After the sorting by the first column the table becomes $$$\begin{matrix} 1&3&3\\\ 1&1&2\\\ 2&3&2. \end{matrix}$$$ After the sorting by the second column the table becomes $$$\begin{matrix} 1&1&2\\\ 1&3&3\\\ 2&3&2, \end{matrix}$$$ and this is what we need. In the third test any sorting does not change anything, because the columns are already sorted. Submitted Solution: ``` rows, columns = map(int, input().split()) table1 = [] table2 = [] for i in range(rows): table1 += [list(map(int, input().split()))] for i in range(rows): table2 += [list(map(int, input().split()))] sorts = [] f = True for i in range(columns): column1 = [] column2 = [] for j in range(rows): column1 += [table1[j][i]] column2 += [table2[j][i]] if column1 == column2: break else: column1.sort() if column1 == column2: column2.sort() if column1 == column2: sorts += [i + 1] else: f = False else: f = False if f: print(len(sorts)) for i in sorts: print(i, end=' ') else: print(-1) ```
instruction
0
104,684
12
209,368
No
output
1
104,684
12
209,369
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x.
instruction
0
104,824
12
209,648
Tags: constructive algorithms, greedy Correct Solution: ``` n,k = map(int,list(input().split())) for i in range(1,(k+3)//2): print(i, k+2-i,end=" ") if k>1 and k%2==0: print(k//2+1,end=" ") for i in range(k+2,n+1): print(i,end=" ") ```
output
1
104,824
12
209,649
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x.
instruction
0
104,825
12
209,650
Tags: constructive algorithms, greedy Correct Solution: ``` [n, k], s = map(int, input().split()), [1] for i in range(k, 0, -1): s.append(s[-1] + (-i if (k - i) % 2 else i)) print(*s + sorted(set(range(1, n + 1)) - set(s))) ```
output
1
104,825
12
209,651
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x.
instruction
0
104,826
12
209,652
Tags: constructive algorithms, greedy Correct Solution: ``` n,k=(int(i) for i in input().split()) l=[i for i in range(1,n-k+1)] l1=[i for i in range(n-k+1,n+1)] k1=0 k2=k-1 for i in range(k): if(i%2==0): l.append(l1[k2]) k2-=1 else: l.append(l1[k1]) k1+=1 print(*l) ```
output
1
104,826
12
209,653
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x.
instruction
0
104,827
12
209,654
Tags: constructive algorithms, greedy Correct Solution: ``` from sys import stdin lines = list(filter(None, stdin.read().split('\n'))) def parseline(line): return list(map(int, line.split())) lines = list(map(parseline, lines)) n, k = lines[0] x = n + 1 for i in range(n, n - k, -1): if (n - i) % 2 == 0: x = x - i else: x = x + i print(x, end=' ') step = -1 if k %2 == 0 else +1 for k in range(n - k): x += step print(x, end=' ') ```
output
1
104,827
12
209,655
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x.
instruction
0
104,828
12
209,656
Tags: constructive algorithms, greedy Correct Solution: ``` n, k = map(int, input().split()) a = [x + 1 for x in range(n)] l = 0 r = n - 1 p = 0 for i in range(k - 1): if p: print(a[r], end = ' ') r -= 1 p = not p else: print(a[l], end = ' ') l += 1 p = not p p = not p if p: for i in range(l, r + 1): print(a[i], end = ' ') else: for i in range(r, l - 1, -1): print(a[i], end = ' ') ```
output
1
104,828
12
209,657
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x.
instruction
0
104,829
12
209,658
Tags: constructive algorithms, greedy Correct Solution: ``` #In the name of Allah from sys import stdin, stdout input = stdin.readline n, k = map(int, input().split()) def f(n): n += 1 l = [] for i in range(1, 1 + n // 2): l.append(str(i)) l.append(str(n - i + 1)) if n % 2 == 1: l.append(str(n // 2 + 1)) return l ans = f(k) for i in range(n - k - 1): ans.append(str(i + k + 2)) stdout.write(" ".join(ans)) ```
output
1
104,829
12
209,659
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x.
instruction
0
104,830
12
209,660
Tags: constructive algorithms, greedy Correct Solution: ``` n, k = map(int, input().split()) a = [] k += 1 one = 0 two = 0 f = False for i in range(1, n + 1): if i % 2 != 0: i -= one a.append(i) one += 1 else: i -= two a.append(n + 2 - i) two += 1 k -= 1 if k == 1: f = True d = set(a) if len(a) % 2 == 0: for j in range(n - 1, 0, -1): if j not in d: a.append(j) else: for j in range(1, n + 1): if j not in d: a.append(j) if f: break print(*a) ```
output
1
104,830
12
209,661
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x.
instruction
0
104,831
12
209,662
Tags: constructive algorithms, greedy Correct Solution: ``` n,k = map(int,input().split()) a = list(range(1,n+1)) for i in range(1,k+1): a[i] = a[i-1] + (k-i+1 if i&1 else i-k-1) print(" ".join(repr(x) for x in a)) ```
output
1
104,831
12
209,663
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Submitted Solution: ``` n,k = map(int,input().split()) a = list(range(1,n+1)) for i in range(1,k+1): a[i] = a[i-1] + (k-i+1 if i&1 else i-k-1) print(" ".join(repr(x) for x in a)) # Made By Mostafa_Khaled ```
instruction
0
104,832
12
209,664
Yes
output
1
104,832
12
209,665
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Submitted Solution: ``` n,k = map(int,input().split()) b = '' i = 1 j = n c = 0 p = 0 while(p < n): if(c < k): if(p % 2 == 0): b += str(i) + ' ' i += 1 c += 1 else: b += str(j) + ' ' j -= 1 c += 1 else: if(p % 2 == 1): for q in range(i,j+1): b += str(q) + ' ' print(b) exit(0) else: for q in range(j,i-1,-1): b += str(q) + ' ' print(b) exit(0) p += 1 print(b) ```
instruction
0
104,833
12
209,666
Yes
output
1
104,833
12
209,667
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Submitted Solution: ``` n,k=map(int,input().split()) i=1 a=[] for j in range(n//2): a.append(i) a.append(n-i+1) i+=1 if n%2==1: a.append(n//2+1) for i in range(k): print(a[i],end=' ') if k<n and a[k-1]>a[k]: s=a[k-1]-1 for i in range(k,n): print(s,end=' ') s-=1 else: s=a[k-1]+1 for i in range(k,n): print(s,end=' ') s+=1 ```
instruction
0
104,834
12
209,668
Yes
output
1
104,834
12
209,669
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Submitted Solution: ``` def getLine(): return list(map(int,input().split())) n,k = getLine() l = [0]*n for i in range(n): if i%2 == 0:l[i] = i//2+1 else : l[i] = n - l[i-1]+1 ans=l for i in range(k,n): if k%2 == 0:l[i] = l[i-1]-1 else : l[i] = l[i-1]+1 for i in ans: print(i,end=" ") ```
instruction
0
104,835
12
209,670
Yes
output
1
104,835
12
209,671
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Submitted Solution: ``` from sys import stdin lines = list(filter(None, stdin.read().split('\n'))) def parseline(line): return list(map(int, line.split())) lines = list(map(parseline, lines)) n, k = lines[0] x = n + 1 for i in range(n, n - k, -1): if 1 <= x + i <= n: x = x + i else: x = x - i print(x, end=' ') step = -1 if k %2 == 0 else +1 for k in range(n - k): x += step print(x, end=' ') ```
instruction
0
104,836
12
209,672
No
output
1
104,836
12
209,673
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Submitted Solution: ``` def getLine(): return list(map(int,input().split())) n,k = getLine() l = [0]*n for i in range(n): if i%2 == 0:l[i] = i//2+1 else : l[i] = n - l[i-1]+1 ans = l[:k-1]+sorted(l[k-1:]) for i in ans: print(i,end=" ") ```
instruction
0
104,837
12
209,674
No
output
1
104,837
12
209,675
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Submitted Solution: ``` def getLine(): return list(map(int,input().split())) n,k = getLine() l = [0]*n for i in range(n): if i%2 == 0:l[i] = i//2+1 else : l[i] = n - l[i-1]+1 ans = l[:k]+sorted(l[k:]) for i in ans: print(i,end=" ") ```
instruction
0
104,838
12
209,676
No
output
1
104,838
12
209,677
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1, p2, ..., pn. Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements. Input The single line of the input contains two space-separated positive integers n, k (1 ≀ k < n ≀ 105). Output Print n integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By |x| we denote the absolute value of number x. Submitted Solution: ``` n,k = map(int,input().split()) b = [] c = [] i = 1 j = n c = 0 p = 0 while(p < n): if(c < k-1): if(i % 2 == 0): b.append(i) i += 1 c += 1 else: b.append(j) j -= 1 c += 1 else: for q in range(i,j+1): b.append(q) break p += 1 for i in b: print(i); ```
instruction
0
104,839
12
209,678
No
output
1
104,839
12
209,679
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
instruction
0
104,865
12
209,730
Tags: constructive algorithms, dp, matrices Correct Solution: ``` f = lambda: map(int, input().split()) n, m = f() t = list(f()) d = {i: j for j, i in enumerate(sorted(set(t)))} t = [d[i] for i in t] k = len(d) a = [0] * k if m < 2 * k: for j in t * m: a[j] += 1 q = a[j] j += 1 while j < k and a[j] < q: a[j] += 1 j += 1 print(a[-1]) exit() a = [0] * k for j in t * k: a[j] += 1 q = a[j] j += 1 while j < k and a[j] < q: a[j] += 1 j += 1 b = [0] * k t.reverse() for j in t * k: b[j] += 1 q = b[j] j -= 1 while j > -1 and b[j] < q: b[j] += 1 j -= 1 print(max(a[j] + (m - 2 * k) * t.count(j) + b[j] for j in range(k))) ```
output
1
104,865
12
209,731
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
instruction
0
104,866
12
209,732
Tags: constructive algorithms, dp, matrices Correct Solution: ``` f = lambda: map(int, input().split()) n, m = f() t = list(f()) s = [0] * 301 d = s[:] for i in t: d[i] += 1 for i in t * min(m, 2 * n): s[i] = max(s[:i + 1]) + 1 print(max(s) + max((m - n * 2) * max(d), 0)) ```
output
1
104,866
12
209,733
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
instruction
0
104,867
12
209,734
Tags: constructive algorithms, dp, matrices Correct Solution: ``` f = lambda: map(int, input().split()) n, m = f() t = list(f()) s = [0] * 301 d = s[:] for i in t: d[i] += 1 for i in t * min(m, 2 * n): s[i] = max(s[:i + 1]) + 1 print(max(s) + max((m - n * 2) * max(d), 0)) # Made By Mostafa_Khaled ```
output
1
104,867
12
209,735
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
instruction
0
104,868
12
209,736
Tags: constructive algorithms, dp, matrices Correct Solution: ``` mul=lambda A,B,r:[[max([A[i][k]+B[k][j] for k in r if A[i][k] and B[k][j]],default=0) for j in r] for i in r] def binpower(A,n,e): r = range(n) B = A #A^0 is invalid, thus start from A^1 e -= 1 while True: if e &1: B = mul(B,A,r) e =e>>1 if e==0: return B A =mul(A,A,r) def f(l,n,T): # Part 1: cal M h = max(l)+1 N = [[0]*h for _ in range(h)] Q = [[0]*h for _ in range(h)] M = [[0]*n for _ in range(n)] for j in range(n): # update Mij based on Quv,(j-1) for i in range(n): M[i][j]=Q[l[i]][l[j]]+1 if l[i]<=l[j] else 0 # update Nuv,(j) and Quv,(j) v = l[j] for u in range(1,v+1): N[u][v] = Q[u][v]+1 for vv in range(v,h): Q[u][vv] = max(Q[u][vv],N[u][v]) return max(max(binpower(M,n,T))) n,T = list(map(int,input().split())) l = list(map(int,input().split())) print(f(l,n,T)) ```
output
1
104,868
12
209,737
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
instruction
0
104,869
12
209,738
Tags: constructive algorithms, dp, matrices Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq,bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary----------------------------------- n,t=map(int,input().split()) l=list(map(int,input().split())) dp=defaultdict(int) freq=defaultdict(int) ans=0 for i in range(n): freq[l[i]]+=1 ans=max(ans,freq[l[i]]) if t<=200: for i in range(t): for j in range(n): dp[l[j]] += 1 for k in range(n): if k == j or l[k] == l[j]: continue if l[k] < l[j]: dp[l[j]] = max(dp[l[j]], dp[l[k]] + 1) ans = max(ans, max(dp.values())) print(ans) sys.exit(0) for i in range(n): for j in range(n): dp[l[j]]+=1 for k in range(n): if k==j or l[k]==l[j]: continue if l[k]<l[j]: dp[l[j]]=max(dp[l[j]],dp[l[k]]+1) ans=max(ans,max(dp.values())) for j in range(n): dp[l[j]]+=(t-2*n) for i in range(n): for j in range(n): dp[l[j]]+=1 for k in range(n): if k==j or l[k]==l[j]: continue if l[k]<l[j]: dp[l[j]]=max(dp[l[j]],dp[l[k]]+1) ans=max(ans,max(dp.values())) print(ans) ```
output
1
104,869
12
209,739
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 of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence. Submitted Solution: ``` size, t = map(int,input().split(sep = " ")) a = [] a = list(map(int,input().split(sep = " "))) at = [] for x in range(t): at += a min = 1 dlugosc = 0 import collections ctr = collections.Counter(a) moda ,times = ctr.most_common(1).pop() for i in range(len(at)): if at[i] == moda & times !=1: j = i for j in range(len(a)): if a[j] == moda: dlugosc+=1 dlugosc += (t-1)*times break elif at[i] == min: dlugosc += 1 elif min + 1 == at[i]: min = at[i] dlugosc += 1 print(dlugosc) ```
instruction
0
104,870
12
209,740
No
output
1
104,870
12
209,741
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 of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence. Submitted Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq,bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary----------------------------------- n,t=map(int,input().split()) l=list(map(int,input().split())) t1=t-1 t=min(n,t)-1 dp=defaultdict(int) freq=defaultdict(int) ans=0 for i in range(n): freq[l[i]]+=1 ans=max(ans,freq[l[i]]) if t>0: for i in range(n): dp[l[i]]=1 for j in range(i): if l[j]<=l[i]: dp[l[i]]=max(dp[l[i]],dp[l[j]]+1) ans=max(dp.values()) for i in range(1,t): for j in range(n): ans=max(ans,dp[l[j]]+(t-i)*freq[l[j]]) for j in range(n): dp[l[j]]+=1 for k in range(n): if k==j or l[k]==l[j]: continue if l[k]<=l[j]: dp[l[j]]=max(dp[l[j]],dp[l[k]]+1) ans=max(ans,max(dp.values())) for j in range(n): dp[l[j]]+=(t1-t) for j in range(n): dp[l[j]]+=1 for k in range(n): if k==j or l[k]==l[j]: continue if l[k]<=l[j]: dp[l[j]]=max(dp[l[j]],dp[l[k]]+1) ans=max(ans,max(dp.values())) print(ans) ```
instruction
0
104,871
12
209,742
No
output
1
104,871
12
209,743
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 of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence. Submitted Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq,bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary----------------------------------- n,t=map(int,input().split()) l=list(map(int,input().split())) t1=t-1 t=min(n,t)-1 dp=defaultdict(int) freq=defaultdict(int) for i in range(n): dp[l[i]]=1 freq[l[i]]+=1 for j in range(i): if l[j]<=l[i]: dp[l[i]]=max(dp[l[i]],dp[l[j]]+1) ans=max(dp.values()) for i in range(1,t): for j in range(n): dp[l[j]]+=1 for k in range(n): if k==j: continue if l[k]<=l[j]: dp[l[j]]=max(dp[l[j]],dp[l[k]]+1) ans=max(ans,max(dp.values())) for j in range(n): dp[l[j]]+=(t1-t)*freq[l[j]] for j in range(n): dp[l[j]]+=1 for k in range(n): if k==j: continue if l[k]<=l[j]: dp[l[j]]=max(dp[l[j]],dp[l[k]]+1) ans=max(ans,max(dp.values())) print(ans) ```
instruction
0
104,872
12
209,744
No
output
1
104,872
12
209,745
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 of positive integers a1, a2, ..., an Γ— T of length n Γ— T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input The first line contains two space-separated integers: n, T (1 ≀ n ≀ 100, 1 ≀ T ≀ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 300). Output Print a single number β€” the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence. Submitted Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq,bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary----------------------------------- n,t=map(int,input().split()) l=list(map(int,input().split())) t1=t-1 t=min(n,t)-1 dp=defaultdict(int) freq=defaultdict(int) ans=0 for i in range(n): freq[l[i]]+=1 ans=max(ans,freq[l[i]]) if t>0: for i in range(n): dp[l[i]]=1 for j in range(i): if l[j]<=l[i]: dp[l[i]]=max(dp[l[i]],dp[l[j]]+1) ans=max(dp.values()) for i in range(1,t): for j in range(n): dp[l[j]]+=1 for k in range(n): if k==j: continue if l[k]<=l[j]: dp[l[j]]=max(dp[l[j]],dp[l[k]]+1) ans=max(ans,max(dp.values())) for j in range(n): dp[l[j]]+=(t1-t)*freq[l[j]] for j in range(n): dp[l[j]]+=1 for k in range(n): if k==j: continue if l[k]<=l[j]: dp[l[j]]=max(dp[l[j]],dp[l[k]]+1) ans=max(ans,max(dp.values())) print(ans) ```
instruction
0
104,873
12
209,746
No
output
1
104,873
12
209,747
Provide tags and a correct Python 3 solution for this coding contest problem. Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size. Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of elements in the list. The second line contains n integers xi (0 ≀ xi ≀ 1 000 000) β€” the ith element of the list. Output In the first line, print a single integer k β€” the size of the subset. In the second line, print k integers β€” the elements of the subset in any order. If there are multiple optimal subsets, print any. Examples Input 4 1 2 3 12 Output 3 1 2 12 Input 4 1 1 2 2 Output 3 1 1 2 Input 2 1 2 Output 2 1 2 Note In the first case, the optimal subset is <image>, which has mean 5, median 2, and simple skewness of 5 - 2 = 3. In the second case, the optimal subset is <image>. Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0.
instruction
0
104,890
12
209,780
Tags: binary search, math, ternary search Correct Solution: ``` from itertools import accumulate from fractions import Fraction n = int(input()) A = [int(x) for x in input().split()] A.sort() B = list(accumulate([0] + A)) def condition(i, z): return (2*z - 1)*(A[i-z] + A[-z]) > 2*(B[i+1] - B[i-z+1] + B[-1] - B[-z]) def average(i, z): return Fraction((B[i+1] - B[i-z] + B[-1] - B[-z-1]) , 2*z + 1) maxans = 0 argmax = (0, 0) for i in range(1, n-1): x, y = 0, min(i, n-1-i) while y - x > 1: z = (x + y)//2 if condition(i, z): x = z else: y = z if condition(i, y): x = y if maxans < average(i, x) - A[i]: maxans = average(i, x) - A[i] argmax = (i, x) print(argmax[1]*2 + 1) for i in range(argmax[0] - argmax[1], argmax[0] + 1): print(A[i], end = ' ') for i in range(-argmax[1], 0): print(A[i], end = ' ') ```
output
1
104,890
12
209,781
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size. Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of elements in the list. The second line contains n integers xi (0 ≀ xi ≀ 1 000 000) β€” the ith element of the list. Output In the first line, print a single integer k β€” the size of the subset. In the second line, print k integers β€” the elements of the subset in any order. If there are multiple optimal subsets, print any. Examples Input 4 1 2 3 12 Output 3 1 2 12 Input 4 1 1 2 2 Output 3 1 1 2 Input 2 1 2 Output 2 1 2 Note In the first case, the optimal subset is <image>, which has mean 5, median 2, and simple skewness of 5 - 2 = 3. In the second case, the optimal subset is <image>. Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0. Submitted Solution: ``` from itertools import accumulate n = int(input()) A = [int(x) for x in input().split()] A.sort() B = list(accumulate([0] + A)) def average(i, k): #average on [i-k; i] U [n-1-k; n-1] return (B[i+1] - B[i-k] + B[-1] - B[-k-1]) / (2*k + 1) maxans = -1 for i in range(n): x, y = 0, min(i, n-1-i) z = (x + y)//2 while y - x > 1: x1 = (x + z)//2 y1 = (z + y)//2 a, b, c = average(i, x1), average(i, z), average(i, y1) if a > b: z, y = x1, z elif c > b: x, z = z, y1 else: x, y = x1, y1 if average(i, x) < average(i, y): x, y = y, x if maxans < average(i, x) - A[i]: maxans = average(i, x) - A[i] argmax = (i, x) print(argmax[1]*2 + 1) for i in range(argmax[0] - argmax[1], argmax[1] + 1): print(A[i], end = ' ') for i in range(- argmax[1], 0): print(A[i], end = ' ') ```
instruction
0
104,891
12
209,782
No
output
1
104,891
12
209,783
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size. Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of elements in the list. The second line contains n integers xi (0 ≀ xi ≀ 1 000 000) β€” the ith element of the list. Output In the first line, print a single integer k β€” the size of the subset. In the second line, print k integers β€” the elements of the subset in any order. If there are multiple optimal subsets, print any. Examples Input 4 1 2 3 12 Output 3 1 2 12 Input 4 1 1 2 2 Output 3 1 1 2 Input 2 1 2 Output 2 1 2 Note In the first case, the optimal subset is <image>, which has mean 5, median 2, and simple skewness of 5 - 2 = 3. In the second case, the optimal subset is <image>. Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0. Submitted Solution: ``` n = int(input()) l = list(map(int, input().split())) arr = sorted(l) j = 0 down = [arr[0]]*n up = [arr[-1]]*n ans = 0 for i in range(1, n): down[i] = down[i-1] + arr[i] up[i] = up[i-1] + arr[n-i-1] for i in range(1, n // 2): kans = (down[i] + up[i-1]) / (2*i+1) - arr[i] if ans < kans: ans = kans j = i print(2*j+1) s = '' for i in range(j+1): s+=str(arr[i])+' ' for i in range(j): s+=str(arr[n-1-i])+' ' print(s) ```
instruction
0
104,892
12
209,784
No
output
1
104,892
12
209,785
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size. Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of elements in the list. The second line contains n integers xi (0 ≀ xi ≀ 1 000 000) β€” the ith element of the list. Output In the first line, print a single integer k β€” the size of the subset. In the second line, print k integers β€” the elements of the subset in any order. If there are multiple optimal subsets, print any. Examples Input 4 1 2 3 12 Output 3 1 2 12 Input 4 1 1 2 2 Output 3 1 1 2 Input 2 1 2 Output 2 1 2 Note In the first case, the optimal subset is <image>, which has mean 5, median 2, and simple skewness of 5 - 2 = 3. In the second case, the optimal subset is <image>. Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0. Submitted Solution: ``` n = int(input()) a = [int(i) for i in input().split()] a.sort() if n <= 2: print(n) print(*a) else: c = 0 ans = [] for i in range(1, n - 1): if ((a[i - 1] + a[i] + a[-1]) / 3) - a[i] > c: c = ((a[i - 1] + a[i] + a[-1]) / 3) - a[i] ans = [a[i - 1], a[i], a[-1]] print(c) print(*ans) ```
instruction
0
104,893
12
209,786
No
output
1
104,893
12
209,787
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size. Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of elements in the list. The second line contains n integers xi (0 ≀ xi ≀ 1 000 000) β€” the ith element of the list. Output In the first line, print a single integer k β€” the size of the subset. In the second line, print k integers β€” the elements of the subset in any order. If there are multiple optimal subsets, print any. Examples Input 4 1 2 3 12 Output 3 1 2 12 Input 4 1 1 2 2 Output 3 1 1 2 Input 2 1 2 Output 2 1 2 Note In the first case, the optimal subset is <image>, which has mean 5, median 2, and simple skewness of 5 - 2 = 3. In the second case, the optimal subset is <image>. Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) if n == 1: print(1) print(a[0]) elif n == 2: print(2) print(a[0], a[1]) else: ans = -10**9 ansa = [] a.sort() for i in range(1, n - 1): if ans < (a[i] + a[i - 1] + a[-1])/3 - a[i]: ans = (a[i] + a[i - 1] + a[-1])/3 - a[i] ansa = [a[i-1], a[i], a[-1]] print(3) print(' '.join(list(map(str, a)))) ```
instruction
0
104,894
12
209,788
No
output
1
104,894
12
209,789
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis. Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row i of the initial table ai, j < ai, k, then in the resulting table a'i, j < a'i, k, and if ai, j = ai, k then a'i, j = a'i, k. Similarly, if in some column j of the initial table ai, j < ap, j then in compressed table a'i, j < a'p, j and if ai, j = ap, j then a'i, j = a'p, j. Because large values require more space to store them, the maximum value in a' should be as small as possible. Petya is good in theory, however, he needs your help to implement the algorithm. Input The first line of the input contains two integers n and m (<image>, the number of rows and the number of columns of the table respectively. Each of the following n rows contain m integers ai, j (1 ≀ ai, j ≀ 109) that are the values in the table. Output Output the compressed table in form of n lines each containing m integers. If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them. Examples Input 2 2 1 2 3 4 Output 1 2 2 3 Input 4 3 20 10 30 50 40 30 50 60 70 90 80 70 Output 2 1 3 5 4 3 5 6 7 9 8 7 Note In the first sample test, despite the fact a1, 2 β‰  a21, they are not located in the same row or column so they may become equal after the compression. Submitted Solution: ``` M, N = map(int, input().split()) matrix = [] for _ in range(M): matrix.append(list(map(int, input().split()))) for m in matrix: print(m) # rank = {i: 0 for i in range(-N, M)} # def find(x): # if uf[x] != x: # uf[x] = find(uf[x]) # return uf[x] # dic = collections.defaultdict(list) # ans = [[0] * N for _ in range(M)] # for i in range(M): # for j in range(N): # dic[matrix[i][j]].append((i, j)) # for d in sorted(dic): # uf = {i: i for i in range(-N, M)} # # rank2 = rank.copy() # for i, j in dic[d]: # pi, pj = find(i), find(~j) # uf[pi] = pj # rank[pj] = max(rank[pi], rank[pj]) # par = {j: rank[find(~j)] for _, j in dic[d]} # for i, j in dic[d]: # rank[i] = rank[~j] = ans[i][j] = par[j] + 1 # print(ans) ```
instruction
0
104,895
12
209,790
No
output
1
104,895
12
209,791