description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
temp_arr = []
temp_count = 0
for n in nums:
if n % 2 != 0:
temp_arr.append(temp_count)
temp_count = 0
else:
temp_count = temp_count + 1
temp_arr.append(temp_count)
if len(temp_arr) - 1 < k:
return 0
i = k - 1
count = 0
while i < len(temp_arr) - 1:
start_len = temp_arr[i - k + 1] + 1
end_len = temp_arr[i + 1] + 1
count = count + start_len * end_len
i = i + 1
return count
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
if len(nums) == 0:
return 0
preprocess = [0] * (len(nums) + 1)
count = 0
for i in range(len(nums)):
if nums[i] % 2 == 1:
count += 1
preprocess[i + 1] = count
print(preprocess)
cont_preprocess = [preprocess[0]]
counts_cont = [1]
last_el = preprocess[0]
for i in range(1, len(preprocess)):
if preprocess[i] == last_el:
counts_cont[-1] += 1
else:
last_el = preprocess[i]
cont_preprocess.append(last_el)
counts_cont.append(1)
result = 0
left_cursor = 0
right_cursor = 0
while left_cursor < len(cont_preprocess):
diff = cont_preprocess[right_cursor] - cont_preprocess[left_cursor]
if diff < k:
right_cursor += 1
elif diff == k:
result += counts_cont[right_cursor] * counts_cont[left_cursor]
right_cursor += 1
else:
left_cursor += 1
if right_cursor == len(cont_preprocess):
right_cursor = len(cont_preprocess) - 1
left_cursor += 1
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Window:
def __init__(self):
self.odd = 0
def add(self, value: int):
if value % 2 == 1:
self.odd += 1
def remove(self, value: int):
if value % 2 == 1:
self.odd -= 1
class Solution:
def numberOfSubarrays(self, A: List[int], k: int) -> int:
window1 = Window()
window2 = Window()
left1 = left2 = answer = 0
for right in A:
window1.add(right)
window2.add(right)
while window1.odd > k:
window1.remove(A[left1])
left1 += 1
while window2.odd >= k:
window2.remove(A[left2])
left2 += 1
answer += left2 - left1
return answer
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_DEF VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def addOrIncrement(self, key, dictionary):
if key in dictionary:
dictionary[key] += 1
else:
dictionary[key] = 1
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
n = len(nums)
if n < k:
return 0
ans = 0
seen = {(0): 1}
curVal = 0
for num in nums:
curVal += 0 if num % 2 == 0 else 1
if curVal - k in seen:
ans += seen[curVal - k]
self.addOrIncrement(curVal, seen)
return ans
|
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
left = collections.defaultdict(int)
odd = 0
res = 0
for n in nums:
left[odd] += 1
odd += n & 1
if odd - k in left:
res += left[odd - k]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
rr = l = r = res = cnt = 0
ll = -1
n = len(nums)
while r < n:
x = nums[r]
if x % 2:
if ll < l:
ll = r
cnt += 1
if cnt == k:
rr = r
while r < n - 1 and nums[r + 1] % 2 == 0:
r += 1
res += (ll - l + 1) * (r - rr + 1)
l = ll + 1
cnt -= 1
ll += 1
while ll < n and nums[ll] % 2 == 0:
ll += 1
r += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
prefix_sum = 0
dict_odds = {(0): 1}
rs = 0
for i, num in enumerate(nums):
if num % 2 == 1:
prefix_sum += 1
if prefix_sum not in dict_odds:
dict_odds[prefix_sum] = 1
else:
dict_odds[prefix_sum] = dict_odds[prefix_sum] + 1
if prefix_sum - k in dict_odds:
rs += dict_odds[prefix_sum - k]
return rs
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
s = 0
e = 0
l = len(nums)
total = 0
num_odd = 0
ae = 0
while s < l:
while e < l and num_odd != k:
if nums[e] % 2 != 0:
num_odd += 1
e += 1
if num_odd == k:
if ae < e:
ae = e
while ae < l and nums[ae] % 2 == 0:
ae += 1
total += ae - (e - 1)
if nums[s] % 2 != 0:
num_odd -= 1
s += 1
return total
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def atMostK(self, nums, k):
start = 0
end = 0
count = 0
for end in range(len(nums)):
if nums[end] % 2 == 1:
k -= 1
while k < 0:
if nums[start] % 2 == 1:
k += 1
start += 1
count += end - start + 1
return count
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
if not nums or len(nums) == 0 or k > len(nums):
return 0
odds = 0
return self.atMostK(nums, k) - self.atMostK(nums, k - 1)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
n = len(nums)
if k > n:
return 0
ans = 0
p1 = p2 = p3 = p4 = -1
while p4 < n:
p2 += 1
while p2 < n and nums[p2] % 2 == 0:
p2 += 1
if p2 == n:
return ans
if p4 == -1:
p3 = p2
remaining = k - 1
while p3 < n and remaining:
p3 += 1
if p3 == n:
return ans
if nums[p3] % 2 == 1:
remaining -= 1
else:
p3 = p4
p4 = p3 + 1
while p4 < n:
if nums[p4] % 2 == 1:
break
p4 += 1
ans += (p2 - p1) * (p4 - p3)
p1 = p2
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
odd_pos = [pos for pos, e in enumerate(nums) if e % 2 == 1]
if len(odd_pos) < k:
return 0
spaces = []
prev_pos = -1
for pos in odd_pos:
spaces.append(pos - prev_pos)
prev_pos = pos
spaces.append(len(nums) - prev_pos)
res = 0
for i in range(len(spaces) - k):
res += spaces[i] * spaces[i + k]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
flattened = [(1 if num % 2 == 1 else 0) for num in nums]
d = {(0): 1}
sum = 0
total = 0
for i in range(len(flattened)):
sum += flattened[i]
total += d.get(sum - k, 0)
d[sum] = d.get(sum, 0) + 1
return total
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
ln = len(nums)
ret = 0
for i in range(ln):
if nums[i] % 2:
nums[i] = 1
else:
nums[i] = 0
mp = {(0): 1}
cnt = 0
for n in nums:
cnt += n
if cnt not in mp:
mp[cnt] = 0
mp[cnt] += 1
if cnt - k in mp:
ret += mp[cnt - k]
return ret
pass
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
cache = {(0): [-1]}
cnt = 0
odds = 0
for i, num in enumerate(nums):
if num % 2:
odds += 1
if odds - k in cache:
cnt += len(cache[odds - k])
x = cache.setdefault(odds, [])
x.append(odds)
return cnt
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
oddnum = 0
l, n = 0, len(nums)
add = 1
sums = 0
for r in range(n):
if nums[r] % 2 == 1:
k -= 1
while l < r and (k < 0 or nums[l] % 2 == 0):
if k < 0:
add = 1
else:
add += 1
if nums[l] % 2 == 1:
k += 1
l += 1
if k == 0:
sums += add
return sums
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
l = [0] * (len(nums) + 1)
for i, n in enumerate(nums):
l[i + 1] = l[i] + n % 2
c = Counter(l)
return sum(c[x - k] * c[x] for x in c)
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, A: List[int], k: int) -> int:
d = []
res = 0
count = 1
for a in A:
if a % 2:
d.append(count)
count = 1
else:
count += 1
d.append(count)
m = len(d)
for i in range(m - k):
res += d[i] * d[i + k]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def checkOdd(self, num):
if num % 2 == 0:
return False
return True
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
oddIndices = []
for i in range(len(nums)):
if self.checkOdd(nums[i]):
oddIndices.append(i)
start = 0
end = k - 1
i = 0
count = 0
while end < len(oddIndices):
if end == len(oddIndices) - 1:
j = len(nums) - 1
else:
j = oddIndices[end + 1] - 1
count = count + (oddIndices[start] - i + 1) * (j - oddIndices[end] + 1)
i = oddIndices[start] + 1
start = start + 1
end = end + 1
return count
|
CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
left_pointer = 0
right_pointer = -1
count = 0
odd = 0
while right_pointer < len(nums) - 1:
right_pointer += 1
if nums[right_pointer] % 2 == 1:
odd += 1
if odd == k:
left_side = 1
right_side = 1
while (
right_pointer < len(nums) - 1 and nums[right_pointer + 1] % 2 == 0
):
right_side += 1
right_pointer += 1
while left_pointer <= right_pointer and nums[left_pointer] % 2 == 0:
left_side += 1
left_pointer += 1
count += left_side * right_side
left_pointer += 1
odd -= 1
return count
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
idxes = []
for i in range(len(nums)):
if nums[i] % 2 == 1:
idxes.append(i)
if len(idxes) < k:
return 0
res = 0
for i in range(k - 1, len(idxes)):
l = -1 if i - k + 1 == 0 else idxes[i - k]
r = len(nums) if i == len(idxes) - 1 else idxes[i + 1]
res += (idxes[i - k + 1] - l) * (r - idxes[i])
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
arr = list(map(lambda x: x % 2, nums))
n = len(arr)
arr = [0] + list(itertools.accumulate(arr))
hm = Counter()
res = 0
for i in arr:
res += hm[i]
hm[i + k] += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
last = 0
hsh = {}
for i in range(len(nums)):
if nums[i] % 2 == 1:
nums[i] = 1
else:
nums[i] = 0
nums[i] += last
last = nums[i]
if nums[i] not in hsh:
hsh[nums[i]] = []
hsh[nums[i]].append(i)
count = 0
if k in hsh:
count = len(hsh[k])
for i in range(len(nums)):
x = nums[i]
if x + k in hsh:
for v in hsh[x + k][::-1]:
if v < i:
break
count += 1
return count
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR FOR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
deque = collections.deque()
deque.append(1)
ans = 0
for num in nums:
if num % 2 == 0:
deque[-1] += 1
continue
if len(deque) == k + 1:
ans += deque[0] * deque[-1]
deque.popleft()
deque.append(1)
if len(deque) == k + 1:
ans += deque[0] * deque[-1]
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
prefix_odd = defaultdict(int)
prefix_odd[0] = 1
ans = count = 0
for num in nums:
if num % 2:
count += 1
prefix_odd[count] += 1
for p in prefix_odd:
if p - k in prefix_odd:
ans += prefix_odd[p] * prefix_odd[p - k]
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
res = 0
i = j = 0
while i <= j:
while k > 0 and j < len(nums):
if nums[j] % 2 == 1:
k -= 1
j += 1
if k != 0:
return res
res += 1
t = j
while t < len(nums):
if nums[t] % 2 == 0:
res += 1
t += 1
else:
break
if nums[i] % 2 == 1:
k += 1
i += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
ans = 0
d = {(0): 0}
start = {}
cumsum = 0
for i, num in enumerate(nums):
cumsum += num % 2
d[cumsum] = i + 1
if cumsum not in start:
start[cumsum] = i
if cumsum == k:
elems = d[0]
ans += elems + 1
elif cumsum > k:
elems = d[cumsum - k] - start.get(cumsum - k, -1)
ans += elems
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
if not nums:
return 0
s = []
evencnt = 0
for num in nums:
if num % 2 == 1:
s.append(evencnt)
evencnt = 0
else:
evencnt += 1
s.append(evencnt)
res = 0
for i in range(len(s) - k):
res += (s[i] + 1) * (s[i + k] + 1)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
front, end = 0, 0
tot = 0
output = 0
ct = 0
while end < len(nums):
if 1 & nums[end]:
tot += 1
ct = 0
end += 1
while tot == k:
tot -= nums[front] & 1
front += 1
ct += 1
output += ct
return output
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
|
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
prefix = {(0): 1}
count = 0
result_count = 0
for num in nums:
if num % 2:
count += 1
prefix[count] = prefix.get(count, 0) + 1
result_count += prefix.get(count - k, 0)
return result_count
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
n, b = map(int, input().split())
finish = [0] * n
t = []
d = []
for _ in range(n):
i = input().split()
t.append(int(i[0]))
d.append(int(i[1]))
current = t[0]
q = []
for i in range(n):
if q != []:
current = max(current, t[q[0]])
while q != [] and current + d[q[0]] <= t[i]:
j = q.pop(0)
current = max(current, t[j])
current += d[j]
finish[j] = current
if len(q) <= b:
q.append(i)
else:
finish[i] = -1
for i in q:
current = max(current, t[i])
current += d[i]
finish[i] = current
print(" ".join([str(i) for i in finish]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR LIST BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
n, b = [int(item) for item in input().split()]
queue = []
for i in range(n):
ti, di = [int(item) for item in input().split()]
while len(queue) > 0 and queue[0] <= ti:
queue.pop(0)
if len(queue) - 1 == b:
print(-1, end=" ")
continue
if not queue:
queue.append(ti + di)
else:
queue.append(queue[-1] + di)
print(queue[-1], end=" ")
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER STRING IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
n, b = map(int, input().split())
queries = []
for _ in range(n):
t, d = map(int, input().split())
queries.append((t, d))
result = [0] * n
queue = []
n_queue = b + 1
process = []
for i in range(n):
if len(queue) != 0 and queries[i][0] >= queue[0]:
queue.pop(0)
if len(queue) == 0:
finish_time = queries[i][0] + queries[i][1]
queue.append(finish_time)
result[i] = finish_time
elif len(queue) < n_queue:
finish_time = max(queue[-1], queries[i][0]) + queries[i][1]
queue.append(finish_time)
result[i] = finish_time
else:
result[i] = -1
print(*result)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
from itertools import product as P
from sys import maxsize as m
class Solution:
def bazinga(self, N, p):
pass
def __starting_point():
n, b = list(map(int, input().split(" ")))
ct, pt, lpt, cb = 0, 0, [], 0
result = []
for i in range(n):
ti, di = list(map(int, input().split()))
res = 0
if pt == 0:
pt += ti + di
lpt += [pt]
res = pt
elif ti < lpt[0]:
if cb < b:
pt += di
lpt += [pt]
res = pt
cb += 1
elif cb >= b:
res = -1
elif ti >= lpt[0]:
if ti > lpt[-1]:
pt += abs(lpt[-1] - ti) + di
else:
pt += di
lpt += [pt]
res = pt
cb += 1
while ti >= lpt[0]:
if len(lpt) == 1:
break
lpt.pop(0)
if cb > 0:
cb -= 1
result += (str(res),)
print(" ".join(result))
__starting_point()
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER NUMBER LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR LIST VAR ASSIGN VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
import sys
input = sys.stdin.readline
q = []
processing = 0
n, b = map(int, input().split())
for _ in range(n):
t, d = map(int, input().split())
while len(q) != 0 and q[0] <= t:
q.pop(0)
if len(q) <= b:
processing = max(processing, t) + d
print(processing, end=" ")
q.append(processing)
else:
print(-1, end=" ")
|
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
import sys
q = []
n, b = map(int, input().split())
t = 0
d = 0
for i in range(0, n):
t, d = map(int, input().split())
while len(q) != 0:
if q[0] <= t:
q.pop(0)
else:
break
if len(q) <= b:
if len(q) != 0:
t = q[-1]
t = t + d
print(t, end=" ")
q.append(t)
else:
print(-1, end=" ")
|
IMPORT ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
def main():
n, b = map(int, input().split())
def endgen():
res = [0]
q = 0
for _ in range(n):
t, d = map(int, input().split())
res.append(t + d if t > res[-1] else res[-1] + d)
while res[q] <= t:
q += 1
if len(res) - q - 1 > b:
res.pop()
yield "-1"
else:
yield str(res[-1])
print(" ".join(endgen()))
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR EXPR FUNC_CALL VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
class zapros:
def __init__(self, t, timeToComplete):
self.t = t
self.timeToComplete = timeToComplete
n = input()
b = int(n.split()[1])
n = int(n.split()[0])
zapQueue = []
result = []
timeAfterComplete = 0
sumTime = 0
for i in range(n):
s = input()
zapr = zapros(int(s.split(" ")[0]), int(s.split(" ")[1]))
while timeAfterComplete <= zapr.t and len(zapQueue) > 0:
obj = zapQueue.pop(0)
timeAfterComplete += obj.timeToComplete
sumTime -= obj.timeToComplete
if timeAfterComplete + sumTime > zapr.t:
if len(zapQueue) < b:
sumTime += zapr.timeToComplete
result.append(timeAfterComplete + sumTime)
zapQueue.append(zapr)
else:
result.append(-1)
else:
zapQueue = []
sumTime = 0
timeAfterComplete = zapr.t + zapr.timeToComplete
result.append(timeAfterComplete)
msg = ""
for i in result:
msg += str(i) + " "
print(msg[:-1])
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER WHILE VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
class Query:
def __init__(self, t, d, f):
self.t = t
self.d = d
self.f = f
queue = []
queries = []
n, b = map(int, input().split())
for i in range(n):
t, d = map(int, input().split())
query = Query(t, d, 0)
queries.append(query)
query = queries[0]
t = query.t + query.d
query.f = t
queue.append(t)
for i in range(1, n, 1):
while len(queue) > 0 and queries[i].t >= queue[0]:
queue.pop(0)
if t <= queries[i].t:
t = queries[i].t + queries[i].d
queries[i].f = t
queue.append(t)
elif len(queue) > b:
queries[i].f = -1
else:
t += queries[i].d
queries[i].f = t
queue.append(t)
for query in queries:
print("{0}".format(query.f), end=" ")
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
def main():
n, hi = map(int, input().split())
l, res, lo, hi = [0], [], 0, -hi
f, p = res.append, l.append
for _ in range(n):
t, d = map(int, input().split())
d += t if t > l[-1] else l[-1]
while lo <= hi and l[lo] <= t:
lo += 1
if hi >= lo:
f("-1")
else:
hi += 1
p(d)
f(str(d))
print(" ".join(res))
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST NUMBER LIST NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
n, b = map(int, input().split())
l = [tuple(map(int, input().split())) for i in range(n)]
ch = [0]
ans = []
bd = [0] * (n + 1)
po = ma = 0
for i in range(n):
t, d = l[i]
bd[i] = bd[i - 1]
ch += [max(ma, t) + d]
while ch[po] <= t:
po += 1
if i - po - (bd[i - 1] - bd[po - 1]) == b:
bd[i] += 1
ch[-1] = -1
if ch[-1] > ma:
ma = ch[-1]
ans += [str(ch[-1])]
print(" ".join(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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR LIST BIN_OP FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR LIST FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
n, b = list(map(int, input().split()))
tasks = []
next_task = 0
time = 0
finish = [-1] * n
for i in range(n):
t, d = list(map(int, input().split()))
while tasks and next_task < len(tasks) and time <= t:
time += tasks[next_task][1]
finish[tasks[next_task][0]] = time
next_task += 1
if len(tasks) - next_task < b:
tasks.append((i, d))
time = max(t, time)
while tasks and next_task < len(tasks):
time += tasks[next_task][1]
finish[tasks[next_task][0]] = time
next_task += 1
print(*finish)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
import sys
def main():
n, b = [int(tok) for tok in sys.stdin.readline().split()]
td_list = []
for i in range(n):
td_list.append([int(tok) for tok in sys.stdin.readline().split()])
queue = []
finish = [(-1) for i in range(n)]
for i, (t, d) in enumerate(td_list):
if len(queue) > 0:
if queue[0] <= t:
queue.pop(0)
if len(queue) == 0 or queue[-1] < t:
queue.append(t + d)
finish[i] = t + d
elif len(queue) == b + 1:
pass
else:
finish[i] = queue[-1] + d
queue.append(queue[-1] + d)
print(" ".join([str(f) for f in finish]))
def __starting_point():
main()
__starting_point()
|
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
n, b = [int(i) for i in input().split()]
q = [0] * n
bg = 0
en = 0
time = 0
res = [-1] * n
for it in range(n):
ev = [int(i) for i in input().split()]
ev.append(it)
while bg < en and max(time, q[bg][0]) <= ev[0]:
time = max(time, q[bg][0])
res[q[bg][2]] = time + q[bg][1]
time += q[bg][1]
bg += 1
if en - bg < b:
q[en] = ev
en += 1
while bg < en:
time = max(time, q[bg][0])
res[q[bg][2]] = time + q[bg][1]
time += q[bg][1]
bg += 1
for i in range(n):
print(res[i], end=" ")
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.
When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
-----Input-----
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 ≤ t_{i}, d_{i} ≤ 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.
-----Output-----
Print the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.
-----Examples-----
Input
5 1
2 9
4 8
10 9
15 2
19 1
Output
11 19 -1 21 22
Input
4 1
2 8
4 8
10 9
15 2
Output
10 18 27 -1
-----Note-----
Consider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22.
|
n, b = map(int, input().split())
q = []
l = 0
r = 0
for i in range(n):
t, d = map(int, input().split())
while l != r:
if q[l] <= t:
l += 1
else:
break
len_queue = r - l - 1
if len_queue >= b:
print(-1, end=" ")
else:
if l == r:
q.append(t + d)
else:
q.append(q[-1] + d)
r += 1
print(q[-1])
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, arr, n):
start = 0
kami = 0
balance = 0
for i in range(n):
balance = balance + arr[i][0] - arr[i][1]
if balance < 0:
start = i + 1
kami += balance
balance = 0
if kami + balance >= 0:
return start
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, l, n):
i = 0
while i < n:
if l[i][1] > l[i][0]:
i += 1
pass
else:
t = 1
s = 0
for j in range(n):
s += l[(i + j) % n][0] - l[(i + j) % n][1]
if s < 0:
t = 0
break
if t == 1:
return i
i = i + j
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR RETURN NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
tp = 0
td = 0
for a, b in lis:
tp += a
td += b
if td > tp:
return -1
i = 0
cp = 0
ans = 0
while i < len(lis):
if cp + lis[i][0] >= lis[i][1]:
cp -= lis[i][1]
cp += lis[i][0]
i += 1
else:
i += 1
ans = i
cp = 0
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
fuel = 0
d = 0
s = 0
for i in range(n):
fuel += lis[i][0] - lis[i][1]
if fuel < 0:
d += fuel
fuel = 0
s = i + 1
return s if fuel + d >= 0 else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR NUMBER VAR NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
bal = 0
start = 0
deficit = 0
for i in range(len(lis)):
p, d = lis[i]
bal += p - d
if bal < 0:
deficit += bal
start = i + 1
bal = 0
if bal + deficit >= 0:
return start
else:
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
s = []
bakaya = 0
tak = 0
for i in range(n):
s = bakaya + lis[i][0] - lis[i][1]
if s < 0:
tak = i + 1
bakaya = 0
continue
if s >= 0:
bakaya = s
for i in range(0, tak):
s = bakaya + lis[i][0] - lis[i][1]
if s < 0:
return -1
if s >= 0:
bakaya = s
else:
return tak
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
index = None
cur = 0
for i in range(n):
cur += lis[i][0] - lis[i][1]
if cur < 0:
cur = 0
index = None
elif index == None:
index = i
if index == None:
return -1
s = 0
for i in range(index):
s += lis[i][0] - lis[i][1]
if s + cur < 0:
return -1
return index
|
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE IF VAR NONE ASSIGN VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
petrol = 0
index = -1
sum1 = 0
sum2 = 0
for i in range(n):
sum1 += lis[i][0]
sum2 += lis[i][1]
petrol += lis[i][0] - lis[i][1]
if petrol < 0:
petrol = 0
index = -1
elif index < 0:
index = i
if sum1 < sum2:
return -1
return index
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
balance = 0
kami = 0
start = 0
for i in range(n):
petrol = lis[i][0]
dis = lis[i][1]
balance = petrol - dis + balance
if balance < 0:
kami = kami + balance
balance = 0
start = i + 1
if balance + kami >= 0:
return start
else:
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
petrol = sum([i for i, j in lis])
distance = sum([j for i, j in lis])
if distance > petrol:
return -1
temp = 0
start = 0
for i in range(n):
temp += lis[i][0] - lis[i][1]
if temp < 0:
temp = 0
start = i + 1
return start
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
start = 0
curr_petrol = 0
total_petrol = 0
for i in range(n):
curr_petrol += lis[i][0] - lis[i][1]
total_petrol += lis[i][0] - lis[i][1]
if curr_petrol < 0:
start = i + 1
curr_petrol = 0
return start if total_petrol >= 0 else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR NUMBER VAR NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
start = 0
end = 1
curr_petrol = lis[start][0] - lis[start][1]
while end != start or curr_petrol < 0:
while curr_petrol < 0 and start != end:
curr_petrol -= lis[start][0] - lis[start][1]
start = (start + 1) % n
if start == 0:
return -1
curr_petrol += lis[end][0] - lis[end][1]
end = (end + 1) % n
return start
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
start = 0
deficit = 0
total = 0
for i in range(n):
petrol, distance = lis[i]
deficit += petrol - distance
total += petrol - distance
if deficit < 0:
start = i + 1
deficit = 0
if total < 0:
return -1
return start
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
tank = 0
start = -1
tankRequired = 0
total = 0
for i in range(n):
petrol, distance = lis[i]
total += petrol - distance
if start < 0:
start = i
tank = tank - distance + petrol
if tank < 0:
tankRequired = abs(tank)
start = -1
tank = 0
return start if total >= 0 else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR NUMBER VAR NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
a = 0
b = 0
for i in range(n):
a = a + lis[i][0]
b = b + lis[i][1]
if b > a:
return -1
res = 0
c = 0
for i in range(n):
res = res + (lis[i][0] - lis[i][1])
if res < 0:
res = 0
c = i + 1
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
lst = []
for i in range(n):
diff = lis[i][0] - lis[i][1]
lst.append(diff)
dist = 0
deficit = 0
index = 0
for i in range(n):
dist += lst[i]
if dist < 0:
index = i + 1
deficit += dist
dist = 0
if dist + deficit >= 0:
return index
else:
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
start = 0
pointer = 0
balance = 0
totalDistBeforeStartIndex = 0
totalPetrolNeededForIndex = 0
totalDist = 0
totalPetrol = 0
while pointer < n:
balance = lis[pointer][0] + balance - lis[pointer][1]
totalDist += lis[pointer][1]
totalPetrol += lis[pointer][0]
pointer += 1
if balance < 0:
start = pointer
balance = 0
totalDistBeforeStartIndex += totalDist
totalPetrolNeededForIndex += totalPetrol
totalDist = 0
totalPetrol = 0
if start == n:
return -1
if pointer >= n:
if totalPetrolNeededForIndex + balance - totalDistBeforeStartIndex >= 0:
return start
else:
return -1
return start
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
data = [(a[0] - a[1]) for a in lis]
if sum(data) == 0:
return -1
st, cap, deff = 0, 0, 0
for j in range(n):
cap += data[j]
if cap < 0:
st = j + 1
deff += cap
cap = 0
if st >= n:
return -1
elif cap + deff >= 0:
return st
else:
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
lis = lis + lis
i = 0
start = i
curr = 0
while i < 2 * n:
if i == start + n:
return start
curr = curr + lis[i][0] - lis[i][1]
if curr < 0:
start = i + 1
curr = 0
i += 1
return -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
n = len(lis)
total_gas = 0
for i in range(n):
total_gas += lis[i][0]
total_cost = 0
for i in range(n):
total_cost += lis[i][1]
if total_gas < total_cost:
return -1
total = 0
res = 0
for i in range(n):
gas = lis[i][0]
cost = lis[i][1]
total += gas - cost
if total < 0:
total = 0
res = i + 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
balance = 0
front = 0
flag = 0
i = 0
while i < n:
balance = balance + lis[i][0]
balance = balance - lis[i][1]
if balance >= 0:
if flag == 0:
front = i
flag = 1
else:
front = -1
balance = 0
flag = 0
i = i + 1
i = 0
while i < front:
balance = balance + lis[i][0]
balance = balance - lis[i][1]
if balance >= 0:
if flag == 0:
front = i
flag = 1
else:
front = -1
balance = 0
flag = 0
i = i + 1
return front
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
total_petrol = 0
total_distance = 0
start = 0
end = 0
while True:
if start == n:
return -1
total_petrol += lis[end][0]
total_distance += lis[end][1]
while total_distance > total_petrol and start != n:
total_petrol -= lis[start][0]
total_distance -= lis[start][1]
start = start + 1
end = (end + 1) % n
if end == start and total_distance != 0:
return start
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN VAR
|
Suppose there is a circle. There are N petrol pumps on that circle. You will be given two sets of data.
1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note : Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 4 petrol pumps with
amount of petrol and distance to next
petrol pump value pairs as {4, 6}, {6, 5},
{7, 3} and {4, 5}. The first point from
where truck can make a circular tour is
2nd petrol pump. Output in this case is 1
(index of 2nd petrol pump).
Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
|
class Solution:
def tour(self, lis, n):
diff = [(0) for i in range(n)]
current_petrol = 0
startingPoint = 0
deficit = 0
for i in range(n):
diff[i] = lis[i][0] - lis[i][1]
current_petrol += diff[i]
if current_petrol < 0:
startingPoint = i + 1
deficit += current_petrol
current_petrol = 0
return startingPoint if deficit + current_petrol >= 0 else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP VAR VAR NUMBER VAR NUMBER
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
def func():
n = int(input())
l = map(int, input().split())
a, b, c = map(int, input().split())
l = list(l)
s = input()
st = 0
en = n - 1
x, y = 1, 0
k = 0
for i in range(0, n):
if s[i] == "A":
y += a
y = y % c
elif s[i] == "M":
x *= b
y *= b
x %= c
y %= c
if s[i] == "R":
if k == st:
k = en
elif k == en:
k = st
res = (x * l[k] + y) % c
if k == st:
st += 1
k = st
elif k == en:
en -= 1
k = en
print(res, end=" ")
def main():
test = int(input())
for x in range(0, test):
func()
print()
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
def process(nums, abc, seq):
n = len(nums)
a, b, c = abc
l, r = 0, n - 1
x, y = 1, 0
for i in range(n):
if seq[i] == "R":
l, r = r, l
elif seq[i] == "A":
y = (y + a) % c
elif seq[i] == "M":
x = x * b % c
y = y * b % c
nums[l] = (nums[l] * x + y) % c
if i == n - 1:
print(nums[l])
else:
print(nums[l], end=" ")
l += 1 if l < r else -1
def solve():
input()
nums = [int(x) for x in input().split()]
abc = tuple(int(x) for x in input().split())
seq = input()
process(nums, abc, seq)
t = int(input())
for i in range(t):
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR VAR VAR NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
while t > 0:
t = t - 1
n = int(input())
l = [int(x) for x in input().split()]
a, b, c = input().split()
a = int(a)
b = int(b)
c = int(c)
s = input()
rev = False
add = 0
mul = 1
curr = 0
start = 0
end = n - 1
for i in range(n):
if s[i] == "R":
rev = not rev
elif s[i] == "A":
add += a
add %= c
elif s[i] == "M":
mul *= b
mul %= c
add *= b
add %= c
if rev:
curr = l[end]
end -= 1
else:
curr = l[start]
start += 1
print((curr * mul + add) % c, "", end="")
print("")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING VAR VAR VAR VAR IF VAR VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
from sys import stdin
T = int(stdin.readline())
for _ in range(T):
N = int(stdin.readline())
L = [int(s) for s in stdin.readline().strip().split()]
A, B, C = (int(s) for s in stdin.readline().strip().split())
S = stdin.readline().strip()
front, back = 0, N - 1
rev = False
mul, add = 1, 0
ans = []
for idx, op in enumerate(S):
if op == "R":
rev = not rev
if rev:
i = back
back -= 1
else:
i = front
front += 1
if op == "A":
add = (add + A) % C
if op == "M":
mul, add = mul * B % C, add * B % C
ans.append((L[i] * mul + add) % C)
print(" ".join(str(i) for i in 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 VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
while t > 0:
t = t - 1
n = int(input())
list = []
list2 = input().split()
for i in list2:
list.append(int(i))
a, b, c = input().split()
a = int(a)
b = int(b)
c = int(c)
s = input()
answer = []
add = 0
fact = 1
i = 0
j = n - 1
reverse = False
for ch in s:
if ch == "R":
reverse = not reverse
elif ch == "A":
add = (add + a) % c
else:
fact = fact * b % c
add = add * b % c
if reverse:
answer.append((list[j] % c * fact % c + add) % c)
j = j - 1
else:
answer.append((list[i] % c * fact % c + add) % c)
i = i + 1
for x in answer:
print(x, end=" ")
print("")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
while t != 0:
n = int(input())
l = list(map(int, input().split()))
a, b, c = map(int, input().split())
s = input()
left, right = 0, n - 1
isRev = False
toAdd = 0
toMul = 1
for i in range(len(s)):
if s[i] == "R":
isRev = not isRev
elif s[i] == "A":
toAdd = (toAdd + a) % c
else:
toMul = toMul * b % c
toAdd = toAdd * b % c
if isRev:
ele = l[right]
right -= 1
else:
ele = l[left]
left += 1
ele = (ele * toMul + toAdd) % c
print(ele, end=" ")
print()
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
def solve():
n = int(input())
a = input().split(" ")
for i in range(n):
a[i] = int(a[i])
A, B, C = input().split(" ")
A = int(A)
B = int(B)
C = int(C)
s = input()
CON = 0
MULT = 1
res = ""
for i in range(n):
if s[i] == "R":
a.reverse()
if s[i] == "A":
CON = (CON + A) % C
if s[i] == "M":
CON = CON * B % C
MULT = MULT * B % C
if i > 0:
res += " "
res += str((a[0] * MULT + CON) % C)
del a[0]
print(res)
t = int(input())
for i in range(t):
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
itr = int(input())
for j in range(itr):
N = int(input())
l = input()
L = [int(x) for x in l.split()]
l = input()
[A, B, C] = [int(x) for x in l.split()]
S = input()
for i in range(N):
if S[i] == "R":
y = L[i:]
L = L[:i] + y[::-1]
elif S[i] == "A":
L[i:] = [(x + A) for x in L[i:]]
elif S[i] == "M":
L[i:] = [(x * B) for x in L[i:]]
L[i:] = [(x % C) for x in L[i:]]
print(L[i], end=" ")
print()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
for _ in range(t):
n = int(input())
l = [int(x) for x in input().split()]
a, b, c = [int(x) for x in input().split()]
s = input()
A, M, left, right, cd = 0, 1, 0, n - 1, 1
for i in range(n):
if s[i] == "A":
A += a
A %= c
elif s[i] == "M":
M = M * b % c
A = A * b % c
elif s[i] == "R":
cd *= -1
if cd == 1:
x = l[left]
left += 1
else:
x = l[right]
right -= 1
print((x * M + A) % c, end=" ")
print("")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
def io(func=str, n=1):
def nfp():
ff = str
try:
it = iter(func)
def nf(val):
nonlocal ff
ff = next(it, ff)
return ff(val)
return type(func)(nf(va) for va in input().split())
except TypeError:
return func(input())
if type(n) is list:
return [_(func, x) for x in n]
else:
t = lambda: nfp()
if n == 1:
return t()
elif n == 0:
return [t() for i in range(int(input()))]
else:
return [t() for i in range(n)]
def main():
T = io(int)
for test in range(T):
res = []
N = io(int)
L = io([int])
A, B, C = io((int, int, int))
commands = io(list)
reverse = False
x = 0
y = N - 1
S = 0
M = 1
for c in commands:
if c == "A":
S += A
elif c == "M":
M *= B
S *= B
elif c == "R":
reverse = not reverse
if reverse:
res.append((L[y] * M + S) % C)
y -= 1
else:
res.append((L[x] * M + S) % C)
x += 1
yield " ".join([str(r) for r in res])
print("\n".join([str(res) for res in main()]))
|
FUNC_DEF VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR IF VAR STRING VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
for q in range(t):
n = int(input())
numbers = input()
x = numbers.split()
a, b, c = map(int, input().split())
s = input()
up = 0
lp = n - 1
add = 0
mult = 1
a = a % c
b = b % c
ci = 1
for i in range(n):
if s[i] == "R":
temp = up
up = lp
lp = temp
ci = -ci
elif s[i] == "A":
add = (add + a) % c
else:
add = add * b % c
mult = mult * b % c
v = int(x[up])
v = (v * mult % c + add) % c
print(v, end=" ")
up = up + ci
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 FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
tc = int(input())
for t in range(tc):
n = int(input())
l = list(map(int, input().split()))
a, b, c = map(int, input().split())
st = input()
fuck = True
temp1 = 0
temp = 1
x = 0
y = n - 1
for i in range(n):
if st[i] == "R":
x, y = y, x
fuck = not fuck
elif st[i] == "A":
temp1 = (temp1 % c + a) % c
else:
temp, temp1 = temp % c * b, temp1 % c * b
l[x] = (l[x] * temp + temp1) % c
if i == n - 1:
print(l[x])
else:
print(l[x], end=" ")
if fuck:
x += 1
else:
x -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING IF VAR VAR NUMBER VAR NUMBER
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
T = int(input())
while T > 0:
N = int(input())
L = [int(x) for x in input().split(" ")]
A, B, C = (int(x) for x in input().split(" "))
st = input()
ans = ""
forward = True
fp = 0
bp = N - 1
tm = 1
ta = 0
for i in range(N):
if st[i] == "R":
forward = not forward
elif st[i] == "A":
ta = (ta + A) % C
else:
ta = ta * B % C
tm = tm * B % C
if forward:
ans += str((L[fp] * tm + ta) % C)
fp += 1
else:
ans += str((L[bp] * tm + ta) % C)
bp -= 1
if i < N - 1:
ans += " "
print(ans)
T -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
import sys
l = []
s = ""
def main():
t = int(input())
while t:
t -= 1
n = int(input())
head = 0
flag = 0
tail = n - 1
mul_value = 1
add_value = 0
l = sys.stdin.readline().strip().split(" ")
a, b, c = sys.stdin.readline().strip().split(" ")
a = int(a)
b = int(b)
c = int(c)
s = sys.stdin.readline()
for i in range(n):
l[i] = int(l[i])
for i in range(n):
if s[i] == "R":
if flag == 0:
flag = 1
else:
flag = 0
temp = head
head = tail
tail = temp
elif s[i] == "A":
add_value = (add_value + a) % c
elif s[i] == "M":
mul_value = mul_value * b % c
add_value = add_value * b % c
l[head] = (l[head] * mul_value % c + add_value) % c
sys.stdout.write(str(l[head]))
if i == n - 1:
sys.stdout.write("\n")
else:
sys.stdout.write(" ")
if flag == 0:
head += 1
else:
head -= 1
main()
|
IMPORT ASSIGN VAR LIST ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
for i in range(t):
n = int(input())
l = list(map(int, input().split()))
a, b, c = map(int, input().split())
s = input()
m = 1
u = 0
for j in range(n):
if s[j] == "A":
u = u + a
elif s[j] == "R":
l[j:] = l[j:][::-1]
elif s[j] == "M":
m = m * b
u = u * b
l[j] = l[j] * m
l[j] = l[j] + u
l[j] = l[j] % c
print(*l)
|
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
import sys
T = int(sys.stdin.readline())
Ans = ""
for t in range(T):
N = int(sys.stdin.readline())
L = list(map(int, sys.stdin.readline().split()))
A, B, C = map(int, sys.stdin.readline().split())
s = sys.stdin.readline().split()[0]
i = 0
ss = 0
end = N
x = 1
a = 0
b = 1
while i != end:
if s[ss] == "A":
a += A
a %= C
L[i] = L[i] * b + a
L[i] %= C
elif s[ss] == "R":
if x == 1:
i, end = end - 1, i - 1
x = -1
else:
i, end = end + 1, i + 1
x = 1
L[i] = L[i] * b + a
L[i] %= C
elif s[ss] == "M":
b *= B
a *= B
b %= C
a %= C
L[i] = L[i] * b + a
L[i] %= C
if ss != N - 1:
Ans += str(L[i] % C) + " "
else:
Ans += str(L[i] % C)
i += x
ss += 1
Ans += "\n"
sys.stdout.write(Ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input().strip())
while t:
n = int(input().strip())
l = [int(x) for x in input().strip().split()]
a, b, c = (int(x) for x in input().strip().split())
S = input().strip()
s, e, i, x, y = 0, n - 1, 1, 1, 0
for C in S:
if C == "R":
s, e, i = e, s, -i
elif C == "A":
y += a
y %= c
elif C == "M":
x *= b
x %= c
y *= b
y %= c
z = (x * l[s] % c + y % c) % c
print(z, end=" ")
s += i
print()
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR IF VAR STRING VAR VAR VAR VAR IF VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
for ti in range(t):
n = int(input())
l = [int(x) for x in input().split()]
a, b, c = [int(x) for x in input().split()]
s = input()
multiplier = 1
addend = 0
rev = False
res = []
i = 0
j = n - 1
for ch in s:
if ch == "R":
rev = not rev
elif ch == "A":
addend = (addend + a) % c
elif ch == "M":
multiplier = multiplier * b % c
addend = addend * b % c
if rev:
x = l[j]
j -= 1
else:
x = l[i]
i += 1
res.append((x * multiplier + addend) % c)
print(*res)
|
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
import sys
T = int(sys.stdin.readline())
while T > 0:
T -= 1
N = int(input())
L = list(map(int, sys.stdin.readline().split()))
a, b, c = map(int, sys.stdin.readline().split())
a = a % c
b = b % c
str = input()
toadd = 0
tomul = 1
rev = 0
co = 0
f = 0
for i in str:
co += 1
if i == "R":
L.reverse()
elif i == "A":
toadd += a
toadd = toadd % c
f = 1
elif i == "M":
tomul *= b
toadd *= b
tomul = tomul % c
toadd = toadd % c
f = 1
if rev == 0:
ans = L[0]
L.pop(0)
else:
ans = L[N - co]
L.pop(N - co)
if f == 1:
ans *= tomul
ans %= c
ans += toadd
ans = ans % c
print(ans, end=" ")
print("")
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR IF VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR STRING VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
def main():
t = int(input())
while t > 0:
t = t - 1
n = int(input())
arr = list(map(int, input().split()))
a, b, c = map(int, input().split())
s = input()
a1 = f = 0
l = m1 = temp = 1
la = n - 1
for i in range(n):
if s[i] == "A":
a1 = (a1 + a) % c
elif s[i] == "M":
m1 = m1 * b % c
a1 = a1 * b % c
else:
tmp = f
f = la
la = tmp
if temp == 1:
temp = 0
else:
temp = 1
arr[f] = (arr[f] * m1 + a1) % c
if i < n - 1:
print(arr[f], end=" ")
else:
print(arr[f])
if temp == 1:
f += 1
else:
f -= 1
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
import sys
data = sys.stdin.read().split()
data.reverse()
def read():
return int(data.pop())
def readStr():
return data.pop()
T = read()
for test in range(T):
n = read()
A = [read() for i in range(n)]
a, b, c = read(), read(), read()
s = readStr()
ans = []
l, r = 0, n - 1
isRev = False
toAdd = 0
toMul = 1
for ch in s:
if ch == "R":
isRev = not isRev
elif ch == "A":
toAdd = (toAdd + a) % c
else:
toMul = toMul * b % c
toAdd = toAdd * b % c
if isRev:
ele = A[r]
r -= 1
else:
ele = A[l]
l += 1
ele = (ele * toMul + toAdd) % c
ans.append(str(ele))
print(" ".join(ans), end="\n")
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
T = int(input())
for i in range(T):
N = int(input())
inp = input().split()
L = [int(x) for x in inp]
inp2 = input().split()
A = int(inp2[0])
B = int(inp2[1])
C = int(inp2[2])
S = input()
pos = 0
s = 0
p = 1
for j in range(N):
if S[j] == "R":
pos = -1 - pos
elif S[j] == "A":
s += A
elif S[j] == "M":
p *= B
s *= B
temp = (L[pos] * p % C + s) % C
L.pop(pos)
print(temp, end=" ")
print()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR 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 VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR VAR IF VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
def sep(s):
a = []
b = ""
for i in list(s):
if i == " ":
a.append(int(b))
b = ""
b = b + i
if b != " ":
a.append(int(b))
return a
t = int(input())
for xx in range(t):
l = []
li = []
n = int(input())
s = input()
l = sep(s)
s = input()
li = sep(s)
A = li[0]
B = li[1]
C = li[2]
stri = input()
list_size = len(l)
start = 0
last = n - 1
added_till_here = 0
multiplied_till_here = 1
for i in range(0, n):
st = start
en = last
if stri[i] == "A":
added_till_here = (added_till_here + A) % C
elif stri[i] == "M":
multiplied_till_here = multiplied_till_here * B % C
added_till_here = added_till_here * B % C
else:
start, last = last, start
l[start] = (l[start] * multiplied_till_here % C + added_till_here) % C
print(l[start], end=" ")
if start > last:
start = start - 1
else:
start = start + 1
print()
|
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
T = int(input())
a = [(0) for i in range(0, 1101)]
c = [(0) for i in range(0, 1101)]
while T > 0:
T -= 1
n = int(input())
tmp = input().split()
ba = 0
bt = 1
L = 1
R = n
for i in range(1, n + 1):
a[i] = int(tmp[i - 1])
f = 0
now = 0
add, mul, mod = [int(x) for x in input().split()]
s = input()
for i in range(1, n + 1):
if s[i - 1] == "R":
f = 1 - f
elif s[i - 1] == "A":
ba = (ba + add) % mod
else:
ba = ba * mul % mod
bt = bt * mul % mod
if f == 1:
now = R
R = R - 1
else:
now = L
L = L + 1
c[i] = (a[now] * bt + ba) % mod
print(" ".join(map(str, c[1 : n + 1])))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
T = int(input())
for _ in range(T):
N = int(input())
L = [int(i) for i in input().split()]
A, B, C = [int(i) for i in input().split()]
S = input()
direction = 1
endPos = N
curPos = 0
finalValues = []
coeff = 1
constant = 0
for op in S:
if op == "A":
constant = (constant + A) % C
elif op == "M":
constant = constant * B % C
coeff = coeff * B % C
elif op == "R":
curPos, endPos = endPos - direction, curPos - direction
direction *= -1
L[curPos] = (L[curPos] * coeff + constant) % C
finalValues.append(L[curPos])
curPos += direction
print(*finalValues, sep=" ")
|
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
for i in range(0, t):
n = int(input())
arr = [int(j) for j in input().split(" ")]
a, b, c = input().split(" ")
a = int(a)
b = int(b)
c = int(c)
s = input()
g = -1
h = n
fl = 0
sum = 0
pro = 1
for j in range(0, n):
if s[j] == "R" and fl == 0:
fl = 1
elif s[j] == "R" and fl == 1:
fl = 0
if fl == 0:
g += 1
v = g
elif fl == 1:
h -= 1
v = h
if s[j] == "A":
sum += a
elif s[j] == "M":
sum *= b
pro *= b
arr[v] *= pro
arr[v] += sum
arr[v] = arr[v] % c
sum %= c
pro %= c
if g > h:
break
print(arr[v], end=" ")
print()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING VAR VAR IF VAR VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
test = int(input())
for t in range(test):
n = int(input())
l = list(map(int, input().split()))
x, y, z = map(int, input().split())
str = input()
a, b, c = 0, 1, z
strt, end = 0, n
p = 1
for i in range(n):
if str[i] == "R":
p += 1
if p % 2 == 0:
end -= 1
ans = (l[end] * b + a) % c
else:
ans = (l[strt] * b + a) % c
strt += 1
elif str[i] == "A":
a = (a + x) % c
if p % 2 == 0:
end -= 1
ans = (l[end] * b + a) % c
else:
ans = (l[strt] * b + a) % c
strt += 1
else:
a, b = a * y % c, b * y % c
if p % 2 == 0:
end -= 1
ans = (l[end] * b + a) % c
else:
ans = (l[strt] * b + a) % c
strt += 1
if i == n - 1:
print(ans)
else:
print(ans, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
T = int(input())
for t in range(T):
N = int(input())
num = input().split()
num = [int(i) for i in num]
temp = input().split()
A, B, C = int(temp[0]), int(temp[1]), int(temp[2])
string = input().strip()
coeff_add = 0
coeff_mult = 1
f = N - 1
s = 0
k = 0
for i in string:
if i == "R":
temp = f
f = s
s = temp
result = (num[s] * coeff_mult % C + coeff_add) % C
if s < f:
s = s + 1
else:
s = s - 1
elif i == "A":
coeff_add = (coeff_add + A) % C
result = (num[s] * coeff_mult % C + coeff_add) % C
if s < f:
s = s + 1
else:
s = s - 1
elif i == "M":
coeff_add = coeff_add * B % C
coeff_mult = coeff_mult * B % C
result = (num[s] * coeff_mult % C + coeff_add) % C
if s < f:
s = s + 1
else:
s = s - 1
k = k + 1
print(result, end=" ")
print("")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
for cs in range(t):
n = int(input())
LS = input().split()
L = []
for str in LS:
L.append(int(str))
A1, B1, C1 = input().split()
A = int(A1)
B = int(B1)
C = int(C1)
S = input()
i = 0
j = n - 1
add = 0
mul = 1
flag = 0
pr = 0
for ch in S:
pr = pr + 1
if ch == "R":
flag = 1 - flag
elif ch == "A":
add = (add + A) % C
elif ch == "M":
mul = mul * B % C
add = add * B % C
if flag == 1:
if pr == n:
print((L[j] * mul + add) % C)
else:
print((L[j] * mul + add) % C, end=" ")
j = j - 1
else:
if pr == n:
print((L[i] * mul + add) % C)
else:
print((L[i] * mul + add) % C, end=" ")
i = i + 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
t = int(input())
j = 0
while t > 0:
n = int(input())
extra, mcnt, rcnt, p1, p2 = 0, 1, 0, -1, n
arr = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = input()
for i in s:
if i == "R":
rcnt += 1
elif i == "A":
extra = (extra + a[0]) % a[2]
else:
extra = extra * a[1] % a[2]
mcnt = mcnt * a[1] % a[2]
if rcnt & 1:
p2 -= 1
j = p2
else:
p1 += 1
j = p1
print((arr[j] * mcnt % a[2] + extra) % a[2], end=" ")
print()
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
from sys import stdin
T = int(stdin.readline())
for t in range(T):
N = int(stdin.readline())
num = [int(i) for i in stdin.readline().split()]
a, b, c = [int(i) for i in stdin.readline().split()]
string = stdin.readline().strip()
A = 0
M = 1
flag = 0
start = 0
end = N - 1
rev = 0
for i in range(N):
if string[i] == "R":
rev = 1 if rev == 0 else 0
elif string[i] == "A":
A += a
flag = 1
elif string[i] == "M":
M = M * b
A = A * b
flag = 1
if rev == 1:
ans = num[end]
end = end - 1
else:
ans = num[start]
start = start + 1
if flag == 1:
ans = ans * M
ans += A
print(ans % c, end=" ")
print("")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Read problems statements in Mandarin Chinese and Russian.
Vidhi went to a magic show last week where she was astounded by a magic trick performed by the great Mandwarf, the brown. His trick was as follows :
Ask a volunteer from the audience to write down a list L of N integers.
Ask another volunteer from the audience to provide three integers A, B, C
Ask another volunteer from the audience to provide N length string called S where each letter is either 'R', 'A' or 'M'
Close his eyes for a split second and give the output of The Ancient Algorithm on this input.
We all know that The Ancient Algorithm is as follows :
for i from 1 to N do
if i^{th} letter of S is 'R'
reverse L[i...N]
else if i^{th} letter of S is 'A'
add A to all numbers of L[i..N].
else if i^{th} letter of S is 'M'
multiply B to all numbers of L[i..N].
for all number in L[i..N], module them by C.
announce L[i] out loud
end
Vidhi's boyfriend got jealous when he saw her getting impressed by Mandwarf, the brown's wisdom. He wants to learn the trick to gain her undivided admiration. How about you help him?
------ Constraints: ------
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
0 ≤ L[i] ≤ 10^{18}
0 ≤ A,B ≤ 10^{18}
2 ≤ C ≤ 10^{18}
------ Input ------
First line contains a single integer T, denoting the number of test cases. Then follow T test case scenarios. Each test case begins with an integer N, the size of the list L. Then in next line, you'd find N space separated integers - the list L itself. In next line, there'd be three space separated integers A, B, C followed by string S in the next line.
------ Output ------
For each test case you've to output N space separated integers - the numbers announced by Mandwarf, the brown.
----- Sample Input 1 ------
2
3
1 1 1
2 3 1000
ARM
4
1 2 3 4
0 1 1000
AMAM
----- Sample Output 1 ------
3 3 9
1 2 3 4
|
import sys
def pow_mod(x, y, z):
number = 1
while y:
if y & 1:
number = number * x % z
y >>= 1
x = x * x % z
return number
tokenizedInput = sys.stdin.read().split()
n = int(tokenizedInput[0])
t = 1
for i in range(0, n):
N = int(tokenizedInput[t])
t = t + 1
the_list = list(map(int, tokenizedInput[t : t + N]))
t = t + N
A = int(tokenizedInput[t])
t = t + 1
B = int(tokenizedInput[t])
t = t + 1
C = int(tokenizedInput[t])
t = t + 1
S = tokenizedInput[t]
t = t + 1
idx = 0
r = 1
ansi = 0
pi = 0
stri = ""
for c in range(0, N):
if S[c] == "R":
the_list[idx:N] = reversed(the_list[idx:N])
idx = idx + 1
ansi = (ansi + int((the_list[c] - pi) * r)) % C
pi = the_list[c]
if S[c] == "A":
ansi = (ansi + A) % C
if S[c] == "M":
r = B * r % C
ansi = ansi * B % C
stri += str(ansi % C) + " "
sys.stdout.write(stri + "\n")
|
IMPORT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING
|
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x_1, x_2, ..., x_{k} (k > 1) is such maximum element x_{j}, that the following inequality holds: $x_{j} \neq \operatorname{max}_{i = 1}^{k} x_{i}$.
The lucky number of the sequence of distinct positive integers x_1, x_2, ..., x_{k} (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s_1, s_2, ..., s_{n} (n > 1). Let's denote sequence s_{l}, s_{l} + 1, ..., s_{r} as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
-----Input-----
The first line contains integer n (1 < n ≤ 10^5). The second line contains n distinct integers s_1, s_2, ..., s_{n} (1 ≤ s_{i} ≤ 10^9).
-----Output-----
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
-----Examples-----
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
-----Note-----
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
|
x = input()
l = list(map(int, input().split()))
ans = 0
d = []
for i in l:
while d != []:
if d[-1] < i:
ans = max(ans, d[-1] ^ i)
d.pop()
else:
ans = max(ans, d[-1] ^ i)
break
d.append(i)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE VAR LIST IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.