Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5), the number of piles. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^3, a_1 + a_2 + ... + a_{n} ≀ 10^6), where a_{i} is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 10^5), the number of juicy worms said by Marmot. The fourth line contains m integers q_1, q_2, ..., q_{m} (1 ≀ q_{i} ≀ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms. -----Output----- Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is. -----Examples----- Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 -----Note----- For the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
import math import random import itertools import collections import sys import time import fractions import os import functools import bisect def timer(f): def tmp(*args, **kwargs): t = time.time() res = f(*args, **kwargs) print("ВрСмя выполнСния Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ: %f" % (time.time()-t)) return res return tmp def contains(l, elem): index = bisect.bisect_left(l, elem) if index < len(l): return l[index] == elem return False n = int(input()) l = list(map(int, input().split(' '))) q = int(input()) qs = list(map(int, input().split(' '))) """ n = 3 l = [5, 3, 4] q = 12 qs = [i+1 for i in range(q)] """ """ n = 5 l = [random.randint(0, 10) for i in range(n)] q = random.randint(0, 15) qs = [random.randint(0, 10) for i in range(q)] l = sorted(l) print(l) print(qs) """ #print(l) #print(qs) partials = list(itertools.accumulate(l)) #print(partials) for i in range(q): kuchka = bisect.bisect_left(partials, qs[i]) print(kuchka+1)
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5), the number of piles. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^3, a_1 + a_2 + ... + a_{n} ≀ 10^6), where a_{i} is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 10^5), the number of juicy worms said by Marmot. The fourth line contains m integers q_1, q_2, ..., q_{m} (1 ≀ q_{i} ≀ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms. -----Output----- Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is. -----Examples----- Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 -----Note----- For the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
n = int(input()) a = list(map(int, input().split())) m = int(input()) q = list(map(int, input().split())) qq = sorted(q) ans = dict() limit = 0 i = 0 for k in qq: while not (limit < k <= limit + a[i]): limit += a[i] i += 1 ans[k] = i + 1 for k in q: print(ans[k])
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5), the number of piles. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^3, a_1 + a_2 + ... + a_{n} ≀ 10^6), where a_{i} is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 10^5), the number of juicy worms said by Marmot. The fourth line contains m integers q_1, q_2, ..., q_{m} (1 ≀ q_{i} ≀ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms. -----Output----- Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is. -----Examples----- Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 -----Note----- For the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
from bisect import bisect_left n=int(input()) S=A=list(map(int,input().split())) for i in range(1,n): S[i] += S[i-1] m=int(input()) for q in list(map(int,input().split())): print(bisect_left(S, q)+1)
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5), the number of piles. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^3, a_1 + a_2 + ... + a_{n} ≀ 10^6), where a_{i} is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 10^5), the number of juicy worms said by Marmot. The fourth line contains m integers q_1, q_2, ..., q_{m} (1 ≀ q_{i} ≀ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms. -----Output----- Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is. -----Examples----- Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 -----Note----- For the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
from sys import stdin def main(): ''' Name: Kevin S. Sanchez Code: B. Worms ''' inp = stdin n = int(inp.readline()) worms = list(map(int, inp.readline().split())) J = int(inp.readline()) Jworms = list(map(int, inp.readline().split())) lunch = list() for i in range (0,len(worms)): lunch += [i+1] * worms[i] for i in Jworms: print(lunch[i-1]) main()
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5), the number of piles. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^3, a_1 + a_2 + ... + a_{n} ≀ 10^6), where a_{i} is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 10^5), the number of juicy worms said by Marmot. The fourth line contains m integers q_1, q_2, ..., q_{m} (1 ≀ q_{i} ≀ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms. -----Output----- Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is. -----Examples----- Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 -----Note----- For the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
from bisect import * n = int(input()) a = list(map(int, input().split())) for i in range(n - 1): a[i + 1] += a[i] input() for i in map(int, input().split()): print(bisect_left(a, i) + 1)
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5), the number of piles. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^3, a_1 + a_2 + ... + a_{n} ≀ 10^6), where a_{i} is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 10^5), the number of juicy worms said by Marmot. The fourth line contains m integers q_1, q_2, ..., q_{m} (1 ≀ q_{i} ≀ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms. -----Output----- Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is. -----Examples----- Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 -----Note----- For the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
from sys import stdin from bisect import bisect_left stdin.readline() x, l = 0, [] for y in map(int, stdin.readline().split()): x += y l.append(x) stdin.readline() for y in map(int, stdin.readline().split()): print(bisect_left(l, y) + 1)
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. -----Input----- The first line contains a single integer n (1 ≀ n ≀ 10^5), the number of piles. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^3, a_1 + a_2 + ... + a_{n} ≀ 10^6), where a_{i} is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 10^5), the number of juicy worms said by Marmot. The fourth line contains m integers q_1, q_2, ..., q_{m} (1 ≀ q_{i} ≀ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms. -----Output----- Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is. -----Examples----- Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 -----Note----- For the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
from sys import stdin from bisect import bisect_left def main(): stdin.readline() x, a = 0, [] for y in map(int, stdin.readline().split()): x += y a.append(x) stdin.readline() for x in map(int, stdin.readline().split()): print(bisect_left(a, x) + 1) main()
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
3 from math import factorial as fact N = 55 c = [1] for i in range(N): c.append(fact(i)) dp = [0] * N dp[0] = 1 for i in range(1, N): for j in range(i): dp[i] += dp[j] * c[i - j - 1] def get_kth_cycle(n, k): if n == 1: return [1] ans = [-1] * n ans[0] = n - 1 fin = [i for i in range(n)] fin[0] = n - 1 init = [i for i in range(n)] init[n - 1] = 0 used = [False] * n used[n - 1] = True for i in range(1, n - 1): j = 0 cur = fact(n - i - 2) while True: while used[j] or (i < n - 1 and j == init[i]): j += 1 if k > cur: k -= cur j += 1 else: fin[init[i]] = fin[j] init[fin[j]] = init[i] ans[i] = j used[j] = True break ans[-1] = init[-1] return [x + 1 for x in ans] def f(n, k): if n == 0: assert k == 1 return [] cl = 1 while c[cl - 1] * dp[n - cl] < k: k -= c[cl - 1] * dp[n - cl] cl += 1 rest = f(n - cl, (k - 1) % dp[n - cl] + 1) rest = [x + cl for x in rest] k = (k - 1) // dp[n - cl] + 1 return get_kth_cycle(cl, k) + rest def solve(): n, k = list(map(int, input().split())) if k > dp[n]: print(-1) return print(*f(n, k)) def main(): t = int(input()) while t > 0: t -= 1 solve() main()
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
3 from math import factorial as fact N = 55 c = [1] for i in range(N): c.append(fact(i)) dp = [0] * N dp[0] = 1 for i in range(1, N): for j in range(i): dp[i] += dp[j] * c[i - j - 1] def get_kth_cycle(n, k): if n == 1: return [1] ans = [-1] * n ans[0] = n - 1 fin = [i for i in range(n)] fin[0] = n - 1 init = [i for i in range(n)] init[n - 1] = 0 used = [False] * n used[n - 1] = True for i in range(1, n - 1): j = 0 cur = fact(n - i - 2) while True: while used[j] or (i < n - 1 and j == init[i]): j += 1 if k > cur: k -= cur j += 1 else: fin[init[i]] = fin[j] init[fin[j]] = init[i] ans[i] = j used[j] = True break ans[-1] = init[-1] return [x + 1 for x in ans] def f(n, k): if n == 0: assert k == 1 return [] cl = 1 while c[cl - 1] * dp[n - cl] < k: k -= c[cl - 1] * dp[n - cl] cl += 1 rest = f(n - cl, (k - 1) % dp[n - cl] + 1) rest = [x + cl for x in rest] k = (k - 1) // dp[n - cl] + 1 return get_kth_cycle(cl, k) + rest def solve(): n, k = map(int, input().split()) if k > dp[n]: print(-1) return print(*f(n, k)) def main(): t = int(input()) while t > 0: t -= 1 solve() main()
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
from math import factorial as fact N = 55 c = [1] for i in range(N): c.append(fact(i)) dp = [0] * N dp[0] = 1 for i in range(1, N): for j in range(i): dp[i] += dp[j] * c[i - j - 1] def get_kth_cycle(n, k): if n == 1: return [1] ans = [-1] * n ans[0] = n - 1 fin = [i for i in range(n)] fin[0] = n - 1 init = [i for i in range(n)] init[n - 1] = 0 used = [False] * n used[n - 1] = True for i in range(1, n - 1): j = 0 cur = fact(n - i - 2) while True: while used[j] or (i < n - 1 and j == init[i]): j += 1 if k > cur: k -= cur j += 1 else: fin[init[i]] = fin[j] init[fin[j]] = init[i] ans[i] = j used[j] = True break ans[-1] = init[-1] return [x + 1 for x in ans] def f(n, k): if n == 0: assert k == 1 return [] cl = 1 while c[cl - 1] * dp[n - cl] < k: k -= c[cl - 1] * dp[n - cl] cl += 1 rest = f(n - cl, (k - 1) % dp[n - cl] + 1) rest = [x + cl for x in rest] k = (k - 1) // dp[n - cl] + 1 return get_kth_cycle(cl, k) + rest def solve(): n, k = map(int, input().split()) if k > dp[n]: print(-1) return print(*f(n, k)) def main(): t = int(input()) while t > 0: t -= 1 solve() main()
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
import math # init maxn = 55 g = [1] for i in range(maxn): g.append(math.factorial(i)) f = [0] * maxn f[0] = 1 for i in range(1, maxn): for j in range(i): f[i] += f[j] * g[i - j - 1] def kth(n, k): if n == 1: return [1] ret = [-1] * n ret[0] = n - 1 p1 = [i for i in range(n)] p2 = [i for i in range(n)] vis = [False] * n p1[0] = n - 1 p2[n - 1] = 0 vis[n - 1] = True for i in range(1, n - 1): j = 0 now = math.factorial(n - i - 2) while True: while vis[j] or (i < n - 1 and j == p2[i]): j += 1 if k > now: k -= now j += 1 else: p1[p2[i]] = p1[j] p2[p1[j]] = p2[i] ret[i] = j vis[j] = True break ret[-1] = p2[-1] return [x + 1 for x in ret] def solve(n, k): if n == 0: return [] i = 1 while g[i - 1] * f[n - i] < k: k -= g[i - 1] * f[n - i] i += 1 rem = solve(n - i, (k - 1) % f[n - i] + 1) rem = [x + i for x in rem] k = (k - 1) // f[n - i] + 1 return kth(i, k) + rem def SOLVE(): n, k = map(int, input().split()) if k > f[n]: print(-1) return ans = solve(n, k) for x in ans: print(x, end = " ") print() T = int(input()) while T > 0: T -= 1 SOLVE()
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
from math import factorial as fact N = 55 c = [1] for i in range(N): c.append(fact(i)) dp = [0] * N dp[0] = 1 for i in range(1, N): for j in range(i): dp[i] += dp[j] * c[i - j - 1] def get_kth_cycle(n, k): if n == 1: return [1] ans = [-1] * n ans[0] = n - 1 fin = [i for i in range(n)] fin[0] = n - 1 init = [i for i in range(n)] init[n - 1] = 0 used = [False] * n used[n - 1] = True for i in range(1, n - 1): j = 0 cur = fact(n - i - 2) while True: while used[j] or (i < n - 1 and j == init[i]): j += 1 if k > cur: k -= cur j += 1 else: fin[init[i]] = fin[j] init[fin[j]] = init[i] ans[i] = j used[j] = True break ans[-1] = init[-1] return [x + 1 for x in ans] def f(n, k): if n == 0: assert k == 1 return [] cl = 1 while c[cl - 1] * dp[n - cl] < k: k -= c[cl - 1] * dp[n - cl] cl += 1 rest = f(n - cl, (k - 1) % dp[n - cl] + 1) rest = [x + cl for x in rest] k = (k - 1) // dp[n - cl] + 1 return get_kth_cycle(cl, k) + rest def solve(): n, k = map(int, input().split()) if k > dp[n]: print(-1) return print(*f(n, k)) def main(): t = int(input()) while t > 0: t -= 1 solve() main()
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
MAXN = 55 f = [0] * MAXN fac = [0] * MAXN fac[0] = 1 for i in range(1, 51): fac[i] = fac[i - 1] * i f[0] = 1; for i in range(1, 51): f[i] += f[i - 1] for j in range(2, i + 1): f[i] += fac[j - 2] * f[i - j] def my_fac(n): if n <= 0: return 1 return fac[n] def solve_first(n, k): ret = [0] * (n + 1) ret[1] = n for p in range(2, n + 1): for i in range(1, n + 1): if i in ret or i == p: continue ret[p] = i cur = p good = True for fuck in range(0, n - 1): cur = ret[cur] if cur == 0: good = True break if cur == p: good = False break if not good: ret[p] = 0 continue k1 = my_fac(n - p - 1) if k > k1: k -= k1 else: break ret[p] = 0 ret.pop(0) assert len(ret) == n return ret; def solve(n, k): if k == 1: ret = [] for i in range(1, n + 1): ret.append(i) return ret tot = 0 first = -1 for i in range(1, n + 1): if tot + my_fac(i - 2) * f[n - i] >= k: first = i break; tot += my_fac(i - 2) * f[n - i] k -= tot cnt1 = my_fac(first - 1) cnt2 = f[n - first] x = k // cnt2 + 1 y = k % cnt2 if y == 0: y = cnt2 x -= 1 ret = solve_first(first, x) for v in solve(n - first, y): ret.append(v + first) return ret T = int(input()) for t in range(0, T): s = input().split() n = int(s[0]) k = int(s[1]) if (k > f[n]): print("-1") else: ans = solve(n, k) for x in ans: print(x, end=" ") print("")
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
import math # init maxn = 55 g = [1] for i in range(maxn): g.append(math.factorial(i)) f = [0] * maxn f[0] = 1 for i in range(1, maxn): for j in range(i): f[i] += f[j] * g[i - j - 1] def kth(n, k): if n == 1: return [1] ret = [-1] * n ret[0] = n - 1 p1 = [i for i in range(n)] p2 = [i for i in range(n)] vis = [False] * n p1[0] = n - 1 p2[n - 1] = 0 vis[n - 1] = True for i in range(1, n - 1): j = 0 now = math.factorial(n - i - 2) while True: while vis[j] or (i < n - 1 and j == p2[i]): j += 1 if k > now: k -= now j += 1 else: p1[p2[i]] = p1[j] p2[p1[j]] = p2[i] ret[i] = j vis[j] = True break ret[-1] = p2[-1] return [x + 1 for x in ret] def solve(n, k): if n == 0: return [] i = 1 while g[i - 1] * f[n - i] < k: k -= g[i - 1] * f[n - i] i += 1 rem = solve(n - i, (k - 1) % f[n - i] + 1) rem = [x + i for x in rem] k = (k - 1) // f[n - i] + 1 return kth(i, k) + rem def SOLVE(): n, k = map(int, input().split()) if k > f[n]: print(-1) return ans = solve(n, k) for x in ans: print(x, end = " ") print() T = int(input()) while T > 0: T -= 1 SOLVE()
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
import math # init maxn = 55 g = [1] for i in range(maxn): g.append(math.factorial(i)) f = [0] * maxn f[0] = 1 for i in range(1, maxn): for j in range(i): f[i] += f[j] * g[i - j - 1] def kth(n, k): if n == 1: return [1] ret = [-1] * n ret[0] = n - 1 p1 = [i for i in range(n)] p2 = [i for i in range(n)] vis = [False] * n p1[0] = n - 1 p2[n - 1] = 0 vis[n - 1] = True for i in range(1, n - 1): j = 0 now = math.factorial(n - i - 2) while True: while vis[j] or (i < n - 1 and j == p2[i]): j += 1 if k > now: k -= now j += 1 else: p1[p2[i]] = p1[j] p2[p1[j]] = p2[i] ret[i] = j vis[j] = True break ret[-1] = p2[-1] return [x + 1 for x in ret] def solve(n, k): if n == 0: return [] i = 1 while g[i - 1] * f[n - i] < k: k -= g[i - 1] * f[n - i] i += 1 rem = solve(n - i, (k - 1) % f[n - i] + 1) rem = [x + i for x in rem] k = (k - 1) // f[n - i] + 1 return kth(i, k) + rem def SOLVE(): n, k = map(int, input().split()) if k > f[n]: print(-1) return ans = solve(n, k) for x in ans: print(x, end = " ") print() T = int(input()) while T > 0: T -= 1 SOLVE() # #
Yeah, we failed to make up a New Year legend for this problem. A permutation of length $n$ is an array of $n$ integers such that every integer from $1$ to $n$ appears in it exactly once. An element $y$ of permutation $p$ is reachable from element $x$ if $x = y$, or $p_x = y$, or $p_{p_x} = y$, and so on. The decomposition of a permutation $p$ is defined as follows: firstly, we have a permutation $p$, all elements of which are not marked, and an empty list $l$. Then we do the following: while there is at least one not marked element in $p$, we find the leftmost such element, list all elements that are reachable from it in the order they appear in $p$, mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of $l$. After all elements are marked, $l$ is the result of this decomposition. For example, if we want to build a decomposition of $p = [5, 4, 2, 3, 1, 7, 8, 6]$, we do the following: initially $p = [5, 4, 2, 3, 1, 7, 8, 6]$ (bold elements are marked), $l = []$; the leftmost unmarked element is $5$; $5$ and $1$ are reachable from it, so the list we want to shift is $[5, 1]$; there is no need to shift it, since maximum is already the first element; $p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]$, $l = [[5, 1]]$; the leftmost unmarked element is $4$, the list of reachable elements is $[4, 2, 3]$; the maximum is already the first element, so there's no need to shift it; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]$, $l = [[5, 1], [4, 2, 3]]$; the leftmost unmarked element is $7$, the list of reachable elements is $[7, 8, 6]$; we have to shift it, so it becomes $[8, 6, 7]$; $p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]$, $l = [[5, 1], [4, 2, 3], [8, 6, 7]]$; all elements are marked, so $[[5, 1], [4, 2, 3], [8, 6, 7]]$ is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of $p = [5, 4, 2, 3, 1, 7, 8, 6]$ is built as follows: the decomposition is $[[5, 1], [4, 2, 3], [8, 6, 7]]$; after sorting the decomposition, it becomes $[[4, 2, 3], [5, 1], [8, 6, 7]]$; $[4, 2, 3, 5, 1, 8, 6, 7]$ is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, $[4, 3, 1, 2, 8, 5, 6, 7]$ is a good permutation; and $[5, 4, 2, 3, 1, 7, 8, 6]$ is bad, since the result of transformation is $[4, 2, 3, 5, 1, 8, 6, 7]$. Your task is the following: given $n$ and $k$, find the $k$-th (lexicographically) good permutation of length $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then the test cases follow. Each test case is represented by one line containing two integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 10^{18}$). -----Output----- For each test case, print the answer to it as follows: if the number of good permutations of length $n$ is less than $k$, print one integer $-1$; otherwise, print the $k$-th good permutation on $n$ elements (in lexicographical order). -----Example----- Input 5 3 3 5 15 4 13 6 8 4 2 Output 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
import math # init maxn = 55 g = [1] for i in range(maxn): g.append(math.factorial(i)) f = [0] * maxn f[0] = 1 for i in range(1, maxn): for j in range(i): f[i] += f[j] * g[i - j - 1] def kth(n, k): if n == 1: return [1] ret = [-1]* n ret[0] = n - 1 p1 = [i for i in range(n)] p2 = [i for i in range(n)] vis = [False] * n p1[0] = n - 1 p2[n - 1] = 0 vis[n - 1] = True for i in range(1, n - 1): j = 0 now = math.factorial(n - i - 2) while True: while vis[j] or (i < n - 1 and j == p2[i]): j += 1 if k > now: k -= now j += 1 else: p1[p2[i]] = p1[j] p2[p1[j]] = p2[i] ret[i] = j vis[j] = True break ret[-1] = p2[-1] return [x + 1 for x in ret] def solve(n, k): if n == 0: return [] i = 1 while g[i - 1] * f[n - i] < k: k -= g[i - 1] * f[n - i] i += 1 rem = solve(n - i, (k - 1) % f[n - i] + 1) rem = [x + i for x in rem] k = (k - 1) // f[n - i] + 1 return kth(i, k) + rem def SOLVE(): n, k = map(int, input().split()) if k > f[n]: print(-1) return ans = solve(n, k) for x in ans: print(x, end = " ") print() T = int(input()) while T > 0: T -= 1 SOLVE()
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
q = int(input()) for z in range(q): n, k1, k2 = map(int, input().split()) arr1 = list(map(int, input().split())) arr2 = list(map(int, input().split())) if max(arr1) > max(arr2): print('YES') else: print('NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t = int(input()) for _ in range(t): n, k1, k2 = list(map(int, input().strip().split())) prvi = list(map(int, input().strip().split())) drugi = list(map(int, input().strip().split())) if max(prvi) > max(drugi): print('YES') else: print('NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
for i in range(int(input())): n, k1, k2 = map(int,input().split()) l1 = list(map(int,input().split())) a = max(l1) l2 = list(map(int,input().split())) b = max(l2) if a > b: print("YES") else : print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
a = int(input()) for i in range(a): n, k1, k2 = list(map(int, input().split())) k11 = list(map(int, input().split())) k22 = list(map(int, input().split())) if max(k11) > max(k22): print('YES') else: print('NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
from math import * import os, sys from bisect import * from io import BytesIO #input = BytesIO(os.read(0, os.fstat(0).st_size)).readline sys.setrecursionlimit(10 ** 9) #sys.stdin = open("moobuzz.in", 'r') #sys.stdout = open("moobuzz.out", 'w') for _ in range(int(input())): n, k1, k2 = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) if max(a) > max(b): print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t = int(input()) for rwuer in range(t): n,k1,k2 = map(int,input().split()) l1 = list(map(int,input().split())) l2 = list(map(int,input().split())) a1 = max(l1) a2 = max(l2) if a1>a2: print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
def solve(): n, k1, k2 = list(map(int, input().split())) m1 = max(list(map(int, input().split()))) m2 = max(list(map(int, input().split()))) if m1 > m2: print('YES') else: print('NO') def main(): t = int(input()) # t = 1 for _ in range(t): solve() main()
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t = int(input()) for _ in range(t): n, k1, k2 = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) if max(a) > max(b): print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t=int(input()) for _ in range(t): n,k1,k2=list(map(int,input().split())) a1=sorted(list(map(int,input().split()))) a2=sorted(list(map(int,input().split()))) if(n in a1): print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
for tc in range(int(input())): input() lsa = list(map(int, input().split())) lsb = list(map(int, input().split())) print('YES' if max(max(lsa),max(lsb)) in lsa else 'NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t = int(input()) for i in range(t): n, k1, k2 = list(map(int, input().split())) a = max(list(map(int, input().split()))) b = max(list(map(int, input().split()))) if a > b: print('YES') else: print('NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
q = int(input()) while q: n, k1, k2 = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) if max(a) > max(b): print("YES") else: print("NO") q -= 1
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t = int(input()) for test in range(t): input() max1 = max(list(map(int,input().split()))) max2 = max(list(map(int,input().split()))) if max1 > max2: print('YES') else: print('NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
import sys import math from collections import defaultdict from collections import deque from itertools import combinations from itertools import permutations input = lambda : sys.stdin.readline().rstrip() read = lambda : list(map(int, input().split())) go = lambda : 1/0 def write(*args, sep="\n"): for i in args: sys.stdout.write("{}{}".format(i, sep)) INF = float('inf') MOD = int(1e9 + 7) YES = "YES" NO = "NO" for _ in range(int(input())): n, x, y = read() X = read() Y = read() if n in X: print(YES) else: print(NO)
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
for case in range(int(input())): input() a=max(list(map(int,input().split()))) b=max(list(map(int,input().split()))) print('YES' if a>b else 'NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t=int(input()) for q in range(t): n,k1,k2=map(int,input().split()) a1=max(list(map(int,input().split()))) a2=max(list(map(int,input().split()))) if a1>a2: print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
for _ in range(int(input())): n,k1A,k2A = list(map(int,input().split())) k1 = list(map(int,input().split())) k2 = list(map(int,input().split())) print("YES" if max(k1) > max(k2) else "NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
from bisect import bisect_left as bl from bisect import bisect_right as br import heapq import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline # M = mod = 998244353 def factors(n):return sorted(list(set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip('\n').split(' ')] def st():return input().rstrip('\n') def val():return int(input().rstrip('\n')) def li2():return [i for i in input().rstrip('\n').split(' ')] def li3():return [int(i) for i in input().rstrip('\n')] for _ in range(val()): n,k1,k2 = li() a = sorted(li()) b = sorted(li()) print('YES' if a[-1]>b[-1] else 'NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
import sys import math import itertools import functools import collections import random def ii(): return int(input()) def mi(): return list(map(int, input().split())) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def wr(arr): return ' '.join(map(str, arr)) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber def cdiv(n, k): return n // k + (n % k != 0) t = ii() for _ in range(t): n, k1, k2 = mi() a = li() b = li() if max(a) > max(b): print('YES') else: print('NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t = int(input()) for query in range(t): n, k1, k2 = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) if n in A: print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t = int(input()) for qwe in range(t): n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if max(a) == n: print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
for _ in range(int(input())): n,k1,k2 = map(int,input().split()) a = list(map(int,input().split())) b = list(map(int,input().split())) if n in a: print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
for i in range(int(input())): n, k1, k2 = list(map(int, input().split())) u = max(list(map(int, input().split()))) v = max(list(map(int, input().split()))) if u < v: print('NO') else: print('YES')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t=int(input()) for l in range(t): n,k1,k2=list(map(int,input().split())) arr1=list(map(int,input().split())) arr2=list(map(int,input().split())) if(max(arr1)>max(arr2)): print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
for i in ' '*int(input()): n,k1,k2=map(int,input().split()) L1=list(map(int,input().split())) L2=list(map(int,input().split())) if max(L1)>max(L2):print('YES') else:print('NO')
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
def case(): n, k1, k2 = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if max(a) > max(b): print("YES") else: print("NO") for _ in range(int(input())): case()
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
for _ in range(int(input())): n,k1,k2 = list(map(int, input().split())) arr = list(map(int, input().split())) brr = list(map(int, input().split())) max1 = max(arr) max2 = max(brr) if max1>max2: print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t = int(input()) for i in range(t): n,k1,k2 = list(map(int,input().split())) s1 = max(list(map(int,input().split()))) s2 = max(list(map(int,input().split()))) if s1 == n: print("YES") else: print("NO")
Two players decided to play one interesting card game. There is a deck of $n$ cards, with values from $1$ to $n$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards. For example, suppose that $n = 5$, the first player has cards with values $2$ and $3$, and the second player has cards with values $1$, $4$, $5$. Then one possible flow of the game is: The first player chooses the card $3$. The second player chooses the card $1$. As $3>1$, the first player gets both cards. Now the first player has cards $1$, $2$, $3$, the second player has cards $4$, $5$. The first player chooses the card $3$. The second player chooses the card $4$. As $3<4$, the second player gets both cards. Now the first player has cards $1$, $2$. The second player has cards $3$, $4$, $5$. The first player chooses the card $1$. The second player chooses the card $3$. As $1<3$, the second player gets both cards. Now the first player has only the card $2$. The second player has cards $1$, $3$, $4$, $5$. The first player chooses the card $2$. The second player chooses the card $4$. As $2<4$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins. Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains three integers $n$, $k_1$, $k_2$ ($2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$)Β β€” the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $k_1$ integers $a_1, \dots, a_{k_1}$ ($1 \le a_i \le n$)Β β€” the values of cards of the first player. The third line of each test case contains $k_2$ integers $b_1, \dots, b_{k_2}$ ($1 \le b_i \le n$)Β β€” the values of cards of the second player. It is guaranteed that the values of all cards are different. -----Output----- For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). -----Example----- Input 2 2 1 1 2 1 5 2 3 2 3 1 4 5 Output YES NO -----Note----- In the first test case of the example, there is only one possible move for every player: the first player will put $2$, the second player will put $1$. $2>1$, so the first player will get both cards and will win. In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
t=int(input()) for i in range(t): n, k1, k2=list(map(int, input().split())) d=max(list(map(int, input().split()))) d1=max(list(map(int, input().split()))) if d>d1: print("YES") else: print("NO")
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
t=int(input()) for tt in range(t): a,b,p=map(int,input().split()) s=input() n=len(s) cost = [0]*n cost[-1] = 0 typ = '' i=n-2 while i>=0: if s[i]==typ: cost[i] = cost[i+1] else: typ = s[i] cost[i] = cost[i+1] + (a if typ=='A' else b) i-=1 i=0 while cost[i] > p: i+=1 print(i+1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
from sys import stdin,stderr def rl(): return [int(w) for w in stdin.readline().split()] t, = rl() for _ in range(t): a,b,p = rl() s = stdin.readline().rstrip() r = 1 t = '' for i in range(len(s)-1,0,-1): if s[i-1] != t: t = s[i-1] p -= a if t == 'A' else b if p < 0: r = i+1 break print(r)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
# from collections import defaultdict for _ in range(int(input())): # n = int(input()) a, b, p = map(int, input().split()) s = input() n = len(s) money = [0] * n last = 'C' for i in range(n-2, -1, -1): if s[i] == last: money[i] = money[i+1] elif s[i] == 'A': money[i] = money[i+1] + a else: money[i] = money[i+1] + b last = s[i] for i in range(1, n+1): if money[i-1] <= p: print(i) break
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
from math import * from collections import * t = int(input()) for y in range(t): a,b,p = map(int,input().split()) s = input() n = len(s) i = n-2 ans = n ct = 0 while(i >= 0): st = s[i] while(i >= 0 and s[i] == st): i -= 1 if(st == 'A'): ct += a else: ct += b if(ct <= p): ans = i+2 print(ans)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
for _ in range(int(input())): a, b, p = list(map(int, input().split())) ar = list(input()) i = len(ar) - 2 now = ar[i] flag = 0 if now == 'A': if p >= a: flag = 1 else: if p >= b: flag = 1 while i >= 0 and p > 0 and flag == 1: if ar[i] == now: i -= 1 else: if ar[i + 1] == 'A': p -= a else: p -= b if ar[i] == 'A': if p < a: break now = 'A' else: if p < b: break now = 'B' print(i + 2)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
t = int(input()) for i in range(t): a, b, p = list(map(int, input().split())) s = input() n = len(s) ind = n sum = 0 while sum <= p and ind > 0: ind -= 1 if ind == n - 1 or s[ind - 1] != s[ind]: if s[ind - 1] == "A": sum += a else: sum += b print(ind + 1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
t = int(input()) for tt in range(t): a, b, p = map(int, input().split()) s = input() cs = {'A':a, 'B':b} c = 0 i = len(s)-1 while i > 0 and c+cs[s[i-1]] <= p: # print(tt, i) c += cs[s[i-1]] i -= 1 while i > 0 and s[i-1] == s[i]: i -= 1 print(i+1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
""" NTC here """ import sys inp= sys.stdin.readline input = lambda : inp().strip() # flush= sys.stdout.flush # import threading # sys.setrecursionlimit(10**6) # threading.stack_size(2**26) def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): T = iin() while T: T-=1 a, b, p = lin() s = list(input()) n = len(s) ans = [] ch = 'D' for i in range( n-1): if s[i]!=ch: ch = s[i] x = a if ch=='B':x=b ans.append([x, i]) l = len(ans) ans = ans[::-1] for i in range(1, l): ans[i][0]+=ans[i-1][0] ans = ans[::-1] for i, j in ans: if p>=i: print(j+1) break else: print(n) #print(ans) main() #threading.Thread(target=main).start()
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
for _ in range(int(input())): a, b, p = list(map(int, input().split())) s = input() naw = 0 for q in range(len(s)-2, -1, -1): if (q == len(s)-2 or s[q] != s[q+1]) and naw+(a if s[q] == 'A' else b) > p: print(q+2) break elif q == len(s)-2 or s[q] != s[q+1]: naw += (a if s[q] == 'A' else b) else: print(1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
import sys input = sys.stdin.readline for j in range(int(input())): a, b, p = list(map(int, input().split(" "))) s = input().rstrip() costs = [0 for x in range(len(s))] costs[len(s)-1] = 0 if(s[len(s)-2] == "B"): costs[len(s)-2] = b else: costs[len(s)-2] = a for it in range(3, len(s)+1): if(s[len(s)-it] !=s[len(s)-it+1]): costs[len(s)-it] = costs[len(s)-it+1]+(s[len(s)-it]=="A")*a+(s[len(s)-it]=="B")*b else: costs[len(s)-it] = costs[len(s)-it+1] for j in range(len(costs)): if(costs[j]<=p): print(j+1) break
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
t = int(input()) for _ in range(t): a,b,p = map(int,input().split()) s = list(input()) n = len(s) flg = 0 ans = n y = "C" k = 0 while s: x = s.pop() if not flg: flg = 1 continue if x == y: ans -= 1 continue else: if x == "A": if p<a: print(ans) k = 1 break else: p -= a if x == "B": if p<b: print(ans) k = 1 break else: p -= b y = x ans -= 1 if s == [] and k == 0: print(1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
for t in range(int(input())): a, b, p = [int(i) for i in input().split()] s = input()[::-1] n = len(s) curr = s[1] res = 0 for i in range(1, n): if (s[i] != curr): if (curr == "A"): p -= a curr = "B" else: p -= b curr = "A" if (p < 0): break else: res = i - 1 if (curr == "A"): p -= a else: p -= b if (p < 0): print(n - res) else: print(1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
# from collections import deque import sys input = lambda: sys.stdin.readline().strip() def d(x): if x=='A': return a return b def f(j): su = d(s[j]) for i in range(j+1, len(s) - 1): if s[i]!=s[i-1]: su+=d(s[i]) return su for i in range(int(input())): a,b,p = map(int,input().split()) s = list(input()) l = -1 r = len(s)-1 while r - l > 1: m = (r + l) // 2 if f(m) > p: l = m else: r = m print(r+1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
from math import inf t = int(input()) for q in range(t): a, b, p = [int(i) for i in input().split()] s = input() l = len(s) dp = [0] * l if s[l - 2] == 'A': dp[l - 2] = a else: dp[l - 2] = b for i in range(l - 3, -1, -1): if s[i] == s[i + 1]: dp[i] = dp[i + 1] else: if s[i] == 'A': dp[i] = dp[i + 1] + a else: dp[i] = dp[i + 1] + b #print(dp) for i in range(l): if p >= dp[i]: print(i + 1) break
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
# import sys # input = sys.stdin.readline t = int(input()) for _ in range(t): a, b, p = map(int, input().split()) s = input() n = len(s) dp = [0] * n for i in range(n-2, -1, -1): if i == n-2: dp[i] = a if s[i] == 'A' else b elif s[i] == s[i+1]: dp[i] = dp[i+1] else: dp[i] = dp[i+1] + (a if s[i] == 'A' else b) ans = -1 for i in range(n): if dp[i] <= p: ans = i+1 break print(ans)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
t = int(input()) while t: t += -1 a, b, p = map(int, input().split()) s = input() l = [] for i in s: l.append(i) cost = 0 for i in range(len(l) - 1): if l[i + 1] != l[i]: if l[i] == 'A': cost += a else: cost += b if l[len(l) - 1] == l[len(l) - 2]: if l[len(l) - 1] == 'A': cost += a else: cost += b # print(cost) ind = -1 for i in range(len(l) - 1): if cost <= p: ind = i break if l[i + 1] != l[i]: if l[i] == 'A': cost -= a else: cost -= b if ind == -1: print(len(l)) else: print(ind + 1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
import sys import math from collections import defaultdict from collections import deque from itertools import combinations from itertools import permutations input = lambda : sys.stdin.readline().rstrip() read = lambda : list(map(int, input().split())) go = lambda : 1/0 def write(*args, sep="\n"): for i in args: sys.stdout.write("{}{}".format(i, sep)) INF = float('inf') MOD = int(1e9 + 7) YES = "YES" NO = "NO" for _ in range(int(input())): try: a, b, p = read() s = input() stack = [[s[0], 1]] for i in s[1:-1]: if i == stack[-1][0]: stack[-1][1] += 1 else: stack.append([i, 1]) ans = len(s) temp = [] # print(stack) if p < a and p < b: print(len(s)) go() while stack: i, j = stack[-1] stack.pop() if i == 'A' and p >= a: p -= a ans -= j temp.append(j) elif i == 'A' and p < a: break if i == 'B' and p >= b: p -= b ans -= j temp.append(j) elif i == 'B' and p < b: break print(ans) except ZeroDivisionError: continue except Exception as e: print(e) continue
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
for _ in range(int(input())): a, b, p = map(int, input().split()) s = input() d = {'A': a, 'B': b} c = '0' inv = [] start = 0 for i in range(1, len(s)): if s[i] != s[i-1]: inv.append((start, i, s[i-1])) start = i elif i == len(s) - 1: inv.append((start, i, s[i])) ans, cost = len(s) - 1, 0 for q in inv[::-1]: cost += d[q[2]] if cost > p: break else: ans = q[0] print(ans + 1)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
for i in range(int(input())): a, b, p = map(int, input().split()) s = input() dp = [0 for i in range(len(s))] for j in range(len(s) - 2, -1, -1): if s[j] != s[j + 1] or dp[j + 1] == 0: if s[j] == 'B': if dp[j + 1] == 0: dp[j] = b else: dp[j] = dp[j + 1] + b else: if dp[j + 1] == 0: dp[j] = a else: dp[j] = dp[j + 1] + a else: dp[j] = dp[j + 1] ans = 1 for i in dp: if i > p: ans += 1 else: break print(ans)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
from math import * def check(a, b, p, s, x): ps = s[x] ans = 0 for i in range(x, len(s)): if ps == s[i]: pass else: if ps == 'A': ans += a else: ans += b if s[i] == 'E': break ps = s[i] return ans <= p zzz = int(input()) for zz in range(zzz): a, b, p = list(map(int, input().split())) s = input() s = list(s) s[-1] = 'E' s = ''.join(s) lb = 0 ub = len(s) + 1 while lb + 1 < ub: tx = (lb + ub) // 2 dx = (ub -lb)//2 if check(a, b, p, s, tx): ub -= dx else: lb += dx #print(ub, lb, dx) x = (lb + ub) // 2 for i in range(4): if x > 0 and check(a, b, p, s, x-1): x -= 1 elif x <= len(s) and not check(a, b, p, s, x): x += 1 print(min(x + 1, len(s)))
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $n$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string $s$ of length $n$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $s_1$) and his goal is to get to the last crossroad (which corresponds to $s_n$). If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a bus station, one can pay $a$ roubles for the bus ticket, and go from $i$-th crossroad to the $j$-th crossroad by the bus (it is not necessary to have a bus station at the $j$-th crossroad). Formally, paying $a$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{A}$ for all $i \le t < j$. If for two crossroads $i$ and $j$ for all crossroads $i, i+1, \ldots, j-1$ there is a tram station, one can pay $b$ roubles for the tram ticket, and go from $i$-th crossroad to the $j$-th crossroad by the tram (it is not necessary to have a tram station at the $j$-th crossroad). Formally, paying $b$ roubles Petya can go from $i$ to $j$ if $s_t = \texttt{B}$ for all $i \le t < j$. For example, if $s$="AABBBAB", $a=4$ and $b=3$ then Petya needs:[Image] buy one bus ticket to get from $1$ to $3$, buy one tram ticket to get from $3$ to $6$, buy one bus ticket to get from $6$ to $7$. Thus, in total he needs to spend $4+3+4=11$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $s_n$) does not affect the final expense. Now Petya is at the first crossroad, and he wants to get to the $n$-th crossroad. After the party he has left with $p$ roubles. He's decided to go to some station on foot, and then go to home using only public transport. Help him to choose the closest crossroad $i$ to go on foot the first, so he has enough money to get from the $i$-th crossroad to the $n$-th, using only tram and bus tickets. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case consists of three integers $a, b, p$ ($1 \le a, b, p \le 10^5$)Β β€” the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $s$, where $s_i = \texttt{A}$, if there is a bus station at $i$-th crossroad, and $s_i = \texttt{B}$, if there is a tram station at $i$-th crossroad ($2 \le |s| \le 10^5$). It is guaranteed, that the sum of the length of strings $s$ by all test cases in one test doesn't exceed $10^5$. -----Output----- For each test case print one numberΒ β€” the minimal index $i$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $i$ to $n$ he should use public transport). -----Example----- Input 5 2 2 1 BB 1 1 1 AB 3 2 8 AABBBBAABB 5 3 4 BBBBB 2 1 1 ABABAB Output 2 1 3 1 6
t = int(input()) for _ in range(t): a,b,p = map(int,input().split()) sl = list(input()) before = "" for idx, i in enumerate(sl[::-1][1:]): cost = a if i == "A" else b if before == i: continue if cost > p: break else: before = i p -= cost else: print(1) continue print(len(sl) - idx)
You are given a sequence $a_1, a_2, \dots, a_n$, consisting of integers. You can apply the following operation to this sequence: choose some integer $x$ and move all elements equal to $x$ either to the beginning, or to the end of $a$. Note that you have to move all these elements in one direction in one operation. For example, if $a = [2, 1, 3, 1, 1, 3, 2]$, you can get the following sequences in one operation (for convenience, denote elements equal to $x$ as $x$-elements): $[1, 1, 1, 2, 3, 3, 2]$ if you move all $1$-elements to the beginning; $[2, 3, 3, 2, 1, 1, 1]$ if you move all $1$-elements to the end; $[2, 2, 1, 3, 1, 1, 3]$ if you move all $2$-elements to the beginning; $[1, 3, 1, 1, 3, 2, 2]$ if you move all $2$-elements to the end; $[3, 3, 2, 1, 1, 1, 2]$ if you move all $3$-elements to the beginning; $[2, 1, 1, 1, 2, 3, 3]$ if you move all $3$-elements to the end; You have to determine the minimum number of such operations so that the sequence $a$ becomes sorted in non-descending order. Non-descending order means that for all $i$ from $2$ to $n$, the condition $a_{i-1} \le a_i$ is satisfied. Note that you have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$)Β β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$)Β β€” the number of elements. The second line of each query contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le n$)Β β€” the elements. It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each query print one integerΒ β€” the minimum number of operation for sorting sequence $a$ in non-descending order. -----Example----- Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 -----Note----- In the first query, you can move all $1$-elements to the beginning (after that sequence turn into $[1, 1, 1, 3, 6, 6, 3]$) and then move all $6$-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all $2$-elements to the beginning.
def main(): from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) inp1 = [-1] * (n + 1) inp2 = [-1] * (n + 1) for i, ai in enumerate(map(int, stdin.readline().split())): if inp1[ai] < 0: inp1[ai] = i inp2[ai] = i inp1 = tuple((inp1i for inp1i in inp1 if inp1i >= 0)) inp2 = tuple((inp2i for inp2i in inp2 if inp2i >= 0)) n = len(inp1) ans = 0 cur = 0 for i in range(n): if i and inp1[i] < inp2[i - 1]: cur = 1 else: cur += 1 ans = max(ans, cur) stdout.write(f'{n - ans}\n') main()
You are given a sequence $a_1, a_2, \dots, a_n$, consisting of integers. You can apply the following operation to this sequence: choose some integer $x$ and move all elements equal to $x$ either to the beginning, or to the end of $a$. Note that you have to move all these elements in one direction in one operation. For example, if $a = [2, 1, 3, 1, 1, 3, 2]$, you can get the following sequences in one operation (for convenience, denote elements equal to $x$ as $x$-elements): $[1, 1, 1, 2, 3, 3, 2]$ if you move all $1$-elements to the beginning; $[2, 3, 3, 2, 1, 1, 1]$ if you move all $1$-elements to the end; $[2, 2, 1, 3, 1, 1, 3]$ if you move all $2$-elements to the beginning; $[1, 3, 1, 1, 3, 2, 2]$ if you move all $2$-elements to the end; $[3, 3, 2, 1, 1, 1, 2]$ if you move all $3$-elements to the beginning; $[2, 1, 1, 1, 2, 3, 3]$ if you move all $3$-elements to the end; You have to determine the minimum number of such operations so that the sequence $a$ becomes sorted in non-descending order. Non-descending order means that for all $i$ from $2$ to $n$, the condition $a_{i-1} \le a_i$ is satisfied. Note that you have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$)Β β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$)Β β€” the number of elements. The second line of each query contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le n$)Β β€” the elements. It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each query print one integerΒ β€” the minimum number of operation for sorting sequence $a$ in non-descending order. -----Example----- Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 -----Note----- In the first query, you can move all $1$-elements to the beginning (after that sequence turn into $[1, 1, 1, 3, 6, 6, 3]$) and then move all $6$-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all $2$-elements to the beginning.
from sys import stdin input = stdin.readline def main(): anses = [] for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) f = [0]*(n+1) d = sorted(list(set(a))) for q in range(1, len(d)+1): f[d[q-1]] = q for q in range(len(a)): a[q] = f[a[q]] n = len(d) starts, ends = [-1]*(n+1), [n+1]*(n+1) for q in range(len(a)): if starts[a[q]] == -1: starts[a[q]] = q ends[a[q]] = q s = [0]*(n+1) max1 = -float('inf') for q in range(1, n+1): s[q] = s[q-1]*(ends[q-1] < starts[q])+1 max1 = max(max1, s[q]) anses.append(str(len(d)-max1)) print('\n'.join(anses)) main()
You are given a sequence $a_1, a_2, \dots, a_n$, consisting of integers. You can apply the following operation to this sequence: choose some integer $x$ and move all elements equal to $x$ either to the beginning, or to the end of $a$. Note that you have to move all these elements in one direction in one operation. For example, if $a = [2, 1, 3, 1, 1, 3, 2]$, you can get the following sequences in one operation (for convenience, denote elements equal to $x$ as $x$-elements): $[1, 1, 1, 2, 3, 3, 2]$ if you move all $1$-elements to the beginning; $[2, 3, 3, 2, 1, 1, 1]$ if you move all $1$-elements to the end; $[2, 2, 1, 3, 1, 1, 3]$ if you move all $2$-elements to the beginning; $[1, 3, 1, 1, 3, 2, 2]$ if you move all $2$-elements to the end; $[3, 3, 2, 1, 1, 1, 2]$ if you move all $3$-elements to the beginning; $[2, 1, 1, 1, 2, 3, 3]$ if you move all $3$-elements to the end; You have to determine the minimum number of such operations so that the sequence $a$ becomes sorted in non-descending order. Non-descending order means that for all $i$ from $2$ to $n$, the condition $a_{i-1} \le a_i$ is satisfied. Note that you have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$)Β β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$)Β β€” the number of elements. The second line of each query contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le n$)Β β€” the elements. It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each query print one integerΒ β€” the minimum number of operation for sorting sequence $a$ in non-descending order. -----Example----- Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 -----Note----- In the first query, you can move all $1$-elements to the beginning (after that sequence turn into $[1, 1, 1, 3, 6, 6, 3]$) and then move all $6$-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all $2$-elements to the beginning.
# | # _` | __ \ _` | __| _ \ __ \ _` | _` | # ( | | | ( | ( ( | | | ( | ( | # \__,_| _| _| \__,_| \___| \___/ _| _| \__,_| \__,_| import sys import math def read_line(): return sys.stdin.readline()[:-1] def read_int(): return int(sys.stdin.readline()) def read_int_line(): return [int(v) for v in sys.stdin.readline().split()] def read_float_line(): return [float(v) for v in sys.stdin.readline().split()] t = read_int() for i in range(t): n = read_int() a = read_int_line() d = {} for i in range(n): if a[i] in d: d[a[i]].append(i) else: d[a[i]] = [i] dp = [1]*len(list(d.keys())) s = list(d.keys()) s.sort() for i in range(len(s)-2,-1,-1): if d[s[i]][-1] < d[s[i+1]][0]: dp[i] = dp[i+1]+1 else: dp[i] = 1 ans = len(s)-max(dp) print(ans)
You are given a sequence $a_1, a_2, \dots, a_n$, consisting of integers. You can apply the following operation to this sequence: choose some integer $x$ and move all elements equal to $x$ either to the beginning, or to the end of $a$. Note that you have to move all these elements in one direction in one operation. For example, if $a = [2, 1, 3, 1, 1, 3, 2]$, you can get the following sequences in one operation (for convenience, denote elements equal to $x$ as $x$-elements): $[1, 1, 1, 2, 3, 3, 2]$ if you move all $1$-elements to the beginning; $[2, 3, 3, 2, 1, 1, 1]$ if you move all $1$-elements to the end; $[2, 2, 1, 3, 1, 1, 3]$ if you move all $2$-elements to the beginning; $[1, 3, 1, 1, 3, 2, 2]$ if you move all $2$-elements to the end; $[3, 3, 2, 1, 1, 1, 2]$ if you move all $3$-elements to the beginning; $[2, 1, 1, 1, 2, 3, 3]$ if you move all $3$-elements to the end; You have to determine the minimum number of such operations so that the sequence $a$ becomes sorted in non-descending order. Non-descending order means that for all $i$ from $2$ to $n$, the condition $a_{i-1} \le a_i$ is satisfied. Note that you have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$)Β β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$)Β β€” the number of elements. The second line of each query contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le n$)Β β€” the elements. It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each query print one integerΒ β€” the minimum number of operation for sorting sequence $a$ in non-descending order. -----Example----- Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 -----Note----- In the first query, you can move all $1$-elements to the beginning (after that sequence turn into $[1, 1, 1, 3, 6, 6, 3]$) and then move all $6$-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all $2$-elements to the beginning.
import os import sys def solve(arr): items = sorted(set(arr)) min_max = [(float("inf"), float("-inf"))] * len(items) item_to_idx = {k: idx for idx, k in enumerate(items)} for idx, a in enumerate(arr): m, M = min_max[item_to_idx[a]] min_max[item_to_idx[a]] = (min(idx, m), max(idx, M)) best = 1 current = 1 for i in range(1, len(items)): _, prev_M = min_max[i - 1] m, _ = min_max[i] if prev_M <= m: current += 1 else: current = 1 best = max(best, current) return len(items) - best def pp(input): T = int(input()) for t in range(T): input() arr = list(map(int, input().strip().split())) print(solve(arr)) if "paalto" in os.getcwd(): from string_source import string_source, codeforces_parse pp( string_source( """3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7""" ) ) else: pp(sys.stdin.readline)
You are given a sequence $a_1, a_2, \dots, a_n$, consisting of integers. You can apply the following operation to this sequence: choose some integer $x$ and move all elements equal to $x$ either to the beginning, or to the end of $a$. Note that you have to move all these elements in one direction in one operation. For example, if $a = [2, 1, 3, 1, 1, 3, 2]$, you can get the following sequences in one operation (for convenience, denote elements equal to $x$ as $x$-elements): $[1, 1, 1, 2, 3, 3, 2]$ if you move all $1$-elements to the beginning; $[2, 3, 3, 2, 1, 1, 1]$ if you move all $1$-elements to the end; $[2, 2, 1, 3, 1, 1, 3]$ if you move all $2$-elements to the beginning; $[1, 3, 1, 1, 3, 2, 2]$ if you move all $2$-elements to the end; $[3, 3, 2, 1, 1, 1, 2]$ if you move all $3$-elements to the beginning; $[2, 1, 1, 1, 2, 3, 3]$ if you move all $3$-elements to the end; You have to determine the minimum number of such operations so that the sequence $a$ becomes sorted in non-descending order. Non-descending order means that for all $i$ from $2$ to $n$, the condition $a_{i-1} \le a_i$ is satisfied. Note that you have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$)Β β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$)Β β€” the number of elements. The second line of each query contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le n$)Β β€” the elements. It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each query print one integerΒ β€” the minimum number of operation for sorting sequence $a$ in non-descending order. -----Example----- Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 -----Note----- In the first query, you can move all $1$-elements to the beginning (after that sequence turn into $[1, 1, 1, 3, 6, 6, 3]$) and then move all $6$-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all $2$-elements to the beginning.
import sys as _sys def main(): q = int(input()) for i_q in range(q): n, = _read_ints() a = tuple(_read_ints()) result = find_min_sorting_cost(sequence=a) print(result) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return list(map(int, _read_line().split(" "))) def find_min_sorting_cost(sequence): sequence = tuple(sequence) if not sequence: return 0 indices_by_values = {x: [] for x in sequence} for i, x in enumerate(sequence): indices_by_values[x].append(i) borders_by_values = { x: (indices[0], indices[-1]) for x, indices in list(indices_by_values.items()) } borders_sorted_by_values = [borders for x, borders in sorted(borders_by_values.items())] max_cost_can_keep_n = curr_can_keep_n = 1 for prev_border, curr_border in zip(borders_sorted_by_values, borders_sorted_by_values[1:]): if curr_border[0] > prev_border[1]: curr_can_keep_n += 1 else: if curr_can_keep_n > max_cost_can_keep_n: max_cost_can_keep_n = curr_can_keep_n curr_can_keep_n = 1 if curr_can_keep_n > max_cost_can_keep_n: max_cost_can_keep_n = curr_can_keep_n return len(set(sequence)) - max_cost_can_keep_n def __starting_point(): main() __starting_point()
You are given a sequence $a_1, a_2, \dots, a_n$, consisting of integers. You can apply the following operation to this sequence: choose some integer $x$ and move all elements equal to $x$ either to the beginning, or to the end of $a$. Note that you have to move all these elements in one direction in one operation. For example, if $a = [2, 1, 3, 1, 1, 3, 2]$, you can get the following sequences in one operation (for convenience, denote elements equal to $x$ as $x$-elements): $[1, 1, 1, 2, 3, 3, 2]$ if you move all $1$-elements to the beginning; $[2, 3, 3, 2, 1, 1, 1]$ if you move all $1$-elements to the end; $[2, 2, 1, 3, 1, 1, 3]$ if you move all $2$-elements to the beginning; $[1, 3, 1, 1, 3, 2, 2]$ if you move all $2$-elements to the end; $[3, 3, 2, 1, 1, 1, 2]$ if you move all $3$-elements to the beginning; $[2, 1, 1, 1, 2, 3, 3]$ if you move all $3$-elements to the end; You have to determine the minimum number of such operations so that the sequence $a$ becomes sorted in non-descending order. Non-descending order means that for all $i$ from $2$ to $n$, the condition $a_{i-1} \le a_i$ is satisfied. Note that you have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$)Β β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$)Β β€” the number of elements. The second line of each query contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le n$)Β β€” the elements. It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$. -----Output----- For each query print one integerΒ β€” the minimum number of operation for sorting sequence $a$ in non-descending order. -----Example----- Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 -----Note----- In the first query, you can move all $1$-elements to the beginning (after that sequence turn into $[1, 1, 1, 3, 6, 6, 3]$) and then move all $6$-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all $2$-elements to the beginning.
import copy def DeleteRepetitionsIn(Array): AlreadyRead = {} index = 0 ConstantArray = copy.deepcopy(Array) for a in range(len(ConstantArray)): if Array[index] not in AlreadyRead: AlreadyRead[Array[index]] = "" index += 1 continue Array = Array[0:index] + Array[index + 1:len(Array)] return Array def DeleteRepetitionsIn2(Array): AlreadyRead = {} for elem in Array: if elem in AlreadyRead: continue AlreadyRead[elem] = "" return list(AlreadyRead) Results = [] ArraysNumber = int(input()) for e in range(ArraysNumber): AbsolutelyUselessNumber = int(input()) Array = list(map(int, input().split())) if len(Array) == 1: Results.append(0) continue #print(Array) TheRightOrder = DeleteRepetitionsIn2(Array) TheRightOrder.sort() TheCurrentOrder = {} for i in range(len(Array)): if Array[i] not in TheCurrentOrder: TheCurrentOrder[Array[i]] = [i, i] continue TheCurrentOrder[Array[i]][1] = i #print(TheRightOrder) #print(TheCurrentOrder) #print(Array) TheCurrentResult = 1 TheMaxResult = 1 for i in range(len(TheRightOrder)): #print("a =", TheCurrentResult) #print("b =", TheMaxResult) if i == len(TheRightOrder) - 1: if TheCurrentResult >= TheMaxResult: TheMaxResult = TheCurrentResult continue if TheCurrentOrder[TheRightOrder[i]][1] > TheCurrentOrder[TheRightOrder[i + 1]][0]: if TheCurrentResult >= TheMaxResult: TheMaxResult = TheCurrentResult TheCurrentResult = 1 continue TheCurrentResult += 1 Results.append(len(TheRightOrder) - TheMaxResult) for i in Results: print(i)
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t = int(input()) for tt in range(t): n,k=list(map(int,input().split())) s = input() ans = [] if s[0] == ')': for i in range(n): if s[i] == '(': ans.append([1,i+1]) s = s[i::-1] + s[i+1:] break for i in range(1,(k-1)*2): if i%2==0: if s[i]!='(': for j in range(i+1,n): if s[j] == '(': ans.append([i+1,j+1]) s = s[:i] + s[j:i-1:-1] + s[j+1:] break else: if s[i]!=')': for j in range(i+1,n): if s[j] == ')': ans.append([i+1,j+1]) s = s[:i] + s[j:i-1:-1] + s[j+1:] break for i in range((k-1)*2,(n+(2*(k-1)))//2+1): if s[i]!='(': for j in range(i+1,n): if s[j] == '(': ans.append([i+1,j+1]) s = s[:i] + s[j:i-1:-1] + s[j+1:] break print(len(ans)) for i in ans: print(*i)
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t = int(input()) for test_i in range(t): n, k = map(int, input().split()) s = list(input()) ans = [] for i in range(k - 1): if s[2 * i] != '(': i0 = s.index('(', 2 * i) ans.append((2 * i + 1, i0 + 1)) s[2 * i], s[i0] = '(', ')' if s[2 * i + 1] != ')': i0 = s.index(')', 2 * i + 1) ans.append((2 * i + 2, i0 + 1)) s[2 * i + 1], s[i0] = ')', '(' for i in range(n // 2 - k + 1): if s[2 * (k - 1) + i] != '(': i0 = s.index('(', 2 * (k - 1) + i) ans.append((2 * (k - 1) + i + 1, i0 + 1)) s[2 * (k - 1) + i], s[i0] = '(', ')' print(len(ans)) for pair in ans: print(*pair)
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
for _ in range(int(input())): n, k = map(int, input().split()) st = input() s = [st[i] for i in range(n)] res = '()' * (k - 1) + '(' * ((n - 2 * (k - 1)) // 2) + ')' * ((n - 2 * (k - 1)) // 2) ans = [] for i in range(n): if s[i] != res[i]: for j in range(i + 1, n): if res[i] == s[j]: ans.append([i + 1, j + 1]) sub = s[i:j + 1] sub = sub[::-1] #print(sub) for k in range(len(sub)): s[i + k] = sub[k] break #print(res, s) print(len(ans)) for i in range(len(ans)): print(ans[i][0], ans[i][1])
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
m = int(input()) for h in range(m): n, b = list(map(int, input().split())) a = input() t = 0 ans = [] while b != 1: if a[t] == ')': for i in range(t, n): if a[i] == '(': k = i break c = a[t:k + 1] a = a[:t] + c[::-1] + a[k + 1:] #print(t, k, a) ans.append([t, k]) if a[t + 1] == '(': for i in range(t + 1, n): if a[i] == ')': k = i break c = a[t + 1:k + 1] a = a[:t + 1] + c[::-1] + a[k + 1:] #print(t, k, a) ans.append([t + 1, k]) t += 2 b -= 1 for i in range(t, t + (n - t) // 2): if a[i] == ')': for j in range(i, n): if a[j] == '(': k = j break #print(i, k) c = a[i:k + 1] a = a[:i] + c[::-1] + a[k + 1:] ans.append([i, k]) #print(a) for i in range(t + (n - t) // 2, n): if a[i] == '(': for j in range(i, n): if a[j] == ')': k = j break c = a[i:k + 1] a = a[:i] + c[::-1] + a[k + 1:] ans.append([i, k]) print(len(ans)) for i in ans: print(i[0] + 1, i[1] + 1)
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t = int(input()) for i in range(t): n, k = map(int, input().split()) a = [] s = input() for j in range(len(s)): a.append(s[j:j + 1]) answer = (k - 1) * "()" + (n // 2 - k + 1) * "(" + (n // 2 - k + 1) * ")" b = [] for j in range(len(answer)): b.append(answer[j:j + 1]) ans = [] j = 0 while j < len(answer): if b[j] == a[j]: j += 1 else: x = j + 1 while a[x] == a[j]: x += 1 ans.append([j + 1, x + 1]) for f in range(j, j + (x - j + 1) // 2): a[f], a[x - f + j] = a[x - f + j], a[f] j += 1 print(len(ans)) for j in range(len(ans)): print(" ".join(map(str, ans[j])))
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t = int(input()) for z in range(t): n, k = map(int, input().split()) arr = list(input()) need = '()' * (k - 1) + '(' * ((n - (k - 1) * 2) // 2) + ')' * ((n - (k - 1) * 2) // 2) #print(need) li = 0 ri = n - 1 ln = 0 rn = n - 1 ret = [] rev = 0 while li < n: if arr[li] != need[li]: ri = li + 1 while arr[ri] != need[li]: ri += 1 ret.append([li, ri]) arr = arr[:li] + list(reversed(arr[li:ri+1])) + arr[ri+1:] li += 1 #print(*arr, sep='') print(len(ret)) for x in ret: print(x[0] + 1, x[1] + 1)
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t=int(input()) for r in range(t): n,k=list(map(int,input().split())) k-=1 want='()'*k+'('*(n//2-k)+')'*(n//2-k) have=input() prn=[] for w in range(len(want)): if have[w]!=want[w]: e=w+have[w:].index(want[w]) have=have[:w]+have[w:e+1][::-1]+have[e+1:] prn+=[[w+1,e+1]] print(len(prn)) for w in prn: print(*w)
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
for _ in range(int(input())): n, k = tuple(map(int, input().split())) s = list(input()) ans = list("()" * (k - 1) + "(" * ((n // 2) - k + 1) + ")" * (n // 2 - k + 1)) ops = [] i = 0 while ans != s and i < n: # print("----" , i, "----") if ans[i] != s[i]: j = s[i:].index(ans[i]) + i # print(0,"|",j, s[j], s[i]) ops.append(str(i + 1) + " " + str(j + 1)) for k in range(i, (j + i + 1) // 2): # print(11, "|", j, s[k], s[j + i - k]) (s[k], s[j + i - k]) = (s[j + i - k], s[k]) # print(12, "|", j, s[k], s[j + i - k]) # print(" ".join(s)) # print(" ".join(ans)) # print("|".join(ops)) i += 1 print(len(ops)) if len(ops) != 0: print("\n".join(ops))
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t = int(input()) for request in range(t): n, k = map(int, input().split()) box = list(input()) pattern = '()' * (k - 1) + '(' + ('()' * ((n - (k) * 2) // 2) ) + ')' changes = [] for i in range(n): if box[i] != pattern[i]: for j in range(i + 1, n): if box[j] == pattern[i]: for z in range((j - i + 1) // 2): box[i + z], box[j - z] = box[j - z], box[i + z] changes.append((i + 1, j + 1)) break print(len(changes)) for i in range(len(changes)): print(*changes[i])
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t = int(input()) for i in range(t): n, k = map(int, input().split()) *s, = input() operations = [] best = (['('] + [')']) * (k - 1) + (['('] * (n // 2 - k + 1) + [')'] * (n // 2 - k + 1)) for startx_pos in range((k - 1) * 2): try: if s[startx_pos - 1] == ')' or startx_pos == 0: end_pos = s.index('(', startx_pos) else: end_pos = s.index(')', startx_pos) except ValueError: continue if startx_pos == end_pos: continue if startx_pos == 0: s = s[:startx_pos] + s[end_pos::-1] + s[end_pos + 1:] else: s = s[:startx_pos] + s[end_pos:startx_pos - 1:-1] + s[end_pos + 1:] operations.append(f'{startx_pos + 1} {end_pos + 1}') for startx_pos in range((k - 1) * 2, (k - 1) * 2 + (n // 2 - k + 1)): try: end_pos = s.index('(', startx_pos) except ValueError: continue if startx_pos == end_pos: continue if startx_pos == 0: s = s[:startx_pos] + s[end_pos::-1] + s[end_pos + 1:] else: s = s[:startx_pos] + s[end_pos:startx_pos - 1:-1] + s[end_pos + 1:] operations.append(f'{startx_pos + 1} {end_pos + 1}') print(len(operations)) if len(operations): print(*operations, sep='\n')
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
def openBracket(i): nonlocal firstOpen, ans ind = index[0][firstOpen] a = s[i: ind + 1] a.reverse() #print(i + 1, ind + 1) s[i: ind + 1] = a ans += [[i + 1, ind + 1]] firstOpen += 1 def closeBracket(i): nonlocal firstClose, ans ind = index[1][firstClose] a = s[i: ind + 1] a.reverse() #print(i + 1, ind + 1) ans += [[i + 1, ind + 1]] s[i: ind + 1] = a firstClose += 1 t = int(input()) for h in range(t): n, k = map(int, input().split()) s = list(input()) ans = [] fl = 0 index = [[], []] firstOpen = 0 firstClose = 0 for i in range(n): if s[i] == "(": index[0] += [i]; else: index[1] += [i]; for i in range(2 * k - 2): if fl == 0: if s[i] != "(": openBracket(i) else: firstOpen += 1 elif fl == 1: if s[i] != ")": closeBracket(i) else: firstClose += 1 fl = abs(fl - 1) fl = 0 for i in range(2 * k - 2, n): if fl == 0: if s[i] != "(": openBracket(i) else: firstOpen += 1 elif fl == 1: if s[i] != ")": closeBracket(i) else: firstClose += 1 if i == n // 2 - k + 2 * k - 2: fl = 1 print(len(ans)) [print(*i) for i in ans]
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t=int(input()) while t: n,k=list(map(int,input().split())) s=list(input()) hyp=[] k1=k while (k-1): hyp.append('(') hyp.append(')') k-=1 ll=(n//2)-(k1-1) for i in range(ll): hyp.append('(') for i in range(ll): hyp.append(')') #print(hyp,s) ans=[] for i in range(n): if hyp[i]!=s[i]: l=[] c=0 for j in range(i,n): l.append(s[j]) c+=1 if s[j]==hyp[i]: ans.append(i+1) ans.append(j+1) break k=i for l2 in range(c-1,-1,-1): s[k]=l[l2] k+=1 l3=len(ans)//2 print(l3) j=0 for i in range(l3): print(ans[j],ans[j+1]) j+=2 t-=1
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
def replace(i, right_s): j = i + 1 while j < n and s[j] != right_s: j += 1 else: for k in range((j - i + 1) // 2): s[i + k], s[j - k] = s[j - k], s[i + k] return j t = int(input()) operations = [] for _ in range(t): n, k = input().split() n = int(n) k = int(k) - 1 s = list(input()) operations.append([]) for i in range(n): if i < 2 * k: if i % 2 and s[i] == '(': operations[_].append([i, replace(i, ')')]) elif i % 2 == 0 and s[i] == ')': operations[_].append([i, replace(i, '(')]) elif i < n // 2 + k and s[i] == ')': operations[_].append([i, replace(i, '(')]) elif i >= n // 2 + k and s[i] == '(': operations[_].append([i, replace(i, ')')]) for i in range(t): print(len(operations[i])) for operation in operations[i]: print(operation[0] + 1, operation[1] + 1)
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
def craftIdeal(length, zeroes): asdf = [] x = 0 for i in range(zeroes - 1): asdf.append(True) asdf.append(False) x += 2 for j in range(x, x + (length - x)//2): asdf.append(True) for k in range(x + (length - x)//2, length): asdf.append(False) return asdf def getAns(string, l, m): real = [] for char in string: if char == ")": real.append(False) else: real.append(True) endgoal = craftIdeal(l, m) operations = [] temp = [] for i in range(l): target = endgoal[i] if real[i] != target: nextDiffIndex = i + 1 while real[nextDiffIndex] != target: nextDiffIndex += 1 temp = real[i:nextDiffIndex + 1] for j in range(i, nextDiffIndex + 1): real[j] = temp[nextDiffIndex - j] operations.append(str(i + 1) + " " + str(nextDiffIndex + 1)) print(len(operations)) for e in operations: print(e) return n = int(input()) for i in range(n): k = [int(x) for x in input().split(' ')] getAns(input(), k[0], k[1])
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
for T in range(int(input())): n, k = list(map(int, input().split())) s = input() lp = 0 rp = 0 l = [] for i in range(k * 2 - 2): while lp < n and s[lp] != '(' or lp < i: lp += 1 while rp < n and s[rp] != ')' or rp < i: rp += 1 if i % 2 == 0 and s[i] == '(' or i % 2 == 1 and s[i] == ')': continue elif i % 2 == 0: lp += 1 s = s[: i] + s[i: lp][::-1] + s[lp:] l.append([i + 1, lp]) rp = i else: rp += 1 s = s[: i] + s[i: rp][::-1] + s[rp: ] l.append([i + 1, rp]) lp = i for i in range(k * 2 - 2, (n+k+k-2)//2): while lp < n and s[lp] != '(' or lp < i: lp += 1 while rp < n and s[rp] != ')' or rp < i: rp += 1 if i<(n+k+k-2)//2 and s[i] == '(' or i>=(n+k+k-2)//2 and s[i] == ')': continue elif i<(n+k+k)//2: lp += 1 s = s[: i] + s[i: lp][::-1] + s[lp: ] l.append([i + 1, lp]) rp = i else: rp += 1 s = s[: i] + s[i: rp][::-1] + s[rp: ] l.append([i + 1, rp]) lp = i print(len(l)) for i in l: print(*i)
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t = int(input()) def conv1(v) : nonlocal z index, q = 0, 0 for i in range(len(v)) : if v[i] == '(' : q += 1 else : q -= 1 if q == 0 and v[i] == '(' : if i != len(v) : v = v[:index] + list(reversed(v[index:i+1])) + v[i+1:] else : v = v[:index] + list(reversed(v[index:i+1])) z.append([index+1, i+1]) index = i+1 elif q == 0 : index = i+1 return v def count(v) : q, k = 0, 0 for i in v : if i == '(' : q += 1 else : q -= 1 if q == 0 : k += 1 return k def conv_min(v, k, n) : nonlocal z q = 0 for i in range(0, len(v)) : if k == n : return v if v[i] == '(' : q += 1 else : q -= 1 if q == 0 : z.append([i+1, i+2]) n -= 1 def conv_max(v, k, n) : nonlocal z q = 0 for i in range(0, len(v)) : if k == n : return v if v[i] == '(' : q += 1 else : if q == 2 : v[i-1], v[i] = v[i], v[i-1] q = 1 z.append([i, i+1]) n += 1 elif q > 2 : v[i-q+1], v[i] = v[i], v[i-q+1] z.append([i-q+1, i+1]) z.append([i-q+1, i-q+2]) q -= 1 n += 1 else : q = 0 if 1 == 2 : s = list('()(())') z = [] print(''.join(conv_max(s, 3, 2))) raise SystemExit for _ in range(t) : _, k = [int(x) for x in input().split()] s = list(input()) z = [] s = conv1(s) ct = count(s) if ct >= k : conv_min(s, k, ct) else : conv_max(s, k, ct) print(len(z)) print('\n'.join(list([str(x[0])+' '+str(x[1]) for x in z])))
You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $s=s_{1}s_{2}\dots s_{n}$ of length $n$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $s$ and reverse it. In other words, you can choose any substring $s[l \dots r]=s_l, s_{l+1}, \dots, s_r$ and change the order of elements in it into $s_r, s_{r-1}, \dots, s_{l}$. For example, if you will decide to reverse substring $s[2 \dots 4]$ of string $s=$"((()))" it will be equal to $s=$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $s$ is a substring that starts at position $1$. For example, for $s=$"(())()" there are $6$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $s$ is a bracket sequence that: the whole string $s$ is a regular bracket sequence; and there are exactly $k$ prefixes of this sequence which are regular (including whole $s$ itself). For example, if $k = 2$, then "(())()" is a neat and clean room. You want to use at most $n$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $n$ or less operations. -----Input----- The first line contains integer number $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the input. Then $t$ test cases follow. The first line of a test case contains two integers $n$ and $k$ ($1 \le k \le \frac{n}{2}, 2 \le n \le 2000$, $n$ is even)Β β€” length of $s$ and required number of regular prefixes. The second line of a test case contains $s$ of length $n$Β β€” the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $\frac{n}{2}$ characters '(' and exactly $\frac{n}{2}$ characters ')' in the given string. The sum of all values $n$ over all the test cases in the input doesn't exceed $2000$. -----Output----- For each test case print an answer. In the first line print integer $m$ ($0 \le m \le n$)Β β€” the number of operations. You do not need to minimize $m$, any value is suitable. In the following $m$ lines print description of the operations, each line should contain two integers $l,r$ ($1 \le l \le r \le n$), representing single reverse operation of $s[l \dots r]=s_{l}s_{l+1}\dots s_{r}$. Operations are applied one after another sequentially. The final $s$ after all operations should be a regular, also it should be exactly $k$ prefixes (including $s$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. -----Example----- Input 4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )( Output 4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2 -----Note----- In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $s$).
t=int(input()) for i3 in range(t): n,k=map(int,input().split()) inp=str(input()) s,ans,x=[],[],[] for i in range(n): x.append(inp[i]) for i in range(k-1): s.append("(") s.append(")") for i in range(n//2-k+1): s.append("(") for i in range(n//2-k+1): s.append(")") for i in range(n): if x[i]==s[i]: pass else: temp=[] for i2 in range(i,n): temp.append(x[i]) if x[i2]==s[i]: ans.append([i+1,i2+1]) temp.reverse() for i3 in range(i,i2+1): x[i3]=temp[i3-i] break print(len(ans)) for i in range(len(ans)): print(ans[i][0],ans[i][1])
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
LOG = 20 def solve(s): n = len(s) res = 0 z = 0 for t in range(0, n): if s[t] == '0': z += 1 continue for l in range(1, min(LOG, n - t + 1)): x = int(s[t:t+l], 2) # print(l, t, x, l + z) if l + z >= x: res += 1 # print(t, l, x, res, z) z = 0 return res t = int(input()) while t > 0: t -= 1 s = input() print(solve(s))
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) c = [0] * 200100 for _ in range(t): s = input() for i in range(len(s)): c[i] = s[i] == '0' if i and s[i] == '0': c[i] += c[i-1] sol = 0 for i in range(len(s)): tmp = 0 for k in range(20): if i - k < 0: break tmp += (s[i - k] == '1') << k if tmp == k + 1: sol += 1 if i >= 20: top = 20 + c[i-20] if tmp > 20 and tmp <= top: sol += 1 print(sol)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
T = int(input()) for t in range(T): s = input() n = len(s) res = 0 zeros = 0 for i, c in enumerate(s): if c == '0': zeros += 1 else: tail = 1 j = 1 while tail <= zeros+j: res += 1 j += 1 if i-1+j == n: break tail *= 2 tail += int(s[i-1+j]) zeros = 0 print(res)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def main(): from sys import stdin, stdout input = stdin.readline print = stdout.write for _ in range(int(input())): ans = 0 i = -1 s = input() for j in range(len(s) - 1): if s[j] == '1': c = 0 for k in range(j, len(s)): c = c * 2 + (s[k] == '1') + (s[k] == '\n') * k if k - i < c: ans += k - j break i = j print(f'{ans}\n') main()
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() q1 = ans = 0 for q in range(len(s)): if s[q] == '0': q1 += 1 else: ans += 1+(q != len(s)-1 and s[q+1] == '0') q2 = size = 1 for q3 in range(1, q1+1): size += 1 if q2 == size: ans += 1 while q2 < size and size-q3+q < len(s): q2 *= 2 q2 += ord(s[q+size-q3])-ord('0') size += 1 if q2 == size: ans += 1 q1 = 0 print(ans)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys D = {} m = 18 for i in range(1, 1<<m): D[bin(i)[2:]] = i for _ in range(int(input())): S = sys.stdin.readline().rstrip() s = 0 N = len(S) ans = 0 for i in range(N): if S[i] == "1": for j in range(1, min(m, N - i) + 1): k = D[S[i:i+j]] if s + j >= k: ans += 1 s = 0 else: s += 1 print(ans)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
from bisect import * strings = [] zeronumber = [] for i in range(1, 200001): strings.append(format(i, "b")) zeronumber.append(i-i.bit_length()) t = int(input()) for _ in range(t): s = input() z = 0 ans = 0 for i in range(len(s)): if s[i] == "0": z += 1 continue else: for j in range(bisect_right(zeronumber, z)): #print(j) #print(s[i:i+len(strings[j])], strings[j]) if i+len(strings[j])-1 <= len(s)-1: if s[i:i+len(strings[j])] == strings[j]: ans += 1 z = 0 print(ans)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for _ in [0]*t: s = input() stack = [] zero_count = 0 ans = 0 for c in map(int, s): new_stack = [] append = new_stack.append if c: append((c, zero_count)) ans += 1 zero_count = 0 else: zero_count += 1 for v, zeros in stack: v = (v << 1) + c need_zeros = v - v.bit_length() if need_zeros <= zeros: ans += 1 append((v, zeros)) stack = new_stack print(ans)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
q = int(input()) for _ in range(q): s=input() n=len(s) ans=100000000 r=[0 for i in range(n)] for i in range(n-1,-1,-1): if(s[i]=='1'): ans=i r[i]=ans ansss=0 for i in range(n): ns=0 for j in range(r[i],n): ns=2*ns+(ord(s[j])-ord('0')) if(ns==j-i+1): ansss+=1 if(ns>n): break print(ansss)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys input = sys.stdin.readline T = int(input()) for testcases in range(T): S=input().strip() LEN=len(S) zeros=0 ANS=0 for i in range(LEN): if S[i]=="0": zeros+=1 else: for j in range(1,min(22,LEN-i+1)): k=int((S[i:i+j]),2) if k==0: continue #print(i,j,k) if zeros>=k-j: ANS+=1 zeros=0 print(ANS)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys import math from collections import defaultdict input = lambda : sys.stdin.readline().rstrip() for t in range(int(input())): ans = 0 s = input() i = 0 while i < len(s): if s[i] == "1": ans += 1 j = i - 1 zero_cnt = 0 while j >= 0 and s[j] == "0": zero_cnt += 1 j -= 1 k = i b = "1" while k + 1 < len(s) and (int(b + s[k+1], 2) - len(b) - 1 <= zero_cnt): ans += 1 b += s[k+1] k += 1 i += 1 print(ans)