description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) c = list(map(int, input().split())) o, t, s = [], [], 0 for i in range(n): s += a[i] if c[i] == 1: o.append(a[i]) else: t.append(a[i]) if s < m: print(-1) else: o.sort(reverse=True) t.sort(reverse=True) i1, i2, M, C = 0, 0, 0, 0 l1, l2 = len(o), len(t) while M < m: s1, s2 = 0, 0 if i1 < l1: s1 = o[i1] if i1 < l1 - 1: s1 += o[i1 + 1] if i2 < l2: s2 = t[i2] if m - M <= s1 or s1 >= s2: M += o[i1] i1 += 1 C += 1 else: M += t[i2] C += 2 i2 += 1 print(C)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys input = lambda: sys.stdin.readline().rstrip() T = int(input()) for _ in range(T): N, M = map(int, input().split()) A = [int(a) for a in input().split()] B = [int(a) for a in input().split()] X = [] Y = [] for a, b in zip(A, B): if b == 1: X.append(a) else: Y.append(a) X = [0] + sorted(X, key=lambda x: -x) Y = [0] + sorted(Y, key=lambda x: -x) for i in range(1, len(X)): X[i] += X[i - 1] for i in range(1, len(Y)): Y[i] += Y[i - 1] if X[-1] + Y[-1] < M: print(-1) continue j = len(Y) - 1 ans = 1 << 50 for i in range(len(X)): while j and X[i] + Y[j - 1] >= M: j -= 1 if X[i] + Y[j] >= M: ans = min(ans, i + j * 2) print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) mem = list(map(int, input().split())) imp = list(map(int, input().split())) one = [] two = [] for mi, pi in zip(mem, imp): if pi == 1: one.append(mi) else: two.append(mi) one.sort(reverse=True) two.sort(reverse=True) pone = [0] for elm in one: pone.append(pone[-1] + elm) ptwo = [0] for elm in two: ptwo.append(ptwo[-1] + elm) low = 0 high = len(one) + 2 * len(two) + 1 while low < high: mid = (low + high) // 2 max_mem = 0 max_two = min(mid // 2, len(two)) for x in range(max_two + 1): if mid - 2 * x > len(one): max_mem = max(max_mem, pone[-1] + ptwo[x]) continue max_mem = max(max_mem, pone[mid - 2 * x] + ptwo[x]) if max_mem >= m: high = mid else: low = mid + 1 if low == len(one) + 2 * len(two) + 1: print(-1) else: print(low)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def main(): for _ in range(int(input())): n, m = map(int, input().split()) d = {"1": [], "2": []} for a, b in zip(map(int, input().split()), input().split()): d[b].append(a) if sum(map(sum, d.values())) < m: print(-1) continue i, j, k, best = 0, 0, len(d["1"]), 1000000 ri = [*sorted(d["1"]), *sorted(d["2"], reverse=True)] while i < n: while i < n and m > 0: m -= ri[i] i += 1 if m <= 0: while m + ri[j] <= 0: m += ri[j] j += 1 x = (i if i < k else i * 2 - k) - (j if j < k else j * 2 - k) if best > x: best = x m += ri[j] j += 1 print(best) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING LIST LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING NUMBER WHILE VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for _ in range(int(input())): n, m = map(int, input().split()) memoryCost = list(map(int, input().split())) convPoints = list(map(int, input().split())) cost1 = list() cost2 = list() for mem, conv in zip(memoryCost, convPoints): if conv == 1: cost1.append(mem) else: cost2.append(mem) cost1.sort(reverse=True) cost2.sort(reverse=True) memory = sum(cost2) convP = 2 * len(cost2) twoCostPointer = len(cost2) - 1 ans = float("inf") for x in range(-1, len(cost1)): if x >= 0: convP += 1 memory += cost1[x] while twoCostPointer >= 0 and memory - cost2[twoCostPointer] >= m: memory -= cost2[twoCostPointer] twoCostPointer -= 1 convP -= 2 if memory >= m: ans = min(ans, convP) print(ans if ans != float("inf") else -1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def find(): n, m = map(int, input().split()) mem = tuple(input().split()) val = tuple(input().split()) a = [] b = [] for i in range(n): a.append(int(mem[i])) if int(val[i]) == 1 else b.append(int(mem[i])) a.sort(reverse=True) b.sort(reverse=True) sumA = 0 sumB = sum(b) j = len(b) res = 0 for i in range(len(a) + 1): while j > 0 and sumA + sumB - b[j - 1] >= m: j -= 1 sumB -= b[j] if sumA + sumB >= m: tmp = i + 2 * j res = res or tmp res = min(res, tmp) if i != len(a): sumA += a[i] print(res if res else -1) for i in range(int(input())): find()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) for _ in range(t): n, g = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if g > sum(a): print(-1) continue p1, p2 = [], [] for i in range(n): if b[i] == 1: p1.append(a[i]) else: p2.append(a[i]) p1, p2 = sorted(p1, reverse=True), sorted(p2, reverse=True) i, j, sp1, sp2, o = -1, len(p2) - 1, 0, sum(p2), 2 * n + 1 for i in range(-1, len(p1)): if i >= 0: sp1 += p1[i] while j >= 0 and sp1 + sp2 - p2[j] >= g: sp2 -= p2[j] j -= 1 if sp1 + sp2 >= g: o = min(o, i + 1 + 2 * (j + 1)) print(o)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
cases = int(input()) for i in range(cases): n, m = map(int, input().split()) twos = [] ones = [] mem = list(map(int, input().split())) conv = list(map(int, input().split())) for i in range(n): if conv[i] == 2: twos.append(mem[i]) else: ones.append(mem[i]) twos.sort() ones.sort() convCount = 0 while m > 0 and twos and ones: if ones[-1] >= m: convCount += 1 m -= ones.pop() break elif twos[-1] >= m: convCount += 2 m -= twos.pop() break if len(ones) == 1 or twos[-1] > ones[-1] + ones[-2]: convCount += 2 m -= twos.pop() else: convCount += 1 m -= ones.pop() while m > 0 and ones: convCount += 1 m -= ones.pop() while m > 0 and twos: convCount += 2 m -= twos.pop() print(convCount if m <= 0 else -1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) release = [int(i) for i in input().split()] cost = [int(i) for i in input().split()] one_cost = [release[i] for i in range(n) if cost[i] == 1] two_cost = [release[i] for i in range(n) if cost[i] == 2] one_cost.sort(reverse=True) two_cost.sort(reverse=True) one_index = 0 two_index = 0 cur = 0 ans = 0 while True: if cur >= m: break if two_index >= len(two_cost) and one_index >= len(one_cost): break if two_index >= len(two_cost): cur += one_cost[one_index] ans += 1 one_index += 1 elif one_index >= len(one_cost): cur += two_cost[two_index] ans += 2 two_index += 1 elif ( one_cost[one_index] >= two_cost[two_index] or cur + one_cost[one_index] >= m ): cur += one_cost[one_index] ans += 1 one_index += 1 elif ( one_index + 1 < len(one_cost) and one_cost[one_index] + one_cost[one_index + 1] >= two_cost[two_index] ): cur += one_cost[one_index] ans += 1 one_index += 1 else: cur += two_cost[two_index] ans += 2 two_index += 1 print(ans if cur >= m else -1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for i in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) one = [] two = [] l1 = l2 = 0 for j in range(n): if b[j] == 1: one.append(a[j]) l1 += 1 else: two.append(a[j]) l2 += 1 one.sort(reverse=True) two.sort(reverse=True) for j in range(1, l1): one[j] += one[j - 1] for j in range(1, l2): two[j] += two[j - 1] if sum(a) < m: print(-1) else: points = 100000000000 for j in range(l2): if two[j] >= m: points = min(points, 2 * (j + 1)) break for j in range(l1): if one[j] >= m: points = min(points, j + 1) break j = 0 k = l2 - 1 while j != l1 and k != -1: if one[j] + two[k] < m: j += 1 else: points = min(points, j + 1 + 2 * (k + 1)) k -= 1 print(points)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) arrA = [] arrB = [] for i in range(n): if b[i] == 1: arrA.append(a[i]) else: arrB.append(a[i]) arrA.sort(reverse=True) arrB.sort(reverse=True) if sum(a) < m: print(-1) else: i = 0 j = 0 space = 0 ans = 0 while space < m: if i < len(arrA): space += arrA[i] i += 1 ans += 1 else: space += arrB[j] j += 1 ans += 2 i -= 1 mini = ans flag = True while i > -1: space -= arrA[i] ans -= 1 i -= 1 while space < m and j < len(arrB): space += arrB[j] j += 1 ans += 2 if space < m: flag = False break if ans < mini: mini = ans print(mini)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def solve(a, b, m): s = 0 while m > 0 and a and b: if a[-1] >= m: m -= a.pop() s += 1 break elif len(a) == 1 or b[-1] > a[-1] + a[-2]: m -= b.pop() s += 2 else: s += 1 m -= a.pop() while m > 0 and a: s += 1 m -= a.pop() while m > 0 and b: s += 2 m -= b.pop() if m <= 0: print(s) else: print(-1) for _ in range(int(input())): n, m = map(int, input().split()) u = list(map(int, input().split())) v = list(map(int, input().split())) a, b = [], [] for i in range(n): if v[i] == 1: a.append(u[i]) else: b.append(u[i]) solve(sorted(a), sorted(b), m)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys t = int(input()) for _ in range(0, t, 1): n, m = map(int, input().split()) one, two, x, ok, mini = [], [], 0, False, 2 * n + 10 A = [int(a) for a in input().split()] B = [int(a) for a in input().split()] for i in range(0, n, 1): if B[i] == 1: one.append(A[i]) else: two.append(A[i]) one.sort(reverse=True) two.sort(reverse=True) one.insert(0, 0) two.insert(0, 0) for i in range(1, len(two), 1): two[i] += two[i - 1] for i in range(0, len(one), 1): x += one[i] hi, lo, mid, ans = len(two) - 1, 0, 0, 0 while hi >= lo: mid = int((hi + lo) / 2) if x + two[mid] >= m: hi = mid - 1 ans = mid else: lo = mid + 1 if x + two[ans] >= m: ok = True mini = min(mini, 2 * ans + i) if ok == True: print(mini) else: print(-1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR LIST LIST NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def find_convenience_lost(n): memory = list(map(int, input().split()))[1] memory_per_app, weight = list(map(int, input().split())), list( map(int, input().split()) ) ones = list( reversed(sorted([a for i, a in enumerate(memory_per_app) if weight[i] == 1])) ) twos = list( reversed(sorted([a for i, a in enumerate(memory_per_app) if weight[i] == 2])) ) i, j = 0, 0 memory_removed = 0 convenience_points_lost = 0 while i < len(ones) - 1 and j < len(twos): if memory_removed + twos[j] >= memory or memory_removed + ones[i] >= memory: return ( convenience_points_lost + 1 if memory_removed + ones[i] >= memory else convenience_points_lost + 2 ) if ones[i] + ones[i + 1] <= twos[j]: memory_removed += twos[j] convenience_points_lost += 2 j += 1 else: memory_removed += ones[i] convenience_points_lost += 1 i += 1 while j < len(twos): if memory_removed >= memory: return convenience_points_lost if i == len(ones) - 1: if ones[i] >= twos[j] or ones[i] + memory_removed >= memory: memory_removed += ones[i] convenience_points_lost += 1 i += 1 continue memory_removed += twos[j] convenience_points_lost += 2 j += 1 while i < len(ones): if memory_removed >= memory: return convenience_points_lost memory_removed += ones[i] convenience_points_lost += 1 i += 1 if memory_removed >= memory: return convenience_points_lost return -1 n = int(input()) for i in range(n): print(find_convenience_lost(i))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys def main(): t = int(input()) allAns = [] for _ in range(t): n, m = readIntArr() oneConvenientPt = [] twoConvenientPts = [] a = readIntArr() b = readIntArr() for i in range(n): if b[i] == 1: oneConvenientPt.append(a[i]) else: twoConvenientPts.append(a[i]) oneConvenientPt.sort(reverse=True) twoConvenientPts.sort(reverse=True) twoPrefSums = twoConvenientPts.copy() for i in range(1, len(twoPrefSums)): twoPrefSums[i] += twoPrefSums[i - 1] minConvenientPts = inf oneTotal = 0 oneCnts = 0 oneConvenientPt = [0] + oneConvenientPt for x in oneConvenientPt: oneTotal += x if oneTotal >= m: minConvenientPts = min(minConvenientPts, oneCnts) else: b = n i = -1 while b > 0: while ( i + b < len(twoPrefSums) and twoPrefSums[i + b] + oneTotal < m ): i += b b //= 2 i += 1 if i < len(twoPrefSums): twoCnts = i + 1 convenientPts = oneCnts + 2 * twoCnts minConvenientPts = min(minConvenientPts, convenientPts) oneCnts += 1 if minConvenientPts == inf: allAns.append(-1) else: allAns.append(minConvenientPts) multiLineArrayPrint(allAns) return input = sys.stdin.buffer.readline def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] inf = float("inf") MOD = 10**9 + 7 main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
tests = int(input()) while tests > 0: apps, clean = list(map(int, input().split())) storage = list(map(int, input().split())) conv = list(map(int, input().split())) if sum(storage) < clean: print(-1) tests -= 1 continue arr1 = list() arr2 = list() for i in range(apps): if conv[i] == 1: arr1.append(storage[i]) else: arr2.append(storage[i]) arr1.sort(reverse=True) arr2.sort(reverse=True) if clean in arr1: print(1) tests -= 1 continue else: index1 = 0 index2 = 0 convenience = 0 while clean > 0: if index1 < len(arr1) and arr1[index1] >= clean: convenience += 1 break state1 = 0 state2 = 0 if index1 < len(arr1): state1 = arr1[index1] if index2 < len(arr2): state2 = arr2[index2] if index1 < len(arr1) - 1: state1 += arr1[index1 + 1] if state2 > state1: clean -= state2 index2 += 1 convenience += 2 else: clean -= arr1[index1] index1 += 1 convenience += 1 print(convenience) tests -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def solve(): n, m = map(int, input().split()) A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] one = [] two = [] for i in range(n): a, b = A[i], B[i] if b == 1: one.append(a) else: two.append(a) one = [0] + sorted(one, reverse=True) two = [0] + sorted(two, reverse=True) ln1 = len(one) ln2 = len(two) for i in range(1, ln1): one[i] += one[i - 1] for i in range(1, ln2): two[i] += two[i - 1] j = 0 INF = float("INF") ans = INF for i in range(ln1)[::-1]: while j < ln2 and m > one[i] + two[j]: j += 1 if j == ln2: break ans = min(ans, i + j * 2) print(ans if ans < INF else -1) t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys input = sys.stdin.readline for _ in range(int(input())): n, m = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) E = sorted(filter(lambda i: B[i] == 1, range(n)), key=A.__getitem__, reverse=True) F = sorted(filter(lambda i: B[i] == 2, range(n)), key=A.__getitem__, reverse=True) G = [0] for e in E: G.append(G[-1] + A[e]) H = [0] for f in F: H.append(H[-1] + A[f]) e = 0 f = len(H) - 1 r = 10**10 while e < len(G) and f >= 0: if G[e] + H[f] >= m: r = min(r, e + 2 * f) f -= 1 else: e += 1 if r == 10**10: print(-1) else: print(r)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
n = 10**6 prime = [(True) for i in range(n + 1)] prime[1] = False p = 2 while p <= 1000: if prime[p]: for i in range(p * p, n + 1, p): prime[i] = False p += 1 t = int(input()) for _ in range(t): n, e = map(int, input().split()) arr = list(map(int, input().split())) ans = 0 for i in range(e): j = i left = 0 right = 0 while j < n: if arr[j] == 1: left += 1 j += e continue if prime[arr[j]]: j += e while j < n and arr[j] == 1: right += 1 j += e ans += left + right + left * right left = right right = 0 else: left = 0 j += e print(ans)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
p = [(1) for i in range(10**6 + 1)] p[0] = 0 p[1] = 0 for i in range(2, len(p)): if p[i] == 1: for j in range(i * 2, len(p), i): if j % i == 0: p[j] = 0 t = int(input()) for test in range(t): n, e = map(int, input().split(" ")) a = list(map(int, input().split(" "))) b = [] for j in range(e): b.append([a[i] for i in range(j, len(a), e)]) ans = 0 for j in range(len(b)): for i in range(len(b[j])): if p[b[j][i]] == 1: l = i r = i Left = True Right = True while Left == True or Right == True: if r + 1 < len(b[j]) and b[j][r + 1] == 1: r += 1 else: Right = False if l - 1 >= 0 and b[j][l - 1] == 1: l -= 1 else: Left = False ans += r - i + (i - l) ans += (r - i) * (i - l) print(ans)
ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
from sys import stdin p = 2 isprime = [True] * (10**6 + 1) isprime[0] = isprime[1] = False while p * p <= 10**6: if isprime[p]: for i in range(p * p, 10**6 + 1, p): isprime[i] = False p += 1 for _ in range(int(stdin.readline())): n, e = map(int, stdin.readline().split()) arr = list(map(int, stdin.readline().split())) onesahead = [(0) for i in range(n)] onesbefore = [(0) for i in range(n)] for i in range(n - 1, -1, -1): if arr[i] == 1: onesahead[i] = 1 if i + e < n: onesahead[i] += onesahead[i + e] for i in range(0, n): if arr[i] == 1: onesbefore[i] = 1 if i - e >= 0: onesbefore[i] += onesbefore[i - e] ans = 0 for i in range(n): if isprime[arr[i]]: a = 0 b = 0 if i + e < n: a = onesahead[i + e] if i - e >= 0: b = onesbefore[i - e] b = b * (a + 1) nn = a + b 3 ans += nn print(ans)
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER WHILE BIN_OP VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
def soe(n): s = set() prime = [(True) for i in range(n + 1)] p = 2 while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 for p in range(2, n + 1): if prime[p]: s.add(p) return s s = soe(10**6) def count(cnt1, cnt2): return (cnt1 - 1) * (cnt2 - 1) for _ in range(int(input())): n, e = map(int, input().split()) a = list(map(int, input().split())) pa = [0] * n ons = [0] * n vis = [0] * n for i in range(n): if a[i] == 1: ons[i] = 1 elif a[i] in s: pa[i] = 1 ans = 0 i = 0 while i < n: if pa[i] == 1: j = i + e cnt1 = 1 while j < n: if ons[j] == 1: cnt1 += 1 vis[j] = 1 else: break j += e j = i - e cnt2 = 1 while j >= 0: if ons[j] == 1: cnt2 += 1 vis[j] = 1 else: break j -= e if cnt1 + cnt2 > 2: ans += count(cnt1, cnt2) + cnt1 + cnt2 - 2 i += 1 else: i += 1 print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
primes = [] d = list(range(10**6 + 1)) for i in range(2, 10**6 + 1): if d[i] == i: primes.append(i) for j in primes: if i * j > 10**6: break if j > d[i]: break d[i * j] = j for _ in " " * int(input()): n, e = map(int, input().split()) a = [int(x) for x in input().split()] ans = 0 for i in range(e): la = a[i::e] delims = [i for i, v in enumerate(la) if v != 1] for i, v in enumerate(delims): u = delims[i - 1] + 1 if i else 0 w = delims[i + 1] - 1 if i < len(delims) - 1 else len(la) - 1 if d[la[v]] == la[v]: ans += (v - u + 1) * (w - v + 1) - 1 print(ans)
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
from sys import stdin input = stdin.readline n = 10**6 prime = [(True) for i in range(n + 1)] prime[1] = False p = 2 while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 for _ in range(int(input())): n, b = map(int, input().split()) li = list(map(int, input().split())) ans = 0 for i in range(n): if prime[li[i]] == True: j, c = i + b, 0 while j < n and li[j] == 1: j, c = j + b, c + 1 d, j = 0, i - b while j >= 0 and li[j] == 1: j, d = j - b, d + 1 ans = ans + c + d * (c + 1) print(ans)
ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
import sys def fprime(): taille = 10**6 + 1 primes = [(True) for x in range(taille)] primes[0] = False primes[1] = False for i in range(2, taille): if primes[i]: j = 2 * i while j < taille: primes[j] = False j += i return primes def main(): input = sys.stdin.readline t = int(input()) primes = fprime() for _ in range(t): n, e = map(int, input().split()) a = list(map(int, input().split())) nbUn = [(0) for i in range(n)] for i in range(n - 1, -1, -1): if a[i] == 1: if i < n - e: nbUn[i] = nbUn[i + e] + 1 else: nbUn[i] = 1 else: nbUn[i] = 0 nbSol = [(0) for i in range(n)] for i in range(n - 1, -1, -1): if a[i] != 1 and i < n - e and nbUn[i + e] > 0: if primes[a[i]]: nbSol[i] = nbUn[i + e] if a[i] == 1 and i < n - e: if a[i + e] == 1: nbSol[i] = nbSol[i + e] elif primes[a[i + e]]: nbSol[i] = nbSol[i + e] + 1 print(sum(nbSol)) main()
IMPORT FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
import sys isPrime = [True] * int(1000000.0 + 2) def solve(): inp = sys.stdin.readline n, e = map(int, inp().split()) a = list(map(int, inp().split())) w = [True] * n r = 0 for i in range(n): if w[i]: j = 0 z = i last = -1 last1 = -1 p = False while z < n: w[z] = False v = a[z] if v > 1: last, last1 = j, last p = isPrime[v] if p: r -= 1 if p: r += last - last1 z += e j += 1 print(r) def main(): M = len(isPrime) isPrime[1] = False isPrime[0] = False i = 2 while i * i < M: if isPrime[i]: j = i * i while j < M: isPrime[j] = False j += i i += 1 for i in range(int(sys.stdin.readline())): solve() main()
IMPORT ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
isPrime = [(1) for j in range(1000003)] def sieve(isPrime): maxN = 1000002 isPrime[0], isPrime[1] = 0, 0 i = 2 while i * i <= maxN: if isPrime[i]: for j in range(i * i, maxN + 1, i): isPrime[j] = 0 i += 1 sieve(isPrime) for i in range(int(input())): n, e = map(int, input().split()) a = [int(j) for j in input().split()] ans = 0 for j in range(len(a)): if a[j] == 1 or isPrime[a[j]]: x, y = 0, 0 k, cnt = 0, 0 while j + k * e < n: if a[j + k * e] == 1: if cnt == 0: a[j + k * e] = -1 x += 1 else: y += 1 k += 1 elif cnt == 0 and isPrime[a[j + k * e]]: a[j + k * e] = -1 cnt += 1 x += 1 k += 1 else: break if k == 1: ans += 0 elif y != 0: ans += (x - 1) * (y + 1) + y elif x > 1 and cnt != 0: ans += x - 1 print(ans)
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
import sys input = sys.stdin.readline n = 10**6 is_prime = [(True) for i in range(n + 1)] is_prime[1] = False p = 2 while p * p <= n: if is_prime[p]: for i in range(p * p, n + 1, p): is_prime[i] = False p += 1 t = int(input()) for _ in range(t): n, e = map(int, input().split()) a = list(map(int, input().split())) ans = 0 for i in range(n): l, r = 0, 0 if is_prime[a[i]] and a[i] != 1: for j in range(i + e, n, e): if a[j] == 1: r += 1 else: break for j in range(i - e, -1, -e): if a[j] == 1: l += 1 else: break ans += l + r + l * r print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
def SieveOfEratosthenes(n): li = [1] * (n + 1) p = 2 while p * p <= n: if li[p] == 1: for i in range(p**2, n + 1, p): li[i] = 0 p += 1 li[0] = 0 li[1] = 0 return li li = SieveOfEratosthenes(10**6 + 1) t = int(input()) for i in range(t): n, e = map(int, input().split()) a = list(map(int, input().split())) lipr = [] c = 0 for i in range(n): if li[a[i]] == 1: lipr.append(i) c = 0 le = len(lipr) for i in range(le): lc = 0 rc = 0 for j in range(lipr[i] + e, n, e): if a[j] == 1: lc += 1 else: break for j in range(lipr[i] - e, -1, -e): if a[j] == 1: rc += 1 else: break c += rc c += lc c += rc * lc print(c)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
max = 10**6 + 1 allPrime = [True] * max allPrime[0] = allPrime[1] = False for i in range(2, max): if allPrime[i]: for j in range(i, max, i): if j != i: allPrime[j] = False test = int(input()) for t in range(test): n, e = map(int, input().split()) array = list(map(int, input().split()))[:n] noOfOnesLeft = [0] * n noOfOnesRight = [0] * n ans = 0 for i in range(n - 1, e - 1, -1): if array[i] == 1: noOfOnesLeft[i - e] = noOfOnesLeft[i] + 1 for i in range(n - e): if array[i] == 1: noOfOnesRight[i + e] = noOfOnesRight[i] + 1 for i in range(n): if allPrime[array[i]]: ans += noOfOnesLeft[i] + noOfOnesRight[i] * (noOfOnesLeft[i] + 1) print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
prime = [(1) for i in range(1000002)] p = 2 prime[0], prime[1] = 0, 0 while p * p <= 1000001: if prime[p]: for i in range(p * p, 1000002, p): prime[i] = 0 p += 1 for _ in range(int(input())): n, e = map(int, input().split()) arr = list(map(int, input().split())) ans = 0 for i in range(n): if prime[arr[i]]: j = i + e c = 0 while j < n and arr[j] == 1: c += 1 j += e j = i - e d = 0 while j >= 0 and arr[j] == 1: d += 1 j -= e ans = ans + c + (c + 1) * d print(ans)
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
While performing complex market analysis William encountered the following problem: For a given array $a$ of size $n$ and a natural number $e$, calculate the number of pairs of natural numbers $(i, k)$ which satisfy the following conditions: $1 \le i, k$ $i + e \cdot k \le n$. Product $a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $e$ $(1 \le e \le n \le 2 \cdot 10^5)$, the number of items in the array and number $e$, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^6)$, the contents of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $(i, k)$ which satisfy the conditions. -----Examples----- Input 6 7 3 10 2 1 3 1 19 3 3 2 1 13 1 9 3 2 4 2 1 1 1 1 4 2 3 1 1 1 1 4 1 1 2 1 1 2 2 1 2 Output 2 0 4 0 5 0 -----Note----- In the first example test case two pairs satisfy the conditions: $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{5} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 19$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions. In the third example test case four pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{4} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{4} \cdot a_{7} = 2$ which is a prime number. $i = 3, k = 1$, for which the product is: $a_{3} \cdot a_{6} = 2$ which is a prime number. $i = 6, k = 1$, for which the product is: $a_{6} \cdot a_{9} = 2$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions. In the fifth example test case five pairs satisfy the conditions: $i = 1, k = 1$, for which the product is: $a_{1} \cdot a_{2} = 2$ which is a prime number. $i = 1, k = 2$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 1, k = 3$, for which the product is: $a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. $i = 2, k = 1$, for which the product is: $a_{2} \cdot a_{3} = 2$ which is a prime number. $i = 2, k = 2$, for which the product is: $a_{2} \cdot a_{3} \cdot a_{4} = 2$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions.
def SieveOfEratosthenes(n): prime = [(True) for i in range(n + 1)] p = 2 while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 m = {} for p in range(2, n): if prime[p]: m[p] = 1 return m h = SieveOfEratosthenes(10**6) a = int(input()) for x in range(a): b, c = map(int, input().split()) d = list(map(int, input().split())) f = 0 for y in range(b): n = 0 p = 0 if h.get(d[y]) != None: for z in range(y + c, b, c): if d[z] == 1: n += 1 else: break for z in range(y - c, -1, -c): if d[z] == 1: p += 1 else: break f += n + p * (n + 1) print(f)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
length = int(input()) arr = input().split(" ") arr = list(map(int, arr)) leftMax = 1 rightMax = 1 for i in range(length - 1): if arr[i] < arr[i + 1]: leftMax += 1 else: break for i in range(length - 1): if arr[-(i + 1)] < arr[-(i + 2)]: rightMax += 1 else: break if leftMax % 2 != 0 or rightMax % 2 != 0 or length == 1: print("Alice") else: print("Bob")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
n = int(input()) vetor = list(map(lambda x: int(x), input().split(" "))) right = True left = True for i in range(0, n - 1): if vetor[i + 1] <= vetor[i]: break left = not left for j in range(n - 1, -1, -1): if vetor[j - 1] <= vetor[j]: break right = not right if right or left: print("Alice") else: print("Bob")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
n = int(input()) num = [int(i) for i in input().split()] l, r = 0, n - 1 while l + 1 < n and num[l] < num[l + 1]: l += 1 while r >= 0 and num[r] < num[r - 1]: r -= 1 l = l + 1 r = n - r if l % 2 or r % 2: print("Alice") else: print("Bob")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
n = int(input()) arr = list(map(int, input().split())) prenum = 1 while prenum < n and arr[prenum] > arr[prenum - 1]: prenum += 1 surnum = n - 2 while surnum >= 0 and arr[surnum] > arr[surnum + 1]: surnum -= 1 front = 0 rear = n - 1 result = 0 while front <= prenum and rear >= surnum: if front == prenum or rear == surnum: result += prenum - front + rear - surnum - 1 break if arr[front] < arr[rear]: if rear - surnum & 1: break front += 1 result += 1 elif arr[front] > arr[rear]: if prenum - front & 1: break rear -= 1 result += 1 else: if prenum - front & 1 or rear - surnum & 1: break result += 1 break if result & 1: print("Bob") else: print("Alice")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
length = int(input()) arr = input().split(" ") arr = list(map(int, arr)) go = True currentHigh = -1 player = 1 leftMax = 1 rightMax = 1 for i in range(length - 1): if arr[i] < arr[i + 1]: leftMax += 1 else: break for i in range(length - 1): if arr[-(i + 1)] < arr[-(i + 2)]: rightMax += 1 else: break def won(player): global go if player % 2 != 0: print("Alice") else: print("Bob") go = False def pick(d): global currentHigh, player if d == "L": if currentHigh >= arr[0]: won(player + 1) currentHigh = arr.pop(0) else: if currentHigh >= arr[-1]: won(player + 1) currentHigh = arr.pop(-1) player += 1 while go: left = arr[0] right = arr[-1] if left == right: if leftMax % 2 != 0 or rightMax % 2 != 0: won(player) else: won(player + 1) if left > right: if leftMax % 2 != 0: won(player) else: rightMax -= 1 pick("R") if right > left: if rightMax % 2 != 0: won(player) else: leftMax -= 1 pick("L")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF IF VAR STRING IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
from sys import stdin eg = False inp = [] it0 = 0 it1 = 0 new = [] it = 0 max1 = -1 max2 = -1 va = 0 for line in stdin: if va == 1: inp = [x for x in line.strip().split()] va += 1 l = len(inp) def only_one_end(it, lr): global inp global l if lr: n1 = int(inp[l - it - 1]) n2 = int(inp[l - it - 2]) while n2 > n1: it += 1 n1 = n2 n2 = int(inp[l - it - 2]) else: n1 = int(inp[0 + it]) n2 = int(inp[0 + it + 1]) while n2 > n1: it += 1 n1 = n2 n2 = int(inp[0 + it + 1]) it += 1 return it la = False for i in range(l): n1 = int(inp[it0]) n2 = int(inp[l - it1 - 1]) if n1 > n2: la = True new.append(1) it1 += 1 if n2 >= int(inp[l - it1 - 1]): it0 = only_one_end(it0, False) break elif n1 < n2: la = False new.append(0) it0 += 1 if n1 >= int(inp[0 + it0]): it1 = only_one_end(it1, True) break elif it0 != l - it1 - 1: eg = True egPo = [it0, it1] it1 = only_one_end(it1, True) it0 = only_one_end(it0, False) break else: if la: it0 += 1 else: it1 += 1 break it3 = 0 res = False con = True for e in new: if e == 1 and it0 % 2 == 1 or e == 0 and it1 % 2 == 1: con = False if res: print("Bob") else: print("Alice") break elif e == 0: it0 -= 1 else: it1 -= 1 res = not res if con: if it0 + it1 != 0 and (it0 % 2 == 0 and it1 % 2 == 0): res = not res if res: print("Bob") else: print("Alice")
ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
n = int(input()) a = list(map(int, input().split())) Left = 0 Right = n - 1 while Left != n - 1 and a[Left + 1] > a[Left]: Left += 1 while Right != 0 and a[Right - 1] > a[Right]: Right -= 1 if (Left + 1) % 2 or (n - Right) % 2: print("Alice") else: print("Bob")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a game. They are given an array $A$ of length $N$. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? -----Input----- The first line contains one integer $N$ ($1 \leq N \leq 2*10^5$) - the length of the array $A$. The second line contains $N$ integers $A_1$, $A_2$,...,$A_N$ ($0 \leq A_i \leq 10^9$) -----Output----- The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". -----Examples----- Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob -----Note----- None
arraySize = int(input()) left, right = 0, arraySize - 1 array = list(map(int, input().split())) while left + 1 < arraySize and array[left] < array[left + 1]: left += 1 while right > 0 and array[right] < array[right - 1]: right -= 1 left += 1 right = arraySize - right if left % 2 or right % 2: print("Alice") else: print("Bob")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers. The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m β‰₯ n β‹… k), and that means every citizen will get a wreath. Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana. Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter. Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces? Input The first line contains four integers m, k, n and s (1 ≀ n, k, m ≀ 5 β‹… 10^5, k β‹… n ≀ m, 1 ≀ s ≀ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively. The second line contains m integers a_1, a_2, ..., a_m (1 ≀ a_i ≀ 5 β‹… 10^5) β€” types of flowers on the liana. The third line contains s integers b_1, b_2, ..., b_s (1 ≀ b_i ≀ 5 β‹… 10^5) β€” the sequence in Diana's schematic. Output If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1. Otherwise in the first line output one integer d β€” the number of flowers to be removed by Diana. In the next line output d different integers β€” the positions of the flowers to be removed. If there are multiple answers, print any. Examples Input 7 3 2 2 1 2 3 3 2 1 2 2 2 Output 1 4 Input 13 4 3 3 3 2 6 4 1 4 4 7 1 3 3 2 4 4 3 4 Output -1 Input 13 4 1 3 3 2 6 4 1 4 4 7 1 3 3 2 4 4 3 4 Output 9 1 2 3 4 5 9 11 12 13 Note In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic. In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic. In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs.
def main(): m, k, n, s = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) b_dict = {} for x in b: b_dict.setdefault(x, 0) b_dict[x] += 1 left = 0 right = 0 max_cut = m - n * k condition_not_met = len(b_dict) a_dict = {} while right < m and condition_not_met > 0: x = a[right] a_dict.setdefault(x, 0) a_dict[x] += 1 if x in b_dict and a_dict[x] == b_dict[x]: condition_not_met -= 1 right += 1 if condition_not_met > 0: print(-1) return def num_to_remove(lft, rgt): lft = lft // k * k num_in_seq = rgt - lft if num_in_seq < k: return 0 return num_in_seq - k def test_plan(): nonlocal left if num_to_remove(left, right) <= max_cut: tot = num_to_remove(left, right) print(tot) left = left // k * k while tot > 0: x = a[left] if x in b_dict: b_dict[x] -= 1 if b_dict[x] == 0: del b_dict[x] else: print(left + 1, end=" ") tot -= 1 left += 1 return True return False while True: while left < right: x = a[left] if x in b_dict and a_dict[x] - 1 < b_dict[x]: break else: a_dict[x] -= 1 if a_dict[x] == 0: del a_dict[x] left += 1 if test_plan(): return if right < m: a_dict.setdefault(a[right], 0) a_dict[a[right]] += 1 right += 1 else: break print(-1) main()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN BIN_OP VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers. The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m β‰₯ n β‹… k), and that means every citizen will get a wreath. Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana. Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter. Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces? Input The first line contains four integers m, k, n and s (1 ≀ n, k, m ≀ 5 β‹… 10^5, k β‹… n ≀ m, 1 ≀ s ≀ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively. The second line contains m integers a_1, a_2, ..., a_m (1 ≀ a_i ≀ 5 β‹… 10^5) β€” types of flowers on the liana. The third line contains s integers b_1, b_2, ..., b_s (1 ≀ b_i ≀ 5 β‹… 10^5) β€” the sequence in Diana's schematic. Output If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1. Otherwise in the first line output one integer d β€” the number of flowers to be removed by Diana. In the next line output d different integers β€” the positions of the flowers to be removed. If there are multiple answers, print any. Examples Input 7 3 2 2 1 2 3 3 2 1 2 2 2 Output 1 4 Input 13 4 3 3 3 2 6 4 1 4 4 7 1 3 3 2 4 4 3 4 Output -1 Input 13 4 1 3 3 2 6 4 1 4 4 7 1 3 3 2 4 4 3 4 Output 9 1 2 3 4 5 9 11 12 13 Note In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic. In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic. In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs.
m, k, n, s = map(int, input().split()) ls = list(map(int, input().split())) seq = map(int, input().split()) dseq = {} for se in seq: if se not in dseq: dseq[se] = 0 dseq[se] += 1 maxwindowsize = m - k * n + k dcurr = {} nrusefull = 0 for i in range(maxwindowsize - k): if ls[i] not in dcurr: dcurr[ls[i]] = 0 dcurr[ls[i]] += 1 if ls[i] in dseq and dseq[ls[i]] >= dcurr[ls[i]]: nrusefull += 1 for j in range(m): if j % k == 0 and j + k <= m: mini = maxwindowsize + j - k for i in range(mini, min(mini + k, m)): if ls[i] not in dcurr: dcurr[ls[i]] = 0 dcurr[ls[i]] += 1 if ls[i] in dseq and dseq[ls[i]] >= dcurr[ls[i]]: nrusefull += 1 if nrusefull == s: printl = [] dlast = {x: (0) for x in dcurr} for x in range(j, j + maxwindowsize): if len(printl) == maxwindowsize - k: break dlast[ls[x]] += 1 if ls[x] not in dseq or dseq[ls[x]] < dlast[ls[x]]: printl.append(str(x + 1)) break dcurr[ls[j]] -= 1 if ls[j] in dseq and dcurr[ls[j]] < dseq[ls[j]]: nrusefull -= 1 else: print(-1) raise SystemExit() print(len(printl)) print(" ".join(printl))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline for __ in range(int(input())): s = input().split()[1] n = len(s) + 1 amin = [] i = 0 t = n while i < n - 1: c = s[i] if c == ">": amin.append(t) t -= 1 i += 1 else: j = i + 1 while j < n - 1 and s[j] != ">": j += 1 amin.extend(range(t - (j - i), t + 1)) t -= j - i + 1 i = j + 1 if len(amin) < n: amin.append(t) print(*amin) amax = [] i = 0 t = 1 while i < n - 1: c = s[i] if c == "<": amax.append(t) t += 1 i += 1 else: j = i + 1 while j < n - 1 and s[j] != "<": j += 1 amax.extend(range(t + (j - i), t - 1, -1)) t += j - i + 1 i = j + 1 if len(amax) < n: amax.append(t) print(*amax)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
from sys import stdin, stdout t = int(stdin.readline().strip()) for _ in range(t): n, comp = stdin.readline().split() n = int(n) clist = [] last, cnt = None, 0 lt = 0 for i in range(len(comp)): if comp[i] == "<": lt += 1 if last: if comp[i] != last: clist.append((last, cnt)) cnt = 0 last = comp[i] cnt += 1 else: clist.append((last, cnt)) i, j = 0, 0 sht, lng = [], [] for c, v in clist: if c == "<": for x in range(i + 1, i + v + 1): lng.append(str(x)) for x in range(lt - i - v + 1, lt - i + 1): sht.append(str(x)) i += v else: for _ in range(v): s = str(n - j) sht.append(s) lng.append(s) j += 1 else: s = str(n - j) sht.append(s) lng.append(s) stdout.write("{}\n{}\n".format(" ".join(sht), " ".join(lng)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NONE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR VAR IF VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING VAR FUNC_CALL STRING VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
a = int(input()) for i in range(a): n, s = map(str, input().split()) n = int(n) ans = [] flag = 1 glag = 1 for i in range(len(s)): if i == 0: if s[i] == "<": ans.append(-flag * n) ans.append(ans[-1] + 1) else: ans.append(-glag * n * n) ans.append(ans[-1] - 1) elif s[i] == s[i - 1] == "<": ans.append(ans[-1] + 1) elif s[i] == s[i - 1] == ">": ans.append(ans[-1] - 1) elif s[i] == ">": flag += 1 ans.append(-glag * n * n) else: glag += 1 ans.append(-flag * n) t = abs(min(ans)) for i in range(len(ans)): ans[i] += t + 1 big = [] for i in range(len(ans)): big.append([ans[i], i]) big.sort() gam = [(0) for i in range(n)] for i in range(len(big)): gam[big[i][1]] = i + 1 print(*gam) ans = [] if s[0] == ">": ans.append(-1) ans.append(-2) else: ans.append(1) ans.append(2) mini = -3 maxa = 3 for i in range(1, len(s)): if s[i] == ">": ans.append(mini) mini -= 1 else: ans.append(maxa) maxa += 1 t = abs(min(ans)) for i in range(len(ans)): ans[i] += t + 1 big = [] for i in range(len(ans)): big.append([ans[i], i]) big.sort() gam = [(0) for i in range(n)] for i in range(len(big)): gam[big[i][1]] = i + 1 print(*gam)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def makeRepeat(s): repeat = [[s[0], 0]] for ch in s: if ch == repeat[-1][0]: repeat[-1][1] += 1 else: repeat.append([ch, 1]) return repeat def solve(N, S): assert len(S) == N - 1 curr = 0 repeat = makeRepeat(S) longest = list(range(1, N + 1)) shortest = list(reversed(range(1, N + 1))) for ch, count in repeat: if ch == ">": longest[curr : curr + count + 1] = reversed( longest[curr : curr + count + 1] ) else: assert ch == "<" shortest[curr : curr + count + 1] = reversed( shortest[curr : curr + count + 1] ) curr += count return " ".join(map(str, shortest)) + "\n" + " ".join(map(str, longest)) (T,) = map(int, input().split()) for t in range(T): N, S = input().split() N = int(N) ans = solve(N, S) print(ans)
FUNC_DEF ASSIGN VAR LIST LIST VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER RETURN VAR FUNC_DEF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def print_arr(arr): for a in arr: print(a, end=" ") print() def solve_max(n, s): div = s.count(">") + 1 l, r = div - 1, div + 1 ans = [div] for c in s: if c == "<": ans.append(r) r += 1 else: ans.append(l) l -= 1 print_arr(ans) def solve_min(n, s): div = s.count(">") + 1 ans = [div] div -= 1 i = 0 while i < len(s): if s[i] == ">": ans.append(div) div -= 1 i += 1 else: j = i while j < len(s) and s[i] == s[j]: j += 1 for it in range(n - j + i + 1, n + 1): ans.append(it) n -= j - i i = j print_arr(ans) def main(): tc = int(input()) for _ in range(tc): n, s = input().split() n = int(n) solve_min(n, s) solve_max(n, s) main()
FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): N, s = input().split() N = int(N) s = s.strip() ts = [] t = 0 for c in s: if c == "<": t += 1 else: ts.append(t) t = 0 ts.append(t) c = 0 rs = [] while ts: t = ts.pop() for j in range(t + 1): rs.append(c + t + 1 - j) c += t + 1 print(*rs[::-1]) ts = [] t = 0 for c in s: if c == ">": t += 1 else: ts.append(t) t = 0 ts.append(t) c = 0 rs = [] for t in ts: for j in range(t + 1): rs.append(c + t + 1 - j) c += t + 1 print(*rs)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline def compress(array): array2 = sorted(set(array)) memo = {value: index for index, value in enumerate(array2)} for i in range(len(array)): array[i] = memo[array[i]] + 1 return array t = int(input()) base = 10**6 for _ in range(t): n, b = list(map(str, input().split())) n = int(n) ans = [0] * n now = base ans[0] = base for i in range(n - 1): if b[i] == ">": now -= base ans[i + 1] = now else: now += 1 ans[i + 1] = now print(*compress(ans)) now = base ans[0] = base for i in range(n - 1): if b[i] == ">": now -= 1 ans[i + 1] = now else: now += base ans[i + 1] = now print(*compress(ans))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys import time def reverse_items(seq, reverse_start, reverse_end): while reverse_start < reverse_end: tmp = seq[reverse_start] seq[reverse_start] = seq[reverse_end] seq[reverse_end] = tmp reverse_start += 1 reverse_end -= 1 def output_shortest_and_longest(max_number, comp_serial): increasing_seq = [str(i) for i in range(1, max_number + 1)] decreasing_seq = [s for s in reversed(increasing_seq)] continuous_lt_start = None continuous_gt_start = None last_comp_symbol = comp_serial[0] for i in range(0, len(comp_serial)): ch = comp_serial[i] if ch == "<": if continuous_lt_start is None: continuous_lt_start = i if ch != last_comp_symbol: reverse_items(increasing_seq, continuous_gt_start, i) continuous_gt_start = None else: if continuous_gt_start is None: continuous_gt_start = i if ch != last_comp_symbol: reverse_items(decreasing_seq, continuous_lt_start, i) continuous_lt_start = None last_comp_symbol = ch if continuous_lt_start is not None and comp_serial[-1] == "<": reverse_items(decreasing_seq, continuous_lt_start, len(comp_serial)) if continuous_gt_start is not None and comp_serial[-1] == ">": reverse_items(increasing_seq, continuous_gt_start, len(comp_serial)) print(" ".join(decreasing_seq)) print(" ".join(increasing_seq)) def solve(): import time left_cases = None for line in sys.stdin: if left_cases is None: left_cases = int(line.strip()) else: vals = line.split() output_shortest_and_longest(int(vals[0]), vals[1]) left_cases -= 1 if left_cases == 0: break solve()
IMPORT IMPORT FUNC_DEF WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING IF VAR NONE ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE IF VAR NONE ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR IF VAR NONE VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NONE VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF IMPORT ASSIGN VAR NONE FOR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
for nt in range(int(input())): n, s = input().split() n = int(n) a = [i for i in range(n, 0, -1)] i = 0 c = [] while i < n: if i < n - 1 and s[i] == "<": start = i while start < n - 1 and s[start] == "<": start += 1 c.append((i, start)) i = start else: i += 1 j, i = 0, 0 ans = [] while i < n: if j < len(c) and c[j][0] == i: for k in range(c[j][1], c[j][0] - 1, -1): ans.append(a[k]) i = c[j][1] + 1 j += 1 else: ans.append(a[i]) i += 1 print(*ans) a = [i for i in range(1, n + 1)] i = 0 c = [] while i < n: if i < n - 1 and s[i] == ">": start = i while start < n - 1 and s[start] == ">": start += 1 c.append((i, start)) i = start else: i += 1 j, i = 0, 0 ans = [] while i < n: if j < len(c) and c[j][0] == i: for k in range(c[j][1], c[j][0] - 1, -1): ans.append(a[k]) i = c[j][1] + 1 j += 1 else: ans.append(a[i]) i += 1 print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline Q = int(input()) Query = [] for _ in range(Q): N, S = map(str, input().rstrip().split()) Query.append((int(N), list(S))) for N, S in Query: L = [] for i, s in enumerate(S): if s == "<": L.append(i) L.append(N - 1) Ls = set(L) Longans = [-1] * N Shortans = [-1] * N P = [] k = [] pre = -2 for i, l in enumerate(L): Longans[l] = i + 1 if l == pre + 1: k.append(l) else: P.append(k) k = [l] pre = l P.append(k) count = 0 for k in reversed(P): for n in k: count += 1 Shortans[n] = count for i in reversed(range(N)): if not i in Ls: count += 1 Longans[i] = count Shortans[i] = count print(*Shortans, sep=" ") print(*Longans, sep=" ")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
t = int(input()) for _ in range(t): n, s = input().split() n = int(n) s = "<" + s + ">" up = [1] dw = [] for i in range(1, n + 1): if s[i - 1 : i + 1] == "<<": up[-1] += 1 elif s[i - 1 : i + 1] == ">>": dw[-1] += 1 elif s[i - 1 : i + 1] == "<>": dw.append(1) else: up.append(1) dw[-1] -= 1 l = len(up) ans = [] now = 0 for i in range(l): z1 = [(now + j + 1) for j in range(up[i] - 1)] now += up[i] - 1 z2 = [(now + j + 1) for j in range(dw[i] + 1)] z2.reverse() now += dw[i] + 1 ans += z1 ans += z2 ans1 = " ".join(list(map(str, ans))) up[0] -= 1 dw[-1] += 1 ans = [] now = n + 1 for i in range(l): z1 = [(now - j - 1) for j in range(dw[i])] now -= dw[i] z2 = [(now - j - 1) for j in range(up[i])] z2.reverse() now -= up[i] ans += z2 ans += z1 ans2 = " ".join(list(map(str, ans))) print(ans2) print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
for _ in range(int(input())): n, s = input().split() n = int(n) mn = [0] * n mx = [0] * n s_i = [0] * n if s[-1] == ">": s_i[-2] = 1 else: s_i[-2] = -1 for i in range(len(s) - 2, -1, -1): if s[i] == ">": if i + 1 < len(s) and s_i[i + 1] > 0: s_i[i] = s_i[i + 1] + 1 else: s_i[i] = 1 elif i + 1 < len(s) and s_i[i + 1] < 0: s_i[i] = s_i[i + 1] - 1 else: s_i[i] = -1 mxm = n i = 0 while i < n: if s_i[i] > 0: ei = i + s_i[i] + 1 if ei == n: while i < n: mx[i] = mxm i += 1 mxm -= 1 else: mx[ei] = mxm mxm -= 1 while i < ei: mx[i] = mxm i += 1 mxm -= 1 i += 1 else: ei = i + abs(s_i[i]) j = ei while j >= i: mx[j] = mxm mxm -= 1 j -= 1 i = ei + 1 i = 0 mnm = 1 while i < n: if s_i[i] > 0: ei = i + abs(s_i[i]) j = ei while j >= i: mn[j] = mnm mnm += 1 j -= 1 i = ei + 1 else: ei = i + abs(s_i[i]) while i <= ei: mn[i] = mnm mnm += 1 i += 1 i = 0 while i < len(s): if s[i] == ">" and mx[i] < mx[i + 1]: mx[i], mx[i + 1] = mx[i + 1], mx[i] i -= 1 continue elif s[i] == "<" and mx[i] > mx[i + 1]: mx[i], mx[i + 1] = mx[i + 1], mx[i] i -= 1 continue i += 1 print(*mx) i = 0 while i < len(s): if s[i] == ">" and mn[i] < mn[i + 1]: mn[i], mn[i + 1] = mn[i + 1], mn[i] i -= 1 continue elif s[i] == "<" and mn[i] > mn[i + 1]: mn[i], mn[i + 1] = mn[i + 1], mn[i] i -= 1 continue i += 1 print(*mn)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
from sys import stdin for case in range(int(stdin.readline())): n, s = stdin.readline().strip().split() n = int(n) parts = [] ind = 0 while ind < n - 1: cur = s[ind] l = 0 while s[ind] == cur: ind += 1 l += 1 if ind == len(s): break if cur == "<": parts.append((0, l)) else: parts.append((1, l)) highOut = [] if parts[0][0] == 1: high = n - 1 tempH = n else: high = n for x in parts: if x[0] == 1: highOut += [tempH] + [(high - i) for i in range(x[1] - 1)] high -= x[1] - 1 else: highOut += [(high - i) for i in range(x[1], 0, -1)] tempH = high high -= x[1] + 1 if parts[-1][0] == 0: highOut.append(tempH) else: highOut.append(1) lowOut = [] if parts[0][0] == 0: low = 2 tempL = 1 else: low = 1 for x in parts: if x[0] == 0: lowOut += [tempL] + [(low + i) for i in range(x[1] - 1)] low += x[1] - 1 else: lowOut += [(low + i) for i in range(x[1], 0, -1)] tempL = low low += x[1] + 1 if parts[-1][0] == 1: lowOut.append(tempL) else: lowOut.append(n) print(" ".join([str(x) for x in highOut])) print(" ".join([str(x) for x in lowOut]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER VAR BIN_OP LIST VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER VAR BIN_OP LIST VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, s = input().rstrip().split() n = int(n) last = 0 num = n ans1 = [0] * n for i in range(n): if i == n - 1 or s[i] == ">": for j in range(last, i + 1)[::-1]: ans1[j] = num num -= 1 last = i + 1 print(*ans1) num = 1 last = 0 ans2 = [0] * n for i in range(n): if i == n - 1 or s[i] == "<": for j in range(last, i + 1)[::-1]: ans2[j] = num num += 1 last = i + 1 print(*ans2)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
for t in range(int(input())): n, s = [i for i in input().split()] n = int(n) s1 = "" s2 = "" for i in range(n - 1): if s[i] == "<": s1 += "0" s2 += "1" else: s1 += "1" s2 += "0" s2 = s2[::-1] res1 = [] res2 = [] temp1 = [1] temp2 = [1] curr = 2 i = 0 while i < n - 1: if s1[i] == "0": while temp1: res1.append(temp1.pop()) temp1.append(i + 2) else: temp1.append(i + 2) if s2[i] == "0": while temp2: res2.append(temp2.pop()) temp2.append(i + 2) else: temp2.append(i + 2) i += 1 while temp1: res1.append(temp1.pop()) while temp2: res2.append(temp2.pop()) print(*res2[::-1]) print(*res1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = lambda: sys.stdin.readline().strip() for _ in range(int(input())): n, s = input().split() n = int(n) lts = [-1, *(i for i in range(len(s)) if s[i] == ">"), n - 1] cur = n + 1 for i in range(1, len(lts)): diff = lts[i] - lts[i - 1] print(*range(cur - diff, cur), end=" ") cur -= diff print() gts = [-1, *(i for i in range(len(s)) if s[i] == "<"), n - 1] cur = 1 for i in range(1, len(gts)): diff = gts[i] - gts[i - 1] print(*reversed(range(cur, cur + diff)), end=" ") cur += diff print()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def calmin(xs): n = len(xs) + 1 ans = [-1] * n high = n i = xs.find("<") while 0 <= i < n - 1: j = xs.find(">", i) if j == -1: j = n - 1 diff = j - i begin = high - diff + 1 for i in range(i, j): ans[i + 1] = begin begin += 1 high -= 1 i = xs.find("<", j) for i in range(n): if ans[i] == -1: ans[i] = high high -= 1 return ans def calmax(xs): n = len(xs) + 1 ans = [-1] * n i = xs.find(">") while 0 <= i < n - 1: low = i j = xs.find("<", i) if j == -1: j = n - 1 diff = j - i begin = low + diff + 1 for i in range(i, j): ans[i] = begin begin -= 1 low += 1 ans[i + 1] = begin i = xs.find(">", j) for i in range(n): if ans[i] == -1: ans[i] = i + 1 return ans T = int(input()) for _ in range(T): ins = input().split() n = int(ins[0]) xs = ins[1] print(" ".join(map(str, calmin(xs)))) print(" ".join(map(str, calmax(xs))))
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def getInput(): line = input().split() return int(line[0]), line[1] def sLIS(n, s): ans = list(range(n, 0, -1)) rev = [] i = 0 while i < n - 1: if s[i] == "<": j = i + 1 while j < n - 1 and s[j] == "<": j += 1 rev.append((i, j)) i = j + 1 else: i += 1 for r in rev: i, j = r while i <= j: ans[i], ans[j] = ans[j], ans[i] i += 1 j -= 1 return ans def lLIS(n, s): ans = list(range(1, n + 1)) rev = [] i = 0 while i < n - 1: if s[i] == ">": j = i + 1 while j < n - 1 and s[j] == ">": j += 1 rev.append((i, j)) i = j + 1 else: i += 1 for r in rev: i, j = r while i <= j: ans[i], ans[j] = ans[j], ans[i] i += 1 j -= 1 return ans for _ in range(int(input())): n, s = getInput() print(*sLIS(n, s)) print(*lLIS(n, s))
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
t = int(input()) while t: n, s = input().split() n = int(n) a = [] temp = n ls = [] for i in range(n - 1): if s[i] == ">": a.append(temp) temp -= 1 if len(ls): j = len(ls) - 1 while j >= 0: a.append(ls[j]) j -= 1 ls = [] else: ls.append(temp) temp -= 1 ls.append(temp) if len(ls): j = len(ls) - 1 while j >= 0: a.append(ls[j]) j -= 1 b = [] temp = 1 ls = [] for i in range(n - 1): if s[i] == "<": b.append(temp) temp += 1 if len(ls): j = len(ls) - 1 while j >= 0: b.append(ls[j]) j -= 1 ls = [] else: ls.append(temp) temp += 1 ls.append(temp) if len(ls): j = len(ls) - 1 while j >= 0: b.append(ls[j]) j -= 1 print(*a) print(*b) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline q = int(input()) for i in range(q): n, s = input().split() n = int(n) direct = [0] for i in range(1, n - 1): if s[i] != s[i - 1]: direct.append(i) if s[-1] == ">": direct.append(n - 1) l = len(direct) if l == 1: print(*list(range(1, n + 1))) print(*list(range(1, n + 1))) continue geq = [] leq = [] for i in range(1, l): if i % 2 and s[0] == ">" or i % 2 == 0 and s[0] == "<": geq.append(direct[i] - direct[i - 1]) else: leq.append(direct[i] - direct[i - 1]) if s[0] == "<": geq.append(1) ans1 = [] ans2 = [] cnt = 0 for i in range(l - 1): if i % 2 == 0: ans1.extend(list(range(n - cnt - leq[i // 2], n - cnt + 1))) cnt += leq[i // 2] + 1 else: ans1.extend(list(range(n - cnt, n - cnt - geq[i // 2] + 1, -1))) cnt += geq[i // 2] - 1 ans1.extend(list(range(1, n - cnt + 1))) cnt = 1 for i in range(l - 1): if i == 0: ans2.extend(list(range(cnt, cnt + leq[i // 2]))) cnt += leq[i // 2] elif i % 2 == 0: ans2.extend(list(range(cnt, cnt + leq[i // 2] - 1))) cnt += leq[i // 2] - 1 else: ans2.extend(list(range(cnt + geq[i // 2], cnt - 1, -1))) cnt += geq[i // 2] + 1 ans2.extend(list(range(cnt, n + 1))) else: ans1 = [] ans2 = [] cnt = 0 for i in range(l - 1): if i == 0: ans1.extend(list(range(n, n - geq[i // 2], -1))) cnt += geq[i // 2] elif i % 2 == 0: ans1.extend(list(range(n - cnt, n - cnt - geq[i // 2] + 1, -1))) cnt += geq[i // 2] - 1 else: ans1.extend(list(range(n - cnt - leq[i // 2], n - cnt + 1))) cnt += leq[i // 2] + 1 ans1.extend(list(range(1, n - cnt + 1))) cnt = 1 for i in range(l - 1): if i % 2 == 0: ans2.extend(list(range(cnt + geq[i // 2], cnt - 1, -1))) cnt += geq[i // 2] else: ans2.extend(list(range(cnt + 1, cnt + leq[i // 2]))) cnt += leq[i // 2] ans2.extend(list(range(cnt + 1, n + 1))) print(*ans1) print(*ans2)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, s = input().split() n = int(n) inc, dec = [[0]], [[0]] for i in range(n - 1): if s[i] == ">": inc.append([]) else: dec.append([]) inc[-1].append(i + 1) dec[-1].append(i + 1) ans1_last = n + 1 ans2_last = 0 ans1 = [0] * n ans2 = [0] * n for l in inc: ans1_last -= len(l) for i in range(len(l)): ans1[l[i]] = ans1_last + i for l in dec: ans2_last += len(l) for i in range(len(l)): ans2[l[i]] = ans2_last - i print(*ans1) print(*ans2)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST NUMBER LIST LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
t = int(input()) for loop in range(t): n, s = input().split() n = int(n) s = list(s) lisUP = [] lisDO = [] now = 1 d = s[0] for i in range(n - 1): if d == s[i]: now += 1 else: if d == "<": lisUP.append(now) else: lisDO.append(now) d = s[i] now = 1 if now > 0: if d == "<": lisUP.append(now) else: lisDO.append(now) if s[0] == "<": ans = [] nmax = sum(lisUP) nmin = 0 for i in range(max(len(lisUP), len(lisDO))): now = [] for j in range(lisUP[i]): now.append(nmax) nmax -= 1 now.reverse() ans += now if len(lisDO) == i: break now = [] for j in range(lisDO[i]): now.append(nmin) nmin -= 1 ans += now else: ans = [] nmax = sum(lisUP) nmin = 0 for i in range(max(len(lisUP), len(lisDO))): now = [] for j in range(lisDO[i]): now.append(nmin) nmin -= 1 ans += now if len(lisUP) == i: break now = [] for j in range(lisUP[i]): now.append(nmax) nmax -= 1 now.reverse() ans += now ma = min(ans) for i in range(n): ans[i] -= ma - 1 print(" ".join(map(str, ans))) s.reverse() lisUP = [] lisDO = [] now = 1 d = s[0] for i in range(n - 1): if d == s[i]: now += 1 else: if d == ">": lisUP.append(now) else: lisDO.append(now) d = s[i] now = 1 if now > 0: if d == ">": lisUP.append(now) else: lisDO.append(now) if s[0] == ">": ans = [] nmax = sum(lisUP) nmin = 0 for i in range(max(len(lisUP), len(lisDO))): now = [] for j in range(lisUP[i]): now.append(nmax) nmax -= 1 now.reverse() ans += now if len(lisDO) <= i: break now = [] for j in range(lisDO[i]): now.append(nmin) nmin -= 1 ans += now else: ans = [] nmax = sum(lisUP) nmin = 0 for i in range(max(len(lisUP), len(lisDO))): now = [] for j in range(lisDO[i]): now.append(nmin) nmin -= 1 ans += now if len(lisUP) == i: break now = [] for j in range(lisUP[i]): now.append(nmax) nmax -= 1 now.reverse() ans += now ma = min(ans) for i in range(n): ans[i] -= ma - 1 ans.reverse() print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = lambda: sys.stdin.readline().rstrip() for _ in range(int(input())): n, m = map(str, input().split()) n = int(n) l = [] g = [] buf = [n] li = n - 1 gi = 2 for i in m: if i == ">": while buf: l.append(buf.pop()) l.append(li) li -= 1 else: buf.append(li) li -= 1 while buf: l.append(buf.pop()) buf = [1] for i in range(len(m)): if m[i] == ">": if l[i] < l[i + 1]: l[i], l[i + 1] = l[i + 1], li[i] elif l[i] > l[i + 1]: l[i], l[i + 1] = l[i + 1], l[i] for i in m: if i == "<": while buf: g.append(buf.pop()) g.append(gi) gi += 1 else: buf.append(gi) gi += 1 while buf: g.append(buf.pop()) for i in range(len(m)): if m[i] == ">": if g[i] < g[i + 1]: g[i], g[i + 1] = g[i + 1], g[i] elif g[i] > g[i + 1]: g[i], g[i + 1] = g[i + 1], g[i] print(*l) print(*g)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF VAR STRING WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys t = int(input()) g = sys.stdin.readlines() for i in range(t): n, s = g[i].split() n = int(n) n -= 1 j = 0 big = [] small = [] while j < n: count = 1 while j < n and s[j] == ">": count += 1 j += 1 big.append(count) j += 1 if sum(big) != n + 1: big.append(1) j = 0 while j < n: count = 1 while j < n and s[j] == "<": count += 1 j += 1 small.append(count) j += 1 if sum(small) != n + 1: small.append(1) count = n + 2 for i in small: count -= i for j in range(count, count + i, 1): print(j, end=" ") count = 0 print() for i in big: count += i for j in range(count, count - i, -1): print(j, end=" ") print()
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
t = int(input()) for _ in range(t): n, s = input().split() s = s + "<" n = int(n) ans1 = [(-1) for i in range(n)] time = 0 now1 = n key = 0 for i in range(n): if i == n - 1: for j in range(n - 1, key - 1, -1): ans1[j] = now1 now1 -= 1 break if time == 0 and s[i] == ">": ans1[i] = now1 now1 -= 1 key = i + 1 elif time == 0 and s[i] == "<": time = 1 key = i elif time == 1 and s[i] == "<": pass elif time == 1 and s[i] == ">": time = 0 for j in range(i, key - 1, -1): ans1[j] = now1 now1 -= 1 key = i + 1 print(*ans1) s = ">" + s[:-1] ans2 = [(-1) for i in range(n)] time = 0 now2 = n key = n - 1 for i in range(n - 1, -1, -1): if i == 0: for j in range(key + 1): ans2[j] = now2 now2 -= 1 break if time == 0 and s[i] == "<": ans2[i] = now2 now2 -= 1 key = i - 1 elif time == 0 and s[i] == ">": time = 1 elif time == 1 and s[i] == ">": pass elif time == 1 and s[i] == "<": time = 0 for j in range(i, key + 1): ans2[j] = now2 now2 -= 1 key = i - 1 print(*ans2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR STRING IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR STRING IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
n = int(input()) for i in range(0, n): lis = input() a, b = lis.split() curr = int(a) pos = 0 arr = [1] * curr while curr > 0 and pos < int(a) - 1: if b[pos] == ">": arr[pos] = curr curr -= 1 pos += 1 else: cal = pos diff = 0 while cal < int(a) - 1 and b[cal] == "<": cal += 1 diff += 1 curr = curr - diff while pos <= cal: arr[pos] = curr curr = curr + 1 pos += 1 curr = curr - diff - 2 for i in range(0, int(a)): print(arr[i], end=" ") print() curr = int(a) pos = curr - 2 arr = [1] * curr while curr > 0 and pos >= 0: if b[pos] == "<": arr[pos + 1] = curr curr -= 1 pos -= 1 else: cal = pos diff = 0 while cal >= 0 and b[cal] == ">": cal -= 1 diff += 1 curr = curr - diff while pos >= cal: arr[pos + 1] = curr curr = curr + 1 pos -= 1 curr = curr - diff - 2 for i in range(0, int(a)): print(arr[i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys def main(): t = int(input()) allans = [] for _ in range(t): n, s = input().split() n = int(n) s = s + "$" seq = [] prev = None l = 0 for c in s: if c != prev: if l > 0: seq.append([prev == "<", l]) l = 1 else: l += 1 prev = c seq[0][1] += 1 minLIS = [] curr = n startend = [] for i in range(len(seq)): l = seq[i][1] if seq[i][0]: if i == 0: start = n - l + 1 end = n curr = start - 1 else: start = startend[i - 1][0] + 1 end = start + l - 1 elif i + 1 < len(seq): start = curr - seq[i + 1][1] end = start - l + 1 curr = end - 1 else: start = l end = 1 startend.append([start, end]) for start, end in startend: if start <= end: for v in range(start, end + 1): minLIS.append(v) else: for v in range(start, end - 1, -1): minLIS.append(v) maxLIS = [] startend = [] curr = 1 for i in range(len(seq)): l = seq[i][1] if seq[i][0]: if i + 1 < len(seq): start = curr + seq[i + 1][1] else: start = curr end = start + l - 1 curr = end + 1 else: if i - 1 >= 0: start = startend[i - 1][0] - 1 else: start = l curr = start + 1 end = start - l + 1 startend.append([start, end]) for start, end in startend: if start <= end: for v in range(start, end + 1): maxLIS.append(v) else: for v in range(start, end - 1, -1): maxLIS.append(v) allans.append(minLIS) allans.append(maxLIS) multiLineArrayOfArraysPrint(allans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(*args): assert ( len(args) >= 2 ), "makeArr args should be (default value, dimension 1, dimension 2,..." if len(args) == 2: return [args[0] for _ in range(args[1])] else: return [makeArr(args[0], *args[2:]) for _ in range(args[1])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR STRING VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
t = int(input()) for _ in range(t): n, s = map(str, input().split()) small = [(0) for i in range(int(n))] big = [(0) for i in range(int(n))] n = int(n) ma = n p = s s = " " + s for i in range(n - 1, -1, -1): if s[i] == "<" or i == 0: big[i] = ma ma -= 1 j = i + 1 while j < n: if not big[j]: big[j] = ma ma -= 1 j += 1 else: break s1 = "" for i in range(n - 1 - 1, -1, -1): if s[i] == "<": s1 = s1 + ">" else: s1 += "<" small = big big = [(0) for i in range(n)] ma = n big[-1] = 1 s = p + " " for i in range(0, n): if s[i] == ">" or i == n - 1: j = i - 1 big[i] = ma ma -= 1 while j >= 0: if not big[j]: big[j] = ma ma -= 1 j -= 1 else: break c = 0 s = p for i in s: if i == ">": c += 1 if c == n - 1: for i in range(1, n + 1): big[i - 1] = n - i + 1 small = big c = 0 for i in s: if i == "<": c += 1 if c == n - 1: for i in range(n - 1, -1, -1): big[i] = i + 1 small = big print(*big) print(*small)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR STRING VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
t = int(input()) for test_case in range(t): n, pattern = input().split() n = int(n) ans = [0] * n num, last = n, 0 for i in range(n): if i == n - 1 or pattern[i] == ">": for j in range(i, last - 1, -1): ans[j] = num num -= 1 last = i + 1 print(" ".join(str(k) for k in ans)) num, last = 1, 0 for i in range(n): if i == n - 1 or pattern[i] == "<": for j in range(i, last - 1, -1): ans[j] = num num += 1 last = i + 1 print(" ".join(str(k) for k in ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys inp = sys.stdin.readline input = lambda: inp().strip() flush = sys.stdout.flush def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): T = iin() while T: T -= 1 n, s = input().split() n = int(n) ansmx, ansmn = [0] * n, [0] * n ch = n i = 0 while i < n - 1: if s[i] == ">": ansmn[i] = ch ch -= 1 else: j = i + 1 while j < n - 1: if s[j] == ">": break j += 1 for k in range(j, i, -1): ansmn[k] = ch ch -= 1 ansmn[i] = ch ch -= 1 i = j i += 1 else: if ansmn[n - 1] == 0: ansmn[n - 1] = ch s = list(s)[::-1] for i in range(n - 1): if s[i] == ">": s[i] = "<" else: s[i] = ">" ch = n i = 0 while i < n - 1: if s[i] == ">": ansmx[i] = ch ch -= 1 else: j = i + 1 while j < n - 1: if s[j] == ">": break j += 1 for k in range(j, i, -1): ansmx[k] = ch ch -= 1 ansmx[i] = ch ch -= 1 i = j i += 1 else: if ansmx[n - 1] == 0: ansmx[n - 1] = ch ansmx = ansmx[::-1] print(*ansmn) print(*ansmx) main()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys t = int(sys.stdin.readline()) for _ in range(t): n, s = sys.stdin.readline().split() n = int(n) num, last = n, 0 ans = [(-1) for _ in range(n)] for i in range(n): if i == n - 1 or s[i] == ">": for j in range(i, last - 1, -1): ans[j] = num num -= 1 last = i + 1 ans1 = [(-1) for _ in range(n)] num, last = 1, 0 for i in range(n): if i == n - 1 or s[i] == "<": for j in range(i, last - 1, -1): ans1[j] = num num += 1 last = i + 1 print(" ".join(str(x) for x in ans)) print(" ".join(str(x) for x in ans1))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
Q = int(input()) total = [] for _ in range(Q): n, s = input().split() n = int(n) seq = [] curlen = 1 for i in range(len(s)): if s[i] == "<": curlen += 1 else: seq.append(curlen) curlen = 1 seq.append(curlen) curr = n ans = [] for sq in seq: ans.extend(range(curr - sq + 1, curr + 1)) curr -= sq total.append(" ".join(map(str, ans))) curr = n ans = [""] * n for i in range(len(s) - 1, -1, -1): if s[i] == "<": ans[i + 1] = str(curr) curr -= 1 for i in range(n): if not ans[i]: ans[i] = str(curr) curr -= 1 total.append(" ".join(ans)) print(*total, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR STRING
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys def main(): import sys input = sys.stdin.readline for _ in range(int(input())): line = list(input().split()) N = int(line[0]) S = line[1] inc_num = S.count("<") ans = [0] * N inc = N dec = N - inc_num for i in range(N - 1): if ans[i + 1]: continue if S[i] == "<": j = i + 1 cnt = 1 while True: if j == N - 1: break if S[j] == "<": cnt += 1 else: break j += 1 for j in range(i + cnt - 1, i - 1, -1): ans[j + 1] = inc inc -= 1 for i in range(N): if ans[i] == 0: ans[i] = dec dec -= 1 print(*ans) ans = [0] * N inc = N - inc_num + 1 dec = N - inc_num for i in range(N - 1): if S[i] == "<": ans[i + 1] = inc inc += 1 for i in range(N): if ans[i] == 0: ans[i] = dec dec -= 1 print(*ans) def __starting_point(): main() __starting_point()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
tc = int(input()) def remap(s): sorted_s = sorted(s) num2idx = {num: i for i, num in enumerate(sorted_s, 1)} return [num2idx[num] for num in s] def get_min(s): n = len(s) res = [0] min_so_far = 0 for i, sign in enumerate(s): if sign == "<": res.append(res[-1] + 1) else: res.append(min_so_far - n - 1) min_so_far = min(min_so_far, res[-1]) return remap(res) def get_max(s): n = len(s) res = [0] max_so_far = 0 for i, sign in enumerate(s): if sign == "<": res.append(res[-1] + n + 1) else: res.append(res[-1] - 1) return remap(res) for _ in range(tc): _, s = input().split() print(*get_min(s)) print(*get_max(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
t = int(input()) for i in range(t): a = input().split() n = int(a[0]) s = a[1] + ">" ans = [] c = [] now = n for j in range(n): if s[j] == "<": c.append(now) now -= 1 elif len(c) > 0: c.append(now) now -= 1 ans += c[::-1] c = [] else: ans.append(now) now -= 1 if len(c) > 0: ans += c[::-1] print(" ".join(map(str, ans))) ans = [] c = [] now = 1 for j in range(n): if s[j] == ">": c.append(now) now += 1 elif len(c) > 0: c.append(now) now += 1 ans += c[::-1] c = [] else: ans.append(now) now += 1 if len(c) > 0: ans += c[::-1] print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
for tc in range(int(input())): n, s = input().split(" ") n = int(n) inc = [-1] * n dec = [-1] * n cnti = 1 for i in range(len(s)): if s[i] == "<": inc[i] = cnti cnti += 1 lcnt = n for i in range(n): if inc[i] == -1: inc[i] = lcnt lcnt -= 1 cntl = 1 i = n - 2 while i >= 0: if s[i] == "<": si = i ei = i while si >= 0 and s[si] != ">": si -= 1 for j in range(si + 1, ei + 1): dec[j] = cntl cntl += 1 i = si else: i -= 1 lcnt = n for i in range(n): if dec[i] == -1: dec[i] = lcnt lcnt -= 1 print(*dec) print(*inc)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
for _ in range(int(input())): n, s = map(str, input().split()) n = int(n) cnt = {"<": 0, ">": 0} for i in range(n - 1): cnt[s[i]] += 1 big = n - cnt["<"] + 1 small = big - 2 ans1 = [big - 1] for i in range(n - 1): if s[i] == "<": ans1.append(big) big += 1 else: ans1.append(small) small -= 1 if cnt["<"] == 0 or cnt[">"] == 0: print(*ans1) print(*ans1) continue s += " " big = n small = n - cnt[">"] - 1 used = [0] * (n + 1) i = 0 ans = [0] * (n + 1) while i < n: add = 0 t = i while s[t] == "<": t += 1 add += 1 CNT = 0 for j in range(add): ans[i + j] = small - add + 1 used[small - add + 1] = 1 add -= 1 CNT += 1 small -= CNT i = t if s[i] == ">": ans[i] = big used[big] = 1 big -= 1 i += 1 del ans[-1] for i in range(1, n + 1): if used[i] == 0: rest = i break ans[-1] = rest print(*ans) print(*ans1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR STRING NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
for _ in range(int(input())): n, s = map(str, input().split()) n = int(n) mn = [0] * n mx = [0] * n for i in range(n): mn[i] = n - i mx[i] = i + 1 j = 0 i = 0 while i < n - 1: while j < n - 1 and s[i] == s[j]: j += 1 if s[i] == "<": mn[i : j + 1] = mn[i : j + 1][::-1] else: mx[i : j + 1] = mx[i : j + 1][::-1] i = j print(*mn) print(*mx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
line = input() q = int(line) for _ in range(q): line = input() n, s = [x for x in line.split()] n = int(n) minseq = [n] potential = 0 maxseq = [1] minel = 1 maxel = 1 for x in s: if x == "<": maxseq.append(maxel + 1) maxel += 1 potential += 1 elif x == ">": if potential == 0: minseq.append(minseq[-1] - 1) else: minseq[-1] -= potential tmp = minseq[-1] for _ in range(potential): minseq.append(minseq[-1] + 1) potential = 0 minseq.append(tmp - 1) maxseq.append(minel - 1) minel -= 1 if potential != 0: minseq[-1] -= potential for _ in range(potential): minseq.append(minseq[-1] + 1) for x in minseq: print(x, end=" ") print("") for x in maxseq: print(x - minel + 1, end=" ") print("")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import itertools def countstrs(s): return [(k, len(list(g))) for k, g in itertools.groupby(s)] def countstrs_withIndex(s): d = countstrs(s) r = [] ind = 0 for i in range(len(d)): r.append((d[i][0], d[i][1], ind)) ind += d[i][1] return r q = int(input()) for _ in range(q): inp = input().split() n = int(inp[0]) s = inp[1] dat = [0] * n num, last = n, 0 for i in range(n): if i == n - 1 or s[i] == ">": for j in range(i, last - 1, -1): dat[j] = str(num) num -= 1 last = i + 1 print(" ".join(dat)) dat = [0] * n num, last = 1, 0 for i in range(n): if i == n - 1 or s[i] == "<": for j in range(i, last - 1, -1): dat[j] = str(num) num += 1 last = i + 1 print(" ".join(dat))
IMPORT FUNC_DEF RETURN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
cases = int(input()) for _ in range(cases): l, s = input().split() l = int(l) seq = [] i = 0 prev = l start_i = 0 while i <= len(s): start_i = i while i < len(s) and s[i] == "<": i += 1 for n in range(prev - (i - start_i), prev + 1): seq.append(n) prev -= i - start_i + 1 i += 1 print(" ".join([str(x) for x in seq])) hi = 1 for c in s: if c == ">": hi += 1 seq = [hi] lo = hi - 1 for c in s: if c == "<": hi += 1 seq.append(hi) else: seq.append(lo) lo -= 1 print(" ".join([str(x) for x in seq]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
from sys import gettrace, stdin if not gettrace(): def input(): return next(stdin)[:-1] def main(): def solve(): nt, ss = input().split() n = int(nt) tt = [] for s in ss[::-1]: if s == ">": tt.append("<") else: tt.append(">") ls = longest_lis("".join(tt)) print(" ".join(map(str, ls[::-1]))) ls = longest_lis(ss) print(" ".join(map(str, ls))) def longest_lis(ss): dl = [0] for s in ss[::-1]: if s == ">": dl.append(dl[-1] + 1) else: dl.append(0) ls = [] mx = 0 curr = 0 dp = 0 for d in dl[::-1]: if dp == 0: mx = mx + 1 curr = mx ls.append(curr + d) mx = max(curr + d, mx) dp = d return ls q = int(input()) for _ in range(q): solve() main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def main(): T = int(input()) for t in range(T): n, a = input().split() n = int(n) ans1 = [0] * n l = 0 while l < n - 1: if a[l] == "<": r = l while r < n - 1 and a[r] == "<": r += 1 x = n - r for i in range(l, r + 1): ans1[i] = x x += 1 l = r l += 1 x = 1 for i in range(n - 1, -1, -1): if ans1[i] == 0: ans1[i] = x x += 1 ans2 = [0] * n l = 0 while l < n - 1: if a[l] == ">": r = l while r < n - 1 and a[r] == ">": r += 1 x = l + 1 for i in range(r, l - 1, -1): ans2[i] = x x += 1 l = r l += 1 x = 1 for i in range(n): if ans2[i] == 0: ans2[i] = x x += 1 print(*ans1) print(*ans2) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
t = int(input()) for _ in range(t): n, s = input().split() n = int(n) s = list(s) ans1 = [(n - i) for i in range(n)] i = 0 while i < n - 1: j = i while j < n - 1 and s[j] == "<": j += 1 for k in range((j - i) // 2 + 1): ans1[i + k], ans1[j - k] = ans1[j - k], ans1[i + k] i = j + 1 print(" ".join([str(item) for item in ans1])) ans2 = [(i + 1) for i in range(n)] i = 0 while i < n - 1: j = i while j < n - 1 and s[j] == ">": j += 1 for k in range((j - i) // 2 + 1): ans2[i + k], ans2[j - k] = ans2[j - k], ans2[i + k] i = j + 1 print(" ".join([str(item) for item in ans2]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys from itertools import groupby reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ t = int(input()) for _ in range(t): n, s = input().split() n = int(n) good = [0] + list(range(1, n + 1)) bad = [0] + list(reversed(range(1, n + 1))) l = 1 for k, g in groupby(s): r = l + len(list(g)) if k == ">": for i in range(l, (r + l) // 2 + 1): good[i], good[r + l - i] = good[r + l - i], good[i] else: for i in range(l, (r + l) // 2 + 1): bad[i], bad[r + l - i] = bad[r + l - i], bad[i] l = r print(*(bad[i] for i in range(1, n + 1))) print(*(good[i] for i in range(1, n + 1)))
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
from sys import stdin input = stdin.readline q = int(input()) def maxmozodprawej(s, n): res = [] ind = 0 maksi = n while True: if ind >= n - 1: break if s[ind] == 1: res.append(maksi) maksi -= 1 ind += 1 if ind >= n - 1: break if s[ind] == 0: i = 0 while i + ind < n - 1 and s[i + ind] == 0: i += 1 for j in range(i + 1): res.append(maksi - i + j) maksi -= i + 1 ind += i + 1 if len(res) == n: return res else: return res + [1] for _ in range(q): l = list(input().split()) n = int(l[0]) s = [(1 if l[1][i] == ">" else 0) for i in range(n - 1)] ss = [((s[i] + 1) % 2) for i in range(n - 1)] najm = maxmozodprawej(s, n) print(*najm) najw = maxmozodprawej(ss, n) for i in range(n): if i < n - 1: print(n + 1 - najw[i], end=" ") else: print(n + 1 - najw[i])
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
for _ in range(int(input())): n, s = input().split() n = int(n) tmp = [] ans = [] for i, x in enumerate(s): tmp.append(n - i) if x == ">": ans += tmp[::-1] tmp = [] tmp.append(1) ans += tmp[::-1] print(" ".join([str(x) for x in ans])) i = 1 j = n ans = [] for x in s: if x == ">": ans.append(j) j -= 1 else: ans.append(i) i += 1 ans.append(i) print(" ".join([str(x) for x in ans]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR STRING VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, S = input().split() n = int(n) max_ans = [0] * n i = j = 0 cur = 1 for s in S + "<": if s == ">": j += 1 elif s == "<": for k in range(j, i - 1, -1): max_ans[k] = cur cur += 1 j += 1 i = j min_ans = [0] * n i = j = 0 cur = 1 for s in S[::-1] + ">": if s == "<": j += 1 elif s == ">": for k in range(j, i - 1, -1): min_ans[k] = cur cur += 1 j += 1 i = j print(*min_ans[::-1]) print(*max_ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR BIN_OP VAR STRING IF VAR STRING VAR NUMBER IF VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR BIN_OP VAR NUMBER STRING IF VAR STRING VAR NUMBER IF VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import itertools import sys input = iter(sys.stdin.readlines()).__next__ t = int(input()) outputs = [] for _ in range(t): line = input().split() n = int(line[0]) s = line[1] out1 = [] out2 = [] j = n i = 1 for k, g in itertools.groupby(s): g_len = len(list(g)) if k == "<": run1 = g_len + 1 if j < n: j = out1.pop() out1 += range(j - run1 + 1, j + 1) j -= run1 run2 = g_len + (1 if i == 1 else 0) out2 += range(i, i + run2) i += run2 elif k == ">": run1 = g_len + (1 if j == n else 0) out1 += range(j, j - run1, -1) j -= run1 run2 = g_len + 1 if i > 1: i = out2.pop() out2 += range(i + run2 - 1, i - 1, -1) i += run2 outputs.append(" ".join(str(x) for x in out1)) outputs.append(" ".join(str(x) for x in out2)) print(*outputs, sep="\n")
IMPORT IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys def reverse(arr, i, j): while i < j: temp = arr[i] arr[i] = arr[j] arr[j] = temp i += 1 j -= 1 return None def main(): t = int(input()) allans = [] for _ in range(t): n, s = input().split() n = int(n) minLIS = list(range(n, 0, -1)) i = 0 while i < n - 1: j = i while j < n - 1 and s[j] == "<": j += 1 reverse(minLIS, i, j) i = j + 1 allans.append(minLIS) maxLIS = list(range(1, n + 1)) i = 0 while i < n - 1: j = i while j < n - 1 and s[j] == ">": j += 1 reverse(maxLIS, i, j) i = j + 1 allans.append(maxLIS) multiLineArrayOfArraysPrint(allans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(*args): assert ( len(args) >= 2 ), "makeArr args should be (default value, dimension 1, dimension 2,..." if len(args) == 2: return [args[0] for _ in range(args[1])] else: return [makeArr(args[0], *args[2:]) for _ in range(args[1])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
T = int(input()) for _ in range(0, T): tt = input().split() n = int(tt[0]) s = str(tt[1]) mx = [] vismx = [0] * (n + 1) mn = [] vismn = [0] * (n + 1) L = [] c = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: L.append((s[i - 1], c)) c = 1 else: c += 1 L.append((s[-1], c)) ptr1 = 1 ptr2 = n L = L[::-1] if s[-1] == ">": mn.append(1) vismn[1] = 1 ptr1 += 1 for i in range(0, len(L)): if L[i][0] == ">": for j in range(L[i][1] - 1): mn.append(ptr1) vismn[ptr1] = 1 ptr1 += 1 else: h = [] for j in range(L[i][1] + 1): h.append(ptr1) vismn[ptr1] = 1 ptr1 += 1 h = h[::-1] mn += h for i in range(1, len(vismn)): if vismn[i] == 0: mn.append(i) break mn = mn[::-1] print(*mn) ptr1 = 1 ptr2 = n L = L[::-1] for i in range(0, len(L)): if L[i][0] == ">": for j in range(L[i][1]): mx.append(ptr2) vismx[ptr2] = 1 ptr2 -= 1 else: for j in range(L[i][1]): mx.append(ptr1) vismx[ptr1] = 1 ptr1 += 1 for i in range(1, len(vismx)): if vismx[i] == 0: mx.append(i) break print(*mx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def max_nvp(rel): n = len(rel) + 1 seq = [0] * n lower, higher = 1, n for i in range(len(rel)): if "<" == rel[i]: seq[i] = lower lower += 1 elif ">" == rel[i]: seq[i] = higher higher -= 1 seq[-1] = lower return " ".join(map(str, seq)) def min_nvp(rel): def ge(i, seq, higher): seq[i] = higher higher -= 1 i -= 1 while i >= 0 and rel[i] == "<": seq[i] = higher higher -= 1 i -= 1 return higher def le(i, seq, higher): return higher n = len(rel) + 1 seq = [0] * n higher = n for i, cur_rel in enumerate(rel): if "<" == cur_rel: higher = le(i, seq, higher) elif ">" == cur_rel: higher = ge(i, seq, higher) ge(len(rel), seq, higher) return " ".join(map(str, seq)) t = int(input()) for testcase_i in range(t): _, rel = input().split() print(min_nvp(rel)) print(max_nvp(rel))
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF STRING VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF STRING VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
def solve1(n, comps): ans = [] rng = iter(list(range(1, n + 1))) for comp in [(len(comp) + 1) for comp in comps.split(">")][::-1]: ans.append([ansi for _, ansi in zip(list(range(comp)), rng)]) fin_ans = [] ans.reverse() for ansi in ans: fin_ans.extend(ansi) return fin_ans def solve2(n, comps): ans = [] rng = iter(list(range(1, n + 1))) for comp in [(len(comp) + 1) for comp in comps.split("<")]: ans.extend([ansi for _, ansi in zip(list(range(comp)), rng)][::-1]) return ans def main(): for _ in range(int(input())): n, comps = input().split() n = int(n) print(*solve1(n, comps)) print(*solve2(n, comps)) main()
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys def reverse(s): return s[::-1] def main(): N = int(sys.stdin.readline().strip()) for _ in range(N): n, s = sys.stdin.readline().strip().split() n = int(n) a0 = [0] * n cnt0 = 1 for i in list(range(n - 1))[::-1]: if s[i] == ">": a0[i + 1] = cnt0 cnt0 += 1 a1 = a0[:] cnt1 = cnt0 for i in list(range(n))[::-1]: if a1[i] == 0 and (i == 0 or a1[i - 1] != 0): j = i while j < n and a1[j] == 0: a1[j] = cnt1 cnt1 += 1 j += 1 print(" ".join(str(x) for x in a1)) a2 = a0[:] cnt2 = cnt0 for i in list(range(n)): if a2[i] == 0 and (i == 0 or a2[i - 1] != 0): j = i while j < n and a2[j] == 0: a2[j] = cnt2 cnt2 += 1 j += 1 print(" ".join(str(x) for x in a2)) main()
IMPORT FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys Q = int(sys.stdin.readline().strip()) for q in range(0, Q): n, s = sys.stdin.readline().strip().split() n = int(n) U = [1] D = [1] for i in range(0, n - 1): if s[i] == "<": U[-1] = U[-1] + 1 D.append(1) else: D[-1] = D[-1] + 1 U.append(1) m = n i = 0 A = [] while m > 0: for j in range(0, U[i]): A.append(str(m - U[i] + j + 1)) m = m - U[i] i = i + 1 print(" ".join(A)) m = 0 i = 0 A = [] while i < len(D): for j in range(0, D[i]): A.append(str(m + D[i] - j)) m = m + D[i] i = i + 1 print(" ".join(A))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
import sys import time class Node: def __init__(self): self.val: int = None self.next: Node = None class NodeLink: def __init__(self): self.head = Node() self.tail = self.head self.size = 0 def append(self, node: Node, val): new_node = Node() new_node.val = val new_node.next = node.next node.next = new_node if node is self.tail: self.tail = new_node def print(self): node = self.head.next output_strings = [] while node is not None: output_strings.append(str(node.val)) node = node.next print(" ".join(output_strings)) def output_shortest_and_longest(max_number, comp_serial): current_smallest_number = 1 current_largest_number = max_number shortest_lis = NodeLink() longest_lis = NodeLink() latest_gt_node = shortest_lis.head latest_lt_node = longest_lis.head shortest_lis.append(shortest_lis.tail, current_largest_number) longest_lis.append(longest_lis.tail, current_smallest_number) for ch in comp_serial: current_smallest_number += 1 current_largest_number -= 1 if ch == "<": latest_lt_node = longest_lis.tail shortest_lis.append(latest_gt_node, current_largest_number) longest_lis.append(longest_lis.tail, current_smallest_number) else: latest_gt_node = shortest_lis.tail shortest_lis.append(shortest_lis.tail, current_largest_number) longest_lis.append(latest_lt_node, current_smallest_number) shortest_lis.print() longest_lis.print() def solve(): import time left_cases = None for line in sys.stdin: if left_cases is None: left_cases = int(line.strip()) else: vals = line.split() output_shortest_and_longest(int(vals[0]), vals[1]) left_cases -= 1 if left_cases == 0: break solve()
IMPORT IMPORT CLASS_DEF FUNC_DEF VAR VAR NONE VAR VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF IMPORT ASSIGN VAR NONE FOR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR
Gildong recently learned how to find the longest increasing subsequence (LIS) in $O(n\log{n})$ time for a sequence of length $n$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $n$ distinct integers between $1$ and $n$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $n-1$, consisting of characters '<' and '>' only. The $i$-th (1-indexed) character is the comparison result between the $i$-th element and the $i+1$-st element of the sequence. If the $i$-th character of the string is '<', then the $i$-th element of the sequence is less than the $i+1$-st element. If the $i$-th character of the string is '>', then the $i$-th element of the sequence is greater than the $i+1$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $n$ distinct integers between $1$ and $n$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. -----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$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $n$ ($2 \le n \le 2 \cdot 10^5$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $n-1$. It is guaranteed that the sum of all $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines with $n$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $1$ and $n$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. -----Example----- Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 -----Note----- In the first case, $1$ $2$ $3$ is the only possible answer. In the second case, the shortest length of the LIS is $2$, and the longest length of the LIS is $3$. In the example of the maximum LIS sequence, $4$ '$3$' $1$ $7$ '$5$' $2$ '$6$' can be one of the possible LIS.
from sys import stderr, stdin def rl(): return [int(w) for w in stdin.readline().split()] def p1(n, s, le): r = [] b = i = 0 while i < n: try: ni = s.index(le, i) + 1 except ValueError: ni = n r += list(range(ni, i, -1)) i = ni return r (t,) = rl() for _ in range(t): n, s = stdin.readline().split() n = int(n) print(*(n - x + 1 for x in p1(n, s, ">"))) print(*p1(n, s, "<"))
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively. He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$. Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$. Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible. Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal. -----Input----- The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β€” the length of the first array. The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β€” elements of the array $A$. The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β€” the length of the second array. The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$. -----Output----- Print a single integer β€” the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal. If there is no way to make array equal, print "-1". -----Examples----- Input 5 11 2 3 5 7 4 11 7 3 7 Output 3 Input 2 1 2 1 100 Output -1 Input 3 1 2 3 3 1 2 3 Output 3
def main(): length1 = int(input()) store = input().split() arr1 = [] for x in range(length1): arr1.append(int(store[x])) length2 = int(input()) store = input().split() arr2 = [] for x in range(length2): arr2.append(int(store[x])) pos1 = 0 pos2 = 0 tot1 = 0 tot2 = 0 bo = False count = 0 choose_1 = True while True: if choose_1: if pos1 == length1: break tot1 += arr1[pos1] pos1 += 1 else: if pos2 == length2: break tot2 += arr2[pos2] pos2 += 1 if tot1 > tot2: choose_1 = False elif tot1 < tot2: choose_1 = True if tot1 == tot2: count += 1 tot1 = 0 tot2 = 0 if pos1 == length1 and pos2 == length2: bo = True break if bo: print(count) else: print(-1) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR