description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: M = max(A) + 1 dp = [] for i in range(len(A)): temp = [1] * (M * 2) dp.append(temp) m = 0 for i in range(len(A)): for j in range(i): delta = A[i] - A[j] k = delta + M dp[i][k] = dp[j][k] + 1 m = max(m, dp[i][k]) return m
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: length = len(A) hash = {} for i in range(length): for j in range(i + 1, length): hash[A[j] - A[i], j] = hash.get((A[j] - A[i], i), 1) + 1 return max(hash.values())
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if not A: return 0 res = 0 records = [collections.defaultdict(int) for _ in range(len(A))] for i in range(len(A)): for j in range(i): diff = A[i] - A[j] current = records[i].get(diff, 0) prev = records[j].get(diff, 0) + 1 records[i][diff] = max(prev, current, 2) res = max(res, records[i][diff]) return res
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if len(A) < 3: return len(A) best = 2 sequences = [{} for _ in A] for right in range(1, len(A)): for left in range(right): diff = A[right] - A[left] if diff in sequences[left]: count = sequences[left][diff] + 1 sequences[right][diff] = count best = max(best, count) else: sequences[right][diff] = 2 return best
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if len(A) == 1: return 1 if len(A) == 2: return 2 N = len(A) ans = 2 seen = {} for i in range(N): for j in range(i + 1, N): diff = A[j] - A[i] if (i, diff) in seen: ans = max(ans, seen[i, diff] + 1) seen[j, diff] = seen[i, diff] + 1 else: seen[j, diff] = 2 return ans
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, nums: List[int]) -> int: n = len(nums) if n <= 2: return n dp = dict() for i in range(n): for j in range(i + 1, n): d = nums[j] - nums[i] if (i, d) in list(dp.keys()): dp[j, d] = dp[i, d] + 1 else: dp[j, d] = 2 return max(dp.values())
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: answer = 2 L = len(A) table = [dict() for _ in range(L)] for i in range(1, L): for j in range(0, i): diff = A[i] - A[j] if not diff in table[j]: table[i][diff] = 2 else: table[i][diff] = table[j][diff] + 1 answer = max(answer, table[i][diff]) return answer
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = [] for i in range(len(A)): dp.append(collections.defaultdict(lambda: 1)) for j in range(i): diff = A[i] - A[j] dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1) return max([max(d.values()) for d in dp])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = [([0] * 501) for i in range(n)] max_val = 0 for i in range(n): for j in range(i): dif = A[i] - A[j] dp[i][dif] = max(dp[i][dif], dp[j][dif] + 1) max_val = max(dp[i][dif], max_val) return max_val + 1 class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = collections.defaultdict(dict) max_val = 0 for i in range(n): for j in range(i): dif = A[i] - A[j] dp[dif].setdefault(i, 0) dp[dif][i] = dp[dif].get(j, 0) + 1 max_val = max(dp[dif][i], max_val) return max_val + 1 class Solution1: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = [collections.defaultdict(int) for _ in range(n)] res = 0 for i in range(1, n): for j in range(0, i): diff = A[i] - A[j] dp[i][diff] = dp[j][diff] + 1 res = max(res, dp[i][diff]) return res + 1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: ans = 2 n = len(A) index = {} dp = [([2] * n) for i in range(n)] for i in range(n - 1): for j in range(i + 1, n): first = A[i] * 2 - A[j] if first in index: dp[i][j] = dp[index[first]][i] + 1 ans = max(ans, dp[i][j]) index[A[i]] = i return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = [{} for _ in range(n)] ans = 2 for i in range(1, n): for j in range(i): key_i = A[i], A[i] - A[j] key_j = A[j], A[i] - A[j] if key_i not in dp[i]: dp[i][key_i] = 2 if key_j in dp[j]: dp[i][key_i] = max(dp[i][key_i], dp[j][key_j] + 1) ans = max(dp[i][key_i], ans) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: ans = 0 for diff in range(-500, 501): dp = defaultdict(int) for e in A: dp[e] = dp[e - diff] + 1 ans = max(ans, dp[e]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if not A: return 0 dp = {} for i in range(len(A)): for j in range(i + 1, len(A)): diff = A[j] - A[i] dp[j, diff] = dp.get((i, diff), 1) + 1 return max(dp.values())
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = dict() for i in range(len(A)): for j in range(i): diff = A[i] - A[j] if diff not in dp: dp[diff] = {i: 2} else: dic = dp[diff] if j in dic: dic[i] = dic[j] + 1 else: dic[i] = 2 return max(max(v1 for k1, v1 in v.items()) for k, v in dp.items())
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = list(dict() for i in range(len(A))) maxsize = 0 for i in range(1, len(A)): for j in range(0, i): if A[j] - A[i] in dp[j]: dp[i][A[j] - A[i]] = dp[j][A[j] - A[i]] + 1 else: dp[i][A[j] - A[i]] = 1 maxsize = max(maxsize, dp[i][A[j] - A[i]]) return maxsize + 1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) m = 1001 dp = [([1] * (m + 1)) for _ in range(n)] ans = float("-inf") for i in range(1, n): for k in range(i): dp[i][A[i] - A[k] + 500] = max( dp[i][A[i] - A[k] + 500], 1 + dp[k][A[i] - A[k] + 500] ) ans = max(max(dp[i]), ans) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: self.results = {} self.results[len(A) - 1] = {} for i in range(len(A) - 2, -1, -1): self.results[i] = {} for j in range(i + 1, len(A), 1): diff = A[i] - A[j] possibility = self.results[j].get(diff, 1) if 1 + possibility > self.results[i].get(diff, 0): self.results[i][diff] = 1 + possibility result = 1 for i in range(0, len(A) - 1, 1): for value in list(self.results[i].values()): result = max(result, value) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) outdict = {} longseq = 0 for i in range(n): for j in range(0, i): diff = A[i] - A[j] if diff not in outdict: outdict[diff] = [1] * n pointer = outdict[diff] pointer[i] = max(pointer[i], pointer[j] + 1) longseq = max(longseq, pointer[i]) return longseq
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def check(x): b = [(1 if ta >= x else -1) for ta in a] for i in range(1, len(a)): b[i] += b[i - 1] mi = 0 for i in range(0, len(a)): if i - k >= 0: mi = min(mi, b[i - k]) if i >= k - 1 and b[i] - mi > 0: return True return False n, k = map(int, input().split()) a = [int(i) for i in input().split()] l, r = 1, n + 1 while l + 1 < r: mid = (l + r) // 2 if check(mid): l = mid else: r = mid print(l)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
n, k = map(int, input().split()) l = list(map(int, input().split())) v = 1 r = n + 1 ans = 0 while r - v > 1: d = [0] mid = (v + r) // 2 m = 100001 for i in range(1, n + 1): if l[i - 1] >= mid: d.append(1) else: d.append(-1) d[i] += d[i - 1] found = 0 for i in range(k, n + 1): m = d[i - k] if d[i - k] < m else m if d[i] - m > 0: found = 1 break if found == 1: v = mid else: r = mid print(v)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def solve(): n, k = [int(t) for t in input().split()] a = [int(t) for t in input().split()] def step(x, y): return 1 if x >= y else -1 l, r = 1, n while l < r: x = l + (r - l + 1) // 2 cur, pref, best = 0, 0, 0 for i in range(n): cur += step(a[i], x) if i >= k: pref += step(a[i - k], x) best = min(best, pref) if i >= k - 1 and cur - best > 0: l = x break else: r = x - 1 print(l) solve()
FUNC_DEF 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 FUNC_DEF RETURN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() inf = 10**16 md = 10**9 + 7 n, k = MI() aa = LI() def ok(m): cs = [0] for a in aa: if a < m: cs.append(cs[-1] - 1) else: cs.append(cs[-1] + 1) mn = inf for r in range(k, n + 1): mn = min(mn, cs[r - k]) if cs[r] - mn > 0: return True return False l, r = 0, n + 1 while l + 1 < r: m = (l + r) // 2 if ok(m): l = m else: r = m print(l)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
from itertools import accumulate n, k = map(int, input().split()) A = list(map(int, input().split())) def is_ok(x): cnt = 0 B = [-1] * n for i, a in enumerate(A): if a >= x: B[i] = 1 C = [0] + B C = list(accumulate(C)) if C[k] > 0: return True minl = C[0] for i in range(k + 1, n + 1): minl = min(minl, C[i - k]) if C[i] - minl > 0: return True else: return False ok = 0 ng = n + 1 while ok + 1 < ng: c = (ng + ok) // 2 if is_ok(c): ok = c else: ng = c print(ok)
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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys input = sys.stdin.readline def check(num): c = [0] * (n + 1) b = [0] * n mini = 10**18 maxi = -(10**19) v = 0 for i in range(n): if a[i] >= num: b[i] = 1 else: b[i] -= 1 v += b[i] c[i + 1] = v if i >= k - 1: mini = min(mini, c[i - k + 1]) maxi = max(maxi, v - mini) if maxi > 0: return True return False n, k = map(int, input().split()) a = list(map(int, input().split())) low = min(a) high = max(a) while low < high: mid = (low + high) // 2 if check(mid): low = mid + 1 else: high = mid - 1 if check(low): print(low) else: print(low - 1)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
n, k = map(int, input().split()) a = list(map(int, input().split())) lo = 0 hi = n + 1 def works(v): c = [(1 if av >= v else -1) for av in a] s = [0] for i in range(len(c)): s.append(s[-1] + c[i]) mins = 0 for i in range(k, len(c) + 1): mins = min(mins, s[i - k]) if s[i] - mins > 0: return True return False while lo + 1 < hi: m = (lo + hi) // 2 if works(m): lo = m else: hi = m print(lo)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys input = sys.stdin.readline n, k = map(int, input().split()) A = list(map(int, input().split())) MIN = min(A) MAX = max(A) OK = MIN NG = MAX + 1 seg_el = 1 << (n + 1).bit_length() def update(n, x, seg_el): i = n + seg_el SEG[i] = x i >>= 1 while i != 0: SEG[i] = max(SEG[i * 2], SEG[i * 2 + 1]) i >>= 1 def getvalues(l, r): L = l + seg_el R = r + seg_el ANS = -1 << 30 while L < R: if L & 1: ANS = max(ANS, SEG[L]) L += 1 if R & 1: R -= 1 ANS = max(ANS, SEG[R]) L >>= 1 R >>= 1 return ANS while NG - OK > 1: mid = (OK + NG) // 2 S = [0] for a in A: if a >= mid: S.append(S[-1] + 1) else: S.append(S[-1] - 1) SEG = [-1 << 30] * (2 * seg_el) for i in range(n + 1): SEG[i + seg_el] = S[i] for i in range(seg_el - 1, 0, -1): SEG[i] = max(SEG[i * 2], SEG[i * 2 + 1]) for i in range(n + 1 - k): t = S[i] if getvalues(i + k, n + 2) > t: OK = mid break else: NG = mid print(OK)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
N, K = map(int, input().split()) A = list(map(int, input().split())) L, R = 1, N while L < R: M = L + R + 1 >> 1 B = [0] * (N + 1) for i in range(N): if A[i] >= M: B[i + 1] = 1 else: B[i + 1] = -1 B[i + 1] += B[i] X = 0 F = 0 for i in range(K, N + 1): X = min(X, B[i - K]) if X < B[i]: F = 1 break if F: L = M else: R = min(R - 1, M) print(L)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
n, k = map(int, input().split()) aa = list(map(int, input().split())) def ok(m): cs = [0] mn = float("inf") for a in aa: cs.append(cs[-1] - 1 if a < m else cs[-1] + 1) for r in range(k, n + 1): mn = min(mn, cs[r - k]) if cs[r] - mn > 0: return True return False l, r = 0, n + 1 while l + 1 < r: m = (l + r) // 2 if ok(m): l = m else: r = m print(l)
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 FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys def checkPossibleMedia(med, a, n, k): cntsDeltaArr = [None for _ in range(n)] cntsDelta = 0 for i, x in enumerate(a): if x >= med: cntsDelta += 1 elif x < med: cntsDelta -= 1 cntsDeltaArr[i] = cntsDelta oddIdxMins = [inf for _ in range(n)] evenIdxMins = [inf for _ in range(n)] oddIdxMins[0] = 0 for i in range(n): if i % 2 == 0: evenIdxMins[i] = min(evenIdxMins[i], cntsDeltaArr[i]) else: oddIdxMins[i] = min(oddIdxMins[i], cntsDeltaArr[i]) if i - 1 >= 0: evenIdxMins[i] = min(evenIdxMins[i], evenIdxMins[i - 1]) oddIdxMins[i] = min(oddIdxMins[i], oddIdxMins[i - 1]) res = -1 for r in range(k - 1, n): l = r - k + 1 if l == 0: if k % 2 == 0: if cntsDeltaArr[r] >= 2: res = 1 elif cntsDeltaArr[r] >= 1: res = 1 elif r % 2 == 0: if cntsDeltaArr[r] - evenIdxMins[l - 1] >= 2: res = 1 if cntsDeltaArr[r] - oddIdxMins[l - 1] >= 1: res = 1 else: if cntsDeltaArr[r] - evenIdxMins[l - 1] >= 1: res = 1 if cntsDeltaArr[r] - oddIdxMins[l - 1] >= 2: res = 1 if res == 1: break return res def main(): n, k = readIntArr() a = readIntArr() med = 0 b = n while b > 0: while med + b <= n and checkPossibleMedia(med + b, a, n, k) == 1: med += b b //= 2 ans = med print(ans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] inf = float("inf") MOD = 10**9 + 7 main()
IMPORT FUNC_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
n, k = map(int, input().split()) a = [int(x) for x in input().split()] p = [0] * n mx = [0] * n def check(x): for i in range(n): cur = 1 if a[i] >= x else -1 if i == 0: mx[i] = p[i] = cur else: p[i] = p[i - 1] + cur mx[i] = max(cur, mx[i - 1] + cur) for i in range(k - 1, n): if p[i] - p[i - k + 1] + mx[i - k + 1] > 0: return 1 return 0 ans = 0 for j in range(20, -1, -1): ans += 1 << j if not check(ans): ans -= 1 << j print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
N, K = map(int, input().split()) A = list(map(int, input().split())) ok = 1 ng = N + 1 while ng - ok > 1: c = ok + ng >> 1 B = [(1 if a >= c else -1) for a in A] cumB = [0] for b in B: cumB.append(cumB[-1] + b) mi = 0 for l, r in zip(cumB, cumB[K:]): if mi > l: mi = l if r - mi > 0: break else: ng = c continue ok = c print(ok)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def solve(N, K, A): L = 1 R = N + 1 while L < R - 1: cur_guess = int((L + R) / 2) smaller_until_end = [0] smaller_before_start = [0] for a in A: if a < cur_guess: smaller_until_end.append(smaller_until_end[-1] + 1) else: smaller_until_end.append(smaller_until_end[-1] - 1) smaller_before_start.append( max(smaller_before_start[-1], smaller_until_end[-1]) ) legalabb = False for i in range(N - K + 1): if smaller_until_end[i + K] < smaller_before_start[i]: legalabb = True if legalabb: L = cur_guess else: R = cur_guess return L def run(): out = "" N, K = [int(x) for x in input().split()] A = [int(x) for x in input().split()] print(solve(N, K, A)) run()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys from itertools import accumulate input = sys.stdin.readline flush = sys.stdout.flush n, k = map(int, input().split()) A = list(map(int, input().split())) left, right = min(A), max(A) while left < right: mid = right - (right - left) // 2 B = [(1 if a >= mid else -1) for a in A] acc = [0] + list(accumulate(B)) minv = [0] * (n + 1) for i in range(1, n + 1): minv[i] = min(minv[i - 1], acc[i]) flag = 0 for i in range(k, n + 1): if acc[i] > minv[i - k]: flag = 1 break if flag: left = mid else: right = mid - 1 print(left)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys input = lambda: sys.stdin.readline().rstrip() N, K = map(int, input().split()) A = [int(a) for a in input().split()] l, r = 0, N + 1 while r - l > 1: m = l + r >> 1 B = [(1 if a >= m else -1) for a in A] S = [0] s = 0 for b in B: s += b S.append(s) mi = 0 for i in range(K, N + 1): mi = min(mi, S[i - K]) if S[i] > mi: f = 1 break else: f = 0 if f: l = m else: r = m print(l)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys from itertools import accumulate input = sys.stdin.readline n, k = map(int, input().split()) a = list(map(int, input().split())) l = 1 r = n + 1 while r > l + 1: median = (l + r) // 2 b = [(1 if i >= median else -1) for i in a] pref_sum = [b[0]] for i in range(1, n): pref_sum.append(pref_sum[i - 1] + b[i]) ans = False if pref_sum[k - 1] > 0: ans = True else: mn = 0 for i in range(k, n): mn = min(mn, pref_sum[i - k]) if pref_sum[i] - mn > 0: ans = True break if ans: l = median else: r = median print(l)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
n, m = (int(num) for num in input().split()) numlist = [int(num) for num in input().split()] r = n + 1 l = 0 while r - l > 1: mid = (r + l) // 2 comlist = [0] for i in numlist: if i < mid: comlist.append(comlist[-1] - 1) else: comlist.append(comlist[-1] + 1) minn = 10**10 jg = 0 for i in range(m, n + 1): minn = min(minn, comlist[i - m]) if comlist[i] - minn > 0: jg = 1 l = mid break if jg == 0: r = mid print(l)
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 ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def find(x): pre = [0] * (n + 1) cur = 0 for i in range(n): if a[i] < x: cur -= 1 else: cur += 1 if i >= k - 1: if cur - pre[i - k] > 0: return 1 pre[i] = min(pre[i - 1], cur) return 0 n, k = map(int, input().split()) a = list(map(int, input().split())) l = 1 r = n while l <= r: m = (l + r) // 2 if find(m): l = m + 1 else: r = m - 1 print(r)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
a = [] n, k = 0, 0 def check(median): mini = 2 * 10**5 + 1 part_sums = [0] for val in a[:k]: part_sums.append(part_sums[-1] + (val >= median) - (val < median)) for key, val in enumerate(a[k:]): mini = min(mini, part_sums[key]) if part_sums[-1] - mini > 0: return True part_sums.append(part_sums[-1] + (val >= median) - (val < median)) mini = min(mini, part_sums[n - k]) if part_sums[-1] - mini > 0: return True return False def search(l, r) -> int: if l == r - 1: return l mid = (l + r) // 2 if check(mid): return search(mid, r) return search(l, mid) n, k = map(int, input().split()) a = list(map(int, input().split())) print(search(1, 2 * 10**5 + 1))
ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
class BIT: def __init__(self, n): self.n = n self.bit = [0] * (n + 1) self.el = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.bit[i] i -= i & -i return s def add(self, i, x): self.el[i] += x while i <= self.n: self.bit[i] += x i += i & -i def get(self, i, j=None): if j is None: return self.el[i] return self.sum(j) - self.sum(i - 1) def lower_bound(self, x): w = i = 0 k = 1 << self.n.bit_length() while k: if i + k <= self.n and w + self.bit[i + k] < x: w += self.bit[i + k] i += k k >>= 1 return i + 1 def check(target): B = [(int(a >= target) * 2 - 1) for a in A] cum = [0] * (N + 1) for i in range(N): cum[i + 1] = cum[i] + B[i] for i in range(N + 1): cum[i] += N + 1 bit = BIT(N * 2 + 1) for i in range(K, N + 1): bit.add(cum[i - K], 1) if bit.sum(cum[i] - 1): return True return False N, K = map(int, input().split()) A = list(map(int, input().split())) low, high = 1, N + 1 mid = (low + high) // 2 while high - low > 1: if check(mid): low = mid else: high = mid mid = (low + high) // 2 ans = low print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF NONE IF VAR NONE RETURN VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR WHILE VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys input = sys.stdin.readline N, K = map(int, input().split()) a = list(map(int, input().split())) def check(x): t = [0] * N for i in range(N): if a[i] < x: t[i] = -1 else: t[i] = 1 cs = [0] * (N + 1) for i in range(N): cs[i + 1] = cs[i] + t[i] csmx = [0] * (N + 1) csmx[-1] = cs[-1] for i in range(N, 0, -1): csmx[i - 1] = max(csmx[i], cs[i - 1]) for i in range(N - K + 1): d = csmx[i + K] - cs[i] if d > 0: return True return False ok = 0 ng = N + 1 while ng - ok > 1: m = (ok + ng) // 2 if check(m): ok = m else: ng = m print(ok)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
n, k = map(int, input().split()) ls = list(map(int, input().split())) def checkMedianAtleastX(x): arr = [(1 if i >= x else -1) for i in ls] prefix_sums = [0] for i in arr: prefix_sums.append(prefix_sums[-1] + i) sum_min = 1000000 for i in range(k, n + 1): sum_min = min(sum_min, prefix_sums[i - k]) if prefix_sums[i] > sum_min: return True return False def binSearch(l, r): if l == r: return l if l + 1 == r: if checkMedianAtleastX(r): return r return l mid = (l + r) // 2 if checkMedianAtleastX(mid): return binSearch(mid, r) return binSearch(l, mid - 1) print(binSearch(1, 2 * 100000))
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 FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN VAR IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR RETURN VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def check(v, n, k, mid): tmp = [] for i in v: if i < mid: tmp.append(-1) else: tmp.append(1) for i in range(1, len(v)): tmp[i] += tmp[i - 1] maxArr = [0] * n maxArr[n - 1] = tmp[n - 1] maxArr[n - 2] = tmp[n - 2] for i in range(n - 3, -1, -1): maxArr[i] = max(maxArr[i + 2], tmp[i]) for i in range(0, n - k + 1): leftval = 0 rightval = maxArr[i + k - 1] if i - 1 >= 0: leftval = tmp[i - 1] if k % 2 == 0: if rightval - leftval >= 2: return True elif rightval - leftval >= 1: return True if i + k < n: rightval = maxArr[i + k] if (k + 1) % 2 == 0: if rightval - leftval >= 2: return True elif rightval - leftval >= 1: return True return False def main(): n, k = map(int, input().split()) v = list(map(int, input().split())) if n == 2: if k == 1: maxele = max(v[0], v[1]) print(f"{maxele}") else: minele = min(v[0], v[1]) print(f"{minele}") return low, high = 1, 100000000000000.0 ans = low while low <= high: mid = int((low + high) / 2) if check(v, n, k, mid): ans = mid low = mid + 1 else: high = mid - 1 print(f"{ans}") return main()
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF 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 IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = a[:] b.sort() def Check(val): mn = [0] tmp = 0 for i in range(n): tmp += 1 if a[i] >= val else -1 mn.append(min(mn[i], tmp)) if i >= k - 1 and tmp > mn[i - k + 1]: return True return False l = 0 r = n - 1 while l < r: mid = (l + r + 1) // 2 if Check(b[mid]): l = mid else: r = mid - 1 print(b[l])
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 ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys from sys import stdin n, k = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) xl = 1 xr = n + 1 while xr - xl != 1: xm = (xl + xr) // 2 b = [0] for i in range(n): if a[i] >= xm: b.append(1) else: b.append(-1) for i in range(len(b) - 1): b[i + 1] += b[i] nmin = 0 flag = False for i in range(k, len(b)): nmin = min(nmin, b[i - k]) if b[i] - nmin > 0: flag = True break if flag: xl = xm else: xr = xm print(xl)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys def get_median(my_list): L = len(my_list) idx = int((L - 1) / 2) my_list = sorted(my_list) return my_list[idx] def first_guess(N, K, A): start = -K / 2 end = 1 * K / 2 max_median = min(A) while end < N: end += K / 2 if end > N: end = N start = end - K cur_seq = A[int(start) : int(end)] cur_median = get_median(cur_seq) if cur_median > max_median: max_median = cur_median return max_median def solve(N, K, A): A = [-1] + A + [-1] N += 2 if K % 2 == 0 and K < N: K += 1 max_median = first_guess(N, K, A) search = True while search: big_or_small = [] for i, a in enumerate(A): if a <= max_median: A[i] = 0 big_or_small.append(-1) else: big_or_small.append(1) seq_sum = sum(big_or_small) left_sum_list = [] right_sum_list = [] left_sub_sum = 0 right_sub_sum = 0 for i in range(N): left_sub_sum += big_or_small[i] right_sub_sum += big_or_small[N - i - 1] left_sum_list.append(left_sub_sum) right_sum_list.append(right_sub_sum) len_sum_list = len(left_sum_list) for i in range(1, len_sum_list): if left_sum_list[i] > left_sum_list[i - 1]: left_sum_list[i] = left_sum_list[i - 1] if right_sum_list[i] > right_sum_list[i - 1]: right_sum_list[i] = right_sum_list[i - 1] max_cut_size = N - K min_cut_sum = 0 for i in range(max_cut_size): if left_sum_list[i] < 0: min_right_sub_sum = right_sum_list[max_cut_size - i - 1] if left_sum_list[i] + min_right_sub_sum < min_cut_sum: min_cut_sum = left_sum_list[i] + min_right_sub_sum best_i = i if min_cut_sum < seq_sum: aux_seq = right_sum_list[: max_cut_size - best_i] len_aux_seq = len(aux_seq) argmin_aux_seq = -1 min_aux_seq = 2 for i in range(len_aux_seq): if aux_seq[i] < min_aux_seq: min_aux_seq = aux_seq[i] argmin_aux_seq = i new_best_sequence = A[best_i + 1 : -argmin_aux_seq - 1] max_median = get_median(new_best_sequence) else: search = False whole_median = get_median(A) if N > 1: whole_median1 = get_median(A[:-1]) whole_median2 = get_median(A[1:]) if whole_median1 > max_median: max_median = whole_median1 if whole_median2 > max_median: max_median = whole_median2 if whole_median > max_median: max_median = whole_median return max_median def run(): out = "" N, K = [int(x) for x in input().split()] A = [int(x) for x in input().split()] print(solve(N, K, A)) run()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys def bSearch(l, r, func): r += 1 while l < r: m = (l + r) // 2 if func(m) == False: l = m + 1 else: r = m return l def checkPossible(med, a, n, k): diff = 0 minDiff = [inf for _ in range(n)] minDiff[0] = 0 res = -1 for i, x in enumerate(a): if x >= med: diff += 1 else: diff -= 1 minDiff[i] = min(minDiff[i], diff) if i > 0: minDiff[i] = min(minDiff[i], minDiff[i - 1]) l = i + 1 - k if l == 0 and diff >= 1: res = 1 break if l - 1 >= 0 and diff - minDiff[l - 1] >= 1: res = 1 break return res def main(): n, k = readIntArr() a = readIntArr() def func(med): if checkPossible(med, a, n, k) == -1: return True else: return False ans = bSearch(1, n, func) - 1 print(ans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] inf = float("inf") MOD = 10**9 + 7 main()
IMPORT FUNC_DEF VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
from sys import stdin, stdout def max_median(n, k, a_a): l = 1 h = 2 * 10**5 while l < h: pre_sum = [0] * (n + 1) min_sum = [0] * (n + 1) found = False m = (l + h + 1) // 2 for i in range(n): v = 1 if a_a[i] >= m else -1 pre_sum[i + 1] = v + pre_sum[i] min_sum[i + 1] = min(min_sum[i], pre_sum[i + 1]) if i + 1 - k >= 0 and pre_sum[i + 1] - min_sum[i + 1 - k] > 0: found = True l = m break if not found: h = m - 1 return l n, k = map(int, stdin.readline().split()) a_a = list(map(int, stdin.readline().split())) r = max_median(n, k, a_a) stdout.write(str(r) + "\n")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys input = sys.stdin.readline n, k = map(int, input().split()) a = [int(item) for item in input().split()] l = 0 r = n + 1 while r - l > 1: mid = (l + r) // 2 is_larger = [] for item in a: if item >= mid: is_larger.append(1) else: is_larger.append(-1) cumsum = [0] for item in is_larger: cumsum.append(cumsum[-1] + item) cummin = [0] for item in cumsum[1:]: cummin.append(min(cummin[-1], item)) ok = False for i in range(k, n + 1): if cumsum[i] - cummin[i - k] > 0: ok = True break if ok: l = mid else: r = mid print(l)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys n, k = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) lena = len(a) def check(t): tmp = [(1 if aa >= t else -1) for aa in a] p = [0] * lena p[0] = tmp[0] for i in range(1, lena): p[i] = tmp[i] + p[i - 1] pm = [0] * lena for i in range(1, lena): pm[i] = min(p[i - 1], pm[i - 1]) for i in range(k - 1, lena): if p[i] - pm[i - k + 1] > 0: return True return False l = 1 r = 2 * 10**5 + 1 while r - l > 1: m = (l + r) // 2 if check(m): l = m else: r = m print(l)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def isValid(A, k, x): n = len(A) B = [(0) for x in range(n)] for i in range(n): B[i] = A[i] if B[i] >= x: B[i] = 1 else: B[i] = -1 prefix = [(0) for x in range(n)] minPrefix = [(0) for x in range(n)] prefix[0] = B[0] minPrefix[0] = B[0] sum = B[0] for i in range(1, n): sum += B[i] prefix[i] = sum minPrefix[i] = min(minPrefix[i - 1], prefix[i]) temp = -554 for i in range(n): if i + 1 < k: pass elif i + 1 == k: temp = max(temp, prefix[i]) else: temp = max(temp, prefix[i]) temp = max(temp, prefix[i] - minPrefix[i - k]) if temp > 0: return True return False def main(A, k): n = len(A) start = 0 if n == 1: return A[0] end = n - 1 C = sorted(A) ans = None while start <= end: mid = (start + end) // 2 if isValid(A, k, C[mid]): ans = C[mid] start = mid + 1 else: end = mid - 1 return ans n, k = map(int, input().split()) A = [int(i) for i in input().split()] print(main(A, k))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
import sys n, k = map(int, input().split()) a = list(map(int, input().split())) l = 0 r = n while l < r: if l + 1 == r: try1 = r flag = 0 tmp = 0 grid = [0] for i in range(n): if a[i] >= try1: tmp += 1 else: tmp -= 1 grid.append(tmp) lowestprev = 0 for i in range(k, n + 1): lowestprev = min(lowestprev, grid[i - k]) if grid[i] - lowestprev > 0: flag = 1 break if flag: l = r break try1 = (l + r) // 2 flag = 0 tmp = 0 grid = [0] for i in range(n): if a[i] >= try1: tmp += 1 else: tmp -= 1 grid.append(tmp) lowestprev = 0 for i in range(k, n + 1): lowestprev = min(lowestprev, grid[i - k]) if grid[i] - lowestprev > 0: flag = 1 break if flag: l = try1 else: r = try1 print(l)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
n, k = map(int, input().split()) l = list(map(int, input().split())) lo = 1 hi = n ans = 1 z = 0 while lo <= hi: m = (lo + hi) // 2 l2 = [] l1 = [0] * n for i in range(n): if l[i] < m: l2.append(-1) else: l2.append(1) for i in range(1, n): l2[i] += l2[i - 1] c = -n for i in range(n - 1, -1, -1): c = max(c, l2[i]) l1[i] = c c = n f = 0 if l1[k - 1] > 0: f = 1 c = l2[0] for i in range(1, n - k + 1): a = l1[i + k - 1] if a - c > 0: f = 1 break c = min(c, l2[i]) if f: ans = m lo = m + 1 else: hi = m if lo == hi: if z: break z += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def check_lower_bound(n, k, a, candidate_min): converted_a = [(1 if a_i >= candidate_min else -1) for a_i in a] prefix_sums = [0] for converted_a_i in converted_a: prefix_sums.append(prefix_sums[-1] + converted_a_i) prefix_min = 0 for r in range(k, n + 1): if prefix_sums[r] - prefix_min > 0: return True prefix_min = min(prefix_min, prefix_sums[r - k + 1]) return False n, k = map(int, input().split()) a = list(map(int, input().split())) low = 0 high = max(a) while low < high: mid = (low + high) // 2 if low + 1 == high: if check_lower_bound(n, k, a, high): low = high else: high = low elif check_lower_bound(n, k, a, mid): low = mid else: high = mid - 1 print(low)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
from itertools import * def check(x): b = [0] + [(1 if xx >= x else -1) for xx in a] sum = list(accumulate(b)) mx = -1000000000 for i in range(k, n + 1): mx = max(mx + b[i], sum[i] - sum[i - k]) if mx > 0: return True return False n, k = map(int, input().split()) a = list(map(int, input().split())) l, r = 1, n while l <= r: mid = (l + r) // 2 if check(mid): l = mid + 1 else: r = mid - 1 print(l - 1)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def check(A, x, k): n = len(A) C = [0] for i in range(len(A)): if A[i] >= x: C.append(C[-1] + 1) else: C.append(C[-1] - 1) mn = C[0] for i in range(k, n + 1): mn = min(mn, C[i - k]) if C[i] - mn > 0: return True return False n, k = map(int, input().split()) begin = 1 A = list(map(int, input().split())) end = max(A) while end - begin >= 2: middle = (end + begin) // 2 if check(A, middle, k): begin = middle else: end = middle if check(A, end, k): print(end) else: print(begin)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def proc1(m: int, arr: list) -> None: global cnt1 global cnt2 cnt1 = [0] * (n + 2) cnt2 = [0] * (n + 2) for i in range(1, n + 1): if arr[i] < m: cnt1[i] = cnt1[i - 1] - 1 else: cnt1[i] = cnt1[i - 1] + 1 for i in range(n, 0, -1): if arr[i] < m: cnt2[i] = max(0, cnt2[i + 1] - 1) else: cnt2[i] = cnt2[i + 1] + 1 pass def proc2(m: int, k: int, arr: list) -> bool: global cnt1 global cnt2 proc1(m, arr) for i in range(1, n + 1): if i + k > n + 1: break c1 = cnt1[i + k - 1] - cnt1[i - 1] c2 = cnt2[i + k] if c1 + c2 > 0: return True return False def solve(n: int, k: int, arr: list) -> int: arr2 = arr[1 : n + 1] arr2.sort() l, r = 1, n while l + 1 < r: mid = (l + r) // 2 median = arr2[mid - 1] if proc2(median, k, arr): l = mid else: r = mid - 1 if proc2(arr2[r - 1], k, arr): return arr2[r - 1] else: return arr2[l - 1] pass read = input().split() n = int(read[0]) k = int(read[1]) read = input().split() arr = [0] * (n + 2) for i in range(1, n + 1): arr[i] = int(read[i - 1]) ans = solve(n, k, arr) print(ans) pass
FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NONE FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are a given an array $a$ of length $n$. Find a subarray $a[l..r]$ with length at least $k$ with the largest median. A median in an array of length $n$ is an element which occupies position number $\lfloor \frac{n + 1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example: $median([1, 2, 3, 4]) = 2$, $median([3, 2, 1]) = 2$, $median([2, 1, 2, 1]) = 1$. Subarray $a[l..r]$ is a contiguous part of the array $a$, i. e. the array $a_l,a_{l+1},\ldots,a_r$ for some $1 \leq l \leq r \leq n$, its length is $r - l + 1$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output one integer $m$ — the maximum median you can get. -----Examples----- Input 5 3 1 2 3 2 1 Output 2 Input 4 2 1 2 3 4 Output 3 -----Note----- In the first example all the possible subarrays are $[1..3]$, $[1..4]$, $[1..5]$, $[2..4]$, $[2..5]$ and $[3..5]$ and the median for all of them is $2$, so the maximum possible median is $2$ too. In the second example $median([3..4]) = 3$.
def check(x): for i in range(n): a[i] = arr[i] for i in range(n): if a[i] >= x: a[i] = 1 else: a[i] = -1 sum_a[0] = a[0] for i in range(1, n): sum_a[i] = sum_a[i - 1] + a[i] min_a[0] = min(0, sum_a[0]) for i in range(1, n): min_a[i] = min(min_a[i - 1], sum_a[i]) for i in range(k - 1, n): if i == k - 1: if sum_a[i] > 0: return 1 elif sum_a[i] - min_a[i - k] > 0: return 1 return 0 n, k = map(int, input().split()) arr = list(map(int, input().split())) sum_a = [0] * n min_a = [0] * n a = [0] * n s = 1 e = n ans = -1 while s <= e: mid = (s + e) // 2 if check(mid): ans = mid s = mid + 1 else: e = mid - 1 print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
In Morse code, an letter of English alphabet is represented as a string of some length from $1$ to $4$. Moreover, each Morse code representation of an English letter contains only dots and dashes. In this task, we will represent a dot with a "0" and a dash with a "1". Because there are $2^1+2^2+2^3+2^4 = 30$ strings with length $1$ to $4$ containing only "0" and/or "1", not all of them correspond to one of the $26$ English letters. In particular, each string of "0" and/or "1" of length at most $4$ translates into a distinct English letter, except the following four strings that do not correspond to any English alphabet: "0011", "0101", "1110", and "1111". You will work with a string $S$, which is initially empty. For $m$ times, either a dot or a dash will be appended to $S$, one at a time. Your task is to find and report, after each of these modifications to string $S$, the number of non-empty sequences of English letters that are represented with some substring of $S$ in Morse code. Since the answers can be incredibly tremendous, print them modulo $10^9 + 7$. -----Input----- The first line contains an integer $m$ ($1 \leq m \leq 3\,000$) — the number of modifications to $S$. Each of the next $m$ lines contains either a "0" (representing a dot) or a "1" (representing a dash), specifying which character should be appended to $S$. -----Output----- Print $m$ lines, the $i$-th of which being the answer after the $i$-th modification to $S$. -----Examples----- Input 3 1 1 1 Output 1 3 7 Input 5 1 0 1 0 1 Output 1 4 10 22 43 Input 9 1 1 0 0 0 1 1 0 1 Output 1 3 10 24 51 109 213 421 833 -----Note----- Let us consider the first sample after all characters have been appended to $S$, so S is "111". As you can see, "1", "11", and "111" all correspond to some distinct English letter. In fact, they are translated into a 'T', an 'M', and an 'O', respectively. All non-empty sequences of English letters that are represented with some substring of $S$ in Morse code, therefore, are as follows. "T" (translates into "1") "M" (translates into "11") "O" (translates into "111") "TT" (translates into "11") "TM" (translates into "111") "MT" (translates into "111") "TTT" (translates into "111") Although unnecessary for this task, a conversion table from English alphabets into Morse code can be found here.
MOD = 10**9 + 7 BAD = [0, 0, 1, 1], [0, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1] def zfunc(s): z = [0] * len(s) l = r = 0 for i in range(1, len(s)): if i <= r: z[i] = min(r - i + 1, z[i - l]) while i + z[i] < len(s) and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: l, r = i, i + z[i] - 1 return z n = int(input()) s = [] sm = 0 for i in range(1, n + 1): s.append(int(input())) cur = 0 f = [0] * (i + 1) sum4 = f[i] = 1 for j in range(i - 1, -1, -1): if j + 4 < i: sum4 -= f[j + 5] if j + 4 <= i and s[j : j + 4] in BAD: f[j] -= f[j + 4] f[j] = (f[j] + sum4) % MOD sum4 += f[j] z = zfunc(s[::-1]) new = i - max(z) sm = (sm + sum(f[:new])) % MOD print(sm)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
try: t = int(input()) for i in range(t): n = int(input()) a = input() b = [0] * n b[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): if a[i] == a[i + 1]: b[i] = b[i + 1] elif ( a[i] == "P" and a[i + 1] == "S" or a[i] == "S" and a[i + 1] == "R" or a[i] == "R" and a[i + 1] == "P" ): b[i] = b[i + 1] else: j = i while j < n - 1: if ( a[i] == "P" and a[j + 1] == "S" or a[i] == "S" and a[j + 1] == "R" or a[i] == "R" and a[j + 1] == "P" or a[i] == a[j + 1] ): b[i] = b[j + 1] break elif j == n - 2: b[i] = a[i] j = j + 1 else: j = j + 1 print(*b, sep="") except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def match(a, b): if a == b: return a elif a == "R" and b == "P" or a == "P" and b == "R": return "P" elif a == "R" and b == "S" or a == "S" and b == "R": return "R" elif a == "P" and b == "S" or a == "S" and b == "P": return "S" else: return False for z in range(int(input())): n = int(input()) s = input() dp_r = [""] * (n + 1) dp_p = [""] * (n + 1) dp_s = [""] * (n + 1) ans = [""] * (n + 1) ans[n] = s[n - 1] dp_r[n] = match("R", s[n - 1]) dp_p[n] = match("P", s[n - 1]) dp_s[n] = match("S", s[n - 1]) for i in range(n - 1, 0, -1): r_res = match("R", s[i - 1]) if r_res == "R": dp_r[i] = dp_r[i + 1] elif r_res == "P": dp_r[i] = dp_p[i + 1] elif r_res == "S": dp_r[i] = dp_s[i + 1] p_res = match("P", s[i - 1]) if p_res == "R": dp_p[i] = dp_r[i + 1] elif p_res == "P": dp_p[i] = dp_p[i + 1] elif p_res == "S": dp_p[i] = dp_s[i + 1] s_res = match("S", s[i - 1]) if s_res == "R": dp_s[i] = dp_r[i + 1] elif s_res == "P": dp_s[i] = dp_p[i + 1] elif s_res == "S": dp_s[i] = dp_s[i + 1] if s[i - 1] == "R": ans[i] = dp_r[i + 1] elif s[i - 1] == "P": ans[i] = dp_p[i + 1] elif s[i - 1] == "S": ans[i] = dp_s[i + 1] for i in range(n + 1): print(ans[i], end="") print()
FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
who_beats = {"R": "P", "P": "S", "S": "R"} for _ in range(int(input())): n = int(input()) s = input() ind = {"R": -1, "P": -1, "S": -1} ans = ["?"] * n for i in reversed(range(n)): indwb = ind[who_beats[s[i]]] ans[i] = s[i] if indwb == -1 else ans[indwb] ind[s[i]] = i print("".join(ans))
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def rps(a, b): if ( a == "R" and b == "R" or a == "P" and b == "P" or a == "S" and b == "S" or a == "R" and b == "S" or a == "S" and b == "P" or a == "P" and b == "R" ): return a else: return b for i in range(int(input())): n = int(input()) s = input() k = [0] k[0] = s[n - 1] for j in range(n - 2, -1, -1): l = j + 1 c = 0 m = s[j] while 1: if rps(m, s[l]) == s[l] and l != n - 1: k.append(k[-c - 1]) break elif l == n - 1: k.append(rps(m, s[l])) break else: m = rps(m, s[l]) c += 1 l += 1 s = "".join(k) print(s[::-1])
FUNC_DEF IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR NUMBER
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
test = int(input()) who_beat = {"R": "P", "P": "S", "S": "R"} for _ in range(test): n = int(input()) s = input() ans = ["P"] * n idx = {"R": -1, "S": -1, "P": -1} for i in range(n - 1, -1, -1): competitor = who_beat[s[i]] cur = idx[competitor] if cur == -1: ans[i] = s[i] else: ans[i] = ans[cur] idx[s[i]] = i print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
t = int(input()) myDict = { ("S", "S"): "S", ("S", "P"): "S", ("S", "R"): "R", ("P", "P"): "P", ("P", "S"): "S", ("P", "R"): "P", ("R", "R"): "R", ("R", "P"): "P", ("R", "S"): "R", } while t: n = int(input()) s = input().rstrip() R_array = [""] * n P_array = [""] * n S_array = [""] * n ans = [""] * n ans[n - 1] = s[n - 1] R_array[n - 1] = myDict["R", s[n - 1]] P_array[n - 1] = myDict["P", s[n - 1]] S_array[n - 1] = myDict["S", s[n - 1]] for i in range(n - 2, -1, -1): comb = "R", s[i] ansOfFight = myDict[comb] if ansOfFight == "R": R_array[i] = R_array[i + 1] elif ansOfFight == "P": R_array[i] = P_array[i + 1] else: R_array[i] = S_array[i + 1] comb = "P", s[i] ansOfFight = myDict[comb] if ansOfFight == "R": P_array[i] = R_array[i + 1] elif ansOfFight == "P": P_array[i] = P_array[i + 1] else: P_array[i] = S_array[i + 1] comb = "S", s[i] ansOfFight = myDict[comb] if ansOfFight == "R": S_array[i] = R_array[i + 1] elif ansOfFight == "P": S_array[i] = P_array[i + 1] else: S_array[i] = S_array[i + 1] if s[i] == "R": ans[i] = R_array[i] elif s[i] == "P": ans[i] = P_array[i] else: ans[i] = S_array[i] for i in range(len(ans)): print(ans[i], end="") print() t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR STRING VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
for _ in range(int(input())): n = int(input()) string = input() P, R, S = -1, -1, -1 res_arr = list(string[:]) if string[-1] == "P": P = n - 1 elif string[-1] == "R": R = n - 1 else: S = n - 1 for i in range(n - 2, -1, -1): if string[i] == "P": P = i if S != -1: res_arr[i] = res_arr[S] else: res_arr[i] = string[i] elif string[i] == "S": S = i if R != -1: res_arr[i] = res_arr[R] else: res_arr[i] = string[i] elif string[i] == "R": R = i if P != -1: res_arr[i] = res_arr[P] else: res_arr[i] = string[i] new_string = "".join(res_arr) print(new_string)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
import sys input = sys.stdin.readline T = int(input()) def find_winner(a, b): if a == "R" and b == "S": return a elif a == "S" and b == "P": return a elif a == "P" and b == "R": return a else: return b def is_a_winner(a, b): if a == "R" and b == "S": return True elif a == "S" and b == "P": return True elif a == "P" and b == "R": return True else: return False def is_won(p1, p2): result = False if p1 == "R": return p2 == "S" elif p1 == "P": return p2 == "R" else: return p2 == "P" def find_ans(m, n): global N, S winner_char = find_winner(m, S[n + 10]) if n == N - 1: return winner_char else: return find_ans(winner_char, n + 1) def testcase(): def roll_win(i, j): result = S[i - 1] while j < N and is_won(S[i - 1], S[j]): j += 1 if j < N: result = answer[j] return result N = int(input()) S = input() answer = [""] * N answer[N - 1] = S[N - 1] for i in range(N - 1, 0, -1): if is_won(S[i - 1], S[i]): j = i + 1 answer[i - 1] = roll_win(i, j) else: answer[i - 1] = answer[i] print("".join(answer)) for _ in range(T): testcase()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR RETURN VAR FUNC_DEF IF VAR STRING VAR STRING RETURN NUMBER IF VAR STRING VAR STRING RETURN NUMBER IF VAR STRING VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR STRING RETURN VAR STRING IF VAR STRING RETURN VAR STRING RETURN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
t = int(input()) for i in range(t): d1 = dict(R="P", S="R", P="S") d2 = dict(R=-1, S=-1, P=-1) n = int(input()) s = list(input()) ans = [0] * n for j in range(n - 1, -1, -1): c = d2[d1[s[j]]] if c == -1: ans[j] = s[j] else: ans[j] = ans[c] d2[s[j]] = j print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING STRING ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
R = 0 P = 1 S = 2 for _ in range(int(input())): input() moves = [] for c in input(): if c == "R": moves.append(R) elif c == "P": moves.append(P) else: moves.append(S) N = len(moves) W = [-1] * N W[-1] = moves[-1] last = [N + 1] * 3 last[moves[-1]] = N - 1 for i in range(N - 2, -1, -1): if moves[i] == moves[i + 1] or (moves[i] + 1) % 3 == moves[i + 1]: W[i] = W[i + 1] else: nextdiff = min(last[moves[i]], last[(moves[i] + 1) % 3]) if nextdiff > N: W[i] = moves[i] else: W[i] = W[nextdiff] last[moves[i]] = i for move in W: if move == R: print("R", end="") elif move == P: print("P", end="") else: print("S", end="") print()
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING STRING IF VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
for _ in range(int(input())): n = int(input()) s = list(input()) result = [] result.append(s[-1]) dp = {} dp[s[-1]] = s[-1] for i in range(n - 1, 0, -1): if s[i - 1] == "P" and "S" in dp: result.append(dp["S"]) dp["P"] = dp["S"] elif s[i - 1] == "S" and "R" in dp: result.append(dp["R"]) dp["S"] = dp["R"] elif s[i - 1] == "R" and "P" in dp: result.append(dp["P"]) dp["R"] = dp["P"] else: result.append(s[i - 1]) dp[s[i - 1]] = s[i - 1] result.reverse() print("".join(_ for _ in result))
FOR VAR 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 ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING STRING VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR STRING VAR STRING IF VAR BIN_OP VAR NUMBER STRING STRING VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR STRING VAR STRING IF VAR BIN_OP VAR NUMBER STRING STRING VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
t = int(input()) for i in range(t): n = int(input()) s = input() if n == 1: print(s) continue s1 = s[n - 1] for j in range(n - 2, -1, -1): temp = s[j] index = -1 for k in range(j + 1, n): if temp == s[k]: index = k break elif temp == "R" and s[k] == "P": index = k break elif temp == "P" and s[k] == "S": index = k break elif temp == "S" and s[k] == "R": index = k break if index != -1: s1 = s1[k - j - 1] + s1 else: s1 = temp + s1 print(s1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR STRING VAR VAR STRING ASSIGN VAR VAR IF VAR STRING VAR VAR STRING ASSIGN VAR VAR IF VAR STRING VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def getWinner(tuple_): if tuple_[0] == tuple_[1]: return tuple_[0] elif "S" in tuple_ and "P" in tuple_: return "S" elif "S" in tuple_ and "R" in tuple_: return "R" else: return "P" def printWinners(si, n): mat_r = ["X" for i in range(n + 1)] mat_p = mat_r.copy() mat_s = mat_r.copy() ans = mat_r.copy() ans[n] = si[n - 1] mat_r[n] = getWinner(("R", si[n - 1])) mat_p[n] = getWinner(("P", si[n - 1])) mat_s[n] = getWinner(("S", si[n - 1])) for i in range(n - 1, 0, -1): r_res = getWinner(("R", si[i - 1])) if r_res == "R": mat_r[i] = mat_r[i + 1] elif r_res == "P": mat_r[i] = mat_p[i + 1] else: mat_r[i] = mat_s[i + 1] p_res = getWinner(("P", si[i - 1])) if p_res == "R": mat_p[i] = mat_r[i + 1] elif p_res == "P": mat_p[i] = mat_p[i + 1] else: mat_p[i] = mat_s[i + 1] s_res = getWinner(("S", si[i - 1])) if s_res == "R": mat_s[i] = mat_r[i + 1] elif s_res == "P": mat_s[i] = mat_p[i + 1] else: mat_s[i] = mat_s[i + 1] if si[i - 1] == "R": ans[i] = mat_r[i + 1] elif si[i - 1] == "P": ans[i] = mat_p[i + 1] else: ans[i] = mat_s[i + 1] return "".join(ans[1:]) for _ in range(int(input())): n = int(input()) si = input() print(printWinners(si, n))
FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER IF STRING VAR STRING VAR RETURN STRING IF STRING VAR STRING VAR RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
import sys input = sys.stdin.readline T = int(input()) def find_winner(a, b): if a == "R" and b == "S": return a elif a == "S" and b == "P": return a elif a == "P" and b == "R": return a else: return b def is_a_winner(a, b): if a == "R" and b == "S": return True elif a == "S" and b == "P": return True elif a == "P" and b == "R": return True else: return False def find_ans(m, n): global N, S winner_char = find_winner(m, S[n + 10]) if n == N - 1: return winner_char else: return find_ans(winner_char, n + 1) for _ in range(T): N = int(input()) S = input() answer = [""] * N answer[N - 1] = S[N - 1] for i in range(N - 1, 0, -1): if is_a_winner(S[i - 1], S[i]): j = i + 1 ans = S[i - 1] while j < N: if is_a_winner(S[i - 1], S[j]): j += 1 else: ans = answer[j] break answer[i - 1] = ans else: answer[i - 1] = answer[i] print("".join(answer))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR RETURN VAR FUNC_DEF IF VAR STRING VAR STRING RETURN NUMBER IF VAR STRING VAR STRING RETURN NUMBER IF VAR STRING VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def win(pl1, pl2): game = pl1 + pl2 if game == "SR" or game == "PS" or game == "RP" or pl1 == pl2: return False return True def SPQR(s): if len(s) == 1: return s res = s[0] order = [(s[0], 0)] for i in range(1, len(s)): winAll = True for ordr in order: if win(ordr[0], s[i]): res += res[ordr[1]] winAll = False break if winAll: res += s[i] if s[i] != order[0][0]: order.insert(0, (s[i], i)) return res[::-1] T = int(input()) for _ in range(T): N = int(input()) S = input() print(SPQR(S[::-1]))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR STRING VAR STRING VAR STRING VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
choices = {"R": ["R", "S", "P"], "P": ["P", "R", "S"], "S": ["S", "P", "R"]} for test in range(int(input())): N = int(input()) S = input() first_answer = S[-1] second_answer = choices[first_answer][2] current_answer = first_answer same = False final_string_list = [first_answer] for x in range(N - 1): if same == True: final_string_list.append(current_answer) elif S[-x - 2] == choices[S[-x - 1]][0]: final_string_list.append(current_answer) elif S[-x - 2] == choices[S[-x - 1]][1]: final_string_list.append(current_answer) same = True elif S[-x - 2] == choices[S[-x - 1]][2]: if current_answer == first_answer: current_answer = second_answer elif current_answer == second_answer: current_answer = first_answer final_string_list.append(current_answer) final_string_list = [ final_string_list[-1 - x] for x in range(len(final_string_list)) ] final_string = "".join(final_string_list) print(final_string)
ASSIGN VAR DICT STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
t = int(input()) l = [] while t > 0: n = int(input()) mov = list(map(str, input())) r = {} p = {} s = {} S = "" if n == 1: l.append(mov[0]) else: S += mov[n - 1] if mov[n - 1] == "R": r[n - 2] = "R" p[n - 2] = "P" s[n - 2] = "R" elif mov[n - 1] == "P": r[n - 2] = "P" p[n - 2] = "P" s[n - 2] = "S" elif mov[n - 1] == "S": r[n - 2] = "R" p[n - 2] = "S" s[n - 2] = "S" if mov[n - 2] == "R": S += r[n - 2] elif mov[n - 2] == "P": S += p[n - 2] else: S += s[n - 2] for i in range(n - 3, -1, -1): if mov[i + 1] == "R": r[i] = r[i + 1] p[i] = p[i + 1] s[i] = r[i + 1] elif mov[i + 1] == "P": r[i] = p[i + 1] p[i] = p[i + 1] s[i] = s[i + 1] elif mov[i + 1] == "S": r[i] = r[i + 1] p[i] = s[i + 1] s[i] = s[i + 1] if mov[i] == "P": S += p[i] elif mov[i] == "R": S += r[i] else: S += s[i] l.append(S[::-1]) t -= 1 print("\n".join(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR IF VAR VAR STRING VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def whowon(s1, s2): if s1 == "P" and s2 == "R": return s1 elif s1 == "P" and s2 == "S": return s2 elif s1 == "P" and s2 == "P": return s1 elif s1 == "R" and s2 == "S": return s1 elif s1 == "R" and s2 == "P": return s2 elif s1 == "R" and s2 == "R": return s1 elif s1 == "S" and s2 == "S": return s1 elif s1 == "S" and s2 == "P": return s1 elif s1 == "S" and s2 == "R": return s2 def getarrayrow(x): if x == "S": return 0 if x == "R": return 1 if x == "P": return 2 T = int(input()) for _ in range(T): answer = "" size = int(input()) data = input() data = list(data) arr = [[(0) for i in range(size)] for j in range(3)] for i in range(size - 2, -1, -1): j = i x = data[j] while j != size - 1: if arr[getarrayrow(x)][j + 1] == 0: x = whowon(x, data[j + 1]) j += 1 else: x = arr[getarrayrow(x)][j + 1] break arr[getarrayrow(data[i])][i + 1] = x for i in range(size - 1): answer += str(arr[getarrayrow(data[i])][i + 1]) answer += data[size - 1] print(answer)
FUNC_DEF IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING RETURN VAR FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def checker(v1, v2): if v1 == v2: return v1 if v1 == "S" and v2 == "R" or v1 == "R" and v2 == "P" or v1 == "P" and v2 == "S": return v2 if v1 == "R" and v2 == "S" or v1 == "P" and v2 == "R" or v1 == "S" and v2 == "P": return v1 def solution(ls: list): n = len(ls) dp_r = [0] * (n + 1) dp_p = [0] * (n + 1) dp_s = [0] * (n + 1) ans = [0] * (n + 1) ans[n] = ls[n - 1] dp_r[n] = checker("R", ls[n - 1]) dp_p[n] = checker("P", ls[n - 1]) dp_s[n] = checker("S", ls[n - 1]) for i in range(n - 1, 0, -1): r_res = checker("R", ls[i - 1]) if r_res == "R": dp_r[i] = dp_r[i + 1] elif r_res == "P": dp_r[i] = dp_p[i + 1] else: dp_r[i] = dp_s[i + 1] p_res = checker("P", ls[i - 1]) if p_res == "R": dp_p[i] = dp_r[i + 1] elif p_res == "P": dp_p[i] = dp_p[i + 1] else: dp_p[i] = dp_s[i + 1] s_res = checker("S", ls[i - 1]) if s_res == "R": dp_s[i] = dp_r[i + 1] elif s_res == "P": dp_s[i] = dp_p[i + 1] else: dp_s[i] = dp_s[i + 1] if ls[i - 1] == "R": ans[i] = dp_r[i + 1] elif ls[i - 1] == "P": ans[i] = dp_p[i + 1] else: ans[i] = dp_s[i + 1] for i in range(1, n + 1): print(ans[i], end="") print() t = int(input()) while t: n = int(input()) string = list(input()) solution(string) t -= 1
FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING RETURN VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING RETURN VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
N = int(input()) for i in range(N): size = int(input()) str = input() res = [] for i in range(size): res.append(" ") last_r = "$" last_p = "$" last_s = "$" i = size - 1 while i >= 0: if str[i] == "R": if last_p == "$": res[i] = "R" else: res[i] = last_p last_r = res[i] elif str[i] == "P": if last_s == "$": res[i] = "P" else: res[i] = last_s last_p = res[i] elif str[i] == "S": if last_r == "$": res[i] = "S" else: res[i] = last_r last_s = res[i] i -= 1 to_print = "" for i in range(size): to_print += res[i] print(to_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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def battle(a, b): if a == b: return a elif a == "R" and b == "P" or a == "P" and b == "R": return "P" elif a == "P" and b == "S" or a == "S" and b == "P": return "S" else: return "R" t = int(input()) for i in range(t): N = int(input()) result = [0] * N a = input() if N == 1: print(a[0]) else: R = [0] * N P = [0] * N S = [0] * N ch = "" result[N - 1] = a[N - 1] R[N - 2] = battle("R", a[N - 1]) P[N - 2] = battle("P", a[N - 1]) S[N - 2] = battle("S", a[N - 1]) if a[N - 2] == "R": result[N - 2] = R[N - 2] elif a[N - 2] == "P": result[N - 2] = P[N - 2] elif a[N - 2] == "S": result[N - 2] = S[N - 2] for i in range(N - 3, -1, -1): temp = battle("R", a[i + 1]) if temp == "R": R[i] = R[i + 1] elif temp == "P": R[i] = P[i + 1] else: R[i] = S[i + 1] temp = battle("P", a[i + 1]) if temp == "R": P[i] = R[i + 1] elif temp == "P": P[i] = P[i + 1] else: P[i] = S[i + 1] temp = battle("S", a[i + 1]) if temp == "R": S[i] = R[i + 1] elif temp == "P": S[i] = P[i + 1] else: S[i] = S[i + 1] if a[i] == "R": result[i] = R[i] elif a[i] == "P": result[i] = P[i] else: result[i] = S[i] print("".join(result))
FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING RETURN STRING 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 LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
for _ in range(int(input())): n = int(input()) l = list(x for x in input()) w = [""] * len(l) d = {"R": -1, "S": -1, "P": -1} for i in range(len(l) - 1, -1, -1): if l[i] == "S": if d["R"] == -1: w[i] = l[i] else: w[i] = w[d["R"]] d["S"] = i elif l[i] == "R": if d["P"] == -1: w[i] = l[i] else: w[i] = w[d["P"]] d["R"] = i if l[i] == "P": if d["S"] == -1: w[i] = l[i] else: w[i] = w[d["S"]] d["P"] = i print("".join(w))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR STRING NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR STRING ASSIGN VAR STRING VAR IF VAR VAR STRING IF VAR STRING NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR STRING ASSIGN VAR STRING VAR IF VAR VAR STRING IF VAR STRING NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR STRING ASSIGN VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
win = {"R": "P", "P": "S", "S": "R"} for i in range(int(input())): size = int(input()) s = input() ans = ["?"] * size d = {"R": -1, "P": -1, "S": -1} for i in range(size - 1, -1, -1): x = d[win[s[i]]] if x == -1: ans[i] = s[i] else: ans[i] = ans[x] d[s[i]] = i for i in ans: print(i, end="") print()
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def w(i): ind = s.find(comp[s[i]], i + 1) if ind == -1: d[f"w({i})"] = s[i] return s[i] else: d[f"w({i})"] = d[f"w({ind})"] return d[f"w({ind})"] for _ in range(int(input())): n = int(input()) s = input() ans = "" d = {} comp = {"R": "P", "S": "R", "P": "S"} for i in range(n - 1, -1, -1): ans += w(i) print(ans[::-1])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING VAR STRING VAR VAR RETURN VAR VAR ASSIGN VAR STRING VAR STRING VAR STRING VAR STRING RETURN VAR STRING VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def is_won(p1, p2): result = False if p1 == "R": result = p2 == "S" elif p1 == "P": result = p2 == "R" else: result = p2 == "P" return result def testcase(): def roll_win(plyr_idx, opp_idx): result = S[plyr_idx] while opp_idx < N and is_won(S[plyr_idx], S[opp_idx]): opp_idx += 1 if opp_idx < N: result = R[opp_idx] return result N = int(input()) S = input() R = [None for s in S] R[N - 1] = S[N - 1] for i in range(N - 2, -1, -1): R[i] = roll_win(i, i + 2) if is_won(S[i], S[i + 1]) else R[i + 1] return "".join(R) for i in range(int(input())): print(testcase())
FUNC_DEF ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING RETURN VAR FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NONE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def logic(list, N): complementary = {"P": "S", "R": "P", "S": "R"} answer = "" if N == 1: answer = answer + list[0] else: stop = False unnecessary = 0 flipped = True for i in range(N - 1): if ( list[N - 1 - i] + list[N - 2 - i] == "SP" or list[N - 1 - i] + list[N - 2 - i] == "PR" or list[N - 1 - i] + list[N - 2 - i] == "RS" ): stop = True unnecessary = i + 1 elif ( list[N - 1 - i] + list[N - 2 - i] == "PS" or list[N - 1 - i] + list[N - 2 - i] == "RP" or list[N - 1 - i] + list[N - 2 - i] == "SR" ): flipped = not flipped if stop: break if stop: for i in range(N - unnecessary): if flipped: answer = answer + list[len(list) - 1] list = list[1:] else: answer = answer + complementary[list[len(list) - 1]] list = list[1:] for i in range(unnecessary - 1): if flipped: answer = answer + list[len(list) - 1] else: answer = answer + complementary[list[len(list) - 1]] if list[0] != list[1]: flipped = not flipped list = list[1:] else: for i in range(N - 1): if flipped: answer = answer + list[len(list) - 1] else: answer = answer + complementary[list[len(list) - 1]] if list[0] != list[1]: flipped = not flipped list = list[1:] answer = answer + list[0] return answer T = int(input()) for i in range(T): N = int(input()) moves = input() print(logic(moves, N))
FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR IF VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
t = int(input()) for i in range(t): n = int(input()) s = input() dp = [0] * n dp[-1] = s[-1] idx = {"R": -1, "P": -1, "S": -1} idx[s[-1]] = n - 1 if n > 1: wins = {"P": "R", "S": "P", "R": "S"} loses = {"P": "S", "R": "P", "S": "R"} if s[-1] == s[-2] or s[-2] == wins[s[-1]]: dp[-2] = s[-1] else: dp[-2] = s[-2] idx[s[-2]] = n - 2 for i in range(n - 3, -1, -1): idx[s[i]] = i j = idx[loses[s[i]]] if j != -1: dp[i] = dp[j] else: dp[i] = s[i] print("".join(dp))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
t = int(input()) for i in range(t): n = int(input()) s = input() win_cycle = {"S": "R", "R": "P", "P": "S"} indx = {"S": -1, "R": -1, "P": -1} ans = [""] * n for j in range(n - 1, -1, -1): ch = indx[win_cycle[s[j]]] if ch == -1: ans[j] = s[j] else: ans[j] = ans[ch] indx[s[j]] = j print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def rps(a, b): if a == b: return a elif a == "R" and b == "P" or a == "P" and b == "R": return "P" elif a == "S" and b == "P" or a == "P" and b == "S": return "S" elif a == "S" and b == "R" or a == "R" and b == "S": return "R" for _ in range(int(input())): n = int(input()) s = input() dpr = [0] * n dpp = [0] * n dps = [0] * n ans = [0] * n ans[n - 1] = s[n - 1] dpr[n - 1] = rps(s[n - 1], "R") dpp[n - 1] = rps(s[n - 1], "P") dps[n - 1] = rps(s[n - 1], "S") for i in range(n - 1, 0, -1): win = rps(s[i - 1], "R") if win == "R": dpr[i - 1] = dpr[i] elif win == "P": dpr[i - 1] = dpp[i] elif win == "S": dpr[i - 1] = dps[i] win = rps(s[i - 1], "P") if win == "R": dpp[i - 1] = dpr[i] elif win == "P": dpp[i - 1] = dpp[i] elif win == "S": dpp[i - 1] = dps[i] win = rps(s[i - 1], "S") if win == "R": dps[i - 1] = dpr[i] elif win == "P": dps[i - 1] = dpp[i] elif win == "S": dps[i - 1] = dps[i] if s[i - 1] == "R": ans[i - 1] = dpr[i] elif s[i - 1] == "P": ans[i - 1] = dpp[i] elif s[i - 1] == "S": ans[i - 1] = dps[i] print("".join(ans))
FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def findans(s, n, overcome, idx): a = [(-1) for k in range(n)] for j in range(n - 1, -1, -1): index = idx[overcome[s[j]]] if index == -1: a[j] = s[j] else: a[j] = a[index] idx[s[j]] = j return "".join(a) t = int(input()) overcome = {"R": "P", "P": "S", "S": "R"} for i in range(t): n = int(input()) s = input() idx = {"R": -1, "P": -1, "S": -1} ans = findans(s, n, overcome, idx) print(ans)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
import sys inp_list = list(sys.stdin) win_map = {"R": "S", "S": "P", "P": "R"} lose_map = {"S": "R", "R": "P", "P": "S"} all_vals_set = set(["R", "S", "P"]) for indx in range(1, len(inp_list)): if indx % 2 == 1: continue s = inp_list[indx].rstrip("\n") single_op_flag = False length = len(s) possible_outputs_set = set([s[-1], lose_map[s[-1]]]) op = s[-1] switch_dict = {op: lose_map[op], lose_map[op]: op} for i in range(1, length): if s[length - i] == s[length - i - 1]: op += op[-1] continue if win_map[s[length - i - 1]] == s[length - i]: op += switch_dict[op[-1]] if lose_map[s[length - i - 1]] == s[length - i]: single_op_flag = True break if single_op_flag: op += op[-1] * (length - len(op)) op = op[::-1] print(op)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
for _ in range(int(input())): n = int(input()) s = list(input()) result = [""] * n map_s = {"R": "P", "P": "S", "S": "R"} map_ind = {} for i in range(n - 1, -1, -1): t = map_s[s[i]] if t in map_ind: result[i] = result[map_ind[t]] else: result[i] = s[i] map_ind[s[i]] = i print("".join(_ for _ in result))
FOR VAR 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 ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def move_is_rock(move2): if move2 == "P": return "P" elif move2 == "S": return "R" def move_is_paper(move2): if move2 == "S": return "S" elif move2 == "R": return "P" def move_is_scissor(move2): if move2 == "R": return "R" elif move2 == "P": return "S" def single_match(move1, move2): if move1 == move2: return move1 elif move1 == "R": return move_is_rock(move2) elif move1 == "P": return move_is_paper(move2) elif move1 == "S": return move_is_scissor(move2) else: exit() def templated_check( sign_RPS, s_moves, i, dest_array, rock_array, paper_array, scissor_array ): result = single_match(sign_RPS, s_moves[i - 1]) if result == "R": dest_array[i] = rock_array[i + 1] elif result == "P": dest_array[i] = paper_array[i + 1] elif result == "S": dest_array[i] = scissor_array[i + 1] tests = int(input()) for test in range(tests): number_of_players = int(input()) s_moves = input() rock_array = [""] * (number_of_players + 1) paper_array = [""] * (number_of_players + 1) scissor_array = [""] * (number_of_players + 1) answer_array = [""] * (number_of_players + 1) answer_array[number_of_players] = s_moves[number_of_players - 1] rock_array[number_of_players] = single_match("R", s_moves[number_of_players - 1]) paper_array[number_of_players] = single_match("P", s_moves[number_of_players - 1]) scissor_array[number_of_players] = single_match("S", s_moves[number_of_players - 1]) for i in range(number_of_players - 1, 0, -1): templated_check( "R", s_moves, i, rock_array, rock_array, paper_array, scissor_array ) templated_check( "P", s_moves, i, paper_array, rock_array, paper_array, scissor_array ) templated_check( "S", s_moves, i, scissor_array, rock_array, paper_array, scissor_array ) paper_result = single_match("P", s_moves[i - 1]) if s_moves[i - 1] == "R": answer_array[i] = rock_array[i + 1] elif s_moves[i - 1] == "P": answer_array[i] = paper_array[i + 1] elif s_moves[i - 1] == "S": answer_array[i] = scissor_array[i + 1] for i in range(1, number_of_players + 1): print(answer_array[i], end="") print()
FUNC_DEF IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING FUNC_DEF IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING FUNC_DEF IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING FUNC_DEF IF VAR VAR RETURN VAR IF VAR STRING RETURN FUNC_CALL VAR VAR IF VAR STRING RETURN FUNC_CALL VAR VAR IF VAR STRING RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
for _ in range(int(input())): n = int(input()) A = input() d = {} d["R"] = "P" d["P"] = "S" d["S"] = "R" ind = {} ind["R"], ind["S"], ind["P"] = -1, -1, -1 ans = [0] * n for i in range(n - 1, -1, -1): idx = ind[d[A[i]]] if idx == -1: ans[i] = A[i] else: ans[i] = ans[idx] ind[A[i]] = i a = "".join(ans) print(a)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING STRING ASSIGN VAR STRING STRING ASSIGN VAR STRING STRING ASSIGN VAR DICT ASSIGN VAR STRING VAR STRING VAR STRING NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
ROCK_SIGN = "R" PAPER_SIGN = "P" SCCISORS_SIGN = "S" def make_turn(player1, player2): if player1 == player2: return player1 if ( player1 == ROCK_SIGN and player2 == SCCISORS_SIGN or player1 == SCCISORS_SIGN and player2 == ROCK_SIGN ): return ROCK_SIGN if ( player1 == ROCK_SIGN and player2 == PAPER_SIGN or player1 == PAPER_SIGN and player2 == ROCK_SIGN ): return PAPER_SIGN if ( player1 == SCCISORS_SIGN and player2 == PAPER_SIGN or player1 == PAPER_SIGN and player2 == SCCISORS_SIGN ): return SCCISORS_SIGN T = int(input()) for _ in range(T): numberOfPlayers = int(input()) input_str = input() res = [" "] * (numberOfPlayers + 1) rock = [" "] * (numberOfPlayers + 1) paper = [" "] * (numberOfPlayers + 1) sccisors = [" "] * (numberOfPlayers + 1) res[numberOfPlayers] = input_str[numberOfPlayers - 1] rock[numberOfPlayers] = make_turn(ROCK_SIGN, input_str[numberOfPlayers - 1]) paper[numberOfPlayers] = make_turn(PAPER_SIGN, input_str[numberOfPlayers - 1]) sccisors[numberOfPlayers] = make_turn(SCCISORS_SIGN, input_str[numberOfPlayers - 1]) for i in range(numberOfPlayers - 1, 0, -1): iPlusOne = i + 1 r_play = make_turn(ROCK_SIGN, input_str[i - 1]) if r_play == ROCK_SIGN: rock[i] = rock[iPlusOne] elif r_play == PAPER_SIGN: rock[i] = paper[iPlusOne] elif r_play == SCCISORS_SIGN: rock[i] = sccisors[iPlusOne] p_play = make_turn(PAPER_SIGN, input_str[i - 1]) if p_play == ROCK_SIGN: paper[i] = rock[iPlusOne] elif p_play == PAPER_SIGN: paper[i] = paper[iPlusOne] elif p_play == SCCISORS_SIGN: paper[i] = sccisors[iPlusOne] s_play = make_turn(SCCISORS_SIGN, input_str[i - 1]) if s_play == ROCK_SIGN: sccisors[i] = rock[iPlusOne] elif s_play == PAPER_SIGN: sccisors[i] = paper[iPlusOne] elif s_play == SCCISORS_SIGN: sccisors[i] = sccisors[iPlusOne] if input_str[i - 1] == ROCK_SIGN: res[i] = rock[iPlusOne] elif input_str[i - 1] == PAPER_SIGN: res[i] = paper[iPlusOne] elif input_str[i - 1] == SCCISORS_SIGN: res[i] = sccisors[iPlusOne] for i in range(1, numberOfPlayers + 1): print(res[i], end="") print()
ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
try: def defence_win(a, b): win = [("R", "P"), ("P", "R"), ("R", "S"), ("S", "R"), ("S", "P"), ("P", "S")] it = win.index((a, b)) w = ["P", "P", "R", "R", "S", "S"] return w[it] for _ in range(int(input())): lat_ner_rr = int(input()) S = input() resultant_pro = [0] * lat_ner_rr i = 0 if lat_ner_rr == 1: resultant_pro = [S] else: while i < lat_ner_rr: calcacher = [i] if resultant_pro[i] == 0: a = S[i] j = i + 1 while j < lat_ner_rr: b = S[j] if a == b: resultant_noob = a else: resultant_noob = defence_win(a, b) a = resultant_noob if resultant_noob == b and resultant_pro[j] == 0: calcacher.append(j) elif resultant_noob == b and resultant_pro[j] != 0: resultant_noob = resultant_pro[j] j = lat_ner_rr j += 1 if i == lat_ner_rr - 1: resultant_noob = a for items in calcacher: resultant_pro[items] = resultant_noob else: pass i += 1 print("".join(resultant_pro)) except: pass
FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR VAR ASSIGN VAR LIST VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def win(pl1, pl2): game = pl1 + pl2 if game == "SR" or game == "RP" or game == "PS": return False return True def SPQR(S): if len(S) == 1: return S res = S[0] order = [(S[0], 0)] for i in range(1, len(S)): if win(order[0][0], S[i]): res = res + res[order[0][1]] elif len(order) == 1: res = res + S[i] else: res = res + res[order[1][1]] if S[i] != order[0][0]: order.insert(0, (S[i], i)) return res[::-1] T = int(input()) for t in range(T): N = int(input()) print(SPQR(input()[::-1]))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def string(s, n): if s[n - 1] is "R": res1 = "R" res2 = "P" elif s[n - 1] is "P": res1 = "P" res2 = "S" else: res1 = "S" res2 = "R" string = s[n - 1] d = {} d[res1] = 0 for i in range(1, n): if s[n - 1 - i] is "P": if "S" in d: string = string + string[d["S"]] else: string = string + "P" d["P"] = i elif s[n - 1 - i] is "R": if "P" in d: string = string + string[d["P"]] else: string = string + "R" d["R"] = i elif s[n - 1 - i] is "S": if "R" in d: string = string + string[d["R"]] else: string = string + "S" d["S"] = i print(string[::-1]) t = int(input()) for _ in range(t): n = int(input()) s = input() string(s, n)
FUNC_DEF IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING IF STRING VAR ASSIGN VAR BIN_OP VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR STRING VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING IF STRING VAR ASSIGN VAR BIN_OP VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR STRING VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING IF STRING VAR ASSIGN VAR BIN_OP VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR STRING VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
There are N players standing in a line, indexed 1 to N from left to right. They all play a game of Rock, Paper, Scissors. Each player has already decided which move they want to play. You are given this information as a string S of length N, i.e, S_{i} is equal to \verb+R+ if player i will play Rock. S_{i} is equal to \verb+P+ if player i will play Paper. S_{i} is equal to \verb+S+ if player i will play Scissors. Let W(i, j) denote the move played by the winner if players i, i+1, \ldots, j compete in order from left to right. That is, First, players i and i+1 play a game The winner of this game plays against player i+2 The winner of the second game plays against player i+3 \vdots The winner of the first j-i-1 games plays against player j, and the move played by the winner of this game is declared to be W(i, j). If i = j, then player i is considered to be the winner and W(i, i) = S_{i}. Your task is to find the value of W(i,N) for all i from 1 to N. Note : If a person with index i and index j (i < j) play against each other, then: If S_{i} \neq S_{j}, the winner is decided by classical rules, i.e, rock beats scissors, scissors beats paper, and paper beats rock. If S_{i} = S_{j}, the player with lower index (in this case, i) wins. ------ 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. - The first line of each test case contains a single integer N, the number of players. - The second line of each test case contains the string S of length N, denoting the moves chosen by the players. ------ Output Format ------ For each test case, print a single line containing a string of length N, whose i-th character is W(i, N). ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 5\cdot 10^{5}$ $S_{i}$ is either $\verb+R+$, $\verb+P+$ or $\verb+S+$ - Sum of $N$ over all test cases doesn't exceed $5 \cdot 10^{5}$ ------ subtasks ------ Subtask 1 (10 points): $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ Sum of $N$ over all test cases doesn't exceed $5000$ Subtask 2 (90 points): Original constraints ----- Sample Input 1 ------ 2 1 S 4 SSPR ----- Sample Output 1 ------ S RRPR ----- explanation 1 ------ Test Case 1. $W(1,1) = \verb+S+$ as there is only one player. Test Case 2. For $W(1,4)$ the game is played as follows : - Player $1$ and $2$ compete, player $1$ wins. - Player $1$ and $3$ compete, player $1$ wins. - Player $1$ and $4$ compete, player $4$ wins. Hence, we print $W(1,4) = S_{4} = \verb+R+$ For $W(3,4)$ the game is played as follows : - Player $3$ and $4$ compete, player $3$ wins. Hence, we print $W(3,4) = S_{3} = \verb+P+$
def ans(l): if l[-1] == l[-2]: return l[-1] if l == ["P", "R"]: return "P" if l == ["R", "S"]: return "R" if l == ["P", "S"]: return "S" for _ in range(int(input())): n = int(input()) s = input() a = s[-1] b = "" for i in range(n - 2, -1, -1): if a != s[i]: b = s[i] break if b == "": print(n * a) elif a == "P" and b == "R": print(n * "P") elif a == "R" and b == "S": print(n * "R") elif a == "S" and b == "P": print(n * "S") else: c = d = o = "" c_ = n - 1 p = True y = c_ k = {} for i in range(n - 1, -1, -1): if i == n - 1: c = s[i] o += s[i] elif d == "": if c != s[i]: d = s[i] o += s[i] elif (s[i] == c or s[i] == d) and p is True: o += d if s[i] == c: y = i elif (s[i] != c and s[i] != d) and p is True: r = s[i] if y == n - 1: o += c p = False k[s[i]] = c else: o += d p = False k[s[i]] = d elif s[i] == r: if k.get(c, 0) == 0: o += k[s[i]] else: o += k[c] k[s[i]] = k[c] elif s[i] == d: o += k[r] k[s[i]] = k[r] elif s[i] == c: if not k.get(d, 0): k[s[i]] = d o += d else: o += k[d] k[s[i]] = k[d] print(o[::-1])
FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER IF VAR LIST STRING STRING RETURN STRING IF VAR LIST STRING STRING RETURN STRING IF VAR LIST STRING STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR STRING IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has $n$ different matryoshkas. Any matryoshka is a figure of volume $out_i$ with an empty space inside of volume $in_i$ (of course, $out_i > in_i$). You don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll — inside the third one and so on. Matryoshka $i$ can be nested inside matryoshka $j$ if $out_i \le in_j$. So only the last doll will take space inside your bag. Let's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to $in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + \dots + (in_{i_k} - out_{i_{k-1}})$, where $i_1$, $i_2$, ..., $i_k$ are the indices of the chosen dolls in the order they are nested in each other. Finally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property. You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index $i$ such that one of the subsets contains the $i$-th doll, and another subset doesn't. Since the answer can be large, print it modulo $10^9 + 7$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of matryoshkas. The next $n$ lines contain a description of each doll: two integers $out_i$ and $in_i$ ($1 \le in_i < out_i \le 10^9$) — the outer and inners volumes of the $i$-th matryoshka. -----Output----- Print one integer — the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo $10^9 + 7$. -----Example----- Input 7 4 1 4 2 4 2 2 1 5 4 6 4 3 2 Output 6 -----Note----- There are $6$ big enough nested subsets with minimum possible extra space in the example: $\{1, 5\}$: we can't add any other matryoshka and keep it nested; it's extra space is $1$; $\{1, 6\}$; $\{2, 4, 5\}$; $\{2, 4, 6\}$; $\{3, 4, 5\}$; $\{3, 4, 6\}$. There are no more "good" subsets because, for example, subset $\{6, 7\}$ is not big enough (we can add the $4$-th matryoshka to it) or subset $\{4, 6, 7\}$ has extra space equal to $2$.
from sys import stdin, stdout mod = 10**9 + 7 n = int(input()) dolls = [] for i in range(n): o, i = map(int, stdin.readline().split()) dolls.append((o, i)) dolls.sort() dolls = [(i, o) for o, i in dolls] def bin_search(i): lo = -1 hi = n - 1 while lo + 1 < hi: mid = (lo + hi - 1) // 2 + 1 top = dolls[mid][1] if top <= i: lo = mid else: hi = mid return hi counts = [1] * (n + 1) dp = [0] * (n + 1) for k in range(n): i, o = dolls[k] m_prev = dp[k] + o - (dolls[k - 1][1] if k > 0 else 0) kk = bin_search(i) m_with = dp[kk] + i - (dolls[kk - 1][1] if kk > 0 else 0) if m_prev < m_with: dp[k + 1] = m_prev counts[k + 1] = counts[k] elif m_prev > m_with: dp[k + 1] = m_with counts[k + 1] = counts[kk] else: dp[k + 1] = m_prev counts[k + 1] = (counts[k] + counts[kk]) % mod best = 10**9 + 10 best_count = 0 maximal = max([i for i, o in dolls]) for i in range(bin_search(maximal), n): ii = bin_search(dolls[i][0]) cur = dp[ii] + dolls[i][0] - (dolls[ii - 1][1] if ii > 0 else 0) if cur < best: best = cur best_count = counts[ii] elif cur == best: best_count += counts[ii] best_count %= mod print(best_count)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has $n$ different matryoshkas. Any matryoshka is a figure of volume $out_i$ with an empty space inside of volume $in_i$ (of course, $out_i > in_i$). You don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll — inside the third one and so on. Matryoshka $i$ can be nested inside matryoshka $j$ if $out_i \le in_j$. So only the last doll will take space inside your bag. Let's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to $in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + \dots + (in_{i_k} - out_{i_{k-1}})$, where $i_1$, $i_2$, ..., $i_k$ are the indices of the chosen dolls in the order they are nested in each other. Finally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property. You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index $i$ such that one of the subsets contains the $i$-th doll, and another subset doesn't. Since the answer can be large, print it modulo $10^9 + 7$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of matryoshkas. The next $n$ lines contain a description of each doll: two integers $out_i$ and $in_i$ ($1 \le in_i < out_i \le 10^9$) — the outer and inners volumes of the $i$-th matryoshka. -----Output----- Print one integer — the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo $10^9 + 7$. -----Example----- Input 7 4 1 4 2 4 2 2 1 5 4 6 4 3 2 Output 6 -----Note----- There are $6$ big enough nested subsets with minimum possible extra space in the example: $\{1, 5\}$: we can't add any other matryoshka and keep it nested; it's extra space is $1$; $\{1, 6\}$; $\{2, 4, 5\}$; $\{2, 4, 6\}$; $\{3, 4, 5\}$; $\{3, 4, 6\}$. There are no more "good" subsets because, for example, subset $\{6, 7\}$ is not big enough (we can add the $4$-th matryoshka to it) or subset $\{4, 6, 7\}$ has extra space equal to $2$.
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline N = int(input()) A = [] B = [] val = set() val.add(0) a_max = -1 for _ in range(N): a, b = map(int, input().split()) a, b = b, a A.append(a) B.append(b) val.add(a) val.add(b) a_max = max(a_max, a) val = sorted(list(val)) val2idx = {v: i for i, v in enumerate(val)} NN = len(val) adj = [[] for _ in range(NN)] for i in range(NN - 1): adj[i].append((i + 1, val[i + 1] - val[i])) for a_, b_ in zip(A, B): a = val2idx[a_] b = val2idx[b_] adj[a].append((b, 0)) dist = [10**10] * NN dist[0] = 0 for i in range(NN - 1): for b, cost in adj[i]: dist[b] = min(dist[b], dist[i] + cost) min_space = 10**10 B_set = set(B) for b in B_set: if b <= a_max: continue ib = val2idx[b] min_space = min(min_space, dist[ib]) dp = [0] * NN dp[0] = 1 for i in range(NN - 1): for b, cost in adj[i]: if dist[i] + cost == dist[b]: dp[b] = (dp[b] + dp[i]) % mod ans = 0 for b in B_set: if b <= a_max: continue ib = val2idx[b] if dist[ib] == min_space: ans = (ans + dp[ib]) % mod print(ans) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Given an array of digits, you can write numbers using each digits[i] as many times as we want.  For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'. Return the number of positive integers that can be generated that are less than or equal to a given integer n.   Example 1: Input: digits = ["1","3","5","7"], n = 100 Output: 20 Explanation: The 20 numbers that can be written are: 1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77. Example 2: Input: digits = ["1","4","9"], n = 1000000000 Output: 29523 Explanation: We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers, 81 four digit numbers, 243 five digit numbers, 729 six digit numbers, 2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers. In total, this is 29523 integers that can be written using the digits array. Example 3: Input: digits = ["7"], n = 8 Output: 1   Constraints: 1 <= digits.length <= 9 digits[i].length == 1 digits[i] is a digit from '1' to '9'. All the values in digits are unique. 1 <= n <= 109
class Solution: def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int: ns = str(n) dp = 1 digits.sort() M, N = len(ns), len(digits) for i in range(M - 1, -1, -1): dp2 = 0 for d in digits: if d > ns[i]: break if d < ns[i]: dp2 += N ** (M - 1 - i) else: dp2 += dp dp = dp2 return dp + sum(N**i for i in range(1, M))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR