Dataset Viewer
Auto-converted to Parquet
inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Polycarp has $n$ different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: "0001", "11", "0" and "0011100". Polycarp wants to offer his set of $n$ binary words to play a game "words". In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: "0101", "1", "10", "00", "00001". Word reversal is the operation of reversing the order of the characters. For example, the word "0111" after the reversal becomes "1110", the word "11010" after the reversal becomes "01011". Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that: the final set of $n$ words still contains different words (i.e. all words are unique); there is a way to put all words of the final set of words in the order so that the final sequence of $n$ words is consistent with the game rules. Polycarp wants to reverse minimal number of words. Please, help him. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains one integer $n$ ($1 \le n \le 2\cdot10^5$) — the number of words in the Polycarp's set. Next $n$ lines contain these words. All of $n$ words aren't empty and contains only characters '0' and '1'. The sum of word lengths doesn't exceed $4\cdot10^6$. All words are different. Guaranteed, that the sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed $4\cdot10^6$. -----Output----- Print answer for all of $t$ test cases in the order they appear. If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain $k$ ($0 \le k \le n$) — the minimal number of words in the set which should be reversed. The second line of the output should contain $k$ distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from $1$ to $n$ in the order they appear. If $k=0$ you can skip this line (or you can print an empty line). If there are many answers you can print any of them. -----Example----- Input 4 4 0001 1000 0011 0111 3 010 101 0 2 00000 00001 4 01 001 0001 00001 Output 1 3 -1 0 2 1 2
for _ in range(int(input())): n = int(input()) mass = [] zo = 0 oz = 0 zz = 0 oo = 0 ozs = [] zos = [] ozss = set() zoss = set() for j in range(n): k = input() mass.append(k) if k[0] == '0' and k[-1] == '1': zoss.add(k) zos.append(j + 1) zo += 1 elif k[0] == '1' and k[-1] == '0': ozss.add(k) ozs.append(j + 1) oz += 1 elif k[0] == '0' and k[-1] == '0': zz += 1 else: oo += 1 if zz and oo and not oz and not zo: print(-1) continue else: if zo > oz: print((zo - oz) // 2) ans = [] need = (zo - oz) // 2 i = 0 while need: zzz = mass[zos[i] - 1][len(mass[zos[i] - 1]) - 1:: -1] if zzz not in ozss: ans.append(zos[i]) need -= 1 i += 1 print(*ans) else: print((oz - zo) // 2) ans = [] need = (oz - zo) // 2 i = 0 while need: zzz = mass[ozs[i] - 1][len(mass[ozs[i] - 1]) - 1:: -1] if zzz not in zoss: ans.append(ozs[i]) need -= 1 i += 1 print(*ans)
Polycarp has $n$ different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: "0001", "11", "0" and "0011100". Polycarp wants to offer his set of $n$ binary words to play a game "words". In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: "0101", "1", "10", "00", "00001". Word reversal is the operation of reversing the order of the characters. For example, the word "0111" after the reversal becomes "1110", the word "11010" after the reversal becomes "01011". Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that: the final set of $n$ words still contains different words (i.e. all words are unique); there is a way to put all words of the final set of words in the order so that the final sequence of $n$ words is consistent with the game rules. Polycarp wants to reverse minimal number of words. Please, help him. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains one integer $n$ ($1 \le n \le 2\cdot10^5$) — the number of words in the Polycarp's set. Next $n$ lines contain these words. All of $n$ words aren't empty and contains only characters '0' and '1'. The sum of word lengths doesn't exceed $4\cdot10^6$. All words are different. Guaranteed, that the sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed $4\cdot10^6$. -----Output----- Print answer for all of $t$ test cases in the order they appear. If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain $k$ ($0 \le k \le n$) — the minimal number of words in the set which should be reversed. The second line of the output should contain $k$ distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from $1$ to $n$ in the order they appear. If $k=0$ you can skip this line (or you can print an empty line). If there are many answers you can print any of them. -----Example----- Input 4 4 0001 1000 0011 0111 3 010 101 0 2 00000 00001 4 01 001 0001 00001 Output 1 3 -1 0 2 1 2
k = int(input()) for i in range(k): is_t = set() a = dict() a['00'] = [] a['11'] = [] a['01'] = [] a['10'] = [] n = int(input()) s = [] for i in range(n): b = input() a[b[0] + b[-1]].append(i) s.append(b) is_t.add(b) c = len(a['10']) d = len(a['01']) if c + d == 0: if len(a['00']) == 0 or len(a['11']) == 0: print(0) else: print(-1) elif c > d: ans = [] i = 0 m = (d + c) // 2 while d != m and i < len(a['10']): s1 = s[a['10'][i]] if s1[::-1] not in is_t: d += 1 ans.append(a['10'][i] + 1) i += 1 if d != m: print(-1) else: print(len(ans)) print(*ans) else: ans = [] i = 0 m = (d + c) // 2 while c != m and i < len(a['01']): s1 = s[a['01'][i]] if s1[::-1] not in is_t: c += 1 ans.append(a['01'][i] + 1) i += 1 if c != m: print(-1) else: print(len(ans)) print(*ans)
Polycarp has $n$ different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: "0001", "11", "0" and "0011100". Polycarp wants to offer his set of $n$ binary words to play a game "words". In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: "0101", "1", "10", "00", "00001". Word reversal is the operation of reversing the order of the characters. For example, the word "0111" after the reversal becomes "1110", the word "11010" after the reversal becomes "01011". Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that: the final set of $n$ words still contains different words (i.e. all words are unique); there is a way to put all words of the final set of words in the order so that the final sequence of $n$ words is consistent with the game rules. Polycarp wants to reverse minimal number of words. Please, help him. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains one integer $n$ ($1 \le n \le 2\cdot10^5$) — the number of words in the Polycarp's set. Next $n$ lines contain these words. All of $n$ words aren't empty and contains only characters '0' and '1'. The sum of word lengths doesn't exceed $4\cdot10^6$. All words are different. Guaranteed, that the sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed $4\cdot10^6$. -----Output----- Print answer for all of $t$ test cases in the order they appear. If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain $k$ ($0 \le k \le n$) — the minimal number of words in the set which should be reversed. The second line of the output should contain $k$ distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from $1$ to $n$ in the order they appear. If $k=0$ you can skip this line (or you can print an empty line). If there are many answers you can print any of them. -----Example----- Input 4 4 0001 1000 0011 0111 3 010 101 0 2 00000 00001 4 01 001 0001 00001 Output 1 3 -1 0 2 1 2
N = int(input()) def ceildiv(x, y): if x % y == 0: return x // y else: return x // y + 1 for _ in range(N): doms = [] oc, zc = 0, 0 n = int(input()) used = set() fulls = dict() for i in range(n): d = input() used.add(d) if d[0] != d[-1]: fulls[i] = d doms.append((i, (d[0], d[-1]))) else: if d[0] == '0': zc = 1 else: oc = 1 if len(doms) == 0: if zc == 1 and oc == 1: print(-1) else: print(0) else: # print(doms) _01 = 0 _10 = 0 _01_indexes = [] _10_indexes = [] for dom in doms: if dom[1] == ('0', '1'): _01 += 1 _01_indexes.append(dom[0]) else: _10 += 1 _10_indexes.append(dom[0]) if _10 < _01: _01, _10 = _10, _01 _01_indexes, _10_indexes = _10_indexes, _01_indexes _10_indexes = [x for x in _10_indexes if fulls[x][::-1] not in used] need = ceildiv(_10-_01-1, 2) if len(_10_indexes) >= need: print(need) print( ' '.join(list([str(x+1) for x in _10_indexes[:need]])) ) else: print(-1) # print("===") # print(ceil(abs(doms.count(('0', '1')) - doms.count(('1', '0'))) - 1, 2))
Polycarp has $n$ different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: "0001", "11", "0" and "0011100". Polycarp wants to offer his set of $n$ binary words to play a game "words". In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: "0101", "1", "10", "00", "00001". Word reversal is the operation of reversing the order of the characters. For example, the word "0111" after the reversal becomes "1110", the word "11010" after the reversal becomes "01011". Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that: the final set of $n$ words still contains different words (i.e. all words are unique); there is a way to put all words of the final set of words in the order so that the final sequence of $n$ words is consistent with the game rules. Polycarp wants to reverse minimal number of words. Please, help him. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains one integer $n$ ($1 \le n \le 2\cdot10^5$) — the number of words in the Polycarp's set. Next $n$ lines contain these words. All of $n$ words aren't empty and contains only characters '0' and '1'. The sum of word lengths doesn't exceed $4\cdot10^6$. All words are different. Guaranteed, that the sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed $4\cdot10^6$. -----Output----- Print answer for all of $t$ test cases in the order they appear. If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain $k$ ($0 \le k \le n$) — the minimal number of words in the set which should be reversed. The second line of the output should contain $k$ distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from $1$ to $n$ in the order they appear. If $k=0$ you can skip this line (or you can print an empty line). If there are many answers you can print any of them. -----Example----- Input 4 4 0001 1000 0011 0111 3 010 101 0 2 00000 00001 4 01 001 0001 00001 Output 1 3 -1 0 2 1 2
t=int(input()) for _ in range(t): n=int(input()) k={"01":0,"00":0,"11":0,"10":0} ab=[] ba=[] a=[] ra=set() rb=set() for i in range(n): s=input() ts=s[0]+s[-1] k[ts]+=1 if ts=="01": ab.append([str(i+1),s]) ra.add(s) if ts=="10": ba.append([str(i+1),s]) rb.add(s) if k["01"]==0 and k["10"]==0 and k["00"]>0 and k["11"]>0: ans=-1 else: if k["01"]==k["10"] or k["01"]==k["10"]+1 or k["01"]==k["10"]-1: ans=0 else: m=(k["01"]+k["10"])//2 if (k["01"]+k["10"])%2==0 else (k["01"]+k["10"])//2+1 if k["01"]>m: ans=k["01"]-m for i in range(len(ab)): psp=ab[i][1] nn=list(psp) nn.reverse() psp="".join(nn) c1=len(rb) rb.add(psp) c2=len(rb) if c1!=c2: a.append(ab[i][0]) if len(a)>=ans: a=a[:ans] else: ans=-1 else: ans=k["10"]-m for i in range(len(ba)): psp=ba[i][1] nn=list(psp) nn.reverse() psp="".join(nn) c1=len(ra) ra.add(psp) c2=len(ra) if c1!=c2: a.append(ba[i][0]) if len(a)>=ans: a=a[:ans] else: ans=-1 print(ans) if ans>0: print(" ".join(a))
Polycarp has $n$ different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: "0001", "11", "0" and "0011100". Polycarp wants to offer his set of $n$ binary words to play a game "words". In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: "0101", "1", "10", "00", "00001". Word reversal is the operation of reversing the order of the characters. For example, the word "0111" after the reversal becomes "1110", the word "11010" after the reversal becomes "01011". Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that: the final set of $n$ words still contains different words (i.e. all words are unique); there is a way to put all words of the final set of words in the order so that the final sequence of $n$ words is consistent with the game rules. Polycarp wants to reverse minimal number of words. Please, help him. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains one integer $n$ ($1 \le n \le 2\cdot10^5$) — the number of words in the Polycarp's set. Next $n$ lines contain these words. All of $n$ words aren't empty and contains only characters '0' and '1'. The sum of word lengths doesn't exceed $4\cdot10^6$. All words are different. Guaranteed, that the sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed $4\cdot10^6$. -----Output----- Print answer for all of $t$ test cases in the order they appear. If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain $k$ ($0 \le k \le n$) — the minimal number of words in the set which should be reversed. The second line of the output should contain $k$ distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from $1$ to $n$ in the order they appear. If $k=0$ you can skip this line (or you can print an empty line). If there are many answers you can print any of them. -----Example----- Input 4 4 0001 1000 0011 0111 3 010 101 0 2 00000 00001 4 01 001 0001 00001 Output 1 3 -1 0 2 1 2
t=int(input()) for i in range(t): n=int(input()) i0,i1=[],[] l0,l1=[],[] h0,h1=False,False for i in range(n): t=input() if t[0]=='0' and t[-1]=='1': i0.append(i) l0.append(t) elif t[0]=='1' and t[-1]=='0': i1.append(i) l1.append(t) elif t[0]==t[-1]=='1': h1=True elif t[0]==t[-1]=='0': h0=True c0,c1=len(l0),len(l1) req,sl=0,[] s0=set(l0) s1=set(l1) if c0>0 or c1>0: if c0-c1>1: req=(c0-c1)//2 sel=0 sl=[] for tt in range(len(l0)): t=l0[tt] if not t[::-1] in s1: req-=1 sl.append(i0[tt]+1) if req==0: break elif c1-c0>1: req=(c1-c0)//2 sel=0 sl=[] for tt in range(len(l1)): t=l1[tt] if not t[::-1] in s0: req-=1 sl.append(i1[tt]+1) if req==0: break if req>0: print(-1) else: print(len(sl)) print(*sl) else: if h0 and h1: print(-1) else: print(0) print(*[])
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q=int(input()) for e in range(q): x,y,k=list(map(int,input().split())) x,y=abs(x),abs(y) x,y=max(x,y),min(x,y) if(x%2!=k%2): k-=1 y-=1 if(x>k): print(-1) continue if((x-y)%2): k-=1 x-=1 print(k)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
# import collections, atexit, math, sys, bisect sys.setrecursionlimit(1000000) def getIntList(): return list(map(int, input().split())) try : #raise ModuleNotFoundError import numpy def dprint(*args, **kwargs): print(*args, **kwargs, file=sys.stderr) dprint('debug mode') except ModuleNotFoundError: def dprint(*args, **kwargs): pass inId = 0 outId = 0 if inId>0: dprint('use input', inId) sys.stdin = open('input'+ str(inId) + '.txt', 'r') #标准输出重定向至文件 if outId>0: dprint('use output', outId) sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #标准输出重定向至文件 atexit.register(lambda :sys.stdout.close()) #idle 中不会执行 atexit Q, = getIntList() for _ in range(Q): N, M, K = getIntList() if max(N,M) >K: print(-1) continue r = K if N%2!= K%2: r-=1 if M%2!= K%2: r-=1 print(r)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) for i in range(q): x, y, k = list(map(int, input().split())) if x > y: x, y = y, x m = y d = y if (y - x) % 2 == 1: d -= 1 if k < m: print(-1) continue r = k - m if r % 2 != 0: r -= 1 if d != m: d += 1 else: d -= 1 d += r print(d)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) otvet = [] for i in range(q): g = input().split() n = int(g[0]) m = int(g[1]) k = int(g[2]) if n < 0: n = -n if m < 0: m = -m if m > k or n > k: otvet.append(-1) elif m % 2 == k % 2 and n % 2 == k % 2: otvet.append(k) elif m % 2 == k % 2 or n % 2 == k % 2: otvet.append(k - 1) else: otvet.append(k - 2) for i in otvet: print(i)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) for i in range(q): a, b, k = list(map(int, input().split())) if a < b: a, b, = b, a if a > k: print(-1) elif a % 2 == b % 2 != k % 2: print(k - 2) elif (a + b) % 2 != 0: print(k - 1) else: print(k)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) for i in range(q): n, m, k = list(map(int, input().split())) m, n = abs(m), abs(n) mx = max(m, n) remaining = k - mx if remaining < 0: print(-1) elif m == n == 0: if k == 1: print(-1) elif k % 2: print(k - 1) else: print(k) elif abs(m - n) % 2 == 0: if remaining % 2 == 0: print(k) else: print(k - 2) else: if not remaining: print(k - 1) elif remaining % 2 == 0: print(k - 1) else: print(k - 1)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
from collections import deque from sys import stdin lines = deque(line.strip() for line in stdin.readlines()) def nextline(): return lines.popleft() def types(cast, sep=None): return tuple(cast(x) for x in strs(sep=sep)) def ints(sep=None): return types(int, sep=sep) def strs(sep=None): return tuple(nextline()) if sep == '' else tuple(nextline().split(sep=sep)) def main(): # lines will now contain all of the input's lines in a list T = int(nextline()) for testCase in range(1, T + 1): n, m, k = ints() min_k = max(n, m) if min_k > k: print(-1) continue if (n - m) % 2 == 0: if k % 2 == n % 2: print(k) continue print(k - 2) continue print(k - 1) def __starting_point(): main() __starting_point()
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) for _ in range(q): n, m, k = list(map(int, input().split())) if max([n, m]) > k: print(-1) else: if (n + m) % 2 == 0: if max([n, m]) % 2 != k % 2: print(k - 2) else: print(k) else: print((k - 1));
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
import math q = int(input()) for i in range(q): x, y, k = map(int, input().split()) if x > k or y > k: print(-1) else: if (x+y)%2 == 0: if (k-max(x,y)) % 2 == 0: print(k) else: print(k - 2) else: if (k-max(x,y)) % 2 == 0: print(k-1) else: print(k-1)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) for _ in range(q): n, m, k = list(map(int, input().split())) if k == 0: if n == 0 and m == 0: print(0) else: print(-1) elif k == 1: if max(abs(n), abs(m)) != 1: print(-1) elif abs(n) == abs(m) == 1: print(1) else: print(0) else: if max(abs(n), abs(m)) > k: print(-1) elif abs(n) == abs(m): if (k - abs(n)) % 2 == 0: print(k) else: print(k - 2) elif (max(abs(n), abs(m)) - min(abs(n), abs(m))) % 2 == 0: if (k - max(abs(n), abs(m))) % 2 == 0: print(k) else: print(k - 2) else: print(k - 1)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
import sys #sys.stdin=open("data.txt") input=sys.stdin.readline for _ in range(int(input())): n,m,k=list(map(int,input().split())) n=abs(n) m=abs(m) if max(n,m)>k: print("-1") else: # you can't 0 0 1 me :D bad1=((n+k)%2==1) bad2=((m+k)%2==1) print(k-bad1-bad2)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
USE_STDIO = False if not USE_STDIO: try: import mypc except: pass def main(): q, = list(map(int, input().split(' '))) for _ in range(q): n, m, k = list(map(int, input().split(' '))) if n > k or m > k: print(-1) elif (n - m) % 2: print(k - 1) elif (n - k) % 2: print(k - 2) else: print(k) def __starting_point(): main() __starting_point()
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q=int(input()) Q=[list(map(int,input().split())) for i in range(q)] for n,m,k in Q: if n>k or m>k: print(-1) continue x=max(n,m)-min(n,m) y=k-max(n,m) if x%2==0 and y%2==0: print(k) elif x%2==0 and y%2==1: print(k-2) elif x%2==1 and y%2==0: print(k-1) elif x%2==1 and y%2==1: print(k-1)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
n = int(input()) for i in range(n): a, b, c = [int(el) for el in input().split()] if ( a > c or b > c): print(-1) else: if (a% 2 + b % 2 == 1): print(c - 1) elif (a%2 == b%2 == c%2): print(c) else: print(c - 2)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
Q = int(input()) src = [tuple(map(int,input().split())) for i in range(Q)] ans = [] for x,y,k in src: d = max(x,y) if (x+y)%2: ans.append(-1 if d > k else k-1) else: if d > k: ans.append(-1) else: ans.append(k-2 if (d+k)%2 else k) print(*ans,sep='\n')
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
def m(): [x, y, k] = [int(i) for i in input().split()] d=min(x, y) x-=d y-=d k-=d if k-x-y<0: print(-1) else: x+=y if x%2 > 0 and k%2>0: print(d+k-1) elif x%2 >0: print(d+k-1) elif k%2>0: print(d+k-2) else: print(d+k) n=int(input()) for i in range(n): m()
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) for i in range(q): (x, y, k) = list(map(int, input().split())) if max(x, y) > k: print(-1) elif x == y and k == x + 1: print(k - 2) continue elif x % 2 == 1 and y % 2 == 1 and k % 2 == 0: print(k - 2) continue elif x % 2 == 0 and y % 2 == 0 and k % 2 == 1: print(k - 2) continue elif (x + y) % 2 == 0: print(k) else: print(k - 1)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
n = int(input()) for q in range(n): x, y, k = list(map(int, input().split())) if max(x, y) > k: print(-1) else: if 0 == (x + y) % 2: if k % 2 == max(x, y) % 2: print(k) else: print(k - 2) else: print(k - 1)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
def go(): n = int(input()) for i in range(n): a, b, d = [int(i) for i in input().split(' ')] if a > d or b > d: print(-1) elif a % 2 == b % 2: if a % 2 == d % 2: print(d) else: print(d - 2) else: if a % 2 == b % 2: if d % 2 == a % 2: print(d) else: print(d - 2) else: print(d - 1) go()
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) for i in range(q): n, m, k = map(int, input().split()) p = min(m, n) r = max(n, m) - p if (p+r) > k: print(-1) elif r % 2 == 1: print(k - 1) elif (k - p) % 2 == 0: print(k) else: print(k - 2)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q = int(input()) for i in range(q): n, m, k = map(int, input().split()) ost = max(n, m) - min(n, m) plus = 0 if ost % 2 != 0: plus = 1 ost -= 1 mini = min(n, m) + ost + plus #print('mini: ' + str(mini)) if k < mini: print(-1) elif (k - mini) % 2 == 0 or plus == 1: print(k - plus) else: print(k - plus - 2)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q=int(input()) for i in range(q): n,m,k=list(map(int,input().split())) if n>k or m>k: print(-1) else: if n%2==0 and m%2==0: if k%2==0: print(k) else: print(k-2) elif (n%2==0 and m%2==1) or (n%2==1 and m%2==0): print(k-1) elif n%2==1 and m%2==1: if k%2==0: print(k-2) else: print(k)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
q=int(input()) for i in range(q): n, m, k = map(int, input().split()) ans=max(n,m) diff=k-ans if diff<0: print(-1) else: if (n%2==0 and m%2==0) or (n%2!=0 and m%2!=0): if diff%2==0: ans+=diff else: ans+=diff-2 else: ans+=diff-1 print(ans)
Mikhail walks on a Cartesian plane. He starts at the point $(0, 0)$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(1, 1)$; $(0, 1)$; $(-1, 1)$; $(-1, 0)$; $(-1, -1)$; $(0, -1)$; $(1, -1)$. If Mikhail goes from the point $(x1, y1)$ to the point $(x2, y2)$ in one move, and $x1 \ne x2$ and $y1 \ne y2$, then such a move is called a diagonal move. Mikhail has $q$ queries. For the $i$-th query Mikhail's target is to go to the point $(n_i, m_i)$ from the point $(0, 0)$ in exactly $k_i$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $(0, 0)$ to the point $(n_i, m_i)$ in $k_i$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^4$) — the number of queries. Then $q$ lines follow. The $i$-th of these $q$ lines contains three integers $n_i$, $m_i$ and $k_i$ ($1 \le n_i, m_i, k_i \le 10^{18}$) — $x$-coordinate of the destination point of the query, $y$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. -----Output----- Print $q$ integers. The $i$-th integer should be equal to -1 if Mikhail cannot go from the point $(0, 0)$ to the point $(n_i, m_i)$ in exactly $k_i$ moves described above. Otherwise the $i$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. -----Example----- Input 3 2 2 3 4 3 7 10 1 9 Output 1 6 -1 -----Note----- One of the possible answers to the first test case: $(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$. One of the possible answers to the second test case: $(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$. In the third test case Mikhail cannot reach the point $(10, 1)$ in 9 moves.
""" KA YM KA AS KA ASKA YASK KA SKAYMA KA KA SKAY SK SK AS AY AY SK SKAY AS AS KA AS AS YM KA AS AS YM KA SK AS YM AS KAYM MA MA AYMA AS MASK SK MA MA SKAYMA KA AS YMASKAYMAS YM AS AS SK YMASKAYMAS AS KA KA AY SK YM AS SK AY SK AS AS KA YM KA KA YM AS SK KA KA SKAYMA """ n=int(input()) for i in range(n): x,y,k=map(int,input().split()) x,y=abs(x),abs(y) min_moves=max(x,y) if min_moves>k: print(-1) else: ans=min(x,y) x-=ans y-=ans p=max(x,y) k-=ans if k==p and p%2==0: print(ans+k) elif k==p and p%2==1: print(ans+k-1) elif p%2==0 and k%2==0: print(ans+k) elif p%2==0 and k%2==1: print(ans+k-2) elif p%2==1: print(ans+k-1)
You are given three sequences: $a_1, a_2, \ldots, a_n$; $b_1, b_2, \ldots, b_n$; $c_1, c_2, \ldots, c_n$. For each $i$, $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$. Find a sequence $p_1, p_2, \ldots, p_n$, that satisfy the following conditions: $p_i \in \{a_i, b_i, c_i\}$ $p_i \neq p_{(i \mod n) + 1}$. In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $i,i+1$ adjacent for $i<n$ and also elements $1$ and $n$) will have equal value. It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence. -----Input----- The first line of input contains one integer $t$ ($1 \leq t \leq 100$): the number of test cases. The first line of each test case contains one integer $n$ ($3 \leq n \leq 100$): the number of elements in the given sequences. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$). The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 100$). The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 100$). It is guaranteed that $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$ for all $i$. -----Output----- For each test case, print $n$ integers: $p_1, p_2, \ldots, p_n$ ($p_i \in \{a_i, b_i, c_i\}$, $p_i \neq p_{i \mod n + 1}$). If there are several solutions, you can print any. -----Example----- Input 5 3 1 1 1 2 2 2 3 3 3 4 1 2 1 2 2 1 2 1 3 4 3 4 7 1 3 3 1 1 1 1 2 4 4 3 2 2 4 4 2 2 2 4 4 2 3 1 2 1 2 3 3 3 1 2 10 1 1 1 2 2 2 3 3 3 1 2 2 2 3 3 3 1 1 1 2 3 3 3 1 1 1 2 2 2 3 Output 1 2 3 1 2 1 2 1 3 4 3 2 4 2 1 3 2 1 2 3 1 2 3 1 2 3 2 -----Note----- In the first test case $p = [1, 2, 3]$. It is a correct answer, because: $p_1 = 1 = a_1$, $p_2 = 2 = b_2$, $p_3 = 3 = c_3$ $p_1 \neq p_2 $, $p_2 \neq p_3 $, $p_3 \neq p_1$ All possible correct answers to this test case are: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$. In the second test case $p = [1, 2, 1, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = a_3$, $p_4 = a_4$. Also we can see, that no two adjacent elements of the sequence are equal. In the third test case $p = [1, 3, 4, 3, 2, 4, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = b_3$, $p_4 = b_4$, $p_5 = b_5$, $p_6 = c_6$, $p_7 = c_7$. Also we can see, that no two adjacent elements of the sequence are equal.
import sys import random from fractions import Fraction from math import * def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def finput(): return float(input()) def tinput(): return input().split() def linput(): return list(input()) def rinput(): return list(map(int, tinput())) def fiinput(): return list(map(float, tinput())) def rlinput(): return list(map(int, input().split())) def trinput(): return tuple(rinput()) def srlinput(): return sorted(list(map(int, input().split()))) def NOYES(fl): if fl: print("NO") else: print("YES") def YESNO(fl): if fl: print("YES") else: print("NO") def main(): n = iinput() #k = iinput() #m = iinput() #n = int(sys.stdin.readline().strip()) #n, k = rinput() #n, m = rinput() #m, k = rinput() #n, k, m = rinput() #n, m, k = rinput() #k, n, m = rinput() #k, m, n = rinput() #m, k, n = rinput() #m, n, k = rinput() q = [rlinput(), rlinput(), rlinput()] #q = linput() ans = q[0].copy() for i in range(1, n): if ans[i] == ans[i - 1]: ans[i] = q[1][i] if i == n - 1: o = 0 while q[o][i] == ans[n - 2] or q[o][i] == ans[0]: o += 1 ans[i] = q[o][i] print(*ans) for i in range(iinput()): main()
You are given three sequences: $a_1, a_2, \ldots, a_n$; $b_1, b_2, \ldots, b_n$; $c_1, c_2, \ldots, c_n$. For each $i$, $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$. Find a sequence $p_1, p_2, \ldots, p_n$, that satisfy the following conditions: $p_i \in \{a_i, b_i, c_i\}$ $p_i \neq p_{(i \mod n) + 1}$. In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $i,i+1$ adjacent for $i<n$ and also elements $1$ and $n$) will have equal value. It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence. -----Input----- The first line of input contains one integer $t$ ($1 \leq t \leq 100$): the number of test cases. The first line of each test case contains one integer $n$ ($3 \leq n \leq 100$): the number of elements in the given sequences. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$). The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 100$). The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 100$). It is guaranteed that $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$ for all $i$. -----Output----- For each test case, print $n$ integers: $p_1, p_2, \ldots, p_n$ ($p_i \in \{a_i, b_i, c_i\}$, $p_i \neq p_{i \mod n + 1}$). If there are several solutions, you can print any. -----Example----- Input 5 3 1 1 1 2 2 2 3 3 3 4 1 2 1 2 2 1 2 1 3 4 3 4 7 1 3 3 1 1 1 1 2 4 4 3 2 2 4 4 2 2 2 4 4 2 3 1 2 1 2 3 3 3 1 2 10 1 1 1 2 2 2 3 3 3 1 2 2 2 3 3 3 1 1 1 2 3 3 3 1 1 1 2 2 2 3 Output 1 2 3 1 2 1 2 1 3 4 3 2 4 2 1 3 2 1 2 3 1 2 3 1 2 3 2 -----Note----- In the first test case $p = [1, 2, 3]$. It is a correct answer, because: $p_1 = 1 = a_1$, $p_2 = 2 = b_2$, $p_3 = 3 = c_3$ $p_1 \neq p_2 $, $p_2 \neq p_3 $, $p_3 \neq p_1$ All possible correct answers to this test case are: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$. In the second test case $p = [1, 2, 1, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = a_3$, $p_4 = a_4$. Also we can see, that no two adjacent elements of the sequence are equal. In the third test case $p = [1, 3, 4, 3, 2, 4, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = b_3$, $p_4 = b_4$, $p_5 = b_5$, $p_6 = c_6$, $p_7 = c_7$. Also we can see, that no two adjacent elements of the sequence are equal.
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) c=list(map(int,input().split())) p=a for i in range(n): if p[i]==p[(i+1)%n]: if p[i]!=b[i] and p[(i-1)%n]!=b[i]:p[i]=b[i] else:p[i]=c[i] print(*p)
You are given three sequences: $a_1, a_2, \ldots, a_n$; $b_1, b_2, \ldots, b_n$; $c_1, c_2, \ldots, c_n$. For each $i$, $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$. Find a sequence $p_1, p_2, \ldots, p_n$, that satisfy the following conditions: $p_i \in \{a_i, b_i, c_i\}$ $p_i \neq p_{(i \mod n) + 1}$. In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $i,i+1$ adjacent for $i<n$ and also elements $1$ and $n$) will have equal value. It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence. -----Input----- The first line of input contains one integer $t$ ($1 \leq t \leq 100$): the number of test cases. The first line of each test case contains one integer $n$ ($3 \leq n \leq 100$): the number of elements in the given sequences. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$). The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 100$). The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 100$). It is guaranteed that $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$ for all $i$. -----Output----- For each test case, print $n$ integers: $p_1, p_2, \ldots, p_n$ ($p_i \in \{a_i, b_i, c_i\}$, $p_i \neq p_{i \mod n + 1}$). If there are several solutions, you can print any. -----Example----- Input 5 3 1 1 1 2 2 2 3 3 3 4 1 2 1 2 2 1 2 1 3 4 3 4 7 1 3 3 1 1 1 1 2 4 4 3 2 2 4 4 2 2 2 4 4 2 3 1 2 1 2 3 3 3 1 2 10 1 1 1 2 2 2 3 3 3 1 2 2 2 3 3 3 1 1 1 2 3 3 3 1 1 1 2 2 2 3 Output 1 2 3 1 2 1 2 1 3 4 3 2 4 2 1 3 2 1 2 3 1 2 3 1 2 3 2 -----Note----- In the first test case $p = [1, 2, 3]$. It is a correct answer, because: $p_1 = 1 = a_1$, $p_2 = 2 = b_2$, $p_3 = 3 = c_3$ $p_1 \neq p_2 $, $p_2 \neq p_3 $, $p_3 \neq p_1$ All possible correct answers to this test case are: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$. In the second test case $p = [1, 2, 1, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = a_3$, $p_4 = a_4$. Also we can see, that no two adjacent elements of the sequence are equal. In the third test case $p = [1, 3, 4, 3, 2, 4, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = b_3$, $p_4 = b_4$, $p_5 = b_5$, $p_6 = c_6$, $p_7 = c_7$. Also we can see, that no two adjacent elements of the sequence are equal.
for __ in range(int(input())): n = int(input()) ar1 = list(map(int, input().split())) ar2 = list(map(int, input().split())) ar3 = list(map(int, input().split())) ans = [ar1[0]] for i in range(1, n - 1): if ar1[i] != ans[-1]: ans.append(ar1[i]) elif ar2[i] != ans[-1]: ans.append(ar2[i]) elif ar3[i] != ans[-1]: ans.append(ar3[i]) if ar1[-1] != ans[-1] and ar1[-1] != ans[0]: ans.append(ar1[-1]) elif ar2[-1] != ans[-1] and ar2[-1] != ans[0]: ans.append(ar2[-1]) elif ar3[-1] != ans[-1] and ar3[-1] != ans[0]: ans.append(ar3[-1]) print(*ans)
You are given three sequences: $a_1, a_2, \ldots, a_n$; $b_1, b_2, \ldots, b_n$; $c_1, c_2, \ldots, c_n$. For each $i$, $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$. Find a sequence $p_1, p_2, \ldots, p_n$, that satisfy the following conditions: $p_i \in \{a_i, b_i, c_i\}$ $p_i \neq p_{(i \mod n) + 1}$. In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $i,i+1$ adjacent for $i<n$ and also elements $1$ and $n$) will have equal value. It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence. -----Input----- The first line of input contains one integer $t$ ($1 \leq t \leq 100$): the number of test cases. The first line of each test case contains one integer $n$ ($3 \leq n \leq 100$): the number of elements in the given sequences. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$). The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 100$). The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 100$). It is guaranteed that $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$ for all $i$. -----Output----- For each test case, print $n$ integers: $p_1, p_2, \ldots, p_n$ ($p_i \in \{a_i, b_i, c_i\}$, $p_i \neq p_{i \mod n + 1}$). If there are several solutions, you can print any. -----Example----- Input 5 3 1 1 1 2 2 2 3 3 3 4 1 2 1 2 2 1 2 1 3 4 3 4 7 1 3 3 1 1 1 1 2 4 4 3 2 2 4 4 2 2 2 4 4 2 3 1 2 1 2 3 3 3 1 2 10 1 1 1 2 2 2 3 3 3 1 2 2 2 3 3 3 1 1 1 2 3 3 3 1 1 1 2 2 2 3 Output 1 2 3 1 2 1 2 1 3 4 3 2 4 2 1 3 2 1 2 3 1 2 3 1 2 3 2 -----Note----- In the first test case $p = [1, 2, 3]$. It is a correct answer, because: $p_1 = 1 = a_1$, $p_2 = 2 = b_2$, $p_3 = 3 = c_3$ $p_1 \neq p_2 $, $p_2 \neq p_3 $, $p_3 \neq p_1$ All possible correct answers to this test case are: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$. In the second test case $p = [1, 2, 1, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = a_3$, $p_4 = a_4$. Also we can see, that no two adjacent elements of the sequence are equal. In the third test case $p = [1, 3, 4, 3, 2, 4, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = b_3$, $p_4 = b_4$, $p_5 = b_5$, $p_6 = c_6$, $p_7 = c_7$. Also we can see, that no two adjacent elements of the sequence are equal.
T = int(input()) for t in range(T): N = int(input()) A = [int(_) for _ in input().split()] B = [int(_) for _ in input().split()] C = [int(_) for _ in input().split()] R = [] for i in range(N): if i == 0: R.append(A[i]) continue if i == N-1: if A[i] != R[0] and A[i] != R[-1]: R.append(A[i]) elif B[i] != R[0] and B[i] != R[-1]: R.append(B[i]) else: R.append(C[i]) continue if A[i] != R[-1]: R.append(A[i]) else: R.append(B[i]) print(' '.join(map(str, R)))
You are given three sequences: $a_1, a_2, \ldots, a_n$; $b_1, b_2, \ldots, b_n$; $c_1, c_2, \ldots, c_n$. For each $i$, $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$. Find a sequence $p_1, p_2, \ldots, p_n$, that satisfy the following conditions: $p_i \in \{a_i, b_i, c_i\}$ $p_i \neq p_{(i \mod n) + 1}$. In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $i,i+1$ adjacent for $i<n$ and also elements $1$ and $n$) will have equal value. It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence. -----Input----- The first line of input contains one integer $t$ ($1 \leq t \leq 100$): the number of test cases. The first line of each test case contains one integer $n$ ($3 \leq n \leq 100$): the number of elements in the given sequences. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$). The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 100$). The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 100$). It is guaranteed that $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$ for all $i$. -----Output----- For each test case, print $n$ integers: $p_1, p_2, \ldots, p_n$ ($p_i \in \{a_i, b_i, c_i\}$, $p_i \neq p_{i \mod n + 1}$). If there are several solutions, you can print any. -----Example----- Input 5 3 1 1 1 2 2 2 3 3 3 4 1 2 1 2 2 1 2 1 3 4 3 4 7 1 3 3 1 1 1 1 2 4 4 3 2 2 4 4 2 2 2 4 4 2 3 1 2 1 2 3 3 3 1 2 10 1 1 1 2 2 2 3 3 3 1 2 2 2 3 3 3 1 1 1 2 3 3 3 1 1 1 2 2 2 3 Output 1 2 3 1 2 1 2 1 3 4 3 2 4 2 1 3 2 1 2 3 1 2 3 1 2 3 2 -----Note----- In the first test case $p = [1, 2, 3]$. It is a correct answer, because: $p_1 = 1 = a_1$, $p_2 = 2 = b_2$, $p_3 = 3 = c_3$ $p_1 \neq p_2 $, $p_2 \neq p_3 $, $p_3 \neq p_1$ All possible correct answers to this test case are: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$. In the second test case $p = [1, 2, 1, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = a_3$, $p_4 = a_4$. Also we can see, that no two adjacent elements of the sequence are equal. In the third test case $p = [1, 3, 4, 3, 2, 4, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = b_3$, $p_4 = b_4$, $p_5 = b_5$, $p_6 = c_6$, $p_7 = c_7$. Also we can see, that no two adjacent elements of the sequence are equal.
gans = [] for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) ans = [a[0]] for i in range(1, n - 1): if a[i] != ans[i - 1]: ans.append(a[i]) else: ans.append(b[i]) if a[-1] != ans[-1] and a[-1] != ans[0]: ans.append(a[-1]) elif b[-1] != ans[-1] and b[-1] != ans[0]: ans.append(b[-1]) else: ans.append(c[-1]) gans.append(' '.join(map(str, ans))) print('\n'.join(gans))
You are given three sequences: $a_1, a_2, \ldots, a_n$; $b_1, b_2, \ldots, b_n$; $c_1, c_2, \ldots, c_n$. For each $i$, $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$. Find a sequence $p_1, p_2, \ldots, p_n$, that satisfy the following conditions: $p_i \in \{a_i, b_i, c_i\}$ $p_i \neq p_{(i \mod n) + 1}$. In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $i,i+1$ adjacent for $i<n$ and also elements $1$ and $n$) will have equal value. It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence. -----Input----- The first line of input contains one integer $t$ ($1 \leq t \leq 100$): the number of test cases. The first line of each test case contains one integer $n$ ($3 \leq n \leq 100$): the number of elements in the given sequences. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$). The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 100$). The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 100$). It is guaranteed that $a_i \neq b_i$, $a_i \neq c_i$, $b_i \neq c_i$ for all $i$. -----Output----- For each test case, print $n$ integers: $p_1, p_2, \ldots, p_n$ ($p_i \in \{a_i, b_i, c_i\}$, $p_i \neq p_{i \mod n + 1}$). If there are several solutions, you can print any. -----Example----- Input 5 3 1 1 1 2 2 2 3 3 3 4 1 2 1 2 2 1 2 1 3 4 3 4 7 1 3 3 1 1 1 1 2 4 4 3 2 2 4 4 2 2 2 4 4 2 3 1 2 1 2 3 3 3 1 2 10 1 1 1 2 2 2 3 3 3 1 2 2 2 3 3 3 1 1 1 2 3 3 3 1 1 1 2 2 2 3 Output 1 2 3 1 2 1 2 1 3 4 3 2 4 2 1 3 2 1 2 3 1 2 3 1 2 3 2 -----Note----- In the first test case $p = [1, 2, 3]$. It is a correct answer, because: $p_1 = 1 = a_1$, $p_2 = 2 = b_2$, $p_3 = 3 = c_3$ $p_1 \neq p_2 $, $p_2 \neq p_3 $, $p_3 \neq p_1$ All possible correct answers to this test case are: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$. In the second test case $p = [1, 2, 1, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = a_3$, $p_4 = a_4$. Also we can see, that no two adjacent elements of the sequence are equal. In the third test case $p = [1, 3, 4, 3, 2, 4, 2]$. In this sequence $p_1 = a_1$, $p_2 = a_2$, $p_3 = b_3$, $p_4 = b_4$, $p_5 = b_5$, $p_6 = c_6$, $p_7 = c_7$. Also we can see, that no two adjacent elements of the sequence are equal.
from math import * from bisect import * from collections import * from random import * from decimal import * import sys input=sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip('\n') def lis(): return list(map(int,input().split())) def ma(): return list(map(int,input().split())) t=inp() while(t): t-=1 n=inp() a=lis() b=lis() c=lis() r=[a[0]] for i in range(1,n): if(i==n-1): if(a[i]!=r[0] and a[i]!=r[-1]): r.append(a[i]) continue if(b[i]!=r[0] and b[i]!=r[-1]): r.append(b[i]) continue if(c[i]!=r[0] and c[i]!=r[-1]): r.append(c[i]) continue if(a[i]!=r[-1]): r.append(a[i]) continue if(b[i]!=r[-1]): r.append(b[i]) continue if(c[i]!=r[-1]): r.append(c[i]) continue print(*r)
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
def solve(): n, k = map(int,input().split()) lst = list(map(int,input().split())) lst.sort() ans = 0 for i in range(n - k - 1, n): ans += lst[i] print(ans) for i in range(int(input())): solve()
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
t=int(input()) for i in range(t): n,k=[int(i) for i in input().split()] a=[int(i) for i in input().split()] a.sort(reverse=True) print(sum(a[:k+1]))
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
# map(int, input().split()) rw = int(input()) for wewq in range(rw): n, k = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() a.reverse() f = 0 for i in range(k + 1): f += a[i] print(f)
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
t=int(input()) for you in range(t): l=input().split() n=int(l[0]) k=int(l[1]) l=input().split() li=[int(i) for i in l] if(k==0): print(max(li)-min(li)) continue z=0 li.sort() li.reverse() for i in range(k+1): z+=li[i] print(z)
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
for _ in range (int(input())): n,k=map(int,input().split()) a=list(map(int,input().split())) a.sort(reverse=True) for i in range (1,k+1): a[0]+=a[i] a[i]=0 print(a[0]-a[1])
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
for __ in range(int(input())): n, k = list(map(int, input().split())) ar = list(map(int, input().split())) ar.sort(reverse=True) ans = 0 for i in range(min(n, k + 1)): ans += ar[i] print(ans)
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
import sys, math import io, os #data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from bisect import bisect_left as bl, bisect_right as br, insort from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter #from itertools import permutations,combinations def data(): return sys.stdin.readline().strip() def mdata(): return list(map(int, data().split())) def outl(var) : sys.stdout.write('\n'.join(map(str, var))+'\n') def out(var) : sys.stdout.write(str(var)+'\n') #from decimal import Decimal #from fractions import Fraction #sys.setrecursionlimit(100000) INF = float('inf') mod=10**9+7 for t in range(int(data())): n,k=mdata() a=sorted(mdata(),reverse=True) s=sum(a[:k+1]) out(s)
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n,k = map(int,input().split()) a = list(map(int,input().split())) a.sort() a.reverse() cum = [a[0]] for i in range(n-1): cum.append(cum[i]+a[i+1]) cum.append(cum[-1]) print(cum[k])
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
t = int(input()) for _ in range(t): #n = int(input()) n, k=map(int, input().split()) a = list(map(int, input().split())) a.sort() s=0 for i in range(k+1): s+=a[n-1-i] print(s)
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
def main(): N, K = list(map(int, input().split())) *A, = list(map(int, input().split())) A.sort() print(A[-1] + sum(A[-K-1:-1])) def __starting_point(): for __ in [0]*int(input()): main() __starting_point()
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
import sys import random # import numpy as np import math import copy from heapq import heappush, heappop, heapify from functools import cmp_to_key from bisect import bisect_left, bisect_right from collections import defaultdict, deque, Counter # sys.setrecursionlimit(1000000) # input aliases input = sys.stdin.readline getS = lambda: input().strip() getN = lambda: int(input()) getList = lambda: list(map(int, input().split())) getZList = lambda: [int(x) - 1 for x in input().split()] INF = float("inf") MOD = 10 ** 9 + 7 divide = lambda x: pow(x, MOD-2, MOD) def judge(at, ax, ay, bt, bx, by): if abs(at - bt) >= abs(ax - bx) + abs(ay - by): return True else: return False def solve(): n, k = getList() li = getList() if k >= n: print(sum(li)) return li.sort(reverse=True) print(sum(li[:k+1])) return def main(): n = getN() for _ in range(n): solve() return def __starting_point(): main() # solve() __starting_point()
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
from sys import stdin t = int(stdin.readline()) for _ in range(t): n, k = tuple(int(x) for x in stdin.readline().split()) lst = sorted(int(x) for x in stdin.readline().split()) print(sum(lst[-k-1:]))
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
t = int(input()) for _ in range(t): n,k = [int(x) for x in input().split()] l = [int(x) for x in input().split()] l.sort() l.reverse() print(sum(l[:min(k+1,n)]))
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
for _ in range(int(input())): n, k = list(map(int, input().split())) A = list(map(int, input().split())) A.sort(reverse=True) if k == 0: print(max(A) - min(A)) else: print(A[0] + sum(A[1:k+1]))
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
n = int(input()) for _ in range(n): n, k = list(map(int, input().split())) arr = list(map(int, input().split())) arr.sort(reverse=True) print(sum(arr[:k+1]))
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
"""T=int(input()) for _ in range(0,T): n=int(input()) a,b=map(int,input().split()) s=input() s=[int(x) for x in input().split()] for i in range(0,len(s)): a,b=map(int,input().split())""" T=int(input()) for _ in range(0,T): n,k=list(map(int,input().split())) s=[int(x) for x in input().split()] s.sort() s=s[::-1] for i in range(1,min(k+1,len(s))): s[0]+=s[i] print(s[0])
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water. You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times. -----Example----- Input 2 4 1 5 5 5 5 3 2 0 0 0 Output 10 0
t=int(input()) while t: t-=1 n,k=list(map(int,input().split())) a=[int(i) for i in input().split()] a.sort() ans=0 a.reverse() for i in range(k+1): ans+=a[i] print(ans)
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
for _ in range(int(input())): input() nums = [int(x) for x in input().split()] new_ar = list(zip(nums,[i for i in range(len(nums))])) new_ar.sort() maxx = new_ar[0][1] minn = new_ar[0][1] s="1" for j in range(1,len(new_ar)): if(new_ar[j][1]>maxx): maxx = new_ar[j][1] if(new_ar[j][1]<minn): minn = new_ar[j][1] if(maxx-minn<j+1): s+="1" else: s+="0" print(s)
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
import sys def I(): return sys.stdin.readline().rstrip() for _ in range(int(I())): n = int(I()) l = list(map(int,I().split())) r = list(range(n)) r.sort(key=lambda x: l[x]) mn, mx = None, None for i in range(n): if mn is None: mn = mx = r[ i ] else: mn = min( mn, r[ i ] ) mx = max( mx, r[ i ] ) l[ i ] = '1' if mx - mn == i else '0' print("".join(l))
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
from sys import stdin def rl(): return [int(w) for w in stdin.readline().split()] k, = rl() for _ in range(k): n, = rl() p = rl() q = [0] * n for i, x in enumerate(p): q[x-1] = i l = r = q[0] m = [] for k, i in enumerate(q): if i < l: l = i elif i > r: r = i m.append('1' if r - l == k else '0') print(''.join(m))
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
# @author import sys class BBeautifulNumbers: def solve(self): for _ in range(int(input())): n = int(input()) p = [int(_) - 1 for _ in input().split()] mn_index = [float('inf')] * n mx_index = [-float('inf')] * n prev = [0] * n for i in range(n): prev[p[i]] = i # print(prev) for i in range(n): mn_index[i] = min(mn_index[i - 1], prev[i]) mx_index[i] = max(mx_index[i - 1], prev[i]) ans = ['0'] * n # print(mn_index, mx_index) for i in range(n): l, r = mn_index[i], mx_index[i] ans[i] = '1' if r - l + 1 == i + 1 else '0' print(''.join(ans)) solver = BBeautifulNumbers() input = sys.stdin.readline solver.solve()
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
def f(L): n=len(L) M=[0]*(len(L)+1) for i in range(len(L)): M[L[i]]=i s=[0]*len(L) s[0]=1 sumof=M[1] mx=M[1] mi=M[1] for i in range(2,n): k=M[i] if k>mx:mx=k if k<mi:mi=k sumof+=k if sumof==(mx*(mx+1))//2-((mi-1)*mi)//2: s[i-1]=1 s[n-1]=1 return s for i in ' '*int(input()): n=int(input()) s=f(list(map(int,input().split()))) for i in s:print(i,end='') print()
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
t=int(input()) for _ in range(t): n=int(input()) a=list(map(int,input().split())) pos=[0 for i in range(n+1)] for i in range(n): pos[a[i]]=i ans=[-1 for i in range(n)] ans[0]=1 l,r=pos[1],pos[1] for i in range(2,n+1): l=min(l,pos[i]) r=max(r,pos[i]) if r-l==i-1: ans[i-1]=1 else: ans[i-1]=0 print("".join(map(str,ans)))
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
t = int(input()) for t_i in range(t): n = int(input()) P = input().split() l, r = -1, -1 for i in range(n): P[i] = int(P[i]) if P[i] == 1: l = i r = i max_seen = 1 beaut = ['1'] for _ in range(n - 1): if l == 0: l_cand = 10**8 else: l_cand = P[l - 1] if r == n - 1: r_cand = 10**8 else: r_cand = P[r + 1] if r_cand > l_cand: l -= 1 max_seen = max(l_cand, max_seen) else: r += 1 max_seen = max(r_cand, max_seen) beaut.append('1' if max_seen == r - l + 1 else '0') print(''.join(beaut))
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
for _ in range(int(input())): n = int(input()) a = list(map(int,input().split())) d = {} for i in range(n): d[a[i]] = i ans = '' mn = 200001 mx = -1 for i in range(1,n+1): if(mn > d[i]): mn = d[i] if(mx < d[i]): mx = d[i] if(mx - mn + 1 > i): ans += '0' else: ans += '1' print(ans)
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
from math import * from collections import * import sys sys.setrecursionlimit(10**9) t = int(input()) for y in range(t): n = int(input()) a = list(map(int,input().split())) ans = ['1'] le = 1 l = a.index(1) l -= 1 r = l + 2 m = 1 while(le < n): if(l != -1 and r != n): if(a[l] > a[r]): m = max(m,a[r]) r += 1 if(m == le+1): ans.append('1') else: ans.append('0') else: m = max(m,a[l]) l -= 1 if(m == le+1): ans.append('1') else: ans.append('0') elif(l != -1): m = max(m,a[l]) l -= 1 if(m == le+1): ans.append('1') else: ans.append('0') else: m = max(m,a[r]) r += 1 if(m == le+1): ans.append('1') else: ans.append('0') le += 1 print("".join(ans))
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) pos = [0]*(n+1) for i, x in enumerate(a): pos[x] = i used = [0, 1] + [0]*n ans = [0]*n l, r = pos[1], pos[1] count = 1 for x in range(1, n+1): if not used[x]: if pos[x] < l: while not used[x]: l -= 1 used[a[l]] = 1 count += 1 else: while not used[x]: r += 1 used[a[r]] = 1 count += 1 if count == x: ans[x-1] = 1 print(*ans, sep='')
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
def mi(): return map(int, input().split()) ''' 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 ''' for _ in range(int(input())): n = int(input()) a = list(mi()) t = a.index(1) dist = [0]*(n+1) dic = [0]*n for i in range(n): dist[a[i]] = abs(t-i) dic[i] = [a[i], i] dic.sort() lm = dic[0][1] rm = dic[0][1] print (1, end = '') for i in range(1, n): if (dic[i][1]<lm): lm = dic[i][1] if (dic[i][1]>rm): rm = dic[i][1] if rm-lm<i+1: print (1, end = '') else: print (0, end = '') print()
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = list(map(int,input().split())) start = 0 for i,v in enumerate(a): if v == 1: start = i break ans = [0]*-~n ans[n-1] = 1 mx = 1 l = start r = start def move(x): nonlocal l,r,mx if x: mx = max(a[r+1],mx) r += 1 else: mx = max(a[l-1],mx) l -= 1 while mx < n: if mx == r-l+1: ans[mx-1] = 1 if l == 0: move(1) elif r == n-1: move(0) else: if a[l-1] > a[r+1]: move(1) else: move(0) print("".join(map(str,ans[:n])))
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
#!/usr/bin/env python3 from itertools import combinations import sys input = sys.stdin.readline INF = 10**9 t = int(input()) for i in range(t): n = int(input()) a = [INF] + [int(item) for item in input().split()] + [INF] ans = [1] l = r = a.index(1) max_val = 1 for i in range(2, n+1): if i == max(max_val, a[l-1]): ans.append(1) l -= 1 max_val = i elif i == max(max_val, a[r+1]): ans.append(1) r += 1 max_val = i elif a[l-1] < a[r+1]: ans.append(0) max_val = max(max_val, a[l-1]) l -= 1 else: ans.append(0) max_val = max(max_val, a[r+1]) r += 1 print("".join([str(item) for item in ans]))
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
for j in range(int(input())): n = int(input()) c = list(map(int,input().split())) index = [0]*n for i in range(n): index[c[i]-1]=i ma = 0 mi = n ans = ['0']*n # print(index) for k in range(n): ma = max(index[k],ma) mi = min(index[k],mi) #print(k,mr,index[k]-index[0]) if ma-mi<=k: ans[k]='1' print(''.join(ans))
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
q=int(input()) for t in range(q): n=int(input()) a=list(map(int,input().split())) ma=1 ans='1' uk1=a.index(1) uk2=uk1 while uk2-uk1+1!=n: if uk2==n-1: uk1-=1 ma=max(ma,a[uk1]) if ma==uk2-uk1+1: ans=ans+'1' else: ans=ans+'0' else: if uk1==0: uk2+=1 ma=max(ma,a[uk2]) if ma == uk2 - uk1 + 1: ans = ans + '1' else: ans=ans+'0' else: if a[uk1-1]<a[uk2+1]: uk1 -= 1 ma = max(ma, a[uk1]) if ma == uk2 - uk1 + 1: ans = ans + '1' else: ans = ans + '0' else: uk2 += 1 ma = max(ma, a[uk2]) if ma == uk2 - uk1 + 1: ans = ans + '1' else: ans = ans + '0' print(ans)
You are given a permutation $p=[p_1, p_2, \ldots, p_n]$ of integers from $1$ to $n$. Let's call the number $m$ ($1 \le m \le n$) beautiful, if there exists two indices $l, r$ ($1 \le l \le r \le n$), such that the numbers $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$. For example, let $p = [4, 5, 1, 3, 2, 6]$. In this case, the numbers $1, 3, 5, 6$ are beautiful and $2, 4$ are not. It is because: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 5$ we will have a permutation $[1, 3, 2]$ for $m = 3$; if $l = 1$ and $r = 5$ we will have a permutation $[4, 5, 1, 3, 2]$ for $m = 5$; if $l = 1$ and $r = 6$ we will have a permutation $[4, 5, 1, 3, 2, 6]$ for $m = 6$; it is impossible to take some $l$ and $r$, such that $[p_l, p_{l+1}, \ldots, p_r]$ is a permutation of numbers $1, 2, \ldots, m$ for $m = 2$ and for $m = 4$. You are given a permutation $p=[p_1, p_2, \ldots, p_n]$. For all $m$ ($1 \le m \le n$) determine if it is a beautiful number or not. -----Input----- The first line contains the only integer $t$ ($1 \le t \le 1000$)  — the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the given permutation $p$. The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are different) — the given permutation $p$. It is guaranteed, that the sum of $n$ from all test cases in the input doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines — the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $n$, there the $i$-th character is equal to $1$ if $i$ is a beautiful number and is equal to $0$ if $i$ is not a beautiful number. -----Example----- Input 3 6 4 5 1 3 2 6 5 5 3 1 2 4 4 1 4 3 2 Output 101011 11111 1001 -----Note----- The first test case is described in the problem statement. In the second test case all numbers from $1$ to $5$ are beautiful: if $l = 3$ and $r = 3$ we will have a permutation $[1]$ for $m = 1$; if $l = 3$ and $r = 4$ we will have a permutation $[1, 2]$ for $m = 2$; if $l = 2$ and $r = 4$ we will have a permutation $[3, 1, 2]$ for $m = 3$; if $l = 2$ and $r = 5$ we will have a permutation $[3, 1, 2, 4]$ for $m = 4$; if $l = 1$ and $r = 5$ we will have a permutation $[5, 3, 1, 2, 4]$ for $m = 5$.
lpn = int(input()) for loop in range(lpn): n = int(input()) p = list(map(int,input().split())) for i in range(n): if p[i] == 1: oneind = i break l = oneind r = oneind nmax = 1 ans = [0] * n ans[0] = 1 for i in range(n-1): if l == 0 or( r != n-1 and p[l-1] > p[r+1]): r += 1 nmax = max(nmax,p[r]) if i+2 == nmax: ans[i+1] = 1 else: l -= 1 nmax = max(nmax,p[l]) if i+2 == nmax: ans[i+1] = 1 print("".join(map(str,ans)))
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
39

Collection including rbelanec/apps