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 an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3.
instruction
0
103,929
12
207,858
Tags: constructive algorithms, sortings Correct Solution: ``` from itertools import combinations, permutations, combinations_with_replacement, product import itertools from timeit import timeit import timeit import time from time import time from random import * import random import collections import bisect import os import math from collections import defaultdict, OrderedDict, Counter from sys import stdin, stdout from bisect import bisect_left, bisect_right # import numpy as np from queue import Queue, PriorityQueue from heapq import * from statistics import * from math import sqrt, log10, log2, log, gcd, ceil, floor import fractions import copy from copy import deepcopy import sys import io import string from string import ascii_lowercase,ascii_uppercase,ascii_letters,digits sys.setrecursionlimit(10000) mod = int(pow(10, 9)) + 7 def ncr(n, r, p=mod): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p def normalncr(n, r): r = min(r, n - r) count = 1 for i in range(n - r, n + 1): count *= i for i in range(1, r + 1): count //= i return count inf = float("inf") adj = defaultdict(set) visited = defaultdict(int) def addedge(a, b): adj[a].add(b) adj[b].add(a) def bfs(v): q = Queue() q.put(v) visited[v] = 1 while q.qsize() > 0: s = q.get_nowait() print(s) for i in adj[s]: if visited[i] == 0: q.put(i) visited[i] = 1 def dfs(v, visited): if visited[v] == 1: return visited[v] = 1 print(v) for i in adj[v]: dfs(i, visited) # a9=pow(10,5)+100 # prime = [True for i in range(a9 + 1)] # def SieveOfEratosthenes(n): # p = 2 # while (p * p <= n): # if (prime[p] == True): # for i in range(p * p, n + 1, p): # prime[i] = False # p += 1 # SieveOfEratosthenes(a9) # prime_number=[] # for i in range(2,a9): # if prime[i]: # prime_number.append(i) def reverse_bisect_right(a, x, lo=0, hi=None): if lo < 0: raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: mid = (lo + hi) // 2 if x > a[mid]: hi = mid else: lo = mid + 1 return lo def reverse_bisect_left(a, x, lo=0, hi=None): if lo < 0: raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: mid = (lo + hi) // 2 if x >= a[mid]: hi = mid else: lo = mid + 1 return lo def get_list(): return list(map(int, input().split())) def make_list(m): m += list(map(int, input().split())) def get_str_list_in_int(): return [int(i) for i in list(input())] def get_str_list(): return list(input()) def get_map(): return map(int, input().split()) def input_int(): return int(input()) def matrix(a, b): return [[0 for i in range(b)] for j in range(a)] def swap(a, b): return b, a def find_gcd(l): a = l[0] for i in range(len(l)): a = gcd(a, l[i]) return a def is_prime(n): sqrta = int(sqrt(n)) for i in range(2, sqrta + 1): if n % i == 0: return 0 return 1 def prime_factors(n): l = [] while n % 2 == 0: l.append(2) n //= 2 sqrta = int(sqrt(n)) for i in range(3, sqrta + 1, 2): while n % i == 0: n //= i l.append(i) if n > 2: l.append(n) return l def p(a): if type(a) == str: print(a + "\n") else: print(str(a) + "\n") def ps(a): if type(a) == str: print(a) else: print(str(a)) def kth_no_not_div_by_n(n, k): return k + (k - 1) // (n - 1) def forward_array(l): """ returns the forward index where the elemetn is just greater than that element [100,200] gives [1,2] because element at index 1 is greater than 100 and nearest similarly if it is largest then it outputs n :param l: :return: """ n = len(l) stack = [] forward = [0] * n for i in range(len(l) - 1, -1, -1): while len(stack) and l[stack[-1]] < l[i]: stack.pop() if len(stack) == 0: forward[i] = len(l) else: forward[i] = stack[-1] stack.append(i) return forward def forward_array_notequal(l): n = len(l) stack = [] forward = [n]*n for i in range(len(l) - 1, -1, -1): while len(stack) and l[stack[-1]] <= l[i]: stack.pop() if len(stack) == 0: forward[i] = len(l) else: forward[i] = stack[-1] stack.append(i) return forward def backward_array(l): n = len(l) stack = [] backward = [0] * n for i in range(len(l)): while len(stack) and l[stack[-1]] < l[i]: stack.pop() if len(stack) == 0: backward[i] = -1 else: backward[i] = stack[-1] stack.append(i) return backward def char(a): return chr(a + 97) def get_length(a): return a.bit_length() def issq(n): sqrta = int(n ** 0.5) return sqrta ** 2 == n def ceil(a, b): return int((a+b-1)/b) def equal_sum_partition(arr, n): sum = 0 for i in range(n): sum += arr[i] if sum % 2 != 0: return False part = [[True for i in range(n + 1)] for j in range(sum // 2 + 1)] for i in range(0, n + 1): part[0][i] = True for i in range(1, sum // 2 + 1): part[i][0] = False for i in range(1, sum // 2 + 1): for j in range(1, n + 1): part[i][j] = part[i][j - 1] if i >= arr[j - 1]: part[i][j] = (part[i][j] or part[i - arr[j - 1]][j - 1]) return part[sum // 2][n] def bin_sum_array(arr, n): for i in range(n): if arr[i] % 2 == 1: return i+1 binarray = [list(reversed(bin(i)[2:])) for i in arr] new_array = [0 for i in range(32)] for i in binarray: for j in range(len(i)): if i[j] == '1': new_array[j] += 1 return new_array def ispalindrome(s): return s == s[::-1] def get_prefix(l): if l == []: return [] prefix = [l[0]] for i in range(1, len(l)): prefix.append(prefix[-1]+l[i]) return prefix def get_suffix(l): if l == []: return [] suffix = [l[-1]]*len(l) for i in range(len(l)-2, -1, -1): suffix[i] = suffix[i+1]+l[i] return suffix nc = "NO" yc = "YES" ns = "No" ys = "Yes" def yesno(a): print(yc if a else nc) def reduce(dict, a): dict[a] -= 1 if dict[a] == 0: dict.pop(a) # import math as mt # MAXN=10**7 # spf = [0 for i in range(MAXN)] # def sieve(): # spf[1] = 1 # for i in range(2, MAXN): # # marking smallest prime factor # # for every number to be itself. # spf[i] = i # # # separately marking spf for # # every even number as 2 # for i in range(4, MAXN, 2): # spf[i] = 2 # # for i in range(3, mt.ceil(mt.sqrt(MAXN))): # # # checking if i is prime # if (spf[i] == i): # # # marking SPF for all numbers # # divisible by i # for j in range(i * i, MAXN, i): # # # marking spf[j] if it is # # not previously marked # if (spf[j] == j): # spf[j] = i # def getFactorization(x): # ret = list() # while (x != 1): # ret.append(spf[x]) # x = x // spf[x] # # return ret # sieve() # if(os.path.exists('input.txt')): # sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w") import sys import io from sys import stdin, stdout # input=sys.stdin.readline input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # print=sys.stdout.write t = 1 t=int(input()) for i in range(t): n=int(input()) l=get_list() l.sort() arr=[0]*2*n; counter=0; for i in range(0,2*n,2): arr[i]=l[counter] counter+=1 for i in range(1,2*n,2): arr[i] = l[counter] counter += 1 # print(*arr) print(" ".join(map(str,arr))+"\n") ```
output
1
103,929
12
207,859
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3.
instruction
0
103,930
12
207,860
Tags: constructive algorithms, sortings Correct Solution: ``` def solve(a, n): i = 0 while i < n: a[i], a[2*n - i - 1] = a[2*n - i - 1], a[i] i += 2 return a for _ in range(int(input())): n = int(input()) a = sorted(list(map(int, input().split()))) print(*solve(a, n)) ```
output
1
103,930
12
207,861
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3. Submitted Solution: ``` for _ in range(int(input())): N=int(input()) A=[int(i) for i in input().split()] A.sort() for i in range(N): print(A[i],A[N+i],end=" ") print() ```
instruction
0
103,931
12
207,862
Yes
output
1
103,931
12
207,863
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3. Submitted Solution: ``` for _ in range(int(input())): n=int(input()) arr=list(map(int, input().split())) arr.sort() if n==1: print(" ".join(map(str, arr))) continue arr_a = arr[n:] final=[] for x in range(n): final.extend([arr[x],arr_a[x]]) print(" ".join(map(str, final))) ```
instruction
0
103,932
12
207,864
Yes
output
1
103,932
12
207,865
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3. Submitted Solution: ``` for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr.sort() while(arr): print(arr[0], end=' ') print(arr[-1], end=' ') arr.pop() arr.pop(0) print() ```
instruction
0
103,933
12
207,866
Yes
output
1
103,933
12
207,867
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3. Submitted Solution: ``` for _ in range(int(input())): n=int(input()) arr=list(map(int,input().split())) arr.sort() pt2=n pt1=0 for i in range(n): print(arr[pt1],arr[pt2],end=' ') pt1+=1 pt2+=1 print() ```
instruction
0
103,934
12
207,868
Yes
output
1
103,934
12
207,869
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3. Submitted Solution: ``` t=int(input()) while t: n=int(input()) l=list(sorted(map(int,input().split()))) if n==1: print(l) else: for i in range(n-1): l[2*i+1],l[2*i+2]=l[2*i+2],l[2*i+1] print(l) t-=1 ```
instruction
0
103,935
12
207,870
No
output
1
103,935
12
207,871
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3. Submitted Solution: ``` from sys import stdin input=stdin.readline from math import * # map(int,input().split()) for _ in range(int(input())): n=int(input()) n*=2 a=list(map(int,input().split())) a.sort() for i in range(2,n//2+1,2): a[i],a[n+1-i]=a[n+1-i],a[i] print(*a) ```
instruction
0
103,936
12
207,872
No
output
1
103,936
12
207,873
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3. Submitted Solution: ``` s = [] for i in range(int(input())): n = int(input()) a = list(map(int, input().split()[:2*n])) a.sort() if len(a) == 1 or len(a) == 2: s.append(a) elif len(a) == 4: a[2], a[1] = a[1], a[2] s.append(a) else: k = 0 while k+2 < n: a[k+2], a[2*n-1-k] = a[2*n-1-k], a[k+2] k += 2 s.append(a) for i in range(len(s)): if i > 0: print('\n', end = '') for j in range(len(s[i])): print(s[i][j], ' ', end = '', sep = '') ```
instruction
0
103,937
12
207,874
No
output
1
103,937
12
207,875
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of 2n distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its 2 neighbours. More formally, find an array b, such that: * b is a permutation of a. * For every i from 1 to 2n, b_i β‰  \frac{b_{i-1}+b_{i+1}}{2}, where b_0 = b_{2n} and b_{2n+1} = b_1. It can be proved that under the constraints of this problem, such array b always exists. Input The first line of input contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer n (1 ≀ n ≀ 25). The second line of each testcase contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of the array. Note that there is no limit to the sum of n over all testcases. Output For each testcase, you should output 2n integers, b_1, b_2, … b_{2n}, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array [3, 1, 4, 2, 5, 6] works, as it's a permutation of [1, 2, 3, 4, 5, 6], and (3+4)/(2)β‰  1, (1+2)/(2)β‰  4, (4+5)/(2)β‰  2, (2+6)/(2)β‰  5, (5+3)/(2)β‰  6, (6+1)/(2)β‰  3. Submitted Solution: ``` t = int(input()) for _ in range(t): n = int(input()) a = list(map(int,input().split())) if 2*n<3: a.reverse() else: for i in range(2*n+1): if i==2*n: if (a[i-1]+a[1])//2 == a[0]: a[i-1],a[1] = a[1],a[i-1] elif i==2*n-1: if (a[i-1]+a[0])//2 == a[i]: a[i-1],a[i] = a[i],a[i-1] else: if (a[i-1]+a[i+1])//2 == a[i]: a[i-1],a[i] = a[i],a[i-1] print(*a) ```
instruction
0
103,938
12
207,876
No
output
1
103,938
12
207,877
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2. Your program should handle the queries of the following two types: 1. "1 x y" (x < y) β€” add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. 2. "2 a b" (a β‰  b) β€” answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval? Answer all the queries. Note, that initially you have an empty set of intervals. Input The first line of the input contains integer n denoting the number of queries, (1 ≀ n ≀ 105). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value. It's guaranteed that all queries are correct. Output For each query of the second type print "YES" or "NO" on a separate line depending on the answer. Examples Input 5 1 1 5 1 5 11 2 1 2 1 2 9 2 1 2 Output NO YES
instruction
0
103,983
12
207,966
Tags: data structures Correct Solution: ``` interval = [] nodes = {} n = int(input()) def func(x, y): a, b = x c, d = y return(c < a < d or c < b < d) for i in range(0, n): t, x, y = map(int, input().split()) if(t == 1): #aΓ±adir intervalo xy = (x, y) interval.append(xy) nodes[xy] = [] for i in interval: if func(i, xy): nodes[i].append(xy) if func(xy, i): nodes[xy].append(i) if(t == 2): #revisar si hay path de x a y x -= 1 y -= 1 a = interval[x] b = interval[y] visited = {a} lst = [a] while len(lst) > 0: i = lst[0] lst = lst[1:] for j in nodes[i]: if not j in visited: lst.append(j) visited = visited | {j} if b in visited: print("YES") else: print("NO") ```
output
1
103,983
12
207,967
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2. Your program should handle the queries of the following two types: 1. "1 x y" (x < y) β€” add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. 2. "2 a b" (a β‰  b) β€” answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval? Answer all the queries. Note, that initially you have an empty set of intervals. Input The first line of the input contains integer n denoting the number of queries, (1 ≀ n ≀ 105). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value. It's guaranteed that all queries are correct. Output For each query of the second type print "YES" or "NO" on a separate line depending on the answer. Examples Input 5 1 1 5 1 5 11 2 1 2 1 2 9 2 1 2 Output NO YES
instruction
0
103,985
12
207,970
Tags: data structures Correct Solution: ``` def to_list(s): return list(map(int, s.split(" "))) def query(q, ranges): start_idx = q[1] - 1 end_idx = q[2] - 1 queue = [start_idx] visited = {} while len(queue) > 0: r = queue.pop(0) visited[r] = True for i in range(0, len(ranges)): if not visited.get(i, False): r_lb = ranges[r][1] r_ub = ranges[r][2] c_lb = ranges[i][1] c_ub = ranges[i][2] if (r_lb < c_ub and r_lb > c_lb) or (r_ub > c_lb and r_ub < c_ub): #print(r_lb, r_ub, c_lb, c_ub) if i == end_idx: print("YES") return else: queue.append(i) print("NO") n = int(input()) ranges = [] queries = [] for _ in range(0, n): request = to_list(input()) if request[0] == 1: ranges.append(request) elif request[0] == 2: query(request, ranges) ```
output
1
103,985
12
207,971
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2. Your program should handle the queries of the following two types: 1. "1 x y" (x < y) β€” add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. 2. "2 a b" (a β‰  b) β€” answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval? Answer all the queries. Note, that initially you have an empty set of intervals. Input The first line of the input contains integer n denoting the number of queries, (1 ≀ n ≀ 105). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value. It's guaranteed that all queries are correct. Output For each query of the second type print "YES" or "NO" on a separate line depending on the answer. Examples Input 5 1 1 5 1 5 11 2 1 2 1 2 9 2 1 2 Output NO YES
instruction
0
103,986
12
207,972
Tags: data structures Correct Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections 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") 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() def bfs(adj,s,k): vis={i:False for i in adj} vis[s]=True qu=[s] while(len(qu)!=0): e=qu.pop(0) if(e==k): return True for i in adj[e]: if(vis[i]!=True): qu.append(i) vis[i]=True return False adj={} inter=[] for _ in range(Int()): q,a,b=value() if(q==1): adj[(a,b)]=[] for i in inter: x=i[0] y=i[1] if(a>x and a<y) or (b>x and b<y): adj[(a,b)].append(i) if(x>a and x<b) or (y>a and y<b): adj[i].append((a,b)) inter.append((a,b)) elif(bfs(adj,inter[a-1],inter[b-1])):print("YES") else:print("NO") ```
output
1
103,986
12
207,973
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2. Your program should handle the queries of the following two types: 1. "1 x y" (x < y) β€” add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. 2. "2 a b" (a β‰  b) β€” answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval? Answer all the queries. Note, that initially you have an empty set of intervals. Input The first line of the input contains integer n denoting the number of queries, (1 ≀ n ≀ 105). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value. It's guaranteed that all queries are correct. Output For each query of the second type print "YES" or "NO" on a separate line depending on the answer. Examples Input 5 1 1 5 1 5 11 2 1 2 1 2 9 2 1 2 Output NO YES
instruction
0
103,987
12
207,974
Tags: data structures Correct Solution: ``` """ Template written to be used by Python Programmers. Use at your own risk!!!! Owned by enraged(rating - 5 star at CodeChef and Specialist at Codeforces). """ import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log from collections import defaultdict as dd, deque, Counter as c from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect # sys.setrecursionlimit(2*pow(10, 6)) # sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") mod = pow(10, 9) + 7 mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(var): sys.stdout.write(str(var)) def outln(var): sys.stdout.write(str(var)+"\n") def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] n = int(data()) intervals = dd(list) graph = dd(set) i = 1 for _ in range(n): a, b, c = sp() intervals[i] = [b, c] if a == 1: for j in intervals.keys(): u, v = intervals[j] if u < b < v and u < c < v: graph[i].add(j) elif b < u < c and b < v < c: graph[j].add(i) elif u < b < v or u < c < v or b < u < c or b < v < c: graph[i].add(j) graph[j].add(i) i += 1 else: d = deque() d.append(b) vis = [0] * 101 vis[b] = 1 while d: temp = d.popleft() if temp == c: outln("YES") break for child in graph[temp]: if not vis[child]: d.append(child) vis[child] = 1 else: outln("NO") ```
output
1
103,987
12
207,975
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2
instruction
0
103,998
12
207,996
Tags: binary search, data structures Correct Solution: ``` from collections import Counter n, m, p = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) def try_from(start, a, b): result = [] seq = a[start::p] i = 0 if len(seq) < len(b): return [] counts_a = Counter(seq[:len(b)]) counts_b = Counter(b) if counts_a == counts_b: result.append(start) for i in range(1, len(seq) - len(b) + 1): counts_a[seq[i-1]] -= 1 counts_a[seq[i+len(b) - 1]] += 1 if not counts_a[seq[i-1]]: del counts_a[seq[i-1]] ok = counts_a == counts_b #ok = sorted(seq[i:i+len(b)]) == sorted(b) if ok: result.append(start + p * i) return [x + 1 for x in result] result = [] for start in range(p): result += try_from(start, a, b) print(len(result)) print(' '.join(map(str, sorted(result)))) ```
output
1
103,998
12
207,997
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2
instruction
0
103,999
12
207,998
Tags: binary search, data structures Correct Solution: ``` from collections import Counter n, m, p = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) def try_from(start, a, b): result = [] seq = a[start::p] i = 0 if len(seq) < len(b): return [] counts_a = Counter(seq[:len(b)]) counts_b = Counter(b) if counts_a == counts_b: result.append(start) for i in range(1, len(seq) - len(b) + 1): counts_a[seq[i-1]] -= 1 counts_a[seq[i+len(b) - 1]] += 1 if not counts_a[seq[i-1]]: del counts_a[seq[i-1]] ok = counts_a == counts_b #ok = sorted(seq[i:i+len(b)]) == sorted(b) if ok: result.append(start + p * i) return [x + 1 for x in result] result = [] for start in range(p): result += try_from(start, a, b) print(len(result)) print(' '.join(map(str, sorted(result)))) # Made By Mostafa_Khaled ```
output
1
103,999
12
207,999
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2
instruction
0
104,000
12
208,000
Tags: binary search, data structures Correct Solution: ``` from sys import stdin, stdout from math import log, sqrt lines = stdin.readlines() n = int(lines[0].split()[0]) m = int(lines[0].split()[1]) p = int(lines[0].split()[2]) a = [int(x) for x in lines[1].split()] b = [int(x) for x in lines[2].split()] hash_map = {} def hash_elem(elem): if hash_map.get(elem, -1) == -1: # elem = int(elem * 1662634645) # elem = int((elem >> 13) + (elem << 19)) # hash_map[elem] = int(elem * 361352451) # hash_map[elem] = int(342153534 + elem + (elem >> 5) + (elem >> 13) + (elem << 17)) if elem < 1000: hash_map[elem] = elem//2 + elem * 1134234546677 + int(elem/3) + int(elem**2) + elem<<2 + elem>>7 + int(log(elem)) + int(sqrt(elem)) + elem&213213 + elem^324234211323 + elem|21319423094023 else: hash_map[elem] = 3 + elem^34 return elem + hash_map[elem] c = [hash_elem(elem) for elem in a] c_new = [sum([c[q + p * i] for i in range(m)]) for q in range(min(p, max(0, n - (m - 1) * p)))] for q in range(p, n - (m - 1) * p): prev = c_new[q - p] # print(len(c_new)-1, q, q + p*(m-1)) c_new.append(prev - c[q - p] + c[q + p * (m - 1)]) b_check = sum([hash_elem(elem) for elem in b]) ans1 = 0 ans = [] for q in range(n - (m - 1) * p): c_check = c_new[q] if b_check != c_check: continue else: ans1 += 1 ans.append(q + 1) print(ans1) stdout.write(' '.join([str(x) for x in ans])) ```
output
1
104,000
12
208,001
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2
instruction
0
104,001
12
208,002
Tags: binary search, data structures Correct Solution: ``` from sys import stdin, stdout from math import log, sqrt lines = stdin.readlines() n = int(lines[0].split()[0]) m = int(lines[0].split()[1]) p = int(lines[0].split()[2]) a = [int(x) for x in lines[1].split()] b = [int(x) for x in lines[2].split()] hash_map = {} def hash_elem(elem): if hash_map.get(elem, -1) == -1: if elem < 1000: hash_map[elem] = elem//2 + elem * 1134234546677 + int(elem/3) + int(elem**2) + elem<<2 + elem>>7 + int(log(elem)) + int(sqrt(elem)) + elem&213213 + elem^324234211323 + elem|21319423094023 else: hash_map[elem] = 3 + elem^34# elem<<2 + elem>>7 + elem&243 + elem^324 + elem|434 return elem + hash_map[elem] c = [hash_elem(elem) for elem in a] c_new = [sum([c[q + p*i] for i in range(m)]) for q in range(min(p, max(0,n - (m-1)*p)))] for q in range(p,n - (m-1)*p): prev = c_new[q - p] c_new.append(prev - c[q - p] + c[q + p*(m-1)]) b_check = sum([hash_elem(elem) for elem in b]) ans1 = 0 ans = [] for q in range(n - (m-1)*p): c_check = c_new[q] if b_check != c_check: continue else: ans1 += 1 ans.append(q+1) print(ans1) stdout.write(' '.join([str(x) for x in ans])) ```
output
1
104,001
12
208,003
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2
instruction
0
104,002
12
208,004
Tags: binary search, data structures Correct Solution: ``` # import sys, io, os # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline from collections import defaultdict,Counter def solve(n,m,p,a,b): ans=[] cb=Counter(b) for q in range(min(p,n-(m-1)*p)): arr = [a[i] for i in range(q,n,p)] lb=len(cb.keys()) cnt=cb.copy() # print(cnt) # print(arr) for v in arr[:m]: cnt[v]-=1 if cnt[v]==0: lb-=1 elif cnt[v]==-1: lb+=1 j=0 if lb==0: ans.append(q+1) for i,v in enumerate(arr[m:],m): cnt[v]-=1 if cnt[v]==0: lb-=1 elif cnt[v]==-1: lb+=1 cnt[arr[j]]+=1 if cnt[arr[j]]==0: lb-=1 elif cnt[arr[j]]==1: lb+=1 j+=1 # print(cnt,lb,j,q) if lb==0: ans.append(q+j*p+1) print(len(ans)) print(*sorted(ans)) n,m,p=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) solve(n,m,p,a,b) ```
output
1
104,002
12
208,005
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2
instruction
0
104,003
12
208,006
Tags: binary search, data structures Correct Solution: ``` from sys import stdin, stdout from math import log, sqrt lines = stdin.readlines() n = int(lines[0].split()[0]) m = int(lines[0].split()[1]) p = int(lines[0].split()[2]) a = [int(x) for x in lines[1].split()] b = [int(x) for x in lines[2].split()] def hash_elem(x): x = (x * 1662634645 + 32544235) & 0xffffffff x = ((x >> 13) + (x << 19)) & 0xffffffff x = (x * 361352451) & 0xffffffff return x c = [hash_elem(elem) for elem in a] c_new = [sum([c[q + p * i] for i in range(m)]) for q in range(min(p, max(0, n - (m - 1) * p)))] for q in range(p, n - (m - 1) * p): prev = c_new[q - p] # print(len(c_new)-1, q, q + p*(m-1)) c_new.append(prev - c[q - p] + c[q + p * (m - 1)]) b_check = sum([hash_elem(elem) for elem in b]) ans1 = 0 ans = [] for q in range(n - (m - 1) * p): c_check = c_new[q] if b_check != c_check: continue else: ans1 += 1 ans.append(q + 1) print(ans1) stdout.write(' '.join([str(x) for x in ans])) ```
output
1
104,003
12
208,007
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2
instruction
0
104,004
12
208,008
Tags: binary search, data structures Correct Solution: ``` # import sys, io, os # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline from collections import Counter def solve(n,m,p,a,b): ans=[] cb=Counter(b) for q in range(min(p,n-(m-1)*p)): arr = [a[i] for i in range(q,n,p)] lb=len(cb.keys()) cnt=cb.copy() for v in arr[:m]: cnt[v]-=1 if cnt[v]==0: lb-=1 elif cnt[v]==-1: lb+=1 j=0 if lb==0: ans.append(q+1) for v in arr[m:]: cnt[v]-=1 if cnt[v]==0: lb-=1 elif cnt[v]==-1: lb+=1 cnt[arr[j]]+=1 if cnt[arr[j]]==0: lb-=1 elif cnt[arr[j]]==1: lb+=1 j+=1 if lb==0: ans.append(q+j*p+1) print(len(ans)) print(*sorted(ans)) n,m,p=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) solve(n,m,p,a,b) ```
output
1
104,004
12
208,009
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2
instruction
0
104,005
12
208,010
Tags: binary search, data structures Correct Solution: ``` from collections import defaultdict n, m, p = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) u = defaultdict(int) for i in b: u[i] += 1 ans = [] for q in range(p): c = a[q: n: p] if len(c) < m: break v = defaultdict(int) for i in c[: m]: v[i] += 1 d = q + 1 if u == v: ans.append(d) for j, k in zip(c[: len(c) - m], c[m: ]): v[j] -= 1 if v[j] == 0: v.pop(j) v[k] += 1 d += p if u == v: ans.append(d) ans.sort() print(len(ans)) print(' '.join(map(str, ans))) ```
output
1
104,005
12
208,011
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2 Submitted Solution: ``` from sys import stdin, stdout lines = stdin.readlines() n = int(lines[0].split()[0]) m = int(lines[0].split()[1]) p = int(lines[0].split()[2]) a = [int(x) for x in lines[1].split()] b = sorted([int(x) for x in lines[2].split()]) def hash_sum(a): ret = 0 for elem in a: ret += elem + elem*117 + elem//3 + elem**(1/2) return ret b_check = hash_sum(b) ans1 = 0 ans = [] for q in range(n - (m-1)*p): cand = [a[q + p*i] for i in range(m)] c_check = hash_sum(cand) if b_check != c_check: continue else: ans1 += 1 ans.append(q+1) print(ans1) stdout.write(' '.join([str(x) for x in ans])) ```
instruction
0
104,008
12
208,016
No
output
1
104,008
12
208,017
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)Β·p ≀ n; q β‰₯ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of q. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 2Β·105, 1 ≀ p ≀ 2Β·105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109). The next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109). Output In the first line print the number of valid qs. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2 Submitted Solution: ``` class pa: pass n,m,p = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) b_dict = {b[i]:i for i in range(m)} a_split_p = [0]*p for i_list in range(p): a_split_p[i_list] = list() for i_a in range(i_list,n,p): newa = pa() newa.key = a[i_a] newa.nom = i_a a_split_p[i_list].append(newa) cnt = [0]*m for i in range(m): cnt[b_dict.get(b[i])] += 1 ans_cnt = 0 ans_array = list() for i_list in range(p): l = 0 use = [0]*m good = 0 for num, i_a in enumerate(a_split_p[i_list]): if not (i_a.key in b_dict): continue num_b_i = b_dict.get(i_a.key) use[num_b_i] +=1 if use[num_b_i] == cnt[num_b_i]: good += 1 if (num-l+1>m): num_b_l = b_dict.get(a_split_p[i_list][l].key) if (use[num_b_l]==cnt[num_b_l]): good -=1 use[num_b_l] -=1 l +=1 if good==m: ans_cnt += 1 ans_array.append(a_split_p[i_list][l].nom) #print("{} {}".format(use,good)) #print("===") print (ans_cnt) ans = "" for i in ans_array: ans = ans + "{} ".format(i+1) print(ans) ```
instruction
0
104,009
12
208,018
No
output
1
104,009
12
208,019
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], ..., a[n]. Then he should perform a sequence of m operations. An operation can be one of the following: 1. Print operation l, r. Picks should write down the value of <image>. 2. Modulo operation l, r, x. Picks should perform assignment a[i] = a[i] mod x for each i (l ≀ i ≀ r). 3. Set operation k, x. Picks should set the value of a[k] to x (in other words perform an assignment a[k] = x). Can you help Picks to perform the whole sequence of operations? Input The first line of input contains two integer: n, m (1 ≀ n, m ≀ 105). The second line contains n integers, separated by space: a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ 109) β€” initial value of array elements. Each of the next m lines begins with a number type <image>. * If type = 1, there will be two integers more in the line: l, r (1 ≀ l ≀ r ≀ n), which correspond the operation 1. * If type = 2, there will be three integers more in the line: l, r, x (1 ≀ l ≀ r ≀ n; 1 ≀ x ≀ 109), which correspond the operation 2. * If type = 3, there will be two integers more in the line: k, x (1 ≀ k ≀ n; 1 ≀ x ≀ 109), which correspond the operation 3. Output For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer. Examples Input 5 5 1 2 3 4 5 2 3 5 4 3 3 5 1 2 5 2 1 3 3 1 1 3 Output 8 5 Input 10 10 6 9 6 7 6 1 10 10 9 5 1 3 9 2 7 10 9 2 5 10 8 1 4 7 3 3 7 2 7 9 9 1 2 4 1 6 6 1 5 9 3 1 10 Output 49 15 23 1 9 Note Consider the first testcase: * At first, a = {1, 2, 3, 4, 5}. * After operation 1, a = {1, 2, 3, 0, 1}. * After operation 2, a = {1, 2, 5, 0, 1}. * At operation 3, 2 + 5 + 0 + 1 = 8. * After operation 4, a = {1, 2, 2, 0, 1}. * At operation 5, 1 + 2 + 2 = 5. Submitted Solution: ``` def get_sum(v, v1, vr, l_, r): if r <= v1 or vr <= l_: return 0 if l_ <= v1 and vr <= r: return sums[v] vm = (v1 + vr) // 2 return get_sum(v * 2, v1, vm, l_, r) + get_sum(v * 2 + 1, vm, vr, l_, r) def do_mod(v, v1, vr, l_, r, x): if r <= v1 or vr <= l_: return if maxes[v] < x: return if v >= n1: sums[v] = maxes[v] = sums[v] % x return vm = (v1 + vr) // 2 va = v * 2 vb = va + 1 do_mod(va, v1, vm, l_, r, x) do_mod(vb, vm, vr, l_, r, x) sums[v] = sums[va] + sums[vb] maxes[v] = max(maxes[va], maxes[vb]) def main(): n1m1 = n1 - 1 for i in range(n + n1, 1, -1): sums[i // 2] += sums[i] maxes[i // 2] = max(maxes[i // 2], maxes[i]) for _ in range(m): typ, data = input().split(maxsplit=1) if typ == '1': l_, r = map(int, data.split()) print(get_sum(1, 0, n1, l_ - 1, r)) elif typ == '2': l_, r, x = map(int, data.split()) do_mod(1, 0, n1, l_ - 1, r, x) elif typ == '3': k, x = map(int, data.split()) v = n1m1 + k x1 = x - sums[v] while v: sums[v] += x1 v //= 2 maxes[v] = max(maxes[v * 2], maxes[v * 2 + 1]) n, m = map(int, input().split()) n1 = 2 ** n.bit_length() while n1 > n: n1 //= 2 while n1 < n: n1 *= 2 sums = [0] * n1 sums.extend(map(int, input().split())) sums.append(0) maxes = sums[:] main() ```
instruction
0
104,015
12
208,030
No
output
1
104,015
12
208,031
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one β€” from 1 to 2, the third one β€” from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≀ q ≀ 500) β€” the number of queries. The i-th of the following q lines contains one integer k_i (1 ≀ k_i ≀ 10^9) β€” the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≀ x_i ≀ 9) β€” the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 Note Answers on queries from the first example are described in the problem statement.
instruction
0
104,503
12
209,006
Tags: binary search, brute force, math Correct Solution: ``` import sys input = sys.stdin.readline # 1keta : 1,2,3,4,5,6,7,8,9 : 45 # 2keta : 11,13,15,... # 9 : 9 , sum = 45 # 99 : 9+(2*90) = 189, sum =((9+2)+(9+2*90))*90//2 +45 = 9045 LIST=[9] for i in range(1,20): LIST.append(LIST[-1]+9*(10**i)*(i+1)) SUM=[45] for i in range(1,19): SUM.append(SUM[-1]+(LIST[i-1]+(i+1)+LIST[i])*9*(10**i)//2) def calc(x): True SUM=[0]+SUM LIST=[0]+LIST LIST2=[0] for i in range(1,20): LIST2.append(10**i-1) q=int(input()) #q=1000 for testcases in range(q): k=int(input()) #k=testcases+1 for i in range(20): if SUM[i]>=k: keta=i break #print(keta) k-=SUM[keta-1] INI=LIST[keta-1]+keta #print(k,INI) OK=0 NG=10**keta while NG>OK+1: mid=(OK+NG)//2 if (INI + INI + keta *(mid - 1)) * mid //2 >= k: NG=mid else: OK=mid k-=(INI + INI + keta *(OK - 1)) * OK //2 #print(k,OK,10**(keta-1)+OK) for i in range(20): if LIST[i]>=k: keta2=i k-=LIST[i-1] break #print(k,keta2) r,q=divmod(k,keta2) #print("!",r,q) if q==0: print(str(LIST2[keta2-1]+r)[-1]) else: print(str(LIST2[keta2-1]+r+1)[q-1]) ```
output
1
104,503
12
209,007
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one β€” from 1 to 2, the third one β€” from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≀ q ≀ 500) β€” the number of queries. The i-th of the following q lines contains one integer k_i (1 ≀ k_i ≀ 10^9) β€” the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≀ x_i ≀ 9) β€” the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 Note Answers on queries from the first example are described in the problem statement.
instruction
0
104,504
12
209,008
Tags: binary search, brute force, math Correct Solution: ``` def digits_until_block_(n): result = 0 for bas in range(1, 30): minimum = int(10 ** (bas - 1)) maximum = int((10 ** bas) - 1) if n < maximum: maximum = n if maximum < minimum: break result += sum_between(n - maximum + 1, n - minimum + 1) * bas return result def digits_until_(n): if n == 0: return 0 if n == 1: return 1 return digits_until_block_(n) - digits_until_block_(n - 1) def sum_between(x, y): try: assert (x <= y) except AssertionError: print(x, y) return ((x + y) * (y - x + 1)) // 2 def solve(q): left = 1 right = 10000000000 while left < right: mid = (left + right) // 2 if digits_until_block_(mid) < q: left = mid + 1 else: right = mid q = q - digits_until_block_(left - 1) left = 1 right = 10000000000 while left < right: mid = (left + right) // 2 if digits_until_(mid) < q: left = mid + 1 else: right = mid q = q - digits_until_(left - 1) return str(left)[q - 1] q = int(input("")) q_list = [] for _ in range(q): q_list.append(int(input(""))) for query in q_list: print(solve(query)) ```
output
1
104,504
12
209,009
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one β€” from 1 to 2, the third one β€” from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≀ q ≀ 500) β€” the number of queries. The i-th of the following q lines contains one integer k_i (1 ≀ k_i ≀ 10^9) β€” the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≀ x_i ≀ 9) β€” the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 Note Answers on queries from the first example are described in the problem statement.
instruction
0
104,505
12
209,010
Tags: binary search, brute force, math Correct Solution: ``` def cached(func): _cache = {} def wrapped(*args): nonlocal _cache if args not in _cache: _cache[args] = func(*args) return _cache[args] return wrapped def len_num(l): """ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ чисСл Π΄Π»ΠΈΠ½Ρ‹ l""" return 10**l - 10**(l - 1) if l > 0 else 0 @cached def len_sum(l): """Π‘ΡƒΠΌΠΌΠ° Π΄Π»ΠΈΠ½ всСх чисСл, Π΄Π»ΠΈΠ½Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… строго мСньшС Ρ‡Π΅ΠΌ l""" if l <= 1: return 0 return len_sum(l - 1) + (len_num(l - 1)) * (l - 1) def block_len(block_num): """Π”Π»ΠΈΠ½Π° Π±Π»ΠΎΠΊΠ° (Ρ‚. Π΅. строки '1234567891011...str(block_num)'""" l = len(str(block_num)) return len_sum(l) + (block_num - 10 ** (l - 1) + 1) * l def arith_sum(n): return n * (n + 1) // 2 @cached def block_len_sum_(l): """Буммарная Π΄Π»ΠΈΠ½Π° всСх Π±Π»ΠΎΠΊΠΎΠ² Π΄Π»ΠΈΠ½Ρ‹ мСньшСй Ρ‡Π΅ΠΌ l""" if l <= 0: return 0 ln = len_num(l - 1) ls = len_sum(l - 1) return block_len_sum_(l - 1) + ls * ln + arith_sum(ln) * (l - 1) def block_len_sum(block_num): """Буммарная Π΄Π»ΠΈΠ½Π° всСх Π±Π»ΠΎΠΊΠΎΠ² подряд Π²ΠΏΠ»ΠΎΡ‚ΡŒ Π΄ΠΎ Π±Π»ΠΎΠΊΠ° block_num Если l = len(str(block_num)) """ l = len(str(block_num)) ls = len_sum(l) ln = block_num - (10 ** (l - 1)) + 1 return block_len_sum_(l) + ls * ln + l * arith_sum(ln) def binary_search(call, val): start = 1 end = 1 while call(end) <= val: end *= 2 result = start while start <= end: mid = (start + end) // 2 if call(mid) <= val: start = mid + 1 result = start else: end = mid - 1 return result cases = int(input()) for _ in range(cases): index = int(input()) - 1 block_num = binary_search(block_len_sum, index) rel_index = index - block_len_sum(block_num - 1) number = binary_search(block_len, rel_index) digit = rel_index - block_len(number - 1) print(str(number)[digit]) ```
output
1
104,505
12
209,011
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one β€” from 1 to 2, the third one β€” from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≀ q ≀ 500) β€” the number of queries. The i-th of the following q lines contains one integer k_i (1 ≀ k_i ≀ 10^9) β€” the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≀ x_i ≀ 9) β€” the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 Note Answers on queries from the first example are described in the problem statement.
instruction
0
104,506
12
209,012
Tags: binary search, brute force, math Correct Solution: ``` import bisect s = [0] # индСкс ΠΏΠ΅Ρ€Π²ΠΎΠΉ Ρ†ΠΈΡ„Ρ€Ρ‹ Π±Π»ΠΎΠΊΠ° n = [1] # Π΄Π»ΠΈΠ½Π° Π±Π»ΠΎΠΊΠ° while s[-1] < 1.1 * 10e9: number = len(s) + 1 n.append(n[number - 1 - 1] + len(str(number))) s.append(s[number - 1 - 1] + n[number - 1 - 1]) block = ''.join(str(i) for i in range(1, len(s) + 2)) def solve(element): block_num = bisect.bisect_left(s, element) if s[block_num] > element: block_num -= 1 relative_index = element - s[block_num] return block[relative_index] q = int(input()) for _ in range(q): print(solve(int(input()) - 1)) ```
output
1
104,506
12
209,013
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one β€” from 1 to 2, the third one β€” from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≀ q ≀ 500) β€” the number of queries. The i-th of the following q lines contains one integer k_i (1 ≀ k_i ≀ 10^9) β€” the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≀ x_i ≀ 9) β€” the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 Note Answers on queries from the first example are described in the problem statement.
instruction
0
104,507
12
209,014
Tags: binary search, brute force, math Correct Solution: ``` def l(n): # length of 112123...1234567891011..n s = 0 for i in range(20): o = 10**i-1 if o > n: break s += (n-o) * (n-o+1) // 2 return s def bs(k): # binary search n so l(n) < k n = 0 for p in range(63,-1,-1): if l(n+2**p) < k: n += 2**p return n def num(n): # return s[n-1] where s = '1234567891011..n' if n<10: return n for i in range(1,19): seglen = i * 9 * 10**(i-1) if n <= seglen: return str(10**(i-1) + (n-1)//i)[(n-1)%i] else: n -= seglen q = int(input()) for _ in range(q): k = int(input()) print(num(k-l(bs(k)))) ```
output
1
104,507
12
209,015
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one β€” from 1 to 2, the third one β€” from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≀ q ≀ 500) β€” the number of queries. The i-th of the following q lines contains one integer k_i (1 ≀ k_i ≀ 10^9) β€” the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≀ x_i ≀ 9) β€” the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 Note Answers on queries from the first example are described in the problem statement.
instruction
0
104,508
12
209,016
Tags: binary search, brute force, math Correct Solution: ``` q=int(input()) a="" for i in range(1,100000): a+=str(i) b=[] ans=0 n=1 i=1 while ans<10**9: ans+=n n+=len(str(i+1)) i+=1 b.append(ans) #print(b) while q: k=int(input()) for i in range(len(b)): if(k>b[i]): x=1 else: if(i>=1): k-=b[i-1] break print(a[k-1]) q-=1 ```
output
1
104,508
12
209,017
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one β€” from 1 to 2, the third one β€” from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≀ q ≀ 500) β€” the number of queries. The i-th of the following q lines contains one integer k_i (1 ≀ k_i ≀ 10^9) β€” the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≀ x_i ≀ 9) β€” the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 Note Answers on queries from the first example are described in the problem statement.
instruction
0
104,510
12
209,020
Tags: binary search, brute force, math Correct Solution: ``` dp, cnt = [0], 1 dp2 = [0] while dp[-1] <= int(1e18): ans = dp2[-1] + (10 ** cnt - 10 ** (cnt - 1)) * cnt dp2.append(ans) ans = dp[-1] + dp2[-2] * (10 ** cnt - 10 ** (cnt - 1)) + cnt * (10 ** cnt - 10 ** (cnt - 1) + 1) * (10 ** cnt - 10 ** (cnt - 1)) / 2 ans = int(ans) cnt += 1 dp.append(ans) def Cal(a, b): return dp2[b - 1] * a + b * int(a * (a + 1) / 2) q = int(input()) for _ in range(q): k = int(input()) i = 0 while k > dp[i]: i += 1 k -= dp[i - 1] l, r = 0, 10 ** i - 10 ** (i - 1) last = int((l + r) / 2) while not(Cal(last, i) < k and Cal(last + 1, i) >= k): if(Cal(last, i) < k): l = last last = int((l + r) / 2 + 1) else: r = last last = int((l + r) / 2) k -= Cal(last, i) j = 0 while dp2[j] < k: j += 1 k -= dp2[j - 1] a = int((k - 1) / j) k -= a * j Long = str(10 ** (j - 1) + a) print(Long[k - 1]) ```
output
1
104,510
12
209,021
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,604
12
209,208
Tags: math, two pointers Correct Solution: ``` t = int(input()) def solve(): n, x, m = map(int, input().split()) l = r = x while m > 0: l_i, r_i = map(int, input().split()) if l in range(l_i, r_i + 1) or r in range(l_i, r_i + 1): l = min(l, l_i) r = max(r, r_i) m -= 1 print (r - l + 1) while t > 0: solve() t -= 1 ```
output
1
104,604
12
209,209
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,605
12
209,210
Tags: math, two pointers Correct Solution: ``` def f(r1,r2): if r1[0]<=r2[0] and r1[1]>=r2[0]: return True elif r1[0]>r2[0] and r2[1]>=r1[0]: return True return False for _ in range(int(input())): n,x,m=map(int,input().split()) ansrange=(x,x) for i in range(m): a,b=map(int,input().split()) res = f((a,b),ansrange) if res: ansrange = (min(a,ansrange[0]),max(ansrange[1],b)) print(ansrange[1]-ansrange[0]+1) ```
output
1
104,605
12
209,211
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,606
12
209,212
Tags: math, two pointers Correct Solution: ``` from sys import stdin, stdout cin = stdin.readline cout = stdout.write mp = lambda: map(int, cin().split()) for _ in range(int(cin())): n, x, m = mp() #ans = 0 #a = [x, x] y = x for _ in range(m): l, r = mp() if y>=l<=r>=x: #x<=r<=l<=y: x = min(l, x) y = max(y, r) #elif l<=a[1]<=r: # a[0] = min(l, a[0]) # a[1] = r cout(str(y-x+1) + '\n') #print(y-x+1) ```
output
1
104,606
12
209,213
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,607
12
209,214
Tags: math, two pointers Correct Solution: ``` for z in range(int(input())): n,x,m=map(int,input().split()) x1=x2=x for i in range(m): a,b=map(int,input().split()) if b>=x1 and a<=x2: if a<=x1: x1=a if b>=x2: x2=b print(x2-x1+1) ```
output
1
104,607
12
209,215
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,608
12
209,216
Tags: math, two pointers Correct Solution: ``` for i in range(int(input())): n, x, m = map(int, input().split()) a, b = x, x for t in range(m): l, r = map(int, input().split()) if r >= a and b >= l: a = min(l, a) b = max(r, b) print(b-a+1) ```
output
1
104,608
12
209,217
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,609
12
209,218
Tags: math, two pointers Correct Solution: ``` def main(n, x, m, left, right): l, r = n, 0 for i in range(len(left)): if x > right[i]: if left[i] <= l <= right[i]: l = left[i] elif x < left[i]: if left[i] <= r <= right[i]: r = right[i] else: l, r = min(l, left[i]), max(r, right[i]) if r-l+1 <= 0: return 1 return r-l+1 for _ in range(int(input())): n, x, m = [int(i) for i in input().split()] left, right = [], [] for i in range(m): inp = input().split() left.append(int(inp[0])) right.append(int(inp[1])) print(main(n, x, m, left , right)) ```
output
1
104,609
12
209,219
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,610
12
209,220
Tags: math, two pointers Correct Solution: ``` def main(): t = int(input()) test_case_num = 1 while test_case_num <= t: n, x, m = map(int, input().split()) interval = (x, x) for q in range(m): li, ri = map(int, input().split()) if max(interval[0], li) <= min(interval[1], ri): interval = (min(interval[0], li), max(interval[1], ri)) print(interval[1] - interval[0] + 1) test_case_num += 1 main() ```
output
1
104,610
12
209,221
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,611
12
209,222
Tags: math, two pointers Correct Solution: ``` t = int(input()) for _ in range(t): n, x , m = map(int, input().rstrip().split(" ")) x = x -1 full_range = [x,x] for i in range(m): l, r = map(int, input().rstrip().split(" ")) l-=1 r-=1 if full_range[0] in range(l, r + 1): full_range[0] = min(l, full_range[0]) full_range[1] = max(r, full_range[1]) elif full_range[1] in range(l, r + 1): full_range[0] = min(l, full_range[0]) full_range[1] = max(r, full_range[1]) print(full_range[1]-full_range[0] + 1) ```
output
1
104,611
12
209,223
Provide tags and a correct Python 2 solution for this coding contest problem. You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation.
instruction
0
104,612
12
209,224
Tags: math, two pointers Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write mod=10**9+7 def ni(): return int(raw_input()) def li(): return map(int,raw_input().split()) def pn(n): stdout.write(str(n)+'\n') def pa(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ for t in range(ni()): n,x,m=li() px,py=x,x for i in range(m): x,y=li() if (px<=x and py>=y) or (px>=x and px<=y) or (py>=x and py<=y): px=min(px,x) py=max(y,py) pn(py-px+1) ```
output
1
104,612
12
209,225
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation. Submitted Solution: ``` import io,os input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import sys def solve(n,x,m,LR): ans_l,ans_r=x,x for l,r in LR: if r<ans_l or l>ans_r: continue ans_l=min(ans_l,l) ans_r=max(ans_r,r) return ans_r-ans_l+1 def main(): t=int(input()) for _ in range(t): n,x,m=map(int,input().split()) LR=[tuple(map(int,input().split())) for _ in range(m)] ans=solve(n,x,m,LR) sys.stdout.write(str(ans)+'\n') if __name__=='__main__': main() ```
instruction
0
104,613
12
209,226
Yes
output
1
104,613
12
209,227
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation. Submitted Solution: ``` def shuf(n,x,m): start=-1 end=-1 for i in range(len(m)): if x>=m[i][0] and x<=m[i][1]: start=m[i][0] end=m[i][1] for j in range(i+1,len(m)): if (m[j][0]<=start and m[j][1]>=start) or (m[j][0]<=end and m[j][1]>=end) : start=min(m[j][0],start) end=max(m[j][1],end) break return(end-start+1) t=int(input()) l=[] for i in range(t): n,x,m=list(map(int,input().split())) ml=[] for i in range(m): q=list(map(int,input().split())) ml.append(q) l.append([n,x,ml]) for r in l: print(shuf(*r)) ```
instruction
0
104,614
12
209,228
Yes
output
1
104,614
12
209,229
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation. Submitted Solution: ``` t = int(input()) for i in range(t): n,x,m = map(int,input().split()) l,r = x,x for i in range(m): a,b = map(int,input().split()) if a <= l and b >= r: l = a r = b elif a <= l and b >= l: l = a elif a <= r and b >= r: r = b print(r-l+1) ```
instruction
0
104,615
12
209,230
Yes
output
1
104,615
12
209,231
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation. Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Thu Jun 18 08:25:23 2020 @author: Harshal """ for _ in range(int(input())): n,x,m = map(int,input().split()) l,r=x,x for _ in range(m): L,R=map(int,input().split()) if max(l,L)<=min(r,R): l=min(l,L) r=max(r,R) print( r-l+1) ```
instruction
0
104,616
12
209,232
Yes
output
1
104,616
12
209,233
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation. Submitted Solution: ``` for _ in range(int(input())) : n,x,m = map(int,input().split()) l = 0 r = 0 cnt = 0 lock = 0 for _ in range(m) : a,b = map(int,input().split()) if lock == 0 : if x >= a and x <= b : cnt = (b-a) + 1 l = a r = b lock = 1 prev_a = a prev_b = b else : if (prev_a >= a and prev_a <=b ) and (prev_b >=a and prev_b<=b) : if (l-a) >= 0 : cnt+=(l-a) if a < l : l = a if (b-r) >=0 : cnt+=(b-r) if b > r : r = b elif prev_a >= a and prev_a <= b : if (l-a) >= 0 : cnt+=(l-a) l = a elif prev_b >= a and prev_b <= b : if (b-r) >= 0 : cnt+=(b-r) r = b else : continue prev_a = a prev_b = b # print(cnt,l,r) print(cnt) ```
instruction
0
104,617
12
209,234
No
output
1
104,617
12
209,235
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation. Submitted Solution: ``` for _ in range(int(input())): a,b,c=map(int,input().split()) m=b for i in range(c): p,q=map(int,input().split()) if p<=b and b<=q: m=max(m,p,q) print(m) ```
instruction
0
104,618
12
209,236
No
output
1
104,618
12
209,237
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation. Submitted Solution: ``` import sys T = int(sys.stdin.readline()) for _ in range(T): n, x, m =map(int, sys.stdin.readline().split()) nowl = -1 nowr = n+1 for _ in range(m): l, r = map(int, sys.stdin.readline().split()) if l<=x<=r and nowl==-1: nowl = l nowr = r if l<=nowl<=r or l<=nowr<=r: nowl = min(nowl, l) nowr = max(nowr, r) print(nowr-nowl+1) ```
instruction
0
104,619
12
209,238
No
output
1
104,619
12
209,239
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 consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0. You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i ≀ c, d ≀ r_i, and swap a_c and a_d. Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the description of t testcases follow. The first line of each test case contains three integers n, x and m (1 ≀ n ≀ 10^9; 1 ≀ m ≀ 100; 1 ≀ x ≀ n). Each of next m lines contains the descriptions of the operations; the i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n). Output For each test case print one integer β€” the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. Example Input 3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2 Output 6 2 3 Note In the first test case, it is possible to achieve a_k = 1 for every k. To do so, you may use the following operations: 1. swap a_k and a_4; 2. swap a_2 and a_2; 3. swap a_5 and a_5. In the second test case, only k = 1 and k = 2 are possible answers. To achieve a_1 = 1, you have to swap a_1 and a_1 during the second operation. To achieve a_2 = 1, you have to swap a_1 and a_2 during the second operation. Submitted Solution: ``` import sys ip=lambda :sys.stdin.readline().rstrip() # a=list(map(int,ip().split())) # n=int(ip()) for _ in range(int(ip())): n,x,m=map(int,ip().split()) a=[] ans=0 ct=0 p=float('inf') p1=float('inf') for _ in range(m): l,r=map(int,ip().split()) if l==1 and r==n: ans=n if x>=l and x<=r: ct=r-l+1 p=r p1=l if ct and l==p or r==p1: ct+= r-l p=r p1=l ans=max(ans,ct) print(ans) ```
instruction
0
104,620
12
209,240
No
output
1
104,620
12
209,241
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,621
12
209,242
Tags: constructive algorithms, greedy, math Correct Solution: ``` from math import * t=int(input()) while t: t=t-1 #n=int(input()) n,k=map(int,input().split()) a=list(map(int,input().split())) s=set(a) n=len(s) if n==k: print(1) continue if k==1 and n>1: print(-1) continue elif k==1 and n==1: print(1) continue cnt=0 s=list(s) for i in range(n): j=0 flag=1 lst=s[i] cnt+=1 #print(s) for l in range(i,n): if s[l]==0 and flag: j+=1 flag=0 continue elif s[l]==0: continue if j<k: lst=s[l] s[l]-=s[l] j+=1 else: s[l]-=lst #print(s) if s[n-1]==0: break print(cnt) ```
output
1
104,621
12
209,243