description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): n = int(input()) if n == 2: print("NO") elif n % 2 == 0: o = [] e = [] print("YES") for i in range(1, n + 1): if i % 2 != 0: o.append(i) else: e.append(i) x = o.pop() print(x, end=" ") for i in o: print(i, end=" ") for i in e[::-1]: print(i, end=" ") print() else: o = [] e = [] print("YES") for i in range(1, n + 1): if i % 2 != 0: o.append(i) else: e.append(i) for i in o: print(i, end=" ") for i in e[::-1]: print(i, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for i in range(t): n = int(input()) if n % 2 == 0: if n < 3: print("NO") elif n == 4: print("YES") ar = [] ar = [2, 1, 4, 3] print(*ar) else: print("YES") ar = [] a = n // 2 - 2 j = 1 while j < a + 1: ar.append(j) j += 1 ar.append(n) ar.append(j + 1) j = j + 1 ar.append(j - 1) ar.append(n - 1) a = j j = n - 2 while j > a: ar.append(j) j -= 1 print(*ar) else: print("YES") ar = [] j = 1 while j < n + 1: ar.append(j) j += 1 j += 1 j = n - 1 while j > 1: ar.append(j) j -= 1 j -= 1 print(*ar)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for i in range(int(input())): n = int(input()) half = n // 2 if n == 2: print("NO") else: print("YES") if n % 2 == 0: ans = [] for i in range(2, half + 1): ans.append(i) ans.append(1) for i in range(n, half, -1): ans.append(i) else: ans = [None for i in range(n)] ans[half] = n inc_dec = 1 counter = 1 while inc_dec <= half: if counter % 2 == 0: ans[half - inc_dec] = n - counter inc_dec += 1 else: ans[half + inc_dec] = n - counter counter += 1 print(" ".join(str(x) for x in ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): n = int(input()) if n == 2: print("NO") else: print("YES") c = n // 2 d = 1 if n % 2 else 2 for i in range(d, c + 1): print(i, end=" ") if n % 2 == 0: print(1, end=" ") for i in range(n, c, -1): print(i, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): N = int(input()) if N == 1: print("YES") print(1) elif N == 2: print("NO") elif N % 2 != 0: print("YES") print(*(list(range(N, (N + 1) // 2, -1)) + list(range(1, (N + 3) // 2)))) else: print("YES") l = [1] l.append(N) print(*(list(range(N - 1, N // 2, -1)) + l + list(range(2, N // 2 + 1))))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for i in range(t): n = int(input()) if n == 2: print("NO") else: print("YES") k = n // 2 if n % 2 != 0: for i in range(1, k + 1): print(i, end=" ") while n != k: print(n, end=" ") n -= 1 else: for i in range(2, k + 1): print(i, end=" ") print(1, end=" ") while n != k: print(n, end=" ") n -= 1 print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING WHILE VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING WHILE VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
def solve(n): if n == 2: return "NO" if n == 3: return "YES\n1 3 2" left = [2, 4] if n % 2 == 0: right = [3, 1] for _ in range(2, n // 2): left.append(left[-1] + 2) right.append(5 if right[-1] == 1 else right[-1] + 2) else: right = [1, 3] for _ in range(2, n // 2): left.append(left[-1] + 2) right.append(right[-1] + 2) right.reverse() middle = [] if n % 2 == 1: middle = [n] return "YES\n" + " ".join(map(str, left + middle + right)) for _ in range(int(input())): n = int(input()) print(solve(n))
FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING ASSIGN VAR LIST NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR RETURN BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
from sys import stdin, stdout nmbr = lambda: int(input()) lst = lambda: list(map(int, input().split())) for _ in range(nmbr()): n = nmbr() if n == 2: print("NO") else: a = [i for i in range(1, n + 1)] pos = n // 2 a = a[:pos] + a[pos:][::-1] if n & 1 == 0: a[pos - 1], a[pos - 2] = a[pos - 2], a[pos - 1] print("YES") print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): n = int(input()) if n <= 2: print("NO") elif n % 2 != 0: even = [] odd = [] for i in range(1, n + 1): if i % 2 == 0: even.append(i) else: odd.append(i) print("YES") print(*odd, *even[::-1]) else: l1 = list(range(1, n // 2 + 1)) l3 = list(range(n // 2 + 1, n + 1)) l2 = l3[::-1] temp = l2[0] + l2[1] l2[0] = temp - l2[0] l2[1] = temp - l2[1] print("YES") print(*l2, *l1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): n = int(input()) if n == 2: print("NO") elif n == 4: print("YES") print(3, 1, 4, 2, sep=" ") elif n % 2 == 0: print("YES") print(1, end=" ") print(n - 1, n, sep=" ", end=" ") s1, s2 = 2, n - 2 while s1 <= n // 2: print(s1, end=" ") if s2 > n // 2: print(s2, end=" ") s1 += 1 s2 -= 1 print() else: print("YES") s1, s2 = n, 1 while s2 < s1: print(s1, s2, sep=" ", end=" ") s1 -= 1 s2 += 1 print(s1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for zz in range(t): n = int(input()) p = n // 2 l = [] l1 = [] if n == 2: print("NO") continue if n % 2 == 0: x = n // 2 print("YES") print(x, end=" ") for j in range(1, x): print(j, end=" ") for j in range(n, x, -1): print(j, end=" ") print() else: print("YES") for i in range(1, n + 1): if i <= p: l.append(i) else: l1.append(i) print(*(l + l1[::-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
def solve(): n = int(input()) l = [] if n <= 2: print("NO") return print("YES") if n & 1: for i in range(1, n // 2 + 1): l.append(i) for i in range(n, n // 2, -1): l.append(i) print(*l) return for i in range(2, n // 2 + 1): l.append(i) l.append(1) for i in range(n, n // 2, -1): l.append(i) print(*l) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER 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 RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
def front(j, strng): for i in range(1, j): strng += str(i) + " " return strng def back(n, j, strng): for i in range(n, j - 1, -1): strng += str(i) + " " return strng for _ in range(int(input())): n = int(input()) if n == 2: print("NO") continue print("YES") if n % 2 != 0: j = n // 2 + 1 strng = "" temp = front(j, strng) temp = back(n, j, temp) print(temp) else: strng = "" strng += str(n // 2) + " " j = n // 2 temp = front(j, strng) temp = back(n, j + 1, temp) print(temp)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR STRING RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
def xxx(N): if N % 2 == 0: k = N // 2 for i in range(1, k): print(2 * i + 1, end=" ") print(1, end=" ") for i in range(k, 0, -1): print(2 * i, end=" ") print("") else: k = (N - 1) // 2 print(1, end=" ") for i in range(1, k + 1): print(2 * i + 1, end=" ") for i in range(k, 0, -1): print(2 * i, end=" ") print("") for _ in range(int(input())): N = int(input()) if N == 2: print("NO") else: print("YES") xxx(N)
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): n = int(input()) if n == 1: print("YES") print(1) elif n % 2 != 0: print("YES") l = list(range(1, n + 1)) l = l[: n // 2] + l[-1 : n // 2 - 1 : -1] print(*l) elif n != 2: print("YES") l = list(range(1, n + 1)) ans = [l[n // 2 - 1]] ans += l[: n // 2 - 1] + l[-1 : n // 2 - 1 : -1] print(*ans) else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): m = int(input()) if m % 2: print("YES") for i in range(1, m // 2 + 1): print(i, end=" ") print(m, end=" ") for i in range(m - 1, m // 2, -1): print(i, end=" ") print() elif m != 2: print("YES") print(m // 2, end=" ") for i in range(1, m // 2): print(i, end=" ") for i in range(m, m // 2, -1): print(i, end=" ") print() else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): k = int(input()) if k == 1: print("YES") print(1) continue if k == 2: print("NO") continue print("YES") if k % 2 == 1: a = [i for i in range(1, k + 1, 2)] b = [i for i in range(2, k, 2)] print(*(a + b[::-1])) else: a = [i for i in range(k // 2 + 1, k + 1)] a = a + [1] b = [i for i in range(2, k // 2 + 1)] print(*(a + b[::-1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): n = int(input()) if n > 2: print("YES") ans3 = [1, 3, 2] ans4 = [2, 1, 4, 3] odd = [] even = [] if n & 1: for i in range(4, n + 1): if i & 1: odd.append(i) else: even.append(i) even = even[::-1] print(*(even + ans3 + odd)) else: for i in range(5, n + 1): if i & 1: odd.append(i) else: even.append(i) odd = odd[::-1] print(*(odd + ans4 + even)) else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for _ in range(t): n = int(input()) if n % 2: print("YES") l = list( map(str, list(range(1, int(n / 2) + 1)) + list(range(n, int(n / 2), -1))) ) print(" ".join(l)) elif n != 2: print("YES") l = ( [int(n / 2)] + [i for i in range(1, int(n / 2))] + [i for i in range(n, int(n / 2), -1)] ) l = list(map(str, l)) print(" ".join(l)) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
T = int(input()) for _ in range(T): n = int(input()) if n == 2: print("NO") elif n % 2 != 0: print("YES") for i in range(1, int(n / 2) + 1): print(i, end=" ") for i in range(n, int(n / 2), -1): print(i, end=" ") print("\n") else: print("YES") print(int(n / 2), end=" ") for i in range(1, int(n / 2)): print(i, end=" ") for i in range(n, int(n / 2), -1): print(i, end=" ") print("\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
T = int(input()) for i in range(T): N = int(input()) if N == 2: print("NO") else: if N % 2 == 0: K = N - 1 else: K = N l = list(range(1, K + 1)) a = l[0 : int(K / 2)] b = l[int(K / 2) :] b = b[::-1] c = a + b print("YES") if N % 2 == 0: d = [c[-1]] + [N] + c[0:-1] c = d print(*c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER LIST VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) while t > 0: n = int(input()) if n == 2: print("NO") else: print("YES") if n % 2 == 1: ans = [i for i in range(1, n // 2 + 1)] for i in range(n, n // 2, -1): ans.append(i) else: ans = [i for i in range(2, n // 2 + 1)] ans.append(1) for i in range(n, n // 2, -1): ans.append(i) for num in ans: print(num, end=" ") print("\n") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for i in range(t): n = int(input()) l = [] for i in range(1, n + 1): l.append(i) if n == 2: print("NO") else: if n % 2 != 0: mid = n // 2 + 1 x = l[: mid - 1] + l[mid - 1 :][::-1] elif n % 2 == 0: mid = n // 2 x = l[1:mid] + [1, n] + l[mid : n - 1][::-1] print("YES") for i in range(n): print(x[i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR LIST NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) def solve(n): ans = [] if n & 1 == 1: i = 1 while i < n // 2 + 1: ans.append(i) i += 1 i = n while i > n // 2: ans.append(i) i -= 1 else: i = 1 ans.append(n // 2) while i < n // 2: ans.append(i) i += 1 i = n while i > n // 2: ans.append(i) i -= 1 return ans for _ in range(t): x = int(input()) if x == 2: print("NO") continue ans = solve(x) print("YES") for i in ans: print(i, end=" ") print("\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): n = int(input()) if n == 2: print("NO") elif n == 4: print("YES") ans = [3, 4, 1, 2] print(*ans) elif n % 2: ans = [] for i in range(n // 2 + 1, n + 1): ans.append(i) for i in range(n // 2, 0, -1): ans.append(i) print("YES") print(*ans) else: ans = [3] for i in range(n - 1, 4, -2): ans.append(i) ans.append(2) ans.append(1) for j in range(6, n + 1, 2): ans.append(j) ans.append(4) print("YES") print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): z = int(input()) if z > 2: if z % 2 == 0: m = [] o = [] h = [1] for i in range(2, z + 1): if i % 2 == 0: m.append(i) else: o.append(i) l = m + h + o[::-1] else: m = [] o = [] h = [z] for i in range(1, z): if i % 2 == 0: m.append(i) else: o.append(i) l = o + h + m[::-1] print("YES\n" + " ".join([str(o) for o in l])) elif z == 2: print("NO") elif z == 1: print("YES") print("5")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for i in range(t): n = int(input()) if n == 2: print("NO") else: print("YES") if n % 2 == 1: for i in range(n // 2): print(i + 1, end=" ") for i in range(n // 2 + 1): print(n - i, end=" ") else: for i in range(n // 2 - 2): print(i + 1, end=" ") print(str(n // 2) + " " + str(n // 2 - 1), end=" ") for i in range(n // 2): print(n - i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
try: t = int(input()) foundation = [] while t: n = int(input()) if n > 2: print("YES") g = n g = g - 1 if n % 2 == 0: while g != 3: print(g, end=" ") g = g - 2 print("2 1 4 3", end=" ") g = 6 while g != n + 2: print(g, end=" ") g = g + 2 print() else: while g != 2: print(g, end=" ") g = g - 2 print("1 3 2", end=" ") g = 5 while g != n + 2: print(g, end=" ") g = g + 2 print() else: print("NO") t = t - 1 except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
import sys input = lambda: sys.stdin.readline().rstrip() for _ in range(int(input())): n = int(input()) if n == 2: print("NO") continue print("YES") a = [0] * n if n & 1: i, v = 0, 1 while i < n: a[i] = v v += 1 i += 2 i, v = 1, n while i < n: a[i] = v v -= 1 i += 2 else: i, v = 0, 1 while i < n: a[i] = v i += 2 v += 1 i, v = 1, n while i < n: a[i] = v v -= 1 i += 2 a[0], a[2] = a[2], a[0] print(*a)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) def fun(lt, n): temp = [] if n % 2 != 0: for i in range(0, n // 2): temp.append(lt[i]) temp.append(lt.pop()) for i in range(n - 2, n // 2 - 1, -1): temp.append(lt[i]) for j in temp: print(j, end=" ") print(end="\n") elif n % 2 == 0: print(n // 2, end=" ") for i in range(1, n // 2): print(i, end=" ") for i in range(n, n // 2, -1): print(i, end=" ") print(end="\n") for i in range(t): n = int(input()) if n == 2: print("NO") else: lt = [j for j in range(1, 1 + n)] print("YES") fun(lt, n)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
def func(): n = int(input()) if n == 2: print("NO") elif n % 2: print("YES") for i in range(1, n // 2 + 1): print(i, end=" ") for i in range(n, n // 2, -1): print(i, end=" ") print() else: print("YES") print(n // 2, end=" ") for i in range(1, n // 2): print(i, end=" ") for i in range(n, n // 2, -1): print(i, end=" ") print() t = int(input()) for i in range(t): func()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
import itertools for _ in range(int(input())): n = int(input()) if n == 2: print("NO") else: print("YES") if n % 2 == 1: ans = [(i + 1) for i in range(n // 2)] ans += [i for i in range(n, n // 2, -1)] print(*ans) elif n == 4: print("2 1 4 3") else: ans = ( [n // 2] + [i for i in range(1, n // 2)] + [i for i in range(n, n // 2, -1)] ) print(*ans)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) test = [] for i in range(t): v = int(input()) test.append(v) for i in test: if i == 2: print("NO") continue print("YES") v = i // 2 ls = [] if i % 2 == 1: v += 1 for j in range(v, 0, -1): ls.append(str(j)) for j in range(v + 1, i + 1): ls.append(str(j)) if i % 2 == 0: temp = ls[len(ls) - 1] ls[len(ls) - 1] = ls[len(ls) - 2] ls[len(ls) - 2] = temp print(" ".join(ls))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for i in range(t): n = int(input()) if n % 2 == 0: if n == 2: print("NO") elif n == 4: print("YES") print(" ".join(["3", "4", "1", "2"])) else: print("YES") print( " ".join( ["1"] + [str(i) for i in range(n // 2 + 2, n + 1)] + [str(i) for i in range(n // 2 + 1, 3, -1)] + ["2", "3"] ) ) else: print("YES") print( " ".join([str(i) for i in range(1, n + 1, 2)]) + " " + " ".join([str(i) for i in range(n - 1, 1, -2)]) )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP BIN_OP LIST STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER LIST STRING STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for test in range(t): n = int(input()) if n == 2: print("NO") continue elif n % 2 == 1: ans = [0] * n i = 1 j = 0 k = n - 1 while j <= k: if j == k: ans[j] = i i += 1 break ans[j] = i i += 1 ans[k] = i i += 1 j += 1 k -= 1 print("YES") print(*ans) else: ans = [0] * n val = n // 2 i = 2 j = 0 while i != val + 1: ans[j] = i i += 1 j += 1 ans[val - 1] = 1 j += 1 i = n while j < n: ans[j] = i i -= 1 j += 1 print("YES") print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
t = int(input()) for i in range(t): t1 = t2 = 0 l3 = [1, 3, 2] l4 = [2, 1, 4, 3] n = int(input()) if n == 2: print("NO") elif n % 2 == 0: t1 = n - 4 k1 = 4 l = l4 while t1 > 0: l.append(k1 + 1) l.insert(0, k1 + 2) k1 = k1 + 2 t1 = t1 - 2 print("YES") for j in l: print(j, sep=" ", end=" ") print() else: t2 = n - 3 k = 3 l = l3 while t2 > 0: l.append(k + 1) l.insert(0, k + 2) k = k + 2 t2 = t2 - 2 print("YES") for j in l: print(j, sep=" ", end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
n = int(input()) for i in range(n): n1 = int(input()) list1 = [] count = 1 if n1 == 2: print("NO") else: print("YES") i = 0 j = n1 - 1 san = True for k in range(n1): list1.insert(k, 0) while i <= j: if san: list1[i] = count i = i + 1 san = False else: list1[j] = count j = j - 1 san = True count = count + 1 if n1 % 2 == 0: print(list1[n1 // 2 - 1], end=" ") for i in list1: if i == list1[n1 // 2 - 1]: continue print(i, end=" ") else: for i in list1: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING FOR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for i in range(int(input())): n = int(input()) if n % 2 == 1: print("YES") l = [i for i in range(1, n + 1)] x = [0] * n a = 0 b = n - 1 f = 0 while f < n: x[a] = l[f] f += 1 if f < n: x[b] = l[f] f += 1 a += 1 b -= 1 for i in x: print(i, end=" ") print() elif n != 2: print("YES") print(n // 2, end=" ") for i in range(1, n // 2): print(i, end=" ") for i in range(n, n // 2, -1): print(i, end=" ") print() else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): num = int(input()) if num == 2: print("NO") continue if num == 4: print("YES") print(2, 4, 1, 3) continue print("YES") if num % 2 == 0: for i in range(1, num - 1, 2): print(i, end=" ") print(num, num - 1, end=" ") for i in range(num - 2, 4, -2): print(i, end=" ") print(2, 4) else: for i in range(1, num // 2 + 1): print(i, end=" ") for i in range(num, num // 2, -1): print(i, end=" ") print("")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
try: for _ in range(int(input())): n = int(input()) if n % 2 == 0: array = [k for k in range(n // 2 + 1, n + 1)] for ll in range(n // 2 - 1, 0, -1): array.append(ll) array.append(n // 2) ans = "YES" if n != 2 else "NO" print(ans) if ans == "YES": print(*array) else: if n == 1: print("YES") print(1) continue elif n == 3: print("YES") print("1 3 2") continue cc = n // 2 array = [1] b = [i for i in range(2, n // 2 + 1)] b = b[::-1] c = [j for j in range(n // 2 + 2, n)] array += c array.append(n) array += b array.append(n // 2 + 1) array[0] = array[-1] array[-1] = 1 print("YES") print(*array) except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
from sys import stdin, stdout t = int(stdin.readline().rstrip()) while t > 0: n = int(stdin.readline().rstrip()) if n == 2: print("NO") elif n % 2 == 0: print("YES") array_odd = [] for i in range(1, n): if i % 2 == 0: array_odd.append(i) else: array_odd.insert(0, i) array_odd.insert(n // 2, n) print(*array_odd) else: array = list(range(1, n + 1)) array1 = array[: n // 2] array2 = array[n // 2 :][::-1] print("YES") array = array1 + array2 print(*array) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for t in range(int(input())): N = int(input()) if N == 2: print("NO") continue print("YES") ArraySoham = [] for i in range(1, N // 2 + 1): ArraySoham.append(i) for i in range(N, N // 2, -1): ArraySoham.append(i) if N % 2 == 0: ArraySoham[(N - 1) // 2], ArraySoham[(N - 1) // 2 - 1] = ( ArraySoham[(N - 1) // 2 - 1], ArraySoham[(N - 1) // 2], ) print(*ArraySoham)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
for _ in range(int(input())): n = int(input()) if n <= 2: print("NO") else: print("YES") even_series = [2, 1, 4, 3] odd_series = [1, 3, 2] if n % 2 == 0: for i in range(5, n + 1, 2): even_series.append(i + 1) even_series.insert(0, i) print(*even_series) else: for i in range(4, n + 1, 2): odd_series.append(i + 1) odd_series.insert(0, i) print(*odd_series)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
For a permutation P of length N, we define L(P) to be the length of the longest increasing subsequence in P. That is, L(P) is the largest integer K such that there exist indices i_{1} < i_{2} < \ldots < i_{K} such that P_{i_{1}} < P_{i_{2}} < \ldots < P_{i_{K}}. Define P^{R} to be the permutation (P_{N}, P_{N-1}, \ldots, P_{1}). You are given a positive integer N. You need to output a permutation P of length N such that L(P) = L(P^{R}), or say that none exist. Note: P is said to be a permutation of length N if P is a sequence of length N consisting of N distinct integers between 1 and N. For example, (3, 1, 2) is a permutation of length 3, but (1, 4, 2), (2, 2, 3) and (2, 1) are not. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of a single line containing one integer N β€” the length of the permutation to be constructed. ------ Output Format ------ For each test case, output on a new line "YES" if there exists a valid permutation, and "NO" if there doesn't. If you outputted "YES", on the next line, output a valid permutation P as N space-separated integers, the i^{th} of which is P_{i}. You can print each letter of the string in any case (upper or lower) (for instance, strings YES, yEs, and yes will be considered identical). ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 2 \cdot 10^{5}$ - The sum of $N$ across all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 2 2 3 ----- Sample Output 1 ------ NO YES 1 3 2 ----- explanation 1 ------ Test Case $1$: There are two permutations of length $2$ β€” $(1, 2)$ and $(2, 1)$. The length of the LIS of $(1, 2)$ is $2$ and the length of the LIS of $(2, 1)$ is $1$. Since these permutations are reverses of each other and have unequal LIS lengths, there is no valid permutation of length $2$. Test Case $2$: The length of the LIS of $(1, 3, 2)$ is $2$, and the length of the LIS of its reverse, $(2, 3, 1)$, is also $2$. Therefore, this is a valid permutation of length $3$.
T = int(input()) for i in range(T): n = int(input()) if n <= 2: print("NO", end="") else: print("YES") if n % 2 != 0: for x in range(1, n // 2 + 1): print(x, end=" ") for t in range(n, n // 2, -1): print(t, end=" ") else: print(n - 1, end=" ") for x in range(1, n - 2, 2): print(x, end=" ") for t in range(n, 1, -2): print(t, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) a = [0] for i in range(n): a.append(list(input())) x = int(input()) b = list(map(int, input().split())) if x == 2: print(2) print(*b) exit() st = [b[0]] st1 = [b[0]] k = b[1] for i in range(1, n + 1): a[i][i - 1] = "1" pr = [] pr.append(b[0]) for j in range(2, x): for l in st1: if a[l][b[j] - 1] == "1": st.append(k) st = [k] st1 = [k] pr.append(k) k = b[j] break else: st1.append(b[j]) k = b[j] pr.append(b[-1]) print(len(pr)) print(*pr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys input = sys.stdin.readline def warshall_floyd(d): for k in range(n): for i in range(n): for j in range(n): d[i][j] = min(d[i][j], d[i][k] + d[k][j]) return d n = int(input()) A = [input()[:-1] for _ in range(n)] m = int(input()) if m == 2: p = list(map(int, input().split())) print(2) print(*p) exit() p = list(map(lambda x: x - 1, map(int, input().split()))) d = [([10**18] * n) for _ in range(n)] for i in range(n): d[i][i] = 0 for i in range(n): for j in range(n): if A[i][j] == "1": d[i][j] = 1 d = warshall_floyd(d) s = 0 t = 2 ans = [p[s]] while t < m: if t - s > d[p[s]][p[t]]: ans.append(p[t - 1]) s = t - 1 t += 1 ans.append(p[-1]) ans = list(map(lambda x: x + 1, ans)) print(len(ans)) print(*ans)
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
def warshall_floyd(d): for k in range(n): for i in range(n): for j in range(n): d[i][j] = min(d[i][j], d[i][k] + d[k][j]) return d n = int(input()) g = [list(input()) for _ in range(n)] d = [([float("inf")] * n) for _ in range(n)] for i in range(n): for j in range(n): if g[i][j] == "1": d[i][j] = 1 warshall_floyd(d) m = int(input()) arr = list(map(int, input().split())) for i in range(m): arr[i] -= 1 ans = [arr[0]] pos1 = 0 pos2 = 1 pos3 = 2 for i in range(m - 2): u, w, v = arr[pos1], arr[pos2], arr[pos3] if u == v or d[u][w] + d[w][v] > d[u][v]: ans.append(w) pos1 = pos2 pos2 = pos1 + 1 pos3 = pos2 + 1 else: pos2 += 1 pos3 += 1 ans.append(arr[-1]) l = len(ans) for i in range(l): ans[i] += 1 print(l) print(*ans)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
def floyd_warshall(graph): n = len(graph) dist = [[float("inf") for _ in range(n)] for _ in range(n)] for i in range(n): for j in range(n): if i == j: dist[i][j] = 0 elif graph[i][j]: dist[i][j] = 1 for thru in range(n): for i in range(n): for j in range(n): candidate = dist[i][thru] + dist[thru][j] if candidate < dist[i][j]: dist[i][j] = candidate return dist def solve(graph, seq): dist = floyd_warshall(graph) result = [seq[0]] count = 0 for i, current in enumerate(seq): if dist[result[-1] - 1][current - 1] < count: result.append(seq[i - 1]) count = 2 else: count += 1 result.append(seq[-1]) return result n = int(input()) graph = [[(False) for _ in range(n)] for _ in range(n)] for i in range(n): row = input() for j in range(n): graph[i][j] = int(row[j]) m = int(input()) seq = list(map(int, input().split()))[:m] solution = solve(graph, seq) print(len(solution)) print(" ".join([str(x) for x in solution]))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys n = int(sys.stdin.readline().strip()) A = [] for i in range(0, n): A.append(sys.stdin.readline().strip()) D = [[(-200) for i in range(0, n)] for j in range(0, n)] L = [] L2 = [] for i in range(0, n): D[i][i] = 0 L.append([i, i]) for i in range(0, n): while len(L) > 0: x, y = L.pop() for j in range(0, n): if A[y][j] == "1" and D[x][j] == -200: D[x][j] = i + 1 L2.append([x, j]) L = L2[:] L2 = [] m = int(sys.stdin.readline().strip()) p = list(map(int, sys.stdin.readline().strip().split())) k = 1 ans = [] i = 0 j = 1 while j < m - 1: if D[p[i] - 1][p[j + 1] - 1] == j + 1 - i: j = j + 1 else: ans.append(p[i]) i = j j = j + 1 ans.append(p[i]) if i != m - 1: ans.append(p[m - 1]) print(len(ans)) print(" ".join(list(map(str, ans))))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) G = [[] for _ in range(n)] for i in range(n): l = input() for j in range(n): if l[j] == "1": G[i].append(j) ds = [([-1] * n) for _ in range(n)] for i in range(n): ds[i][i] = 0 l = [i] while len(l) > 0: l2 = [] for j in l: for k in G[j]: if ds[i][k] == -1: ds[i][k] = ds[i][j] + 1 l2.append(k) l = l2 m = int(input()) p = list(map(int, input().split())) res = 2 vs = [str(p[0])] i, j = 0, 1 while j < m: if ds[p[i] - 1][p[j] - 1] < j - i: res += 1 i = j - 1 vs.append(str(p[j - 1])) j += 1 vs.append(str(p[-1])) print(res) print(" ".join(vs))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR 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 LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) dist = [] for i in range(n): s = input() tem = [] for j in s: if j == "0": tem.append(101) else: tem.append(1) dist.append(tem) m = int(input()) p = list(map(int, input().split())) for k in range(0, n): for i in range(0, n): for j in range(0, n): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) ind = [1] * m count = 0 t = 0 for i in range(1, m - 1): if dist[p[t] - 1][p[i + 1] - 1] == i - t + 1 and p[t] != p[i + 1]: ind[i] = 0 count += 1 else: t = i print(m - count) for i in range(m): if ind[i] == 1: print(p[i], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys def input(): return sys.stdin.readline()[:-1] n = int(sys.stdin.readline()) s = [input() for _ in range(n)] d = [([pow(10, 30)] * n) for i in range(n)] for i in range(n): for j in range(n): if s[i][j] == "1": d[i][j] = 1 for k in range(n): for i in range(n): for j in range(n): d[i][j] = min(d[i][j], d[i][k] + d[k][j]) m = int(sys.stdin.readline()) p = list(map(int, sys.stdin.readline().split())) ans = [p[0]] cur = 1 while cur < m - 1: if ans[-1] == p[cur + 1]: ans.append(p[cur]) elif ( d[ans[-1] - 1][p[cur] - 1] + d[p[cur] - 1][p[cur + 1] - 1] > d[ans[-1] - 1][p[cur + 1] - 1] ): ans.append(p[cur]) cur += 1 ans.append(p[-1]) print(len(ans)) print(*ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) a = [] t = 0 b = 0 for i in range(n): t = input() b = [] for j in range(n): b.append(int(t[j])) if i != j and t[j] == "0": b[j] = 10**9 a.append(b) for k in range(n): for i in range(n): for j in range(n): a[i][j] = min(a[i][j], a[i][k] + a[k][j]) m = int(input()) p = list(map(int, input().split())) i = 1 maps = [p[0]] while i < m - 1: if a[maps[-1] - 1][p[i] - 1] + 1 != a[maps[-1] - 1][p[i + 1] - 1]: maps.append(p[i]) i += 1 maps.append(p[m - 1]) print(len(maps)) print(*maps)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR 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 LIST VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys def main(): def input(): return sys.stdin.readline()[:-1] n = int(input()) G = [""] * n for k in range(n): G[k] = input() D = [([float("inf")] * n) for k in range(n)] for k in range(n): D[k][k] = 0 for k in range(n): for m in range(n): if G[k][m] == "1": D[k][m] = 1 for k in range(n): for l in range(n): for m in range(n): D[l][m] = min(D[l][m], D[l][k] + D[k][m]) m = int(input()) p = list(map(int, input().split())) s = 0 e = 1 kyori = 1 ans = [p[0]] while e < m: if D[p[s] - 1][p[e] - 1] < kyori: ans += [p[e - 1]] kyori = 1 s = e - 1 e = s + 1 else: kyori += 1 e += 1 ans += [p[-1]] print(len(ans)) print(*ans) main()
IMPORT FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR LIST VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
MOD = 10**9 + 7 I = lambda: list(map(int, list(input()))) n = int(input()) g = [] for i in range(n): g.append(I()) m = int(input()) p = [(int(i) - 1) for i in input().split()] d = g[:] for i in range(n): for j in range(n): if not d[i][j] and i != j: d[i][j] = 1000 for i in range(n): for j in range(n): for k in range(n): d[j][k] = min(d[j][k], d[j][i] + d[i][k]) ans = [1] * m if m == 2: print(len(p)) print(*[(i + 1) for i in p]) exit() f = m - 3 l = m - 1 while f >= 0 and f + 1 < l: k = d[p[f]][p[l]] if k == l - f: ans[f + 1] = 0 f -= 1 else: l -= 1 while f + 1 == l or ans[l] == 0: if f + 1 == l: f -= 1 else: l -= 1 print(sum(ans)) for i in range(m): if ans[i]: print(p[i] + 1, end=" ")
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
def bfs(s): q = [s] visited[s] = True while q: s = q.pop(0) for i in range(n): if adj[s][i] == "1": if not visited[i]: paths[j][i] = paths[j][s] + 1 visited[i] = True q.append(i) n = int(input()) adj = [None] * n for i in range(n): adj[i] = input() m = int(input()) p = [int(o) for o in input().split()] paths = [dict() for i in range(n)] for j in range(n): visited = [False] * n paths[j][j] = 0 bfs(j) stack = [p[0] - 1] ans = [p[0]] cur = 1 while cur < m - 1: if ans[-1] == p[cur + 1]: ans.append(p[cur]) elif ( paths[ans[-1] - 1][p[cur] - 1] + paths[p[cur] - 1][p[cur + 1] - 1] > paths[ans[-1] - 1][p[cur + 1] - 1] ): ans.append(p[cur]) cur += 1 ans.append(p[-1]) print(len(ans)) print(*ans)
FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
def path(s): s1 = {s} s2 = set() used = [0] * (n + 1) used[s] = 1 ans = 0 while s1: ans += 1 for v in s1: for u in graph[v]: if used[u] == 0: paths[s][u] = ans used[u] = 1 s2.add(u) s1 = s2 s2 = set() n = int(input()) A = [list(map(int, input())) for _ in range(n)] graph = {i: set() for i in range(1, n + 1)} for i in range(n): for j in range(n): if A[i][j]: graph[i + 1].add(j + 1) m = int(input()) P = list(map(int, input().split())) paths = [([0] * (n + 1)) for _ in range(n + 1)] for i in range(1, n + 1): path(i) u = 0 v = P[0] ans = [] while u < m - 1: ans.append(P[u]) l = u r = min(m, u + n + 1) while r - l > 1: mid = (l + r) // 2 if paths[P[u]][P[mid]] == mid - u: l = mid else: r = mid u = l ans.append(P[-1]) print(len(ans)) print(*ans)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) a = [] for i in range(n): a.append(list(map(int, list(input())))) m = int(input()) b = list(map(int, input().split())) for i in range(n): for j in range(n): if i != j and a[i][j] == 0: a[i][j] = 10**18 for k in range(n): for i in range(n): for j in range(n): a[i][j] = min(a[i][j], a[i][k] + a[k][j]) ans = [] for i in range(m): if i == 0 or i == m - 1: ans.append(b[i]) continue x = ans[-1] - 1 if a[x][b[i] - 1] + a[b[i] - 1][b[i + 1] - 1] == a[x][b[i + 1] - 1]: continue ans.append(b[i]) print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys input = sys.stdin.readline def main(): N = int(input()) graph = [[int(a) for a in list(input().rstrip())] for _ in range(N)] M = int(input()) P = list(map(int, input().split())) INF = 10**16 dp = [([None] * N) for _ in range(N)] for i in range(N): for j in range(N): if i == j: dp[i][j] = 0 elif graph[i][j] == 0: dp[i][j] = INF else: dp[i][j] = 1 for k in range(N): for i in range(N): for j in range(N): dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]) ans = [P[0]] last = 0 for i in range(1, M): p0, p1 = P[i - 1], P[i] if dp[ans[-1] - 1][p1 - 1] < i + 1 - last: ans.append(p0) last = i ans.append(P[M - 1]) ans = ans[1:] print(len(ans)) print(" ".join([str(a) for a in ans])) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
N = int(input()) E = [([N**2] * N) for i in range(N)] for i in range(N): (*X,) = map(int, input()) for j in range(N): if X[j]: E[i][j] = 1 E[i][i] = 0 for k in range(N): for i in range(N): for j in range(N): E[i][j] = min(E[i][j], E[i][k] + E[k][j]) M = int(input()) (*S,) = map(int, input().split()) v = S[0] - 1 prv = 0 L = [v + 1] for i in range(1, M): w = S[i] - 1 if i - prv != E[v][w]: v = S[i - 1] - 1 prv = i - 1 L.append(v + 1) L.append(S[-1]) print(len(L)) print(*L)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) l = [] p = [] ans = [] for i in range(n): l.append(list(map(int, input()))) for i in range(n): for j in range(n): if i != j and l[i][j] == 0: l[i][j] = 9999999 for k in range(n): for i in range(n): for j in range(n): if l[i][j] > l[i][k] + l[k][j]: l[i][j] = l[i][k] + l[k][j] m = int(input()) p = list(map(int, input().split(" "))) ans.append(p[0]) dis = 0 for i in range(1, m): dis = dis + l[p[i - 1] - 1][p[i] - 1] if dis > l[ans[len(ans) - 1] - 1][p[i] - 1]: ans.append(p[i - 1]) dis = l[ans[len(ans) - 1] - 1][p[i] - 1] ans.append(p[m - 1]) print(len(ans)) for i in ans: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) distance = [[(1000000) for _ in range(n)] for _ in range(n)] adjMatrix = [[] for _ in range(n)] for i in range(n): adjMatrix[i] = [int(x) for x in input()] length = int(input()) path = [(int(x) - 1) for x in input().split()] for i in range(n): for k in range(n): if adjMatrix[i][k] == 1: distance[i][k] = 1 for i in range(n): for k in range(n): for j in range(n): distance[k][j] = min(distance[k][j], distance[k][i] + distance[i][j]) answer = [] answer.append(path[0]) leng = 1 for i in range(1, len(path)): if distance[answer[-1]][path[i]] >= leng and answer[-1] != path[i]: leng += 1 else: leng = 2 answer.append(path[i - 1]) answer.append(path[-1]) print(len(answer)) for i in answer: print(i + 1, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys input = sys.stdin.readline INF = 10**12 n = int(input()) edge = [[] for _ in range(n)] for i in range(n): line = input().rstrip() for j, ch in enumerate(line): if i == j: edge[i].append(0) elif ch == "1": edge[i].append(1) else: edge[i].append(INF) m = int(input()) a = [int(item) for item in input().split()] if m == 2: print(2) print(" ".join([str(item) for item in a])) exit() for k in range(n): for i in range(n): for j in range(n): edge[i][j] = min(edge[i][j], edge[i][k] + edge[k][j]) frm_id = 0 mid_id = 1 to_id = 2 ans_num = 0 ans = [a[frm_id]] ans_num += 1 while to_id < len(a): frm = a[frm_id] - 1 mid = a[mid_id] - 1 to = a[to_id] - 1 if frm == to or not edge[frm][mid] + edge[mid][to] == edge[frm][to]: ans.append(mid + 1) ans_num += 1 frm_id = mid_id to_id += 1 mid_id += 1 else: to_id += 1 mid_id += 1 ans.append(to + 1) ans_num += 1 print(ans_num) print(" ".join([str(item) for item in ans]))
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) inf = 10**9 matrix = [list(map(lambda x: int(x) or inf, input())) for _ in range(n)] for k in range(n): for i in range(n): for j in range(n): if matrix[i][j] > matrix[i][k] + matrix[k][j]: matrix[i][j] = matrix[i][k] + matrix[k][j] for i in range(n): matrix[i][i] = 0 m = int(input()) path = list(map(lambda x: int(x) - 1, input().split())) ans = [path[0]] dist = 0 for i in range(1, m): dist += 1 if dist != matrix[ans[-1]][path[i]]: ans.append(path[i - 1]) dist = 1 ans.append(path[-1]) print(len(ans)) print(*map(lambda x: x + 1, ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
from sys import setrecursionlimit as SRL from sys import stdin SRL(10**7) rd = stdin.readline rrd = lambda: map(int, rd().strip().split()) dis = [([1000000000] * 101) for _i in range(101)] n = int(rd()) for i in range(n + 1): dis[i][i] = 0 for i in range(1, n + 1): s = str(rd().strip()) for j in range(n): if s[j] == "1": dis[i][j + 1] = 1 for k in range(1, n + 1): for i in range(1, n + 1): for j in range(1, n + 1): dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]) m = int(rd()) p = list(rrd()) ans = [] s = 0 pre = 0 l = 0 for i in p: if len(ans) == 0: s = i pre = i ans.append(i) continue if dis[s][i] == l + 1: pre = i l += 1 continue else: ans.append(pre) s = pre pre = i l = 1 if s != pre: ans.append(pre) print(len(ans)) print(*ans)
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) nei = [] for _ in range(n): s = input() nei.append(list(int(c) for c in s)) m = int(input()) p = list([(int(x) - 1) for x in input().split()]) shortest = [] for i in range(n): vals = [] for j, con in enumerate(nei[i]): if i == j: vals.append(0) elif con: vals.append(1) else: vals.append(10**6) shortest.append(vals) for k in range(n): for i in range(n): for j in range(n): shortest[i][j] = min(shortest[i][j], shortest[i][k] + shortest[k][j]) seq = [p[0]] dist = 0 for prev, cur in zip(p, p[1:]): dist += shortest[prev][cur] if shortest[seq[-1]][cur] < dist: dist = shortest[prev][cur] seq.append(prev) seq.append(p[-1]) print(len(seq)) print(*[(val + 1) for val in seq])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
def do(): n = int(input()) graph = [([0] * n) for _ in range(n)] for i in range(n): tmp = [int(c) for c in input()] for j in range(len(tmp)): graph[i][j] = 1 if tmp[j] == 1 else float("inf") n_path = int(input()) path = [(int(c) - 1) for c in input().split(" ")] dis = [([float("inf")] * n) for _ in range(n)] for i in range(n): for j in range(n): if i == j: dis[i][i] = 0 else: dis[i][j] = graph[i][j] for k in range(n): for i in range(n): if i != k: for j in range(n): dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]) res = [path[0]] cur = 0 for i in range(1, n_path): cur += dis[path[i - 1]][path[i]] if cur > dis[res[-1]][path[i]]: res.append(path[i - 1]) cur = dis[res[-1]][path[i]] if path[-1] != res[-1]: res.append(path[-1]) print(len(res)) print(" ".join([str(c + 1) for c in res])) do()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
from sys import stdin input = stdin.readline def floyd(a, n): for i in range(n): for j in range(n): if i != j and a[i][j] == 0: a[i][j] = 10**18 for k in range(n): for i in range(n): for j in range(n): a[i][j] = min(a[i][j], a[i][k] + a[k][j]) n = int(input()) a = [] for i in range(n): r = list(input()) a.append(list(map(int, r[: len(r) - 1]))) floyd(a, n) m = int(input()) t = list(map(int, input().split())) s = [] for i in range(m): if i == 0 or i == m - 1: s.append(t[i]) continue x = s[-1] - 1 if a[x][t[i] - 1] + a[t[i] - 1][t[i + 1] - 1] == a[x][t[i + 1] - 1]: continue s.append(t[i]) print(len(s)) print(*s)
ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
N = int(1000000.0 + 3) n = int(input()) dis = list([N] * n for _ in range(n)) for i in range(n): for j in range(n): if i == j: dis[i][j] = 0 for i in range(n): s = input() for j in range(n): if s[j] == "1": dis[i][j] = 1 for k in range(n): for i in range(n): for j in range(n): dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]) m = int(input()) arr = list(map(int, input().split())) for i in range(m): arr[i] -= 1 e, cnt = m - 1, 0 for i in range(m - 3, -1, -1): x, y, z = arr[i], arr[i + 1], arr[e] if dis[x][z] < dis[x][y] + dis[y][z]: e = i + 1 else: arr[i + 1] = -1 cnt += 1 print(m - cnt) print(*[(i + 1) for i in arr if i != -1])
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) gr = list() for _ in range(n): gr.append(list(map(lambda x: [10000000000, 1][int(x)], input()))) m = int(input()) p = list(map(lambda x: int(x) - 1, input().split())) for k in range(n): for i in range(n): for j in range(n): gr[i][j] = min(gr[i][j], gr[i][k] + gr[k][j]) skip = [[[(0) for _ in range(n)] for __ in range(n)] for ___ in range(n)] for i in range(n): for k in range(n): for j in range(n): skip[i][k][j] = [0, gr[i][j] == gr[i][k] + gr[k][j]][i != j] ans = [p[0]] for v in range(len(p[1:-1])): if not skip[ans[-1]][p[v + 1]][p[v + 2]]: ans.append(p[v + 1]) ans.append(p[-1]) ans = list(map(lambda x: x + 1, ans)) print(len(ans)) for i in ans: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR LIST NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) d = [] for i in range(n): d += [[int(i) for i in input()]] inf = 200 for i in range(n): for j in range(n): if d[i][j] == 0 and i != j: d[i][j] = inf for k in range(n): for j in range(n): for i in range(n): d[i][j] = min(d[i][j], d[i][k] + d[k][j]) m = int(input()) p = list(map(int, input().split())) path = [p[0]] j = 0 k = 0 i = 0 while i <= m - 1: if d[p[j] - 1][p[i] - 1] == i - j: k = i i += 1 elif j != k: path.append(p[k]) j = k i -= 1 path.append(p[m - 1]) print(len(path)) print(" ".join(map(str, path)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
from sys import setcheckinterval, stdin setcheckinterval(1000) iin = lambda: int(stdin.readline()) lin = lambda: list(map(int, stdin.readline().split())) def BFS(s, adj): level = {s: 0} parent = {s: None} lv = 1 u = [s] while u: nextu = [] for i in u: for v in adj[i]: if v not in parent: parent[v] = i level[v] = lv nextu.append(v) lv += 1 u = nextu.copy() return level n = iin() adj = [[] for i in range(n)] for i in range(n): s = input() for j in range(n): if s[j] == "1": adj[i].append(j) m = iin() p = lin() sol = dict() pp = [BFS(i, adj) for i in range(n)] i = 1 x = 0 ans = [p[0]] while i < m: if pp[p[x] - 1][p[i] - 1] == i - x: i += 1 else: ans.append(p[i - 1]) x = i - 1 ans.append(p[-1]) print(len(ans)) print(*ans)
EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT VAR NUMBER ASSIGN VAR DICT VAR NONE ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
R = lambda: map(int, input().split()) n = int(input()) L = [] for i in range(n): L.append([int(j) for j in input()]) for i in range(n): for j in range(n): if i != j and L[i][j] == 0: L[i][j] = 10**9 for k in range(n): for i in range(n): for j in range(n): L[i][j] = min(L[i][j], L[i][k] + L[k][j]) m = int(input()) A = list(R()) i = 1 res = [A[0]] while i < m - 1: if L[res[-1] - 1][A[i] - 1] + 1 != L[res[-1] - 1][A[i + 1] - 1]: res.append(A[i]) i += 1 res.append(A[m - 1]) print(len(res)) print(*res)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys def main(): INF = int(1000000000.0) n = int(sys.stdin.readline()) g = [[]] * n for i in range(n): s = sys.stdin.readline().strip() sg = list(map(int, list(s))) g[i] = sg m = int(sys.stdin.readline()) p = list(map(lambda x: int(x) - 1, sys.stdin.readline().split())) for i in range(n): for j in range(n): if i != j and g[i][j] == 0: g[i][j] = INF for k in range(n): for i in range(n): for j in range(n): g[i][j] = min(g[i][j], g[i][k] + g[k][j]) MAXM = int(1000000.0) p_len = len(p) ans = [0] * p_len ans_ind = 0 ind = 0 while ind < p_len: cur = p[ind] ans[ans_ind] = cur ans_ind += 1 new = ind + 1 while new < p_len and g[cur][p[new]] == new - ind: new += 1 if new == p_len: ans[ans_ind] = p[-1] ans_ind += 1 break if g[cur][p[new]] != new - ind: new -= 1 ind = new ans = [(x + 1) for x in ans[:ans_ind]] print(len(ans)) print(" ".join(map(str, ans[:ans_ind]))) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) mat = [] for _ in range(n): mat.append([int(i) for i in input()]) short = [] for i in range(n): short.append([-1] * n) for i in range(n): layer = {i} cnt = 1 sht = short[i] sht[i] = 0 while len(layer) > 0: new = set() for v in layer: for j in range(n): if mat[v][j] and sht[j] == -1: sht[j] = cnt new.add(j) cnt += 1 layer = new ln = int(input()) path = [(int(i) - 1) for i in input().split()] lastv = path[0] ans = [str(lastv + 1)] dist = 1 for i in range(1, ln): v1 = path[i] if dist != short[lastv][v1]: dist = 1 lastv = path[i - 1] ans.append(str(lastv + 1)) dist += 1 ans.append(str(path[-1] + 1)) print(len(ans)) print(" ".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
from sys import stdin input = stdin.readline a = [] n = int(input()) for _ in range(n): s = input() a.append(s) m = int(input()) b = list(map(int, input().split())) c = [0] * m k = 0 for i in range(1, m - 1): if k == 1: k = 0 continue if a[b[i - 1] - 1][b[i + 1] - 1] == "0" and b[i - 1] != b[i + 1]: k = 1 c[i] = 1 print(c.count(0)) for i in range(m): if c[i] == 0: print(b[i], end=" ")
ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys MOD = pow(10, 9) + 7 INF = 999999999999999 def IN(f=0): if f == 0: return [int(i) for i in sys.stdin.readline().split()] else: return int(sys.stdin.readline()) def floydWarshall(graph, V): dist = [] for i in range(V): dist.append(graph[i].copy()) for k in range(V): for i in range(V): for j in range(V): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) return dist n = int(input()) a = [] for i in range(n): t = list(input()) t = [int(j) for j in t] a.append(t) for i in range(n): for j in range(n): if i != j and a[i][j] == 0: a[i][j] = INF f = floydWarshall(a, n) m = IN(1) s = IN() p = [s[0]] i = 1 j = 0 while i < len(s) - 1: ab = f[p[j] - 1][s[i] - 1] bc = f[s[i] - 1][s[i + 1] - 1] ac = f[p[j] - 1][s[i + 1] - 1] if ab + bc != ac: p.append(s[i]) j += 1 i += 1 else: i += 1 p.append(s[-1]) print(len(p)) print(*p)
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) INF = 10**18 g = [[INF for i in range(n)] for _ in range(n)] for i in range(n): s = input().rstrip() for j in range(n): if s[j] == "1": g[i][j] = 1 g[i][i] = 0 for k in range(n): for i in range(n): for j in range(n): g[i][j] = min(g[i][j], g[i][k] + g[k][j]) m = int(input()) p = [(int(i) - 1) for i in input().split()] ptr = 1 ans = [p[0]] while ptr + 1 < len(p): s = ans[-1] if g[s][p[ptr]] + 1 != g[s][p[ptr + 1]]: ans.append(p[ptr]) ptr += 1 ans.append(p[-1]) print(len(ans)) for i in ans: print(i + 1, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
nodes = int(input()) dist = {} for i in range(1, nodes + 1): for j in range(1, nodes + 1): dist[i, j] = float("inf") for i in range(nodes): lst = input() for j in range(len(lst)): if lst[j] == "1": dist[i + 1, j + 1] = 1 n = input() path = [int(x) for x in input().split()] for n in range(1, nodes + 1): dist[n, n] = 0 for i in range(1, nodes + 1): for j in range(1, nodes + 1): for k in range(1, nodes + 1): if dist[j, k] > dist[j, i] + dist[i, k]: dist[j, k] = dist[j, i] + dist[i, k] final = [] final.append(0) for i in range(1, len(path)): if dist[path[final[-1]], path[i]] < i - final[-1]: final.append(i - 1) final.append(len(path) - 1) print(len(final)) print(" ".join([str(path[x]) for x in final]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
import sys input = lambda: sys.stdin.readline().strip() n = int(input()) d = [] for i in range(n + 1): d.append([]) for j in range(n + 1): d[-1].append(0) for i in range(1, n + 1): s = input() for j in range(1, n + 1): if i != j: if s[j - 1] == "0": d[i][j] = sys.maxsize else: d[i][j] = 1 for k in range(1, n + 1): for i in range(1, n + 1): for j in range(1, n + 1): d[i][j] = min(d[i][k] + d[k][j], d[i][j]) m = int(input()) ls = list(map(int, input().split())) ans = [ls[0]] cur = 0 for i in range(1, m): if d[ans[-1]][ls[i]] < i - cur: ans.append(ls[i - 1]) cur = i - 1 ans.append(ls[-1]) print(len(ans)) for i in ans: print(i, end=" ") print()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
gi = lambda: list(map(int, input().split())) (n,) = gi() g = [list(map(int, list(input()))) for _ in range(n)] (lenp,) = gi() p = gi() ans = p[:] lenans = lenp k = 1 while k < lenp - 1: if g[ans[k - 1] - 1][ans[k + 1] - 1] == 0 and ans[k - 1] != ans[k + 1]: p[k] = -1 ans[k] = ans[k - 1] lenans -= 1 k += 1 k += 1 print(lenans) for k in range(lenp): if p[k] != -1: print(ans[k], end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) dis = [] for i in range(n): a = list(map(int, input())) for j in range(n): if a[j] == 0: a[j] = 200 a[i] = 0 dis.append(a) for k in range(n): for i in range(n): for j in range(n): dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]) m = int(input()) a = list(map(int, input().split())) ans = [0] for i in range(1, m): if dis[a[ans[-1]] - 1][a[i] - 1] != i - ans[-1]: ans.append(i - 1) ans.append(m - 1) print(len(ans)) for i in ans: print(a[i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
def floyd(n, dist): for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) n = int(input()) graph = [([0] * n) for _ in range(n)] for i in range(n): s = input() for j in range(n): graph[i][j] = int(s[j]) if s[j] != "0" or i == j else float("+inf") m = int(input()) path = [(x - 1) for x in list(map(int, input().split()))] floyd(n, graph) ans = [path[0]] last = 0 for i in range(1, m): if i - last > graph[ans[-1]][path[i]]: ans.append(path[i - 1]) last = i - 1 ans.append(path[-1]) print(len(ans)) print(*[(x + 1) for x in ans])
FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR STRING VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
def gns(): return list(map(int, input().split())) n = int(input()) mp = [] for i in range(n): mp.append(input()) def bfs(i): nxt = [i] ans = [None] * n ans[i] = 0 v = [False] * n v[i] = True p = 0 while len(nxt) > 0: p += 1 nxt_ = [] for ni in nxt: for nn in range(n): if v[nn] or mp[ni][nn] == "0": continue v[nn] = True ans[nn] = p nxt_.append(nn) nxt = nxt_ return ans dis = [None] * n for i in range(n): dis[i] = bfs(i) m = int(input()) ms = gns() ms = [(x - 1) for x in ms] ans = [ms[0]] l = ms[0] for i in range(1, m - 1): c = ms[i] cn = ms[i + 1] if dis[l][cn] <= dis[l][c]: ans.append(c) l = c ans.append(ms[-1]) ans = [(x + 1) for x in ans] print(len(ans)) print(" ".join(map(str, ans)))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) edges = [[] for i in range(n)] for i in range(n): s = input() for j in range(n): if s[j] == "1": edges[i].append(j) m = int(input()) path = [(int(i) - 1) for i in input().split()] dist = [[(0) for i in range(n)] for j in range(n)] for i in range(n): visited = [(-1) for j in range(n)] visited[0] = i act = 0 num_visited = 1 while act < num_visited: for neigh in edges[visited[act]]: if neigh not in visited: dist[i][neigh] = dist[i][visited[act]] + 1 visited[num_visited] = neigh num_visited += 1 act += 1 ans = [path[0]] act = 0 for i in range(1, m): if dist[ans[-1]][path[i]] < i - act: act = i - 1 ans.append(path[i - 1]) ans.append(path[-1]) print(len(ans)) print(" ".join([str(i + 1) for i in ans]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) adj = [] for i in range(n): adj.append(str(input())) dist = [[(999999999999999999999999999) for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): if i == j: dist[i][j] = 0 elif adj[i][j] == "1": dist[i][j] = 1 m = int(input()) p = list(map(int, input().split())) for i in range(m): p[i] -= 1 for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) last_i = 0 ans = [] ans.append(0) for i in range(1, m - 1): if dist[p[last_i]][p[i + 1]] != i + 1 - last_i: last_i = i ans.append(i) print(len(ans) + 1) for i in ans: print(p[i] + 1, end=" ") print(p[-1] + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
n = int(input()) g = [] def bfs(src): q = [] dist = [] visited = [] for i in range(n): visited.append(False) dist.append(1e18) dist[src] = 0 visited[src] = True q.append(src) while q: src = q.pop(0) for i in g[src]: if visited[i] == False: visited[i] = True dist[i] = dist[src] + 1 q.append(i) return dist for i in range(n): t = input() g.append([]) for j in range(len(t)): if t[j] == "1": g[i].append(j) m = int(input()) p = list(map(int, input().split())) for i in range(m): p[i] -= 1 dist = [] for i in range(n): dist.append(bfs(i)) ans = [p[0]] k = 0 for i in range(1, m): if dist[ans[-1]][p[i]] == 1e18 or dist[ans[-1]][p[i]] < i - k: ans.append(p[i - 1]) k = i - 1 ans.append(p[-1]) l = len(ans) for i in range(l): ans[i] += 1 ans[i] = str(ans[i]) print(len(ans)) print(" ".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
from sys import stdin, stdout input = stdin.readline print = stdout.write n = int(input()) dist = [] for i in range(n): dist.append(list(map(lambda x: 1 if x == "1" else 200, input()))) m = int(input()) p = list(map(int, input().split())) def solve(n, dist, m, p): for i in range(n): dist[i][i] = 0 for t in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j], dist[i][t] + dist[t][j]) prev = 0 cur = 1 a = p[prev] res = [a] while cur < m: if cur - prev == 1 and p[prev] == p[cur]: res.append(p[cur]) prev = cur cur += 1 continue b = p[cur] if dist[a - 1][b - 1] != cur - prev: res.append(p[cur - 1]) prev = cur - 1 cur -= 1 a = p[prev] cur += 1 res.append(p[cur - 1]) return res res = solve(n, dist, m, p) print(str(len(res)) + "\n") print(" ".join(map(str, res)))
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING NUMBER NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$)Β β€” the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$)Β β€” the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$)Β β€” the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$)Β β€” the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$)Β β€” the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$.
def floyd(g): n = len(g) for k in range(n): for i in range(n): if g[i][k] is None: continue for j in range(n): if g[k][j] is None: continue cand = g[i][k] + g[k][j] if g[i][j] is None or g[i][j] > cand: g[i][j] = cand n = int(input()) g = [] for i in range(n): s = [(None if d == "0" else 1) for d in input()] g.append(s) m = int(input()) p = map(int, input().split()) p = [(x - 1) for x in p] floyd(g) ans = [p[0]] cI = 0 for i in range(1, m): d = i - cI dMin = g[p[cI]][p[i]] if d != dMin: cI = i - 1 skip = False if ans[-1] == p[cI]: ans.append(p[cI - 1]) if g[p[cI - 1]][p[i]] == 2: skip = True cI -= 1 if not skip: ans.append(p[cI]) ans.append(p[-1]) print(len(ans)) print(" ".join([str(x + 1) for x in ans]), end="") print(" ")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NONE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING NONE NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR STRING
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
def myFunc(e): return e[0] + e[1] count, rating = list(map(int, input().split())) goodJob = [] badJob = [] taken = 0 for i in range(count): a, b = list(map(int, input().split())) if b >= 0: goodJob.append([a, b]) else: badJob.append([a, b]) goodJob.sort() badJob.sort(reverse=True, key=myFunc) for job in goodJob: if job[0] <= rating: rating += job[1] taken += 1 else: break dp = [] for i in range(len(badJob) + 1): row = [] for j in range(rating + 2): row.append(0) dp.append(row) dp[0][rating] = taken for i in range(len(badJob)): for curRating in range(rating + 1): if curRating >= badJob[i][0] and curRating + badJob[i][1] >= 0: dp[i + 1][curRating + badJob[i][1]] = max( dp[i + 1][curRating + badJob[i][1]], dp[i][curRating] + 1 ) dp[i + 1][curRating] = max(dp[i + 1][curRating], dp[i][curRating]) ans = 0 for curRating in range(rating + 1): ans = max(ans, dp[len(badJob)][curRating]) print(ans)
FUNC_DEF RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
import sys input = sys.stdin.readline n, r = map(int, input().split()) l = [] for _ in range(n): l.append(list(map(int, input().split()))) p = 0 ans = 0 while p < n: if l[p][0] <= r and l[p][1] >= 0: r += l[p][1] l = l[:p] + l[p + 1 :] p = 0 n -= 1 ans += 1 else: p += 1 if l == []: print(ans) return q = len(l) for i in range(q): l[i][0] = max(l[i][0], -l[i][1]) l = sorted(l, key=lambda x: x[0] + x[1]) l.reverse() dp = [[(0) for _ in range(r + 1)] for _ in range(q + 1)] for i in range(q): for j in range(r + 1): if j >= l[i][0] and 0 <= j + l[i][1] <= r: dp[i + 1][j + l[i][1]] = max(dp[i + 1][j + l[i][1]], dp[i][j] + 1) dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]) print(max(dp[-1]) + ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
n, r = map(int, input().split()) pt = [] nt = [] r2 = r result = 0 for _ in range(n): a, b = map(int, input().split()) r2 += b if b >= 0: pt.append((a, b)) else: nt.append((a, b)) pt.sort() nt.sort(key=lambda t: t[1] + t[0], reverse=True) for a, b in pt: if r < a: break result += 1 r += b k = len(nt) ws = [([0] * (k + 1)) for i in range(r + 1)] for i in range(1, k + 1): for w in range(1, r + 1): a, b = nt[i - 1] if w + b >= 0 and a <= r and ws[r - a][i - 1] >= ws[w][i - 1]: ws[w][i] = max(ws[w][i - 1], 1 + ws[w + b][i - 1]) else: ws[w][i] = ws[w][i - 1] result += ws[r][k] print(result)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
n, r = list(map(int, input().split())) a = [list(map(int, input().split())) for i in range(n)] pos = [] neg = [] ans = 0 for x in a: if x[1] > 0: pos.append(x) else: neg.append(x) pos.sort(key=lambda k: k[0]) flag = True for x in pos: if r >= x[0]: r += x[1] ans += 1 neg.sort(key=lambda i: i[0] + i[1], reverse=True) arr = [0] * (r + 1) for i in range(len(neg)): for j in range(neg[i][0], r + 1): if j + neg[i][1] >= 0: arr[j + neg[i][1]] = max(arr[j + neg[i][1]], arr[j] + 1) ans += max(arr) print(ans)
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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
inp = str(input()).split() size = int(inp[0]) r = int(inp[1]) pos = [] neg = [] for i in range(size): inp = str(input()).split() a = int(inp[0]) b = int(inp[1]) if b >= 0: pos.append((a, b)) else: neg.append((a, b)) pos = sorted(pos) projects = 0 for ab in pos: a, b = ab if r >= a: r += b projects += 1 else: break neg = sorted(neg, key=lambda ab: ab[0] + ab[1], reverse=True) n = len(neg) dp = [([0] * (r + 1)) for _ in range(n + 1)] dp[0][r] = projects for i in range(0, n): for j in range(0, r + 1): if j >= neg[i][0] and j + neg[i][1] >= 0: dp[i + 1][j + neg[i][1]] = max(dp[i + 1][j + neg[i][1]], dp[i][j] + 1) dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]) print(max(dp[n]))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
from sys import setrecursionlimit as SRL from sys import stdin SRL(10**7) rd = stdin.readline rrd = lambda: list(map(int, rd().strip().split())) n, r = rrd() pos = [] neg = [] for i in range(n): a, b = rrd() if b < 0: neg.append([a, b]) else: pos.append([a, b]) pos.sort(key=lambda x: x[0]) neg.sort(key=lambda x: x[0] + x[1]) ans = 0 for a, b in pos: if r >= a: r += b ans += 1 else: break dp = [([0] * 105) for _i in range(60005)] for i in range(r + 10): for j in range(len(neg)): if i >= neg[j][0] and i + neg[j][1] >= 0: if j: dp[i][j] = max(dp[i][j], dp[i + neg[j][1]][j - 1] + 1, dp[i][j - 1]) else: dp[i][j] = 1 elif j: dp[i][j] = dp[i][j - 1] print(dp[r][len(neg) - 1] + ans)
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
N, R = [int(x) for x in input().split()] projects = [[int(x) for x in input().split()] for _ in range(N)] pos = [] neg = [] for a, b in projects: if b < 0: neg.append((a, b)) else: pos.append((a, b)) pos.sort() ans = 0 for a, b in pos: if R >= a: R += b ans += 1 neg.sort(key=sum, reverse=True) memo = {} def dp(i, r): if (i, r) in memo: return memo[i, r] if i == len(neg): return 0 a, b = neg[i] ans = dp(i + 1, r) if r >= a and r + b >= 0: ans = max(ans, dp(i + 1, r + b) + 1) memo[i, r] = ans return ans print(dp(0, R) + ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
n, r = map(int, input().split()) arr = [list(map(int, input().split())) for _ in range(n)] def solve1(cur, arr): cnt = 0 while len(arr) > 0: max_inc = -9999 choose = None for a, b in arr: if cur >= a and max_inc < b: max_inc = b choose = a if choose is None: flg = False break cnt += 1 cur += max_inc arr.remove([choose, max_inc]) return cur, cnt arr1 = [[x, y] for x, y in arr if y >= 0] arr2 = [[x, y] for x, y in arr if y < 0] r, cnt = solve1(r, arr1) n = len(arr2) arr2 = [[]] + sorted(arr2, key=lambda x: x[0] + x[1], reverse=True) dp = [([-1] * (n + 1)) for _ in range(n + 1)] for i in range(n + 1): dp[i][0] = r for i in range(1, n + 1): for j in range(1, i + 1): dp[i][j] = dp[i - 1][j] if dp[i - 1][j - 1] >= arr2[i][0] and dp[i - 1][j - 1] + arr2[i][1] >= 0: dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + arr2[i][1]) ans = 0 for j in range(n + 1): if dp[n][j] >= 0: ans = j print(ans + cnt)
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 FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $r$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. -----Input----- The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) β€” the number of projects and the initial rating of Polycarp, respectively. The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) β€” the rating required to complete the $i$-th project and the rating change after the project completion. -----Output----- Print one integer β€” the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. -----Examples----- Input 3 4 4 6 10 -2 8 -1 Output 3 Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Output 5 Input 3 2 300 -300 1 299 1 123 Output 3
n, r = map(int, input().split()) a = [] cnt = 0 for i in range(n): a.append([int(j) for j in input().split()]) flag = True while flag: flag = False for i in a: if r >= i[0] and i[1] >= 0: flag = True r += i[1] cnt += 1 a.remove(i) break a = sorted(a, key=lambda x: x[0] + x[1]) dp = [([0] * (r + 1)) for i in range(len(a) + 1)] for i in range(len(a)): for j in range(r + 1): dp[i][j] = dp[i - 1][j] if j >= a[i][0] and j + a[i][1] >= 0: dp[i][j] = max(dp[i][j], dp[i - 1][j + a[i][1]] + 1) print(cnt + dp[len(a) - 1][r])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices. A subtree of a tree T is a tree with both vertices and edges as subsets of vertices and edges of T. You're given a tree with n vertices. Consider its vertices numbered with integers from 1 to n. Additionally an integer is written on every vertex of this tree. Initially the integer written on the i-th vertex is equal to v_{i}. In one move you can apply the following operation: Select the subtree of the given tree that includes the vertex with number 1. Increase (or decrease) by one all the integers which are written on the vertices of that subtree. Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero. -----Input----- The first line of the input contains n (1 ≀ n ≀ 10^5). Each of the next n - 1 lines contains two integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ n;Β a_{i} β‰  b_{i}) indicating there's an edge between vertices a_{i} and b_{i}. It's guaranteed that the input graph is a tree. The last line of the input contains a list of n space-separated integers v_1, v_2, ..., v_{n} (|v_{i}| ≀ 10^9). -----Output----- Print the minimum number of operations needed to solve the task. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 3 1 2 1 3 1 -1 1 Output 3
import sys def minp(): return sys.stdin.readline().strip() n = int(minp()) e = [0] p = [None] * (n + 1) for i in range(n): e.append([]) for i in range(n - 1): a, b = map(int, minp().split()) e[a].append(b) e[b].append(a) v = list(map(int, minp().split())) plus = [0] * (n + 1) minus = [0] * (n + 1) was = [False] * (n + 1) was[1] = True i = 0 j = 1 q = [0] * (n + 100) q[0] = 1 p[1] = 0 while i < j: x = q[i] i += 1 for y in e[x]: if not was[y]: was[y] = True p[y] = x q[j] = y j += 1 i = j - 1 while i >= 0: x = q[i] i -= 1 s = minus[x] - plus[x] z = v[x - 1] + s pp = p[x] minus[pp] = max(minus[x], minus[pp]) plus[pp] = max(plus[x], plus[pp]) if z > 0: plus[pp] = max(plus[pp], plus[x] + z) elif z < 0: minus[pp] = max(minus[pp], minus[x] - z) print(plus[0] + minus[0])
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices. A subtree of a tree T is a tree with both vertices and edges as subsets of vertices and edges of T. You're given a tree with n vertices. Consider its vertices numbered with integers from 1 to n. Additionally an integer is written on every vertex of this tree. Initially the integer written on the i-th vertex is equal to v_{i}. In one move you can apply the following operation: Select the subtree of the given tree that includes the vertex with number 1. Increase (or decrease) by one all the integers which are written on the vertices of that subtree. Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero. -----Input----- The first line of the input contains n (1 ≀ n ≀ 10^5). Each of the next n - 1 lines contains two integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ n;Β a_{i} β‰  b_{i}) indicating there's an edge between vertices a_{i} and b_{i}. It's guaranteed that the input graph is a tree. The last line of the input contains a list of n space-separated integers v_1, v_2, ..., v_{n} (|v_{i}| ≀ 10^9). -----Output----- Print the minimum number of operations needed to solve the task. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 3 1 2 1 3 1 -1 1 Output 3
n = int(input()) r = [[] for i in range(n + 1)] r[1] = [0] for i in range(n - 1): a, b = map(int, input().split()) r[a].append(b) r[b].append(a) t = list(map(int, input().split())) u, v = [0] * (n + 1), [0] * (n + 1) for i, j in enumerate(t, 1): if j < 0: u[i] = -j else: v[i] = j t, p = [1], [0] * (n + 1) while t: a = t.pop() for b in r[a]: if p[b]: continue p[b] = a t.append(b) k = [len(t) for t in r] t = [a for a in range(2, n + 1) if k[a] == 1] x, y = [0] * (n + 1), [0] * (n + 1) while t: a = t.pop() b = p[a] x[b] = max(x[b], u[a]) y[b] = max(y[b], v[a]) k[b] -= 1 if k[b] == 1: t.append(b) if u[b] > 0: if x[b] - y[b] > u[b]: u[b], v[b] = x[b], x[b] - u[b] else: u[b], v[b] = y[b] + u[b], y[b] elif y[b] - x[b] > v[b]: u[b], v[b] = y[b] - v[b], y[b] else: u[b], v[b] = x[b], x[b] + v[b] print(u[1] + v[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER