message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
41,398
12
82,796
Tags: binary search, brute force, data structures, dp, two pointers Correct Solution: ``` import os import sys from io import BytesIO, IOBase import heapq as h from types import GeneratorType BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") import time start_time = time.time() import collections as col import math, string from functools import reduce def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) """ AAAABBBAAAA Given a block of As on the left, we need a matching block on the right, and a block of Bs in the middle For any given B block, we need to know the max """ def solve(): N = getInt() A = getInts() A = [A[i]-1 for i in range(N)] #prefix count of occurrences of j in the first i dp = [[0 for j in range(201)] for i in range(N+1)] #list of positions for each j pos = [[] for j in range(201)] for i in range(N): for j in range(201): dp[i+1][j] = dp[i][j] dp[i+1][A[i]] += 1 pos[A[i]].append(i) ans = 0 for i in range(201): ans = max(ans,len(pos[i])) for j in range(len(pos[i])//2): l = pos[i][j]+1 r = pos[i][len(pos[i])-j-1]-1 for k in range(201): x = dp[r+1][k] - dp[l][k] ans = max(ans, (j+1)*2 + x) return ans for _ in range(getInt()): print(solve()) ```
output
1
41,398
12
82,797
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
41,399
12
82,798
Tags: binary search, brute force, data structures, dp, two pointers Correct Solution: ``` from sys import stdin,stdout from math import * input = stdin.readline # print=stdout.write for i in range(int(input())): n=int(input()) ar=list(map(int,input().split())) d1=[] d2=[] for i in range(n): d1.append([0]*27) d2.append([0]*27) d1[0][ar[0]]+=1 for i in range(1,n): for j in range(27): d1[i][j]=d1[i-1][j] d1[i][ar[i]]+=1 d2[-1][ar[-1]]+=1 for i in range(n-2,-1,-1): for j in range(27): d2[i][j]=d2[i+1][j] d2[i][ar[i]]+=1 ans=1 # print(d1) # print(d2) for i in range(n): for j in range(i+1,n): a=[0]*27 b=[0]*27 for k in range(27): x=d1[i][k] y=d2[j][k] a[k]=min(x,y)*2 b[k]=d1[j-1][k]-d1[i][k] if(ans<max(a)+max(b)): ans=max(a)+max(b) # a=d1[1][26] # b=d2[3][26] # c=d1[2][14]-d1[1][14] # print(a,b,c) print(ans) ```
output
1
41,399
12
82,799
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
41,400
12
82,800
Tags: binary search, brute force, data structures, dp, two pointers Correct Solution: ``` def distribution(n): return (n-1)//2 def problem_A(): print(distribution(int(input()))) s = 'abcdefghijklmnopqrstuvwxyz' def cons_str(n,b): a = s[:b] ans = '' while n > b: ans += a n -= b ans += a[:n] return ans def problem_B(): n,a,b = list(map(int,input().split())) print(cons_str(n,b)) def comp_teams(a): if len(a) < 2: return 0 s = len(set(a)) if s == len(a): return 1 a.sort() t = 0 temp = 1 for i in range(1,len(a)): if a[i] == a[i-1]: temp += 1 else: if t < temp: t = temp temp = 1 if t < temp: t = temp if s <= t - 1: return s if s == t: return s - 1 else: return t def problem_C(): l = int(input()) a = list(map(int,input().split())) print(comp_teams(a)) def antisudoku(a): for i in range(9): for j in range(9): if a[i][j] == '2': a[i][j] = '1' return a def problem_D(): a = [] for _ in range(9): a.append(list(input())) b = antisudoku(a) for i in range(9): print(''.join(b[i])) def three_block_palindrome(s): a = [] b = [] c = [] l = len(s) for _ in range(l): a = [] for _ in range(l+1): a.append([0]*26) l += 1 for i in range(1,l): k = s[i-1] - 1 for j in range(26): a[i][j] = a[i-1][j] a[i][k] += 1 maxlength = 0 for i in range(l): for j in range(i,l): maxa = 0 maxb = 0 for k in range(26): if a[l-1][k]-a[j][k] > a[i][k]: if maxa < a[i][k]: maxa = a[i][k] else: if maxa < a[l-1][k]-a[j][k]: maxa = a[l-1][k]-a[j][k] if maxb < a[j][k] - a[i][k]: maxb = a[j][k] - a[i][k] if maxlength < 2*maxa + maxb: maxlength = 2*maxa + maxb return maxlength def problem_E1(): l = int(input()) s = list(map(int,input().split())) print(three_block_palindrome(s)) cases = int(input()) for _ in range(cases): # problem_A() # problem_B() # problem_C() # problem_D() problem_E1() ```
output
1
41,400
12
82,801
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
41,401
12
82,802
Tags: binary search, brute force, data structures, dp, two pointers Correct Solution: ``` for _ in range(int(input())): n = int(input()) arr = list(map(lambda x : int(x) - 1, input().split())) nxt = [[-1] * 26 for _ in range(n)] prv = [[-1] * 26 for _ in range(n)] fst = [-1] * 26 lst = [-1] * 26 initc = [0] * 26 for i in range(n): initc[arr[i]] += 1 if fst[arr[i]] == -1: fst[arr[i]] = i else: for j in range(i - 1, -1, -1): if arr[j] == arr[i]: prv[i][arr[i]] = j nxt[j][arr[i]] = i break for i in range(n - 1, -1, -1): if lst[arr[i]] == -1: lst[arr[i]] = i ans = 0 for i in range(26): cnt = initc[:] ans = max(cnt[i], ans) cnt[i] = 0 if fst[i] == -1: continue l = fst[i] r = lst[i] if l == r: ans = max(1, ans) continue real_l = 0 real_r = n - 1 prsf = 2 ans = max(ans, max(cnt)) while l < r: while l > real_l: cnt[arr[real_l]] -= 1 real_l += 1 while r < real_r: cnt[arr[real_r]] -= 1 real_r -= 1 ans = max(ans, max(cnt) + prsf) l = nxt[l][i] r = prv[r][i] prsf += 2 print(ans) ```
output
1
41,401
12
82,803
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
41,402
12
82,804
Tags: binary search, brute force, data structures, dp, two pointers Correct Solution: ``` import sys import collections import threading import string def sparsetable(a): tmp = [0]*201 st = [] for i in range(len(a)): tmp[a[i]] += 1 st.append(tmp[:]) return st def func(x, y, st): if x>=y: return 0 ans = 0 for i in range(201): ans = max(ans, st[y][i] - st[x][i] ) return ans def main(): testn = int(input()) for _ in range(testn): n = int( input() ) a = list(map(int, input().split() )) d = collections.defaultdict(list) st = sparsetable(a) for i in range(n): d[ a[i] ].append(i) ans = 1 for key in d: z = len(d[key]) l = d[key] for i in range(z//2): ans = max(ans, (i+1)*2 + func(l[i], l[z-1-i]-1, st)) print(ans) input = sys.stdin.readline main() ```
output
1
41,402
12
82,805
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
41,403
12
82,806
Tags: binary search, brute force, data structures, dp, two pointers Correct Solution: ``` '''input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 ''' import sys import math import copy from collections import Counter debug = 1 readln = sys.stdin.readline #sys.setrecursionlimit(1000000) def readint(): return int(readln().strip()) def readints(): return map(int, readln().split()) def readstr(): return readln().strip() def readstrs(): return readln().split() def dbg(*args): if debug: print(' '.join(map(str, args))) def subr(i, j, cter): if j < i: return 0 elif j == i: return 1 else: c = cter[j] - (cter[i-1] if i > 0 else Counter()) return c.most_common(1)[0][1] def solve(n,A): maxa = 0 pos = [[] for _ in range(201)] cter = [] for i, a in enumerate(A): pos[a].append(i) maxa = max(maxa, a) if i > 0: cter.append(cter[i-1].copy()) else: cter.append(Counter()) cter[i][a] += 1 keys = [a for a in range(len(pos)) if pos[a]] ans = 0 for x in keys: posx = pos[x] lenx = len(posx) for i in range(len(posx) // 2): ans = max(ans, (i+1)*2 + subr(posx[i]+1, posx[lenx-i-1]-1, cter)) return max(ans, subr(0, n-1, cter)) t = readint() for _ in range(t): n = readint() A = readints() print(solve(n,A)) ```
output
1
41,403
12
82,807
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
41,404
12
82,808
Tags: binary search, brute force, data structures, dp, two pointers Correct Solution: ``` from collections import defaultdict import bisect from sys import stdin input = stdin.readline def check(i,j,d): #print(i,j) maxi = 0 for k in d: k = d[k] a = bisect.bisect_right(k,i) b = bisect.bisect_left(k,j) #print(k,a,b) maxi = max(maxi,b-a) #print(maxi) return maxi t = int(input()) for _ in range(t): n = int(input()) A = [int(j) for j in input().split()] maxi = 0 d = defaultdict(list) for i in range(n): d[A[i]].append(i) #print(d) for k in d: k = d[k] i = 0 j = len(k)-1 travel = 0 while i<=j: if i==j: travel+=1 maxi = max(maxi,travel) break a = k[i] b = k[j] travel+=2 temp = check(a,b,d) maxi = max(maxi,temp+travel) #print(travel,maxi) i+=1 j-=1 print(maxi) ```
output
1
41,404
12
82,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) P = [[] for _ in range(27)] S = set() for i, a in enumerate(A): P[a].append(i) S.add(a) ans = 0 for a in S: for i in range(0, (len(P[a])//2)): minp = P[a][i] maxp = P[a][-(1+i)] s = 0 for b in S: if a == b: continue s = max(s, sum([p > minp and p < maxp for p in P[b]])) ans = max(ans, (i+1)*2 + s) ans = max(ans, max([len(p) for p in P])) print(ans) ```
instruction
0
41,405
12
82,810
Yes
output
1
41,405
12
82,811
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` from collections import deque, defaultdict, Counter from bisect import bisect_left, bisect #bisect(list, num, beg, end) import math def solve(inf): n = inf[0] a = list(map(int, input().split())) uni = list(set(a)) if len(uni) == 1: return n ans = 0 count = defaultdict(lambda:0) indxs = defaultdict(lambda:[]) for i in range(len(a)): indxs[a[i]].append(i) pre = [] for i in range(len(a)): count[a[i]] += 1 pre.append(count.copy()) pre.append(defaultdict(lambda:0)) # pre.append(pre[-1]) # print(newa) # pre.append([0,0]) # print(pre) for key in indxs: l = 0 r = len(indxs[key]) - 1 ans = max(ans, r + 1) for i in range((r+1)//2): p = indxs[key][i] q = indxs[key][r - i] # if key == a: aaa...bbbb...aaa # print((p, q)) for newkey in indxs: if newkey == key: continue # print('key', key, 'newkey', newkey, pre[q-1][newkey] - pre[p][newkey], 2*(i+1)) ans = max(ans, pre[q-1][newkey] - pre[p][newkey] + 2*(i+1)) return ans for _ in range(int(input())): inf = list(map(int, input().split())) print(solve(inf)) ```
instruction
0
41,406
12
82,812
Yes
output
1
41,406
12
82,813
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` import bisect for _ in range(int(input())): n=int(input()) a=list(map(lambda x: int(x)-1,input().split())) mxl=1 dp=[[0]*(len(a)) for _ in range(26)] for i in range(len(a)): for j in range(26): dp[j][i]=dp[j][i-1] dp[a[i]][i]+=1 for i in range(len(a)): for j in range(i+1,len(a)): if a[i]==a[j]: mxl=max(mxl,dp[a[i]][-1]) continue if dp[a[i]][i]>0 and dp[a[j]][j]-dp[a[j]][i]>0 and dp[a[i]][-1]-dp[a[i]][j]>0: mxl=max(2*min(dp[a[i]][i],dp[a[i]][-1]-dp[a[i]][j])+dp[a[j]][j]-dp[a[j]][i],mxl) print(mxl) ```
instruction
0
41,407
12
82,814
Yes
output
1
41,407
12
82,815
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` numbers = 26 for _ in range(int(input())): n = int(input()) seq = [int(x)-1 for x in input().split()] #TODO pre = [(n+1)*[0] for i in range(numbers)] for j, val in enumerate(seq, 1): for i in range(numbers): if val == i: pre[i][j] = pre[i][j-1] + 1 else: pre[i][j] = pre[i][j-1] ans = 1 for i in range(numbers): # for x value max_val = pre[i][-1] // 2 for x in range(1, 1+max_val): # for min x len bmin = pre[i].index(x) # leftmost bmax = n+1-pre[i][::-1].index(pre[i][-1]-x) # right most # print(pre[i],x,bmin, bmax) ymax = 0 if bmax - bmin > 1: for j in range(numbers): # for y ymax = max(ymax, pre[j][bmax-1] - pre[j][bmin]) # print('sol', i,x,ymax) ans = max(ans, ymax + 2*x) # print("ANS ", end='') print(ans) ```
instruction
0
41,408
12
82,816
Yes
output
1
41,408
12
82,817
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` def find_leftmost_right(a, k): return indices[a][-k] def most_repeats(j1, j2): if j2 >= j1: return max(repeats[j0][j2]-repeats[j0][j1]+1 for j0 in range(200)) return 0 T = int(input()) for t in range(T): N = int(input()) A = list(map(int, input().split())) repeats = [[0]*N for _ in range(200)] indices = [[] for _ in range(200)] for i in range(0, N): if i > 0: for j in range(200): repeats[j][i] = repeats[j][i-1] repeats[A[i]-1][i] += 1 indices[A[i]].append(i) result = 1 for i1 in range(len(A)): c = repeats[A[i1]-1][i1] i2 = find_leftmost_right(A[i1], c) if i2 > i1: # print("{}\n c is {} i1 is {} i2 is {} most_repeats is {}". # format(A, c, i1, i2, most_repeats(i1 + 1, i2 - 1))) result = max(result, 2 * c + most_repeats(i1+1, i2-1)) print(result) ```
instruction
0
41,409
12
82,818
No
output
1
41,409
12
82,819
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` import sys input = sys.stdin.readline from math import * t=int(input()) while t>0: t-=1 n=int(input()) a=[int(x) for x in input().split()] ff=[{} for i in range(27)] fl=[{} for i in range(27)] count=[0 for i in range(27)] cal=[[0 for i in range(27)] for i in range(n+1)] for i in range(n): for j in range(27): if i==0: cal[i][j]=int(a[i]==j) else: cal[i][j]=cal[i-1][j]+int(a[i]==j) for i in range(n//2+1): count[a[i]]+=1 ff[a[i]][count[a[i]]]=i count=[0 for i in range(27)] for i in range(n-1,n//2-1,-1): count[a[i]]+=1 fl[a[i]][count[a[i]]]=i maxi=1 for i in range(1,27): for j in ff[i]: l=ff[i][j] if j not in fl[i]: continue r=fl[i][j] c=2*j #print(c,maxi,i,fl[i][j],ff[i][j]) g=0 for k in range(1,27): if r-1<l: continue g=cal[r-1][k]-cal[l][k] maxi=max(maxi,c+g) print(maxi) ```
instruction
0
41,410
12
82,820
No
output
1
41,410
12
82,821
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` import io,os input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import sys from collections import defaultdict def solve(n,A): L=[[] for _ in range(27)] for idx,val in enumerate(A): L[val].append(idx) ans=0 for i in range(1,26): for j in range(i+1,27): len_i,len_j=len(L[i]),len(L[j]) if len_i==0 or len_j==0: ans=max(ans,len_i,len_j) continue B=[] for a in A: if a==i or a==j: B.append(a) lenB=len(B) #print(i,j,B) C=[] l=1 prev=B[0] for b in B[1:]: if b!=prev: C.append(l) l=1 else: l+=1 prev=b C.append(l) lenC=len(C) #print(i,j,C) if lenC==2: ans=max(ans,max(C)) continue for k in range(lenC-2): tmp=C[k+1]+min(C[k],C[k+2])*2 #print(i,j,k,tmp) ans=max(ans,tmp) return ans def main(): t=int(input()) for _ in range(t): n=int(input()) A=tuple(map(int,input().split())) ans=solve(n,A) sys.stdout.write(str(ans)+'\n') if __name__=='__main__': main() ```
instruction
0
41,411
12
82,822
No
output
1
41,411
12
82,823
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 2000) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2000) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26. It is guaranteed that the sum of n over all test cases does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` t=int(input()) u=0 while(u<t): n=int(input()) a=list(map(int,input().split())) rama=[[0]*n for i in range(27)] sita=[[] for i in range(27)] for i in range(n): if(i>0): s=a[i] for j in range(27): if(j==s): rama[a[i]][i]=rama[a[i]][i-1]+1 sita[a[i]].append(i) else: rama[j][i]=rama[j][i-1] else: rama[a[i]][i]+=1 sita[a[i]].append(i) maxi=0 result=0 for i in range(n): d=rama[a[i]][i] e=sita[a[i]][-d] if(e>i): for j in range(27): maxi=max(maxi,rama[j][e-1]-rama[j][i]) result=max(2*d+maxi,result) if(result==0): print('1') else: print(result) u+=1 ```
instruction
0
41,412
12
82,824
No
output
1
41,412
12
82,825
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
41,465
12
82,930
Tags: constructive algorithms, graphs, greedy, implementation Correct Solution: ``` import sys input = sys.stdin.readline from collections import * def tran(): pos = [] epos = [] for i in range(n-2, n): for j in range(m-2, m): if s[i][j]==1: pos.append((i, j)) else: epos.append((i, j)) if len(pos) in [1, 2]: row = (pos[0][0], pos[0][1], epos[0][0], epos[0][1], epos[1][0], epos[1][1]) ans.append(row) for j in range(3): x = row[2*j] y = row[2*j+1] s[x][y] ^= 1 else: row = (pos[0][0], pos[0][1], pos[1][0], pos[1][1], pos[2][0], pos[2][1]) ans.append(row) for j in range(3): x = row[2*j] y = row[2*j+1] s[x][y] ^= 1 t = int(input()) for _ in range(t): n, m = map(int, input().split()) s = [list(map(int, input()[:-1])) for _ in range(n)] ans = [] for i in range(n-1): for j in range(m-1): if i==n-2 and j==m-2: continue if s[i][j]==1: ans.append((i, j, i+1, j, i, j+1)) s[i][j] ^= 1 s[i+1][j] ^= 1 s[i][j+1] ^= 1 for i in range(m-2): if s[n-2][i]==1 and s[n-1][i]==1: row = (n-2, i, n-1, i, n-2, i+1) elif s[n-2][i]==1 and s[n-1][i]==0: row = (n-2, i, n-2, i+1, n-1, i+1) elif s[n-2][i]==0 and s[n-1][i]==1: row = (n-1, i, n-2, i+1, n-1, i+1) else: continue for j in range(3): x = row[2*j] y = row[2*j+1] s[x][y] ^= 1 ans.append(row) for i in range(n-2): if s[i][m-2]==1 and s[i][m-1]==1: row = (i, m-2, i, m-1, i+1, m-2) elif s[i][m-2]==1 and s[i][m-1]==0: row = (i, m-2, i+1, m-2, i+1, m-1) elif s[i][m-2]==0 and s[i][m-1]==1: row = (i, m-1, i+1, m-2, i+1, m-1) else: continue for j in range(3): x = row[2*j] y = row[2*j+1] s[x][y] ^= 1 ans.append(row) while s[n-2][m-2]==1 or s[n-2][m-1]==1 or s[n-1][m-2]==1 or s[n-1][m-1]==1: tran() for i in range(n): for j in range(m): if s[i][j]==1: 1/0 print(len(ans)) for a, b, c, d, e, f in ans: print(a+1, b+1, c+1, d+1, e+1, f+1) ```
output
1
41,465
12
82,931
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
41,466
12
82,932
Tags: constructive algorithms, graphs, greedy, implementation Correct Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase import threading from bisect import bisect_right from math import gcd,log from collections import Counter,defaultdict from pprint import pprint import copy # 3 4 # 3 11 12 22 33 35 38 67 69 71 94 99 def main(case_no): n,m=map(int,input().split()) grid=[] for i in range(n): grid.append([int(c) for c in input()]) ans=[] llt=copy.deepcopy(grid) for i in range(n-2): for j in range(m): if grid[i][j]==1: grid[i][j]=(grid[i][j]+1)%2 grid[i+1][j]=(grid[i+1][j]+1)%2 if j+1<m : grid[i][j+1]=(grid[i][j+1]+1)%2 ans.append((i,j,i+1,j,i,j+1)) else: grid[i+1][j-1]=(grid[i+1][j-1]+1)%2 ans.append((i,j,i+1,j,i+1,j-1)) for i in range(m-2): if grid[-2][i]==1: grid[n-2][i+1]=(grid[n-2][i+1]+1)%2 grid[n-1][i+1]=(grid[n-1][i+1]+1)%2 grid[n-2][i]=(grid[n-2][i]+1)%2 ans.append((n-2,i+1,n-1,i+1,n-2,i)) if grid[-1][i]==1: grid[n-2][i+1]=(grid[n-2][i+1]+1)%2 grid[n-1][i+1]=(grid[n-1][i+1]+1)%2 grid[n-1][i]=(grid[n-1][i]+1)%2 ans.append((n-2,i+1,n-1,i+1,n-1,i)) # print(grid) cc=[(n-1,m-1),(n-1,m-2),(n-2,m-1),(n-2,m-2)] ct=0 for x,y in cc : if grid[x][y]==1: ct+=1 if ct==4 : a=[n-1,m-1,n-1,m-2,n-2,m-1] ans.append(tuple(a)) grid[a[0]][a[1]]=(grid[a[0]][a[1]]+1)%2 grid[a[2]][a[3]]=(grid[a[2]][a[3]]+1)%2 grid[a[4]][a[5]]=(grid[a[4]][a[5]]+1)%2 ct=1 if ct==1: a=[] for x,y in cc : if grid[x][y]==0: a.append(x) a.append(y) if len(a)==4: break for x,y in cc : if grid[x][y]==1: a.append(x) a.append(y) break ans.append(tuple(a)) grid[a[0]][a[1]]=(grid[a[0]][a[1]]+1)%2 grid[a[2]][a[3]]=(grid[a[2]][a[3]]+1)%2 grid[a[4]][a[5]]=(grid[a[4]][a[5]]+1)%2 # pprint(grid) if ct==1 or ct==2: a=[] for x,y in cc : if grid[x][y]==0: a.append(x) a.append(y) for x,y in cc : if grid[x][y]==1: a.append(x) a.append(y) break grid[a[0]][a[1]]=(grid[a[0]][a[1]]+1)%2 grid[a[2]][a[3]]=(grid[a[2]][a[3]]+1)%2 grid[a[4]][a[5]]=(grid[a[4]][a[5]]+1)%2 ans.append(a) # pprint(grid) if ct>0 : a=[] for x,y in cc : if grid[x][y]==1: a.append(x) a.append(y) ans.append(a) grid[a[0]][a[1]]=(grid[a[0]][a[1]]+1)%2 grid[a[2]][a[3]]=(grid[a[2]][a[3]]+1)%2 grid[a[4]][a[5]]=(grid[a[4]][a[5]]+1)%2 # pprint(grid) # pprint(llt) for a in ans : llt[a[0]][a[1]]=(llt[a[0]][a[1]]+1)%2 llt[a[2]][a[3]]=(llt[a[2]][a[3]]+1)%2 llt[a[4]][a[5]]=(llt[a[4]][a[5]]+1)%2 # pprint(llt) # pprint(grid) # print(ans) for i in range(n) : for j in range(m) : # print(i,j) assert(grid[i][j]==llt[i][j]==0) print(len(ans)) assert(len(ans)<=m*n) for li in ans : print(*[i+1 for i in li]) 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") # endregion if __name__ == "__main__": for _ in range(int(input())): main(_+1) ```
output
1
41,466
12
82,933
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
41,467
12
82,934
Tags: constructive algorithms, graphs, greedy, implementation Correct Solution: ``` import sys def printm(): for row in M: print(''.join(map(str, row))) def change1(i, j): if i: di = -1 else: di = 1 if j: dj = -1 else: dj = 1 out = [] out.append((i, j, i + di, j, i + di, j + dj)) out.append((i, j, i, j + dj, i + di, j + dj)) out.append((i, j, i + di, j, i, j + dj)) return out def change3(i, j, pos): if pos == 0: return [(i, j + 1, i + 1, j, i + 1, j + 1)] if pos == 1: return [(i, j, i + 1, j, i + 1, j + 1)] if pos == 2: return [(i, j, i, j + 1, i + 1, j + 1)] if pos == 3: return [(i, j, i, j + 1, i + 1, j)] def change2(i, j, pos): out = [] if pos == 0: out.append((i, j, i + 1, j, i, j + 1)) out.append((i + 1, j + 1, i + 1, j, i, j + 1)) elif pos == 1: out.append((i, j, i, j + 1, i + 1, j + 1)) out.append((i + 1, j, i, j, i + 1, j + 1)) elif pos == 2: out.append((i, j, i, j + 1, i + 1, j + 1)) out.append((i, j + 1, i + 1, j, i + 1, j + 1)) elif pos == 3: out.append((i, j, i + 1, j, i, j + 1)) out.append((i, j, i + 1, j, i + 1, j + 1)) elif pos == 4: out.append((i, j, i + 1, j, i + 1, j + 1)) out.append((i, j + 1, i + 1, j, i + 1, j + 1)) elif pos == 5: out.append((i, j, i + 1, j, i, j + 1)) out.append((i, j, i, j + 1, i + 1, j + 1)) return out t = int(input()) for _t in range(t): n, m = map(int, sys.stdin.readline().split()) M = [] out = [] for i in range(n): M.append(list(map(int, list(sys.stdin.readline().strip())))) # printm() if n % 2: start_i = 1 for j in range(m): if M[0][j]: if j == m - 1: M[0][j] = 0 M[1][j] = 1 - M[1][j] M[1][j - 1] = 1 - M[1][j - 1] out.append((0, j, 1, j, 1, j - 1)) elif M[0][j + 1]: M[0][j] = 0 M[0][j + 1] = 0 M[1][j] = 1 - M[1][j] out.append((0, j, 0, j + 1, 1, j)) else: M[0][j] = 0 M[1][j] = 1 - M[1][j] M[1][j + 1] = 1 - M[1][j + 1] out.append((0, j, 1, j, 1, j + 1)) else: start_i = 0 if m % 2: start_j = 1 for i in range(start_i, n): if M[i][0]: if i == n - 1: M[i][0] = 0 M[i][1] = 1 - M[i][1] M[i - 1][1] = 1 - M[i - 1][1] out.append((i, 0, i, 1, i - 1, 1)) elif M[i + 1][0]: M[i][0] = 0 M[i + 1][0] = 0 M[i][1] = 1 - M[i][1] out.append((i, 0, i + 1, 0, i, 1)) else: M[i][0] = 0 M[i][1] = 1 - M[i][1] M[i + 1][1] = 1 - M[i + 1][1] out.append((i, 0, i, 1, i + 1, 1)) else: start_j = 0 # printm() for i in range(start_i, n, 2): for j in range(start_j, m, 2): a, b, c, d = M[i][j], M[i][j + 1], M[i + 1][j], M[i + 1][j + 1] if a + b + c + d == 1: if a: out += change1(i, j) elif b: out += change1(i, j + 1) elif c: out += change1(i + 1, j) else: out += change1(i + 1, j + 1) elif a + b + c + d == 2: if a and d: out += change2(i, j, 0) elif b and c: out += change2(i, j, 1) elif a and c: out += change2(i, j, 2) elif b and d: out += change2(i, j, 3) elif a and b: out += change2(i, j, 4) else: out += change2(i, j, 5) elif a + b + c + d == 3: if not a: out += change3(i, j, 0) elif not b: out += change3(i, j, 1) elif not c: out += change3(i, j, 2) else: out += change3(i, j, 3) elif a + b + c + d == 4: out += change3(i, j, 0) out += change1(i, j) print(len(out)) for row in out: for elem in row: print(elem + 1, end=' ') print() ```
output
1
41,467
12
82,935
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
41,468
12
82,936
Tags: constructive algorithms, graphs, greedy, implementation Correct Solution: ``` import sys,os,io # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline input = sys.stdin.readline for _ in range (int(input())): n,m = [int(i) for i in input().split()] a = [] for i in range (n): s = input().strip() a.append([int(i) for i in s]) ans = [] for i in range (n-2): for j in range (0,m,2): if j==m-1: if a[i][j]==1: ans.append([i+1, j+1, i+2, j+1, i+2, j]) a[i][j] ^= 1 a[i+1][j] ^= 1 a[i+1][j-1] ^= 1 continue if a[i][j] == 1 and a[i][j+1]==1: ans.append([i+1, j+1, i+1, j+2, i+2, j+1]) a[i][j]^=1 a[i][j+1]^=1 a[i+1][j]^=1 continue if a[i][j]==1: ans.append([i+1, j+1, i+2, j+1, i+2, j+2]) a[i][j]^=1 a[i+1][j]^=1 a[i+1][j+1]^=1 continue if a[i][j+1]==1: ans.append([i+1, j+2, i+2, j+1, i+2, j+2]) a[i][j+1]^=1 a[i+1][j]^=1 a[i+1][j+1]^=1 continue for j in range (m-2): if a[n-1][j] and a[n-2][j]: ans.append([n, j+1, n-1, j+1, n-1, j+2]) a[n-1][j]^=1 a[n-2][j]^=1 a[n-2][j+1]^=1 continue if a[n-1][j]: ans.append([n, j+1, n-1, j+2, n, j+2]) a[n-1][j]^=1 a[n-2][j+1]^=1 a[n-1][j+1]^=1 continue if a[n-2][j]: ans.append([n-1, j+1, n-1, j+2, n, j+2]) a[n-2][j]^=1 a[n-2][j+1]^=1 a[n-1][j+1]^=1 continue if a[n-2][m-2] ^ a[n-2][m-1] ^ a[n-1][m-2]: ans.append([n-1, m-1, n-1, m, n, m-1]) if a[n-2][m-2] ^ a[n-1][m-2] ^ a[n-1][m-1]: ans.append([n-1,m-1,n,m-1, n,m]) if a[n-1][m-2] ^ a[n-2][m-1] ^ a[n-1][m-1]: ans.append([n,m-1,n-1,m,n,m]) if a[n-2][m-1] ^ a[n-2][m-2] ^ a[n-1][m-1]: ans.append([n-1,m, n-1,m-1, n,m]) print(len(ans)) for i in ans: print(*i) ```
output
1
41,468
12
82,937
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
41,469
12
82,938
Tags: constructive algorithms, graphs, greedy, implementation Correct Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): for t in range(int(input())): n,m = map(int, input().split()) a = [] for i in range(n): a.append(list(input())) ops = [] def f(b,c,e,f2,g,h): ops.append((b+1,c+1,e+1,f2+1,g+1,h+1)) a[b][c] = '1' if a[b][c] == '0' else '0' a[e][f2] = '1' if a[e][f2] == '0' else '0' a[g][h] = '1' if a[g][h] == '0' else '0' # for i in range(n): # print(''.join(a[i])) # print('') if m % 2 == 1: for i in range(n-1): if a[i][0] == '1': f(i,0,i,1,i+1,1) if a[n-1][0] == '1': f(n-1,0,n-1,1,n-2,1) for j in range(m%2,m,2): for i in range(n-2): if a[i][j] == '1': f(i,j,i+1,j,i+1,j+1) if a[i][j+1] == '1': f(i,j+1,i+1,j,i+1,j+1) q = ''.join([a[n-2][j],a[n-2][j+1],a[n-1][j],a[n-1][j+1]]) if q == '1111': f(n-2,j,n-2,j+1,n-1,j) #1110 f(n-2,j,n-2,j+1,n-1,j+1) #1101 f(n-2,j,n-1,j,n-1,j+1) #1011 f(n-2,j+1,n-1,j,n-1,j+1) #0111 elif q == '1110': f(n-2,j,n-2,j+1,n-1,j) #1110 elif q == '1011': f(n-2,j,n-1,j,n-1,j+1) #1011 elif q == '1101': f(n-2,j,n-2,j+1,n-1,j+1) #1101 elif q == '0111': f(n-2,j+1,n-1,j,n-1,j+1) #0111 elif q == '1010': f(n-2,j,n-2,j+1,n-1,j+1) #1101 f(n-2,j+1,n-1,j,n-1,j+1) #0111 elif q == '1001': f(n-2,j,n-2,j+1,n-1,j) #1110 f(n-2,j+1,n-1,j,n-1,j+1) #0111 elif q == '1100': f(n-2,j,n-1,j,n-1,j+1) #1011 f(n-2,j+1,n-1,j,n-1,j+1) #0111 elif q == '0101': f(n-2,j,n-2,j+1,n-1,j) #1110 f(n-2,j,n-1,j,n-1,j+1) #1011 elif q == '0110': f(n-2,j,n-2,j+1,n-1,j+1) #1101 f(n-2,j,n-1,j,n-1,j+1) #1011 elif q == '0011': f(n-2,j,n-2,j+1,n-1,j) #1110 f(n-2,j,n-2,j+1,n-1,j+1) #1101 elif q == '1000': f(n-2,j,n-2,j+1,n-1,j) #1110 f(n-2,j,n-2,j+1,n-1,j+1) #1101 f(n-2,j,n-1,j,n-1,j+1) #1011 elif q == '0100': f(n-2,j,n-2,j+1,n-1,j) #1110 f(n-2,j,n-2,j+1,n-1,j+1) #1101 f(n-2,j+1,n-1,j,n-1,j+1) #0111 elif q == '0010': f(n-2,j,n-2,j+1,n-1,j) #1110 f(n-2,j,n-1,j,n-1,j+1) #1011 f(n-2,j+1,n-1,j,n-1,j+1) #0111 elif q == '0001': f(n-2,j,n-2,j+1,n-1,j+1) #1101 f(n-2,j,n-1,j,n-1,j+1) #1011 f(n-2,j+1,n-1,j,n-1,j+1) #0111 print(len(ops)) for op in ops: print(op[0],op[1],op[4],op[5],op[2],op[3]) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
output
1
41,469
12
82,939
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
41,470
12
82,940
Tags: constructive algorithms, graphs, greedy, implementation Correct Solution: ``` import math def selectTiles(mepas, x, y): ans = [] if mepas[x][y] == 1: ans.append(x + 1) ans.append(y + 1) mepas[x][y] = 0 if mepas[x][y] else 1 if mepas[x][y+1] == 1 or len(ans) < 2: ans.append(x + 1) ans.append(y + 2) mepas[x][y+1] = 0 if mepas[x][y+1] else 1 if mepas[x+1][y] == 1 or len(ans) < 4: ans.append(x + 2) ans.append(y + 1) mepas[x+1][y] = 0 if mepas[x+1][y] else 1 if len(ans) < 6: ans.append(x + 2) ans.append(y + 2) mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1 return mepas, tuple(ans) def selectTiles2(mepas, x, y): ans = [] if mepas[x][y] == 1: ans.append(x + 1) ans.append(y + 1) mepas[x][y] = 0 if mepas[x][y] else 1 if mepas[x+1][y] == 1 or len(ans) < 2: ans.append(x + 2) ans.append(y + 1) mepas[x+1][y] = 0 if mepas[x+1][y] else 1 if mepas[x][y+1] == 1 or len(ans) < 4: ans.append(x + 1) ans.append(y + 2) mepas[x][y+1] = 0 if mepas[x][y+1] else 1 if len(ans) < 6: ans.append(x + 2) ans.append(y + 2) mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1 return mepas, tuple(ans) def countOnes(mepas, x, y): m = 0 m += mepas[x][y] m += mepas[x+1][y] m += mepas[x][y+1] m += mepas[x+1][y+1] return m def finishingTouch(mepas, x, y): ans = [] c = countOnes(mepas, x, y) if c == 1: mepas, a1 = oneLeft(mepas, x, y) mepas, a2 = twoLeft(mepas, x, y) mepas, a3 = selectTiles(mepas, x, y) ans.append(a1) ans.append(a2) ans.append(a3) elif c == 2: mepas, a2 = twoLeft(mepas, x, y) mepas, a3 = selectTiles(mepas, x, y) ans.append(a2) ans.append(a3) elif c == 3: mepas, a3 = selectTiles(mepas, x, y) ans.append(a3) elif c == 4: mepas, a0 = selectTiles(mepas, x, y) mepas, a1 = oneLeft(mepas, x, y) mepas, a2 = twoLeft(mepas, x, y) mepas, a3 = selectTiles(mepas, x, y) ans.append(a0) ans.append(a1) ans.append(a2) ans.append(a3) return mepas, ans def oneLeft(mepas, x, y): ones = 1 zeros = 2 ans = [] for xx in range(2): for yy in range(2): if mepas[x+xx][y+yy] == 1 and ones > 0: ones -= 1 ans.append(x + xx + 1) ans.append(y + yy + 1) mepas[x+xx][y+yy] = 0 elif mepas[x+xx][y+yy] == 0 and zeros > 0: zeros -= 1 ans.append(x + xx + 1) ans.append(y + yy + 1) mepas[x+xx][y+yy] = 1 return mepas, tuple(ans) def twoLeft(mepas, x, y): ones = 1 zeros = 2 ans = [] for xx in range(2): for yy in range(2): if mepas[x+xx][y+yy] == 1 and ones > 0: ones -= 1 ans.append(x + xx + 1) ans.append(y + yy + 1) mepas[x+xx][y+yy] = 0 elif mepas[x+xx][y+yy] == 0 and zeros > 0: zeros -= 1 ans.append(x + xx + 1) ans.append(y + yy + 1) mepas[x+xx][y+yy] = 1 return mepas, tuple(ans) T = int(input()) for t in range(T): X, Y = map(int, input().split()) mepas = [] ANS = [] for x in range(X): y = [int(c) for c in input()] mepas.append(y) # print(*mepas, sep="\n") if X % 2 == 1: x = 0 for y in range(Y-1): if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1): mepas, temporary = selectTiles(mepas, x, y) ANS.append(temporary) if Y % 2 == 1: y = 0 for x in range(X-1): if mepas[x][y] == 1 or (x == X - 2 and mepas[x+1][y] == 1): mepas, temporary = selectTiles2(mepas, x, y) ANS.append(temporary) for x in range(0, X-1, 2): x += X % 2 for y in range(0, Y-1, 2): y += Y % 2 # if x < X-2: # if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1): # mepas, temporary = selectTiles(mepas, x, y) # ans.append(temporary) # else: # if mepas[x][y] == 1 or mepas[x+1][y] == 1: # mepas, temporary = selectTiles2(mepas, x, y) # ans.append(temporary) mepas, temporary = finishingTouch(mepas, x, y) ANS = ANS + temporary print(len(ANS)) for a in ANS: print(*a) # print(*mepas, sep="\n") ```
output
1
41,470
12
82,941
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
41,471
12
82,942
Tags: constructive algorithms, graphs, greedy, implementation Correct Solution: ``` ''' Auther: ghoshashis545 Ashis Ghosh College: jalpaiguri Govt Enggineering College ''' from os import path import sys from heapq import heappush,heappop from functools import cmp_to_key as ctk from collections import deque,Counter,defaultdict as dd from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right from itertools import permutations from datetime import datetime from math import ceil,sqrt,log,gcd def ii():return int(input()) def si():return input().rstrip() def mi():return map(int,input().split()) def li():return list(mi()) abc='abcdefghijklmnopqrstuvwxyz' # mod=1000000007 mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def bo(i): return ord(i)-ord('0') file = 1 def solve(): for _ in range(ii()): n,m = mi() a = [] for i in range(n): s = si() p = [] for j in s: p.append(int(j)) a.append(p) def change(p): for i in range(0,5,2): a[p[i]][p[i+1]] ^= 1 ans = [] for i in range(n-2): for j in range(m): if a[i][j] == 0: if j==0 or a[i][j-1]==0: continue else: ans.append([i,j-1,i+1,j-1,i+1,j]) change(ans[-1]) else: if j > 0 and a[i][j-1]==1: if a[i+1][j-1] == 1: ans.append([i,j,i+1,j-1,i,j-1]) else: ans.append([i,j,i,j-1,i+1,j]) elif(j < m-1 and (a[i][j+1] or a[i+1][j+1])): if a[i][j+1]==1: if a[i+1][j]==1: ans.append([i,j,i+1,j,i,j+1]) else: ans.append([i,j,i+1,j+1,i,j+1]) else: ans.append([i,j,i+1,j,i+1,j+1]) else: if j > 0: ans.append([i,j,i+1,j-1,i+1,j]) else: ans.append([i,j,i+1,j+1,i+1,j]) change(ans[-1]) def count2(x1,y1,x2,y2): o,z = [],[] for i in range(x1,x2+1): for j in range(y1,y2+1): if a[i][j]: o.append(i) o.append(j) else: z.append(i) z.append(j) p = [] ans.append(z+o[:2]) change(ans[-1]) ans.append(z+o[2:]) change(ans[-1]) def count1(x1,y1,x2,y2): p = [] cnt = 0 for i in range(x1,x2+1): for j in range(y1,y2+1): if a[i][j]: p.append(i) p.append(j) elif(cnt < 2): cnt +=1 p.append(i) p.append(j) ans.append(p) change(ans[-1]) count2(x1,y1,x2,y2) def func(x1,y1,x2,y2): cnt = 0 for i in range(x1,x2+1): for j in range(y1,y2+1): cnt += a[i][j] if cnt == 0: return if cnt == 3: p = [] for i in range(x1,x2+1): for j in range(y1,y2+1): if a[i][j]: p.append(i) p.append(j) ans.append(p) change(ans[-1]) elif(cnt == 2): count2(x1,y1,x2,y2) elif(cnt == 1): count1(x1,y1,x2,y2) else: p = [] for i in range(x1,x2+1): for j in range(y1,y2+1): if a[i][j]: p.append(i) p.append(j) if len(p)==6: break ans.append(p) change(ans[-1]) count1(x1,y1,x2,y2) if m&1: if a[n-2][m-1] and a[n-1][m-1]: ans.append([n-2,m-1,n-1,m-1,n-2,m-2]) change(ans[-1]) elif a[n-2][m-1]: ans.append([n-2,m-1,n-1,m-2,n-2,m-2]) change(ans[-1]) elif a[n-1][m-1]: ans.append([n-2,m-2,n-1,m-1,n-1,m-2]) change(ans[-1]) for i in range(0,m-1,2): func(n-2,i,n-1,i+1) print(len(ans)) for i in ans: for j in i: print(j+1,end=" ") print() if __name__ =="__main__": if(file): if path.exists('input.txt'): sys.stdin=open('input.txt', 'r') sys.stdout=open('output.txt','w') else: input=sys.stdin.readline solve() ```
output
1
41,471
12
82,943
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed, that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
41,472
12
82,944
Tags: constructive algorithms, graphs, greedy, implementation Correct Solution: ``` #!/usr/bin/env python3.9 def inverse_1_2x2(i, j): ''' inverse one '1' in square 2x2, cost 3 ''' res = [] fstr = '{} {} {} {} {} {}' step_i, step_j = 1, 1 if i % 2 == 0: step_i = -1 if j % 2 == 0: step_j = -1 res.append(fstr.format(i+step_i, j+step_j, i+step_i, j, i, j)) res.append(fstr.format(i+step_i, j, i, j, i, j+step_j)) res.append(fstr.format(i, j, i, j+step_j, i+step_i, j+step_j)) return res def inverse_2_2x2(i, j, x1, x2): ''' inverse two '1' in square 2x2, cost 4 1) xx oo oo -> xo -> inverse_1 2) xo ox ox -> oo -> inverse_1 ''' # x1, x2 = *ones # x2 bigger than x1 # x3_delta = (0, 0) delta = x2[0] - x1[0], x2[1] - x1[1] # if sum(delta) == 1: if delta[0] == 0 or delta[1] == 0: # variant 1: choose another neigh of x1 if x1[0] == i and x1[1] == j: x3 = x1[0] + delta[1], x1[1] + delta[0] else: x3 = x1[0] - delta[1], x1[1] - delta[0] else: # variant 2: take down(i+1) neigh of x1 x3 = x1[0] + 1, x1[1] res = [] fstr = '{} {} {} {} {} {}' res.append(fstr.format(*x1, *x2, *x3)) res += inverse_1_2x2(*x3) return res def inverce_cell(i, j, a): i -= 1 j -= 1 if a[i][j] == '1': a[i][j] = '0' else: a[i][j] = '1' def process_last_row(a): res = [] fstr = '{} {} {} {} {} {}' i = len(a) # last row j = 1 while j < len(a[-1])-1: if a[i-1][j-1] == '1': res.append(fstr.format(i, j, i-1, j, i-1, j+1)) # inverce_cell(i, j, a) inverce_cell(i-1, j, a) inverce_cell(i-1, j+1, a) j += 1 # i, j points to (-1, -2) cell if a[i-1][j-1] == '1': # num_ops += 1 if a[i-1][j] == '1': # 11 -> L shape res.append(fstr.format(i, j, i, j+1, i-1, j)) # inverce_cell(i, j, a) # inverce_cell(i, j+1, a) inverce_cell(i-1, j, a) else: # 10 -> Π“ shape res.append(fstr.format(i, j, i-1, j, i-1, j+1)) # inverce_cell(i, j, a) inverce_cell(i-1, j, a) inverce_cell(i-1, j+1, a) else: if a[i-1][j] == '1': # 01 -> κ“Ά res.append(fstr.format(i, j+1, i-1, j+1, i-1, j)) # inverce_cell(i, j+1, a) inverce_cell(i-1, j+1, a) inverce_cell(i-1, j, a) return res def process_last_col(a): res = [] fstr = '{} {} {} {} {} {}' j = len(a[0]) # last column for i in range(1, len(a), 2): if a[i-1][j-1] == '1' and a[i][j-1] == '1': res.append(fstr.format(i, j, i, j-1, i+1, j-1)) res.append(fstr.format(i, j-1, i+1, j-1, i+1, j)) # inverce_cell(i, j, a) # inverce_cell(i+1, j, a) elif a[i-1][j-1] == '1': res.append(fstr.format(i, j, i, j-1, i+1, j-1)) # inverce_cell(i, j, a) inverce_cell(i, j-1, a) inverce_cell(i+1, j-1, a) elif a[i][j-1] == '1': res.append(fstr.format(i, j-1, i+1, j-1, i+1, j)) inverce_cell(i, j-1, a) inverce_cell(i+1, j-1, a) # inverce_cell(i+1, j, a) return res for _ in range(int(input())): n, m = list(map(int, input().split(' '))) a = [list(input()) for _ in range(n)] # num_ops = 0 out = [] if n % 2 == 1: out += process_last_row(a) # [print(*row) for row in a] # print() if m % 2 == 1: # proceed only even num_cells out += process_last_col(a) # num_ops += len(col_ops) # out += col_ops # [print(*row) for row in a] for blk_i in range(n//2): i = 2*blk_i + 1 for blk_j in range(m//2): j = 2*blk_j + 1 ones = [ (i+ii, j+jj) for ii in range(2) for jj in range(2) if a[i+ii-1][j+jj-1] == '1' ] if len(ones) == 1: x1 = ones.pop() out += inverse_1_2x2(*x1) # num_ops += 3 elif len(ones) == 2: out += inverse_2_2x2(i, j, *ones) # num_ops += 4 elif len(ones) == 3: x1, x2, x3 = ones # print(x1, x2, x3) out.append('{} {} {} {} {} {}'.format(*x1, *x2, *x3)) # num_ops += 1 elif len(ones) == 4: x1, x2, x3, x4 = ones out.append( '{} {} {} {} {} {}'.format(*x1, *x2, *x3)) out += inverse_1_2x2(*x4) # num_ops += 4 # print(out) print(len(out)) print('\n'.join(out)) ```
output
1
41,472
12
82,945
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The only difference is that in this version q = 1. You can make hacks only if both versions of the problem are solved. There is a process that takes place on arrays a and b of length n and length n-1 respectively. The process is an infinite sequence of operations. Each operation is as follows: * First, choose a random integer i (1 ≀ i ≀ n-1). * Then, simultaneously set a_i = min\left(a_i, \frac{a_i+a_{i+1}-b_i}{2}\right) and a_{i+1} = max\left(a_{i+1}, \frac{a_i+a_{i+1}+b_i}{2}\right) without any rounding (so values may become non-integer). See notes for an example of an operation. It can be proven that array a converges, i. e. for each i there exists a limit a_i converges to. Let function F(a, b) return the value a_1 converges to after a process on a and b. You are given array b, but not array a. However, you are given a third array c. Array a is good if it contains only integers and satisfies 0 ≀ a_i ≀ c_i for 1 ≀ i ≀ n. Your task is to count the number of good arrays a where F(a, b) β‰₯ x for q values of x. Since the number of arrays can be very large, print it modulo 10^9+7. Input The first line contains a single integer n (2 ≀ n ≀ 100). The second line contains n integers c_1, c_2 …, c_n (0 ≀ c_i ≀ 100). The third line contains n-1 integers b_1, b_2, …, b_{n-1} (0 ≀ b_i ≀ 100). The fourth line contains a single integer q (q=1). The fifth line contains q space separated integers x_1, x_2, …, x_q (-10^5 ≀ x_i ≀ 10^5). Output Output q integers, where the i-th integer is the answer to the i-th query, i. e. the number of good arrays a where F(a, b) β‰₯ x_i modulo 10^9+7. Example Input 3 2 3 4 2 1 1 -1 Output 56 Note The following explanation assumes b = [2, 1] and c=[2, 3, 4] (as in the sample). Examples of arrays a that are not good: * a = [3, 2, 3] is not good because a_1 > c_1; * a = [0, -1, 3] is not good because a_2 < 0. One possible good array a is [0, 2, 4]. We can show that no operation has any effect on this array, so F(a, b) = a_1 = 0. Another possible good array a is [0, 1, 4]. In a single operation with i = 1, we set a_1 = min((0+1-2)/(2), 0) and a_2 = max((0+1+2)/(2), 1). So, after a single operation with i = 1, a becomes equal to [-1/2, 3/2, 4]. We can show that no operation has any effect on this array, so F(a, b) = -1/2.
instruction
0
41,496
12
82,992
Tags: dp, math Correct Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() N = int(input()) C = [int(a) for a in input().split()] B = [int(a) for a in input().split()] Q = int(input()) x = int(input()) P = 10 ** 9 + 7 dp = [[0] * 20100 for _ in range(N + 1)] dp[0][0] = 1 ans = 0 s = x t = s for i in range(N): for j in range(20050, t - 1, -1): if j < 0: break dp[i+1][j] = (dp[i+1][j+1] + dp[i][max(j-C[i], 0)] - dp[i][j+1]) % P for j in range(min(t - 1, 20050), -1, -1): dp[i+1][j] = dp[i+1][j+1] if i < N - 1: s += B[i] t += s print(dp[-1][0] % P) ```
output
1
41,496
12
82,993
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The only difference is that in this version q = 1. You can make hacks only if both versions of the problem are solved. There is a process that takes place on arrays a and b of length n and length n-1 respectively. The process is an infinite sequence of operations. Each operation is as follows: * First, choose a random integer i (1 ≀ i ≀ n-1). * Then, simultaneously set a_i = min\left(a_i, \frac{a_i+a_{i+1}-b_i}{2}\right) and a_{i+1} = max\left(a_{i+1}, \frac{a_i+a_{i+1}+b_i}{2}\right) without any rounding (so values may become non-integer). See notes for an example of an operation. It can be proven that array a converges, i. e. for each i there exists a limit a_i converges to. Let function F(a, b) return the value a_1 converges to after a process on a and b. You are given array b, but not array a. However, you are given a third array c. Array a is good if it contains only integers and satisfies 0 ≀ a_i ≀ c_i for 1 ≀ i ≀ n. Your task is to count the number of good arrays a where F(a, b) β‰₯ x for q values of x. Since the number of arrays can be very large, print it modulo 10^9+7. Input The first line contains a single integer n (2 ≀ n ≀ 100). The second line contains n integers c_1, c_2 …, c_n (0 ≀ c_i ≀ 100). The third line contains n-1 integers b_1, b_2, …, b_{n-1} (0 ≀ b_i ≀ 100). The fourth line contains a single integer q (q=1). The fifth line contains q space separated integers x_1, x_2, …, x_q (-10^5 ≀ x_i ≀ 10^5). Output Output q integers, where the i-th integer is the answer to the i-th query, i. e. the number of good arrays a where F(a, b) β‰₯ x_i modulo 10^9+7. Example Input 3 2 3 4 2 1 1 -1 Output 56 Note The following explanation assumes b = [2, 1] and c=[2, 3, 4] (as in the sample). Examples of arrays a that are not good: * a = [3, 2, 3] is not good because a_1 > c_1; * a = [0, -1, 3] is not good because a_2 < 0. One possible good array a is [0, 2, 4]. We can show that no operation has any effect on this array, so F(a, b) = a_1 = 0. Another possible good array a is [0, 1, 4]. In a single operation with i = 1, we set a_1 = min((0+1-2)/(2), 0) and a_2 = max((0+1+2)/(2), 1). So, after a single operation with i = 1, a becomes equal to [-1/2, 3/2, 4]. We can show that no operation has any effect on this array, so F(a, b) = -1/2.
instruction
0
41,497
12
82,994
Tags: dp, math Correct Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() N = int(input());C = [int(a) for a in input().split()];B = [int(a) for a in input().split()];Q = int(input());x = int(input());P = 10 ** 9 + 7; dp = [[0] * 20100 for _ in range(N + 1)];dp[0][0] = 1;ans = 0;s = x;t = s for i in range(N): for j in range(20050, t - 1, -1): if j < 0: break dp[i+1][j] = (dp[i+1][j+1] + dp[i][max(j-C[i], 0)] - dp[i][j+1]) % P for j in range(min(t - 1, 20050), -1, -1):dp[i+1][j] = dp[i+1][j+1] if i < N - 1:s += B[i];t += s print(dp[-1][0] % P) ```
output
1
41,497
12
82,995
Provide tags and a correct Python 3 solution for this coding contest problem. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3
instruction
0
41,520
12
83,040
Tags: implementation, sortings Correct Solution: ``` import sys input = sys.stdin.readline def prog(): n = int(input()) cs = list(map(int,input().split())) nums = [] locations = {} locations2 = {} i = 1 for c in cs: curr_nums = list(map(int,input().split())) for j in range(c): num = curr_nums[j] nums.append(num) if num in locations: locations[num].append([i,j+1]) else: locations[num] = [[i,j+1]] locations2[(i,j+1)] = num i += 1 nums.sort() swaps = [] i = 1 idx = 0 for c in cs: for j in range(c): if locations2[(i,j+1)] != nums[idx]: other = locations[nums[idx]][-1] swaps.append([i,j+1,other[0],other[1]]) locations[nums[idx]].pop() del locations[locations2[(i,j+1)]][locations[locations2[(i,j+1)]].index([i,j+1])] locations[locations2[(i,j+1)]].append(other) locations2[tuple(other)] = locations2[(i,j+1)] locations2[(i,j+1)] = nums[idx] idx += 1 i += 1 print(len(swaps)) for swap in swaps: print(*swap) prog() ```
output
1
41,520
12
83,041
Provide tags and a correct Python 3 solution for this coding contest problem. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3
instruction
0
41,521
12
83,042
Tags: implementation, sortings Correct Solution: ``` n, k = int(input()), 1 c = [0] + list(map(int, input().split())) p, q, t = [0] * (sum(c) + 1), {}, [] for i in range(1, n + 1): for j, x in enumerate(tuple(map(int, input().split())), 1): p[x], q[(i, j)] = (i, j), x for i in range(1, n + 1): for j in range(1, c[i] + 1): if p[k] != (i, j): x, y = q[(i, j)], p[k] t.append(str(i) + ' ' + str(j) + ' ' + str(y[0]) + ' ' + str(y[1])) p[x], q[y] = y, x k += 1 print(len(t)) print('\n'.join(t)) ```
output
1
41,521
12
83,043
Provide tags and a correct Python 3 solution for this coding contest problem. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3
instruction
0
41,522
12
83,044
Tags: implementation, sortings Correct Solution: ``` n = int(input()) I = lambda : map(int,input().split()) li = list (I()) dd = {} arr = [ [0 for i in range(51)] for j in range(51) ] l2 = [ ] c=0 for i in range (1,n+1) : l1 = list(I()) l2 = l2 + l1 c = c + len(l1) for j in range(li[i-1]) : arr[i][j+1] = l1[j] dd[l1[j]] = [i , j+1] #print(dd) #print(l2) l3 = l2[:] l3.sort() #print(l3) ans= 0 d1 = {} for i,j in enumerate(l2) : d1[j]=i #print(d1) answer = [ ] #print(dd) #print(l2) for i in range(c) : if l2[i]!=l3[i] : t1 = l2[i] t2 = i+1 #print(t1,t2) #temp = dd[t1] #temp1 = dd[t2] answer = answer + [dd[t1] + dd[t2]] l2[i] = i+1 l2[d1[t2]] = t1 d1[t1] = d1[t2] d1[i+1] = i temp2 = dd[t1] dd[t1] = dd[t2] dd[t2] = temp2 ans+=1 #print(l2) print(ans) for i in range(ans) : print(*answer[i]) ```
output
1
41,522
12
83,045
Provide tags and a correct Python 3 solution for this coding contest problem. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3
instruction
0
41,523
12
83,046
Tags: implementation, sortings Correct Solution: ``` n, k = int(input()), 1 c = [0] + list(map(int, input().split())) p, q, t = [0] * (sum(c) + 1), {}, [] for i in range(1, n + 1): for j, x in enumerate(tuple(map(int, input().split())), 1): p[x], q[(i, j)] = (i, j), x for i in range(1, n + 1): for j in range(1, c[i] + 1): if p[k] != (i, j): x, y = q[(i, j)], p[k] t.append(str(i) + ' ' + str(j) + ' ' + str(y[0]) + ' ' + str(y[1])) p[x], q[y] = y, x k += 1 print(len(t)) print('\n'.join(t)) # Made By Mostafa_Khaled ```
output
1
41,523
12
83,047
Provide tags and a correct Python 3 solution for this coding contest problem. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3
instruction
0
41,524
12
83,048
Tags: implementation, sortings Correct Solution: ``` def find(a,p,n): for i in range(n): l = len(a[i]) for j in range(l): if a[i][j]==p: return [i+1,j+1] n = int(input()) b = list(map(int,input().split())) a = [] for i in range(n): c = list(map(int,input().split())) a.append(c) p = 1 d = [] for i in range(n): for j in range(b[i]): k = [i+1,j+1] z = find(a,p,n) if k==z: p+=1 continue else: d.append([k,z]) a[z[0]-1][z[1]-1]=a[i][j] a[i][j]=p p+=1 print(len(d)) for i in d: print(i[0][0],i[0][1],i[1][0],i[1][1]) ```
output
1
41,524
12
83,049
Provide tags and a correct Python 3 solution for this coding contest problem. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3
instruction
0
41,525
12
83,050
Tags: implementation, sortings Correct Solution: ``` n, p, l, v = int(input()), [[0, 0]], [0], [] for i, c in enumerate(map(int, input().split())): p.extend([[i + 1, j + 1] for j in range(c)]) l.extend(list(map(int, input().split()))) for i in range(1, len(l)): if l[i] != i: j = l.index(i) v.append(p[i] + p[j]) l[i], l[j] = l[j], l[i] print(len(v)) for x in v: print(' '.join(map(str, x))) ```
output
1
41,525
12
83,051
Provide tags and a correct Python 3 solution for this coding contest problem. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3
instruction
0
41,526
12
83,052
Tags: implementation, sortings Correct Solution: ``` n = int(input()) col=list(map(int,input().split())) arr = [list(map(int, input().split())) for _ in range(n)] mm=sum(col) ans=[(0,0)]*(mm+1) res=[] for i in range(n): for j in range(col[i]): ans[arr[i][j]]=(i,j) #print(ans) b=0 for i in range(n): for j in range(col[i]): b+=1 if arr[i][j]!=b: res.append([i,j,ans[b][0],ans[b][1]]) arr[ans[b][0]][ans[b][1]]=arr[i][j] ans[arr[i][j]]=ans[b] print(len(res)) for i in res: for j in i: print(j+1,end=' ') print() ```
output
1
41,526
12
83,053
Provide tags and a correct Python 3 solution for this coding contest problem. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3
instruction
0
41,527
12
83,054
Tags: implementation, sortings Correct Solution: ``` n=int(input()) p=[[0,0]] l=[0] v=[] for i, c in enumerate(map(int, input().split())): p.extend([[i + 1, j + 1] for j in range(c)]) l.extend(list(map(int, input().split()))) for i in range(1, len(l)): if l[i] != i: j = l.index(i) v.append(p[i] + p[j]) l[i], l[j] = l[j], l[i] print(len(v)) for x in v: print(' '.join(map(str, x))) ```
output
1
41,527
12
83,055
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3 Submitted Solution: ``` def search(t): for i in range(n - 1, -1, -1): for j in range(len(mas[i])): if mas[i][j] == t: return (i, j) def print1(a, b, c, d): ans.append((a, b, c, d)) n = int(input()) counts = list(map(int, input().split())) d = dict() need = dict() mas = [[] for i in range(n)] s = sum(counts) for i in range(n): mas[i] = list(map(int, input().split())) t = 1 ans = [] p = sum(counts) for i in range(n): for j in range(counts[i]): pos = search(t); if (pos != (i,j)): mas[i][j], mas[pos[0]][pos[1]] = mas[pos[0]][pos[1]], mas[i][j] print1(i + 1, j + 1, pos[0] + 1, pos[1] + 1) if t == p + 1: break t += 1 if t == p + 1: break print(len(ans)) for i in ans: print(*i) ```
instruction
0
41,528
12
83,056
Yes
output
1
41,528
12
83,057
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3 Submitted Solution: ``` import math import re from fractions import Fraction from collections import Counter class Task: table = [] operationCounter = 0 answer = [] def __init__(self): numberOfRow = int(input()) input() for i in range(numberOfRow): self.table += [[int(x) for x in input().split()]] def solve(self): table = self.table ceils = [] for row in range(0, len(table)): for column in range(0, len(table[row])): ceils += [[table[row][column], row, column]] for start in range(0, len(ceils) - 1): positionOfMin = start for i in range(start + 1, len(ceils)): if ceils[i][0] < ceils[positionOfMin][0]: positionOfMin = i if positionOfMin == start: continue tmp = ceils[start][0] ceils[start][0] = ceils[positionOfMin][0] ceils[positionOfMin][0] = tmp transposition = ceils[start][1:] + ceils[positionOfMin][1:] self.answer += [[x + 1 for x in transposition]] self.operationCounter += 1 def printAnswer(self): print(self.operationCounter) for line in self.answer: print(re.sub('[\[\],]', '', str(line))) task = Task() task.solve() task.printAnswer() ```
instruction
0
41,529
12
83,058
Yes
output
1
41,529
12
83,059
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3 Submitted Solution: ``` def find(A,target): for i in range(len(A)): for j in range(len(A[i])): if A[i][j] == target: return [i+1,j+1] n = int(input()) C = list(map(int,input().split())) X = [] for i in range(n): Y = list(map(int,input().split())) X.append(Y) cnt = 1 tot = 0 ans = [] for i in range(len(X)): for j in range(len(X[i])): t = find(X,cnt) X[t[0]-1][t[1]-1],X[i][j] = X[i][j],X[t[0]-1][t[1]-1] if t[0]!=i +1 or t[1]!=j+1: ans.append(str(i+1)+" " + str(j+1) + " " + str(t[0]) + " " + str(t[1])) tot += 1 cnt += 1 print(tot) for i in ans: print(i) ```
instruction
0
41,530
12
83,060
Yes
output
1
41,530
12
83,061
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3 Submitted Solution: ``` n = int(input()) c = list(map(int, input().split())) arr = [list(map(int, input().split())) for _ in range(n)] s = sum(c) f, b, res = [(0, 0)] * (s + 1), 0, [] for i in range(n): for j in range(c[i]): f[arr[i][j]] = (i, j) for i in range(n): for j in range(c[i]): b += 1 if arr[i][j] != b: res.append(((i, j), f[b])) arr[f[b][0]][f[b][1]] = arr[i][j] f[arr[i][j]] = f[b] print(len(res)) for f, t in res: print(f[0] + 1, f[1] + 1, t[0] + 1, t[1] + 1) ```
instruction
0
41,531
12
83,062
Yes
output
1
41,531
12
83,063
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3 Submitted Solution: ``` n, k = int(input()), 1 c = [0] + list(map(int, input().split())) p, q, t = [0] * (sum(c) + 1), {}, [] for i in range(1, n + 1): s = str(i) + ' ' for j, x in enumerate(tuple(map(int, input().split())), 1): y = s + str(j) p[x], q[y] = y, x for y in q: a, b = q[y], p[k] if p[k] != y: t.append(y + ' ' + b) p[a], q[b] = b, a k += 1 print(len(t)) print('\n'.join(t)) ```
instruction
0
41,532
12
83,064
No
output
1
41,532
12
83,065
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3 Submitted Solution: ``` import math import re from fractions import Fraction from collections import Counter class Task: table = [] operationCounter = 0 answer = [] def __init__(self): numberOfRow = int(input()) input() for i in range(numberOfRow): self.table += [[int(x) for x in input().split()]] def solve(self): table = self.table ceils = [] for row in range(0, len(table)): for column in range(0, len(table[row])): ceils += [[table[row][column], row, column]] for start in range(0, len(ceils) - 1): positionOfMin = start for i in range(start, len(ceils)): if ceils[i][0] < ceils[positionOfMin][0]: positionOfMin = i tmp = ceils[start] ceils[start] = ceils[positionOfMin] ceils[positionOfMin] = tmp transposition = ceils[start][1:] + ceils[positionOfMin][1:] self.answer += [[x + 1 for x in transposition]] self.operationCounter += 1 def printAnswer(self): print(self.operationCounter) for line in self.answer: print(re.sub('[\[\],]', '', str(line))) task = Task() task.solve() task.printAnswer() ```
instruction
0
41,533
12
83,066
No
output
1
41,533
12
83,067
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3 Submitted Solution: ``` def find(A,target): for i in range(len(A)): for j in range(len(A[i])): if A[i][j] == target: return [i+1,j+1] n = int(input()) C = list(map(int,input().split())) X = [] for i in range(n): Y = list(map(int,input().split())) X.append(Y) cnt = 1 print(sum(C)) for i in range(len(X)): for j in range(len(X[i])): t = find(X,cnt) X[t[0]-1][t[1]-1],X[i][j] = X[i][j],X[t[0]-1][t[1]-1] print(str(i+1)+" " + str(j+1) + " " + str(t[0]) + " " + str(t[1])) cnt += 1 ```
instruction
0
41,534
12
83,068
No
output
1
41,534
12
83,069
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≀ n) holds ci ≀ ci - 1. Let's denote s as the total number of cells of table a, that is, <image>. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≀ n; 1 ≀ j ≀ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≀ i ≀ n; 1 < j ≀ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations. Input The first line contains a single integer n (1 ≀ n ≀ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≀ ci ≀ 50; ci ≀ ci - 1) β€” the numbers of cells on the corresponding rows. Next n lines contain table Π°. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct. Output In the first line print a single integer m (0 ≀ m ≀ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≀ xi, pi ≀ n; 1 ≀ yi ≀ cxi; 1 ≀ qi ≀ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. Examples Input 3 3 2 1 4 3 5 6 1 2 Output 2 1 1 2 2 2 1 3 1 Input 1 4 4 3 2 1 Output 2 1 1 1 4 1 2 1 3 Submitted Solution: ``` n = int(input()) I = lambda : map(int,input().split()) li = list (I()) dd = {} arr = [ [0 for i in range(51)] for j in range(51) ] l2 = [ ] c=0 for i in range (1,n+1) : l1 = list(I()) l2 = l2 + l1 c = c + len(l1) for j in range(li[i-1]) : arr[i][j+1] = l1[j] dd[l1[j]] = [i , j+1] #print(dd) #print(l2) l3 = l2[:] l3.sort() #print(l3) ans= 0 d1 = {} for i,j in enumerate(l2) : d1[j]=i #print(d1) answer = [ ] for i in range(c-1) : if l2[i]!=l3[i] : t1 = l2[i] t2 = l3[i] ans+=1 answer = answer + [dd[l2[i]] + dd[l3[i]]] #print(*dd[l2[i]] , *dd[l3[i]]) temp = d1[i+1] l2[temp]=t1 temp = dd[l2[i]] dd[l2[i]] = dd[l3[i]] dd[l3[i]] = temp #print(ans) #print(dd) #print(l2) print(max(ans,0)) for i in answer[:] : print(*i) ```
instruction
0
41,535
12
83,070
No
output
1
41,535
12
83,071
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn. We'll call position i (1 ≀ i ≀ n) in permutation p1, p2, ..., pn good, if |p[i] - i| = 1. Count the number of permutations of size n with exactly k good positions. Print the answer modulo 1000000007 (109 + 7). Input The single line contains two space-separated integers n and k (1 ≀ n ≀ 1000, 0 ≀ k ≀ n). Output Print the number of permutations of length n with exactly k good positions modulo 1000000007 (109 + 7). Examples Input 1 0 Output 1 Input 2 1 Output 0 Input 3 2 Output 4 Input 4 1 Output 6 Input 7 4 Output 328 Note The only permutation of size 1 has 0 good positions. Permutation (1, 2) has 0 good positions, and permutation (2, 1) has 2 positions. Permutations of size 3: 1. (1, 2, 3) β€” 0 positions 2. <image> β€” 2 positions 3. <image> β€” 2 positions 4. <image> β€” 2 positions 5. <image> β€” 2 positions 6. (3, 2, 1) β€” 0 positions
instruction
0
41,536
12
83,072
Tags: combinatorics, dp, math Correct Solution: ``` # METO Bot 0.9.9 mod=10**9+7 n,k=map(int,input().split()) A=[0]*(n+1) B=[0]*(n+1) C=[0]*(n+1) F=[0]*(n+1) G=[0]*(n+1) F[0]=G[0]=1 for i in range(1,n+1): G[i]=F[i]=F[i-1]*i%mod G[i]=pow(F[i],(mod-2),mod) for i in range(0,n): if i*2>n: break B[i]=(F[n-i]*G[i]*G[n-i*2])%mod for i in range(0,n//2+1): for j in range(0,n//2+1): A[i+j]=(A[i+j]+B[i]*B[j])%mod for i in range(0,n+1): A[i]=A[i]*F[n-i]%mod for i in range(0,n+1): for j in range(0,i+1): C[j]=(C[j]+A[i]*F[i]*G[j]*G[i-j]*(1-(i-j)%2*2))%mod print(C[k]%mod) ```
output
1
41,536
12
83,073
Provide tags and a correct Python 3 solution for this coding contest problem. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn. We'll call position i (1 ≀ i ≀ n) in permutation p1, p2, ..., pn good, if |p[i] - i| = 1. Count the number of permutations of size n with exactly k good positions. Print the answer modulo 1000000007 (109 + 7). Input The single line contains two space-separated integers n and k (1 ≀ n ≀ 1000, 0 ≀ k ≀ n). Output Print the number of permutations of length n with exactly k good positions modulo 1000000007 (109 + 7). Examples Input 1 0 Output 1 Input 2 1 Output 0 Input 3 2 Output 4 Input 4 1 Output 6 Input 7 4 Output 328 Note The only permutation of size 1 has 0 good positions. Permutation (1, 2) has 0 good positions, and permutation (2, 1) has 2 positions. Permutations of size 3: 1. (1, 2, 3) β€” 0 positions 2. <image> β€” 2 positions 3. <image> β€” 2 positions 4. <image> β€” 2 positions 5. <image> β€” 2 positions 6. (3, 2, 1) β€” 0 positions
instruction
0
41,537
12
83,074
Tags: combinatorics, dp, math Correct Solution: ``` mod=10**9+7 n,k=map(int,input().split()) A=[0]*(n+1) B=[0]*(n+1) C=[0]*(n+1) F=[0]*(n+1) G=[0]*(n+1) F[0]=G[0]=1 for i in range(1,n+1): G[i]=F[i]=F[i-1]*i%mod G[i]=pow(F[i],(mod-2),mod) for i in range(0,n): if i*2>n: break B[i]=(F[n-i]*G[i]*G[n-i*2])%mod for i in range(0,n//2+1): for j in range(0,n//2+1): A[i+j]=(A[i+j]+B[i]*B[j])%mod for i in range(0,n+1): A[i]=A[i]*F[n-i]%mod for i in range(0,n+1): for j in range(0,i+1): C[j]=(C[j]+A[i]*F[i]*G[j]*G[i-j]*(1-(i-j)%2*2))%mod print(C[k]%mod) ```
output
1
41,537
12
83,075
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn. We'll call position i (1 ≀ i ≀ n) in permutation p1, p2, ..., pn good, if |p[i] - i| = 1. Count the number of permutations of size n with exactly k good positions. Print the answer modulo 1000000007 (109 + 7). Input The single line contains two space-separated integers n and k (1 ≀ n ≀ 1000, 0 ≀ k ≀ n). Output Print the number of permutations of length n with exactly k good positions modulo 1000000007 (109 + 7). Examples Input 1 0 Output 1 Input 2 1 Output 0 Input 3 2 Output 4 Input 4 1 Output 6 Input 7 4 Output 328 Note The only permutation of size 1 has 0 good positions. Permutation (1, 2) has 0 good positions, and permutation (2, 1) has 2 positions. Permutations of size 3: 1. (1, 2, 3) β€” 0 positions 2. <image> β€” 2 positions 3. <image> β€” 2 positions 4. <image> β€” 2 positions 5. <image> β€” 2 positions 6. (3, 2, 1) β€” 0 positions Submitted Solution: ``` for s in[*open(0)][2::2]:print(*map(int,s.split()[::-1])) ```
instruction
0
41,538
12
83,076
No
output
1
41,538
12
83,077
Provide tags and a correct Python 3 solution for this coding contest problem. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≀ k ≀ n ≀ 5Β·105) β€” the number of elements in a and the parameter k. The second line contains n integers ai (0 ≀ ai ≀ 106) β€” the elements of the array a. Output Print two integers l, r (1 ≀ l ≀ r ≀ n) β€” the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
instruction
0
41,657
12
83,314
Tags: binary search, data structures, two pointers Correct Solution: ``` ''' Created on Jan 25, 2016 @author: KANDARP ''' import sys n=int(sys.stdin.readline()); arr=[int(x) for x in input().split()]; sets=set(); result=[]; ans=0; l=0; r=0; for i in arr: if(i in sets): ans=ans+1; sets.clear(); result.append([l+1,r+1]); l=r+1; else: sets.add(i); r=r+1; if ans==0: print(-1); else: result[len(result)-1]=[result[len(result)-1][0],n]; print(ans); print('\n'.join('{0} {1}'.format(p, q) for (p, q) in result)); ```
output
1
41,657
12
83,315
Provide tags and a correct Python 3 solution for this coding contest problem. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≀ k ≀ n ≀ 5Β·105) β€” the number of elements in a and the parameter k. The second line contains n integers ai (0 ≀ ai ≀ 106) β€” the elements of the array a. Output Print two integers l, r (1 ≀ l ≀ r ≀ n) β€” the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
instruction
0
41,658
12
83,316
Tags: binary search, data structures, two pointers Correct Solution: ``` n=int(input()) l=list(map(int,input().split())) s=set() ans=[] for i in range(n): if l[i] in s: if ans: ans.append([ans[-1][1]+1,i+1]) else: ans=[[1,i+1]] s=set() else: s.add(l[i]) if ans and s: ans[-1][1]+=len(s) if ans: print(len(ans)) for i in ans: print(i[0],i[1]) else: print(-1) ```
output
1
41,658
12
83,317
Provide tags and a correct Python 3 solution for this coding contest problem. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≀ k ≀ n ≀ 5Β·105) β€” the number of elements in a and the parameter k. The second line contains n integers ai (0 ≀ ai ≀ 106) β€” the elements of the array a. Output Print two integers l, r (1 ≀ l ≀ r ≀ n) β€” the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
instruction
0
41,659
12
83,318
Tags: binary search, data structures, two pointers Correct Solution: ``` #!/bin/python3 import sys n = int(input()) a = list(map(int, input().split())) ans = []; sa = sorted(a); cmpr = {}; for i in range(0, len(a)): cmpr[sa[i]] = i; cid = [0 for i in range(0,n)]; id = 1; pi = 0 for i in range(0,n): if cid[cmpr[a[i]]] == id: ans.append(i + 1) id+=1 continue; cid[cmpr[a[i]]] = id if len(ans) == 0: print(-1) else: ans[-1] = n; print(len(ans)) ans = [0] + ans for i in range(0, len(ans) - 1): print(ans[i] + 1 , ans[i + 1] ) ```
output
1
41,659
12
83,319
Provide tags and a correct Python 3 solution for this coding contest problem. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≀ k ≀ n ≀ 5Β·105) β€” the number of elements in a and the parameter k. The second line contains n integers ai (0 ≀ ai ≀ 106) β€” the elements of the array a. Output Print two integers l, r (1 ≀ l ≀ r ≀ n) β€” the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
instruction
0
41,660
12
83,320
Tags: binary search, data structures, two pointers Correct Solution: ``` import os import sys from io import BytesIO, IOBase from itertools import combinations_with_replacement,permutations from collections import defaultdict # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # code region n = int(input()) arr = list(map(int,input().split())) freq = defaultdict(int) cur = arr[0] ind = 1 freq[cur]+=1 last = 1 ans = [] while ind<n: freq[arr[ind]]+=1 if freq[arr[ind]]==2: ans.append([last,ind+1]) freq = defaultdict(int) if ind+1<n: ind+=1 last = ind+1 else: ind+=1 if ans==[]: print(-1) else: ans[-1][1] = n print(len(ans)) for i in ans: print(*i) ```
output
1
41,660
12
83,321
Provide tags and a correct Python 3 solution for this coding contest problem. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≀ k ≀ n ≀ 5Β·105) β€” the number of elements in a and the parameter k. The second line contains n integers ai (0 ≀ ai ≀ 106) β€” the elements of the array a. Output Print two integers l, r (1 ≀ l ≀ r ≀ n) β€” the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
instruction
0
41,661
12
83,322
Tags: binary search, data structures, two pointers Correct Solution: ``` n, k = map(int, input().split()) a = [*map(int, input().split())] from collections import defaultdict d = defaultdict(int) l = 0 r = 1 ans = (0, 0) for i in a: d[i] += 1 if len(d) <= k: ans = (l, r) if ans[1] - ans[0] < r - l else ans else: while len(d) > k: d[a[l]] -= 1 if d[a[l]] == 0: del d[a[l]] l += 1 r += 1 ans = (ans[0] + 1, ans[1]) print(*ans) ```
output
1
41,661
12
83,323
Provide tags and a correct Python 3 solution for this coding contest problem. The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Input The first line contains two integers n, k (1 ≀ k ≀ n ≀ 5Β·105) β€” the number of elements in a and the parameter k. The second line contains n integers ai (0 ≀ ai ≀ 106) β€” the elements of the array a. Output Print two integers l, r (1 ≀ l ≀ r ≀ n) β€” the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
instruction
0
41,662
12
83,324
Tags: binary search, data structures, two pointers Correct Solution: ``` import sys inputNum=input() inputTypes=input().split(' ') set1=set() stack2=[] stack3=[] for i in range(len(inputTypes)): if len(set1)==0: set1.add(inputTypes[i]) stack2.append(str(i+1)) else: if inputTypes[i] in set1: stack2.append(str(i+1)) stack3.append(' '.join(stack2)) set1.clear() stack2.clear() else: set1.add(inputTypes[i]) if len(stack3)==0: print(-1) sys.exit() if len(set1)==0: print(len(stack3)) for x in stack3: print(x) else: print(len(stack3)) lastitem=str(stack3[-1].split()[-1]) for x in stack3: print(x.replace(lastitem,str(len(inputTypes)))) ```
output
1
41,662
12
83,325